Skip to content

Commit a8fc1e3

Browse files
committed
test
1 parent 24afc8d commit a8fc1e3

File tree

3 files changed

+49
-21
lines changed

3 files changed

+49
-21
lines changed

include/geode/basic/database.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,10 @@ namespace geode
117117
std::unique_ptr< DataType > take_data( const uuid& id )
118118
{
119119
get_data( id ).get< DataType >();
120+
DEBUG( "get" );
120121
auto* data =
121122
dynamic_cast< DataType* >( steal_data( id ).release() );
123+
DEBUG( "steal" );
122124
return std::unique_ptr< DataType >{ data };
123125
}
124126

src/geode/basic/database.cpp

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
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>
@@ -36,7 +39,7 @@
3639

3740
namespace
3841
{
39-
constexpr auto DATA_EXPIRATION = absl::Minutes( 5 );
42+
constexpr auto DATA_EXPIRATION = std::chrono::minutes( 5 );
4043
} // namespace
4144

4245
namespace 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

tests/basic/test-database.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,18 @@ void register_foo_deserializer( geode::PContext& context )
6060
void test_take_data( geode::Database& database, const geode::uuid& id )
6161
{
6262
auto stolen_foo = database.take_data< Foo >( id );
63+
DEBUG( "take 1" );
6364
OPENGEODE_EXCEPTION(
6465
stolen_foo->value_ == 42, "[Test] Wrong value after take data" );
6566
auto foo_data = database.get_data( id );
67+
DEBUG( "take 2" );
6668
const auto& foo = foo_data.get< Foo >();
69+
DEBUG( "take 3" );
6770
OPENGEODE_EXCEPTION(
6871
foo.value_ == 42, "[Test] Wrong value after register data" );
6972
OPENGEODE_EXCEPTION( stolen_foo.get() != &foo,
7073
"[Test] Objects adresses should be different" );
74+
DEBUG( "take 4" );
7175
}
7276

7377
void test_take_wrong_data( geode::Database& database, const geode::uuid& id )
@@ -127,11 +131,17 @@ void test()
127131
geode::Database database( "temp" );
128132
database.register_serializer_functions(
129133
register_foo_serializer, register_foo_deserializer );
134+
DEBUG( "functions" );
130135
auto foo0 = test_register_data( database );
136+
DEBUG( "register" );
131137
test_register_unique_data( database );
138+
DEBUG( "register unique" );
132139
test_take_data( database, foo0 );
140+
DEBUG( "take" );
133141
test_modify_data( database, foo0 );
142+
DEBUG( "modify" );
134143
test_take_wrong_data( database, foo0 );
144+
DEBUG( "wrong" );
135145
OPENGEODE_EXCEPTION(
136146
database.nb_data() == 2, "[Test] Database incomplete" );
137147
}

0 commit comments

Comments
 (0)