@@ -62,10 +62,13 @@ namespace geode
6262 Logger::debug ( count_, " -> " , " ~Storage" );
6363 terminate_storage ();
6464 std::unique_lock< std::mutex > locking{ lock_ };
65- condition_.wait ( locking, [this ] {
65+ do
66+ {
6667 clean_queue ();
67- return queue_.empty ();
68- } );
68+ } while ( !condition_.wait_for (
69+ locking, std::chrono::microseconds ( 10 ), [this ] {
70+ return queue_.empty ();
71+ } ) );
6972 Logger::debug ( count_, " -> " , " ~Storage end" );
7073 }
7174
@@ -83,9 +86,10 @@ namespace geode
8386 {
8487 const std::lock_guard< std::mutex > locking{ lock_ };
8588 counter_++;
89+ last_++;
8690 std::ostringstream oss;
8791 oss << std::this_thread::get_id () << " " << this ;
88- Logger::debug ( count_, " -> " , " new " , counter_, " " , oss. str () );
92+ Logger::debug ( count_, " -> " , " new " , counter_, " " );
8993 }
9094
9195 void delete_data_reference ()
@@ -96,8 +100,7 @@ namespace geode
96100 counter_--;
97101 std::ostringstream oss;
98102 oss << std::this_thread::get_id () << " " << this ;
99- Logger::debug (
100- count_, " -> " , " delete " , counter_, " " , oss.str () );
103+ Logger::debug ( count_, " -> " , " delete " , counter_, " " );
101104 if ( unused () )
102105 {
103106 clean_queue ();
@@ -132,8 +135,7 @@ namespace geode
132135 oss << std::this_thread::get_id () << " " << this ;
133136 Logger::debug ( count_, " -> " , " begin terminate_storage" );
134137 terminate_ = true ;
135- Logger::debug (
136- count_, " -> " , " calls " , queue_.size (), " " , oss.str () );
138+ Logger::debug ( count_, " -> " , " calls " , queue_.size (), " " );
137139 condition_.notify_all ();
138140 Logger::debug ( count_, " -> " , " end terminate_storage" );
139141 }
@@ -152,47 +154,42 @@ namespace geode
152154
153155 void wait_for_memory_release ()
154156 {
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 " );
160161 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] {
166166 Logger::debug ( count_, " -> " , " terminate " ,
167- terminate_.load (), " " , oss. str () );
167+ terminate_.load (), " " );
168168 return terminate_.load ();
169169 } ) )
170170 {
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_ )
175174 {
176- Logger::debug ( count_, " -> " , " wait reset" , " " ,
177- oss.str (), " " ,
178- reinterpret_cast < size_t >( this ) );
175+ Logger::debug ( count_, " -> " , " wait reset" , " " );
179176 data_.reset ();
180177 }
181178 }
182- Logger::debug ( count_, " -> " , " wait out + " , queue_.size (),
183- " " , oss.str () );
184- condition_.notify_all ();
179+ Logger::debug (
180+ count_, " -> " , " wait out + " , queue_.size (), " " );
185181 locking.unlock ();
182+ condition_.notify_all ();
186183 } ) );
187184 }
188185
189186 private:
190187 std::unique_ptr< Identifier > data_;
191188 std::atomic< bool > terminate_{ false };
192189 index_t counter_{ 0 };
193- std::chrono::time_point< std::chrono::system_clock > last_used_;
194190 std::mutex lock_;
195191 std::condition_variable condition_;
192+ index_t last_;
196193 int count_;
197194 std::queue< async::task< void > > queue_;
198195 };
0 commit comments