From 251845cbfab8bd83f3e88f7df083b76ee66549c7 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Wed, 17 Jul 2019 16:29:36 +0800 Subject: [PATCH] test/test_snap_mapper: s/Mutex/ceph::mutex/ Signed-off-by: Kefu Chai --- src/test/test_snap_mapper.cc | 83 +++++++++++++++++------------------- 1 file changed, 40 insertions(+), 43 deletions(-) diff --git a/src/test/test_snap_mapper.cc b/src/test/test_snap_mapper.cc index f7224cbdb11..9b6dbdd2b0c 100644 --- a/src/test/test_snap_mapper.cc +++ b/src/test/test_snap_mapper.cc @@ -89,36 +89,37 @@ public: }; private: - Mutex lock; + ceph::mutex lock = ceph::make_mutex("PausyAsyncMap"); map store; class Doer : public Thread { static const size_t MAX_SIZE = 100; PausyAsyncMap *parent; - Mutex lock; - Cond cond; + ceph::mutex lock = ceph::make_mutex("Doer lock"); + ceph::condition_variable cond; int stopping; bool paused; list queue; public: explicit Doer(PausyAsyncMap *parent) : - parent(parent), lock("Doer lock"), stopping(0), paused(false) {} + parent(parent), stopping(0), paused(false) {} void *entry() override { while (1) { list ops; { - Mutex::Locker l(lock); - while (!stopping && (queue.empty() || paused)) - cond.Wait(lock); + std::unique_lock l{lock}; + cond.wait(l, [this] { + return stopping || (!queue.empty() && !paused); + }); if (stopping && queue.empty()) { stopping = 2; - cond.Signal(); + cond.notify_all(); return 0; } ceph_assert(!queue.empty()); ceph_assert(!paused); ops.swap(queue); - cond.Signal(); + cond.notify_all(); } ceph_assert(!ops.empty()); @@ -127,44 +128,42 @@ private: ops.erase(i++)) { if (!(rand()%3)) usleep(1+(rand() % 5000)); - Mutex::Locker l(parent->lock); + std::lock_guard l{parent->lock}; (*i)->operate(&(parent->store)); } } } void pause() { - Mutex::Locker l(lock); + std::lock_guard l{lock}; paused = true; - cond.Signal(); + cond.notify_all(); } void resume() { - Mutex::Locker l(lock); + std::lock_guard l{lock}; paused = false; - cond.Signal(); + cond.notify_all(); } void submit(list &in) { - Mutex::Locker l(lock); - while (queue.size() >= MAX_SIZE) - cond.Wait(lock); + std::unique_lock l{lock}; + cond.wait(l, [this] { return queue.size() < MAX_SIZE;}); queue.splice(queue.end(), in, in.begin(), in.end()); - cond.Signal(); + cond.notify_all(); } void stop() { - Mutex::Locker l(lock); + std::unique_lock l{lock}; stopping = 1; - cond.Signal(); - while (stopping != 2) - cond.Wait(lock); - cond.Signal(); + cond.notify_all(); + cond.wait(l, [this] { return stopping == 2; }); + cond.notify_all(); } } doer; public: - PausyAsyncMap() : lock("PausyAsyncMap"), doer(this) { + PausyAsyncMap() : doer(this) { doer.create("doer"); } ~PausyAsyncMap() override { @@ -173,7 +172,7 @@ public: int get_keys( const set &keys, map *out) override { - Mutex::Locker l(lock); + std::lock_guard l{lock}; for (set::const_iterator i = keys.begin(); i != keys.end(); ++i) { @@ -186,7 +185,7 @@ public: int get_next( const string &key, pair *next) override { - Mutex::Locker l(lock); + std::lock_guard l{lock}; map::iterator j = store.upper_bound(key); if (j != store.end()) { if (next) @@ -202,30 +201,29 @@ public: } void flush() { - Mutex lock("flush lock"); - Cond cond; + ceph::mutex lock = ceph::make_mutex("flush lock"); + ceph::condition_variable cond; bool done = false; class OnFinish : public Context { - Mutex *lock; - Cond *cond; + ceph::mutex *lock; + ceph::condition_variable *cond; bool *done; public: - OnFinish(Mutex *lock, Cond *cond, bool *done) + OnFinish(ceph::mutex *lock, ceph::condition_variable *cond, bool *done) : lock(lock), cond(cond), done(done) {} void finish(int) override { - Mutex::Locker l(*lock); + std::lock_guard l{*lock}; *done = true; - cond->Signal(); + cond->notify_all(); } }; Transaction t; t.add_callback(new OnFinish(&lock, &cond, &done)); submit(&t); { - Mutex::Locker l(lock); - while (!done) - cond.Wait(lock); + std::unique_lock l{lock}; + cond.wait(l, [&] { return done; }); } } @@ -443,7 +441,7 @@ class MapperVerifier { snapid_t next; uint32_t mask; uint32_t bits; - Mutex lock; + ceph::mutex lock = ceph::make_mutex("lock"); public: MapperVerifier( @@ -452,8 +450,7 @@ public: uint32_t bits) : driver(driver), mapper(new SnapMapper(g_ceph_context, driver, mask, bits, 0, shard_id_t(1))), - mask(mask), bits(bits), - lock("lock") {} + mask(mask), bits(bits) {} hobject_t random_hobject() { return hobject_t( @@ -478,7 +475,7 @@ public: } void create_object() { - Mutex::Locker l(lock); + std::lock_guard l{lock}; if (snap_to_hobject.empty()) return; hobject_t obj; @@ -503,7 +500,7 @@ public: } void trim_snap() { - Mutex::Locker l(lock); + std::lock_guard l{lock}; if (snap_to_hobject.empty()) return; map >::iterator snap = @@ -545,7 +542,7 @@ public: } void remove_oid() { - Mutex::Locker l(lock); + std::lock_guard l{lock}; if (hobject_to_snap.empty()) return; map>::iterator obj = @@ -569,7 +566,7 @@ public: } void check_oid() { - Mutex::Locker l(lock); + std::lock_guard l{lock}; if (hobject_to_snap.empty()) return; map>::iterator obj = -- 2.39.5