@@ -62,10 +62,13 @@ namespace geode
62
62
Logger::debug ( count_, " -> " , " ~Storage" );
63
63
terminate_storage ();
64
64
std::unique_lock< std::mutex > locking{ lock_ };
65
- condition_.wait ( locking, [this ] {
65
+ do
66
+ {
66
67
clean_queue ();
67
- return queue_.empty ();
68
- } );
68
+ } while ( !condition_.wait_for (
69
+ locking, std::chrono::microseconds ( 10 ), [this ] {
70
+ return queue_.empty ();
71
+ } ) );
69
72
Logger::debug ( count_, " -> " , " ~Storage end" );
70
73
}
71
74
@@ -83,9 +86,10 @@ namespace geode
83
86
{
84
87
const std::lock_guard< std::mutex > locking{ lock_ };
85
88
counter_++;
89
+ last_++;
86
90
std::ostringstream oss;
87
91
oss << std::this_thread::get_id () << " " << this ;
88
- Logger::debug ( count_, " -> " , " new " , counter_, " " , oss. str () );
92
+ Logger::debug ( count_, " -> " , " new " , counter_, " " );
89
93
}
90
94
91
95
void delete_data_reference ()
@@ -96,8 +100,7 @@ namespace geode
96
100
counter_--;
97
101
std::ostringstream oss;
98
102
oss << std::this_thread::get_id () << " " << this ;
99
- Logger::debug (
100
- count_, " -> " , " delete " , counter_, " " , oss.str () );
103
+ Logger::debug ( count_, " -> " , " delete " , counter_, " " );
101
104
if ( unused () )
102
105
{
103
106
clean_queue ();
@@ -132,8 +135,7 @@ namespace geode
132
135
oss << std::this_thread::get_id () << " " << this ;
133
136
Logger::debug ( count_, " -> " , " begin terminate_storage" );
134
137
terminate_ = true ;
135
- Logger::debug (
136
- count_, " -> " , " calls " , queue_.size (), " " , oss.str () );
138
+ Logger::debug ( count_, " -> " , " calls " , queue_.size (), " " );
137
139
condition_.notify_all ();
138
140
Logger::debug ( count_, " -> " , " end terminate_storage" );
139
141
}
@@ -152,47 +154,42 @@ namespace geode
152
154
153
155
void wait_for_memory_release ()
154
156
{
155
- queue_.emplace ( async::spawn ( [this ] {
156
- std::ostringstream oss;
157
- oss << std::this_thread::get_id () << " " << this ;
158
- Logger::debug ( count_, " -> " , " wait start " , oss.str () );
159
- Logger::debug ( count_, " -> " , " wait start 2 " , oss.str () );
157
+ const auto last = last_;
158
+ queue_.emplace ( async::spawn ( [this , last] {
159
+ Logger::debug ( count_, " -> " , " wait start " );
160
+ Logger::debug ( count_, " -> " , " wait start 2 " );
160
161
std::unique_lock< std::mutex > locking{ lock_ };
161
- last_used_ = std::chrono::system_clock::now ();
162
- Logger::debug ( count_, " -> " , " wait 2 + " , oss.str () );
163
- if ( !condition_.wait_for ( locking,
164
- DATA_EXPIRATION + std::chrono::seconds ( 1 ),
165
- [this , &oss] {
162
+ Logger::debug ( count_, " -> " , " wait 2 + " );
163
+ Logger::debug ( count_, " -> " , " last " , last, " " , last_ );
164
+ if ( !condition_.wait_for (
165
+ locking, DATA_EXPIRATION, [this , last] {
166
166
Logger::debug ( count_, " -> " , " terminate " ,
167
- terminate_.load (), " " , oss. str () );
167
+ terminate_.load (), " " );
168
168
return terminate_.load ();
169
169
} ) )
170
170
{
171
- Logger::debug ( count_, " -> " , " wait in" , " " , oss.str () );
172
- if ( !terminate_ && unused ()
173
- && std::chrono::system_clock::now () - last_used_
174
- > DATA_EXPIRATION )
171
+ Logger::debug (
172
+ count_, " -> " , " wait in" , " " , last, " " , last_ );
173
+ if ( last == last_ )
175
174
{
176
- Logger::debug ( count_, " -> " , " wait reset" , " " ,
177
- oss.str (), " " ,
178
- reinterpret_cast < size_t >( this ) );
175
+ Logger::debug ( count_, " -> " , " wait reset" , " " );
179
176
data_.reset ();
180
177
}
181
178
}
182
- Logger::debug ( count_, " -> " , " wait out + " , queue_.size (),
183
- " " , oss.str () );
184
- condition_.notify_all ();
179
+ Logger::debug (
180
+ count_, " -> " , " wait out + " , queue_.size (), " " );
185
181
locking.unlock ();
182
+ condition_.notify_all ();
186
183
} ) );
187
184
}
188
185
189
186
private:
190
187
std::unique_ptr< Identifier > data_;
191
188
std::atomic< bool > terminate_{ false };
192
189
index_t counter_{ 0 };
193
- std::chrono::time_point< std::chrono::system_clock > last_used_;
194
190
std::mutex lock_;
195
191
std::condition_variable condition_;
192
+ index_t last_;
196
193
int count_;
197
194
std::queue< async::task< void > > queue_;
198
195
};
0 commit comments