Commit e88f12fc authored by Leif Walsh's avatar Leif Walsh Committed by Yoni Fogel

refs #5380 fix unit test to take mutex around rwlock functions

git-svn-id: file:///svn/toku/tokudb@47044 c7de825b-a66e-492c-adef-691d508d4ae1
parent f8baef6c
...@@ -89,129 +89,117 @@ static void launch_reader(void) { ...@@ -89,129 +89,117 @@ static void launch_reader(void) {
sleep(1); sleep(1);
} }
static bool locks_are_expensive(void) {
toku_mutex_lock(&mutex);
assert(w.write_lock_is_expensive() == w.read_lock_is_expensive());
bool is_expensive = w.write_lock_is_expensive();
toku_mutex_unlock(&mutex);
return is_expensive;
}
static void test_write_cheapness(void) { static void test_write_cheapness(void) {
toku_mutex_init(&mutex, NULL); toku_mutex_init(&mutex, NULL);
w.init(&mutex); w.init(&mutex);
// single expensive write lock // single expensive write lock
grab_write_lock(true); grab_write_lock(true);
assert(w.write_lock_is_expensive()); assert(locks_are_expensive());
assert(w.read_lock_is_expensive());
release_write_lock(); release_write_lock();
assert(!w.write_lock_is_expensive()); assert(!locks_are_expensive());
assert(!w.read_lock_is_expensive());
// single cheap write lock // single cheap write lock
grab_write_lock(false); grab_write_lock(false);
assert(!w.write_lock_is_expensive()); assert(!locks_are_expensive());
assert(!w.read_lock_is_expensive());
release_write_lock(); release_write_lock();
assert(!w.write_lock_is_expensive()); assert(!locks_are_expensive());
assert(!w.read_lock_is_expensive());
// multiple read locks // multiple read locks
grab_read_lock(); grab_read_lock();
assert(!w.write_lock_is_expensive()); assert(!locks_are_expensive());
assert(!w.read_lock_is_expensive());
grab_read_lock(); grab_read_lock();
grab_read_lock(); grab_read_lock();
assert(!w.write_lock_is_expensive()); assert(!locks_are_expensive());
assert(!w.read_lock_is_expensive());
release_read_lock(); release_read_lock();
release_read_lock(); release_read_lock();
release_read_lock(); release_read_lock();
assert(!w.write_lock_is_expensive()); assert(!locks_are_expensive());
assert(!w.read_lock_is_expensive());
// expensive write lock and cheap writers waiting // expensive write lock and cheap writers waiting
grab_write_lock(true); grab_write_lock(true);
launch_cheap_waiter(); launch_cheap_waiter();
assert(w.write_lock_is_expensive()); assert(locks_are_expensive());
assert(w.read_lock_is_expensive());
launch_cheap_waiter(); launch_cheap_waiter();
launch_cheap_waiter(); launch_cheap_waiter();
assert(w.write_lock_is_expensive()); assert(locks_are_expensive());
assert(w.read_lock_is_expensive());
release_write_lock(); release_write_lock();
sleep(1); sleep(1);
assert(!w.write_lock_is_expensive()); assert(!locks_are_expensive());
assert(!w.read_lock_is_expensive());
// cheap write lock and expensive writer waiter // cheap write lock and expensive writer waiter
grab_write_lock(false); grab_write_lock(false);
launch_expensive_waiter(); launch_expensive_waiter();
assert(w.write_lock_is_expensive()); assert(locks_are_expensive());
assert(w.read_lock_is_expensive());
release_write_lock(); release_write_lock();
sleep(1); sleep(1);
// expensive write lock and expensive waiter // expensive write lock and expensive waiter
grab_write_lock(true); grab_write_lock(true);
launch_expensive_waiter(); launch_expensive_waiter();
assert(w.write_lock_is_expensive()); assert(locks_are_expensive());
assert(w.read_lock_is_expensive());
release_write_lock(); release_write_lock();
sleep(1); sleep(1);
// cheap write lock and cheap waiter // cheap write lock and cheap waiter
grab_write_lock(false); grab_write_lock(false);
launch_cheap_waiter(); launch_cheap_waiter();
assert(!w.write_lock_is_expensive()); assert(!locks_are_expensive());
assert(!w.read_lock_is_expensive());
release_write_lock(); release_write_lock();
sleep(1); sleep(1);
// read lock held and cheap waiter // read lock held and cheap waiter
grab_read_lock(); grab_read_lock();
launch_cheap_waiter(); launch_cheap_waiter();
assert(!w.write_lock_is_expensive()); assert(!locks_are_expensive());
assert(!w.read_lock_is_expensive());
// add expensive waiter // add expensive waiter
launch_expensive_waiter(); launch_expensive_waiter();
assert(w.write_lock_is_expensive()); assert(locks_are_expensive());
assert(w.read_lock_is_expensive());
release_read_lock(); release_read_lock();
sleep(1); sleep(1);
// read lock held and expensive waiter // read lock held and expensive waiter
grab_read_lock(); grab_read_lock();
launch_expensive_waiter(); launch_expensive_waiter();
assert(w.write_lock_is_expensive()); assert(locks_are_expensive());
assert(w.read_lock_is_expensive());
// add expensive waiter // add expensive waiter
launch_cheap_waiter(); launch_cheap_waiter();
assert(w.write_lock_is_expensive()); assert(locks_are_expensive());
assert(w.read_lock_is_expensive());
release_read_lock(); release_read_lock();
sleep(1); sleep(1);
// cheap write lock held and waiting read // cheap write lock held and waiting read
grab_write_lock(false); grab_write_lock(false);
launch_reader(); launch_reader();
assert(!w.write_lock_is_expensive()); assert(!locks_are_expensive());
assert(!w.read_lock_is_expensive());
launch_expensive_waiter(); launch_expensive_waiter();
toku_mutex_lock(&mutex);
assert(w.write_lock_is_expensive()); assert(w.write_lock_is_expensive());
// tricky case here, because we have a launched reader // tricky case here, because we have a launched reader
// that should be in the queue, a new read lock // that should be in the queue, a new read lock
// should piggy back off that // should piggy back off that
assert(!w.read_lock_is_expensive()); assert(!w.read_lock_is_expensive());
toku_mutex_unlock(&mutex);
release_write_lock(); release_write_lock();
sleep(1); sleep(1);
// expensive write lock held and waiting read // expensive write lock held and waiting read
grab_write_lock(true); grab_write_lock(true);
launch_reader(); launch_reader();
assert(w.write_lock_is_expensive()); assert(locks_are_expensive());
assert(w.read_lock_is_expensive());
launch_cheap_waiter(); launch_cheap_waiter();
assert(w.write_lock_is_expensive()); assert(locks_are_expensive());
assert(w.read_lock_is_expensive());
release_write_lock(); release_write_lock();
sleep(1); sleep(1);
w.deinit(); w.deinit();
toku_mutex_destroy(&mutex); toku_mutex_destroy(&mutex);
} }
...@@ -220,4 +208,3 @@ int main (int UU(argc), const char* UU(argv[])) { ...@@ -220,4 +208,3 @@ int main (int UU(argc), const char* UU(argv[])) {
test_write_cheapness(); test_write_cheapness();
return 0; return 0;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment