2222 */
2323#include < geode/basic/database.h>
2424
25+ #include < chrono>
26+ #include < condition_variable>
27+ #include < mutex>
28+
2529#include < async++.h>
2630
2731#include < ghc/filesystem.hpp>
2832
2933#include < absl/container/flat_hash_map.h>
30- #include < absl/synchronization/mutex.h>
3134#include < absl/time/time.h>
3235
3336#include < geode/basic/identifier.h>
3639
3740namespace
3841{
39- constexpr auto DATA_EXPIRATION = absl::Minutes ( 5 );
42+ constexpr auto DATA_EXPIRATION = std::chrono::minutes ( 5 );
4043} // namespace
4144
4245namespace geode
@@ -51,13 +54,15 @@ namespace geode
5154
5255 ~Storage ()
5356 {
54- absl::MutexLock locking{ &lock_ };
57+ DEBUG ( " ~Storage" );
58+ std::unique_lock< std::mutex > locking{ lock_ };
5559 terminate_ = true ;
56- lock_.Await ( absl::Condition (
57- +[]( index_t * nb_calls ) {
58- return *nb_calls == 0 ;
59- },
60- &nb_calls_ ) );
60+ DEBUG ( nb_calls_ );
61+ condition_.notify_all ();
62+ condition_.wait ( locking, [this ] {
63+ return nb_calls_ == 0 ;
64+ } );
65+ DEBUG ( " ~Storage end" );
6166 }
6267
6368 bool expired () const
@@ -72,13 +77,13 @@ namespace geode
7277
7378 void new_data_reference ()
7479 {
75- absl::MutexLock locking{ & lock_ };
80+ const std::lock_guard< std::mutex > locking{ lock_ };
7681 counter_++;
7782 }
7883
7984 void delete_data_reference ()
8085 {
81- absl::MutexLock locking{ & lock_ };
86+ const std::lock_guard< std::mutex > locking{ lock_ };
8287 OPENGEODE_ASSERT (
8388 counter_ > 0 , " [Database::Storage] Cannot decrement" );
8489 counter_--;
@@ -106,24 +111,30 @@ namespace geode
106111 private:
107112 void wait_for_memory_release ()
108113 {
109- last_used_ = absl::Now ();
114+ last_used_ = std::chrono::system_clock::now ();
110115 async::spawn ( [this ] {
111- absl::MutexLock locking{ &lock_ };
116+ DEBUG ( " wait" );
117+ std::unique_lock< std::mutex > locking{ lock_ };
112118 nb_calls_++;
113- if ( !lock_. AwaitWithTimeout ( absl::Condition (
114- +[]( bool * terminate ) {
115- return *terminate;
116- },
117- & terminate_ ),
118- DATA_EXPIRATION + absl::Seconds ( 1 ) ) )
119+ DEBUG ( nb_calls_ );
120+ DEBUG ( " wait 2 " );
121+ if ( !condition_. wait_for ( locking,
122+ DATA_EXPIRATION + std::chrono::seconds ( 1 ), [ this ] {
123+ return terminate_;
124+ } ) )
119125 {
126+ DEBUG ( " wait in" );
120127 if ( !terminate_ && unused ()
121- && absl::Now () - last_used_ > DATA_EXPIRATION )
128+ && std::chrono::system_clock::now () - last_used_
129+ > DATA_EXPIRATION )
122130 {
131+ DEBUG ( " wait reset" );
123132 data_.reset ();
124133 }
125134 }
135+ DEBUG ( " wait out" );
126136 nb_calls_--;
137+ condition_.notify_all ();
127138 } );
128139 }
129140
@@ -132,8 +143,9 @@ namespace geode
132143 bool terminate_{ false };
133144 index_t nb_calls_{ 0 };
134145 index_t counter_{ 0 };
135- absl::Time last_used_;
136- absl::Mutex lock_;
146+ std::chrono::time_point< std::chrono::system_clock > last_used_;
147+ std::mutex lock_;
148+ std::condition_variable condition_;
137149 };
138150
139151 class Database ::Impl
@@ -175,10 +187,14 @@ namespace geode
175187 auto & storage = storage_.at ( id );
176188 if ( storage && storage->unused () && !storage->expired () )
177189 {
190+ DEBUG ( " in" );
178191 auto * data = storage->steal_data ();
192+ DEBUG ( " steal" );
179193 storage.reset ();
194+ DEBUG ( " reset" );
180195 return std::unique_ptr< Identifier >{ data };
181196 }
197+ DEBUG ( " load" );
182198 return load_data ( id );
183199 }
184200
0 commit comments