]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
test/mon: s/Mutex/ceph::mutex/
authorKefu Chai <kchai@redhat.com>
Sun, 7 Jul 2019 04:42:36 +0000 (12:42 +0800)
committerKefu Chai <kchai@redhat.com>
Sat, 3 Aug 2019 03:27:19 +0000 (11:27 +0800)
Signed-off-by: Kefu Chai <kchai@redhat.com>
src/test/mon/test-mon-msg.cc
src/test/mon/test_mon_workloadgen.cc

index 229aceda3f26b6c5a55a1181a6e61951c3e7c62a..ad7ab6c8e23a1505f4704b829726aa60f2fa31f1 100644 (file)
@@ -24,8 +24,7 @@
 #include "common/version.h"
 #include "common/dout.h"
 #include "common/debug.h"
-#include "common/Cond.h"
-#include "common/Mutex.h"
+#include "common/ceph_mutex.h"
 #include "common/Timer.h"
 #include "common/errno.h"
 #include "mon/MonClient.h"
@@ -55,7 +54,7 @@ protected:
   Messenger *msg;
   MonClient monc;
 
-  Mutex lock;
+  ceph::mutex lock = ceph::make_mutex("mon-msg-test::lock");
 
   set<int> wanted;
 
@@ -65,8 +64,7 @@ public:
     : Dispatcher(cct_),
       cct(cct_),
       msg(NULL),
-      monc(cct_),
-      lock("mon-msg-test::lock")
+      monc(cct_)
   { }
 
 
@@ -222,12 +220,11 @@ class MonMsgTest : public MonClientHelper,
 protected:
   int reply_type = 0;
   Message *reply_msg = nullptr;
-  Mutex lock;
-  Cond cond;
+  ceph::mutex lock = ceph::make_mutex("lock");
+  ceph::condition_variable cond;
 
   MonMsgTest() :
-    MonClientHelper(g_ceph_context),
-    lock("lock") { }
+    MonClientHelper(g_ceph_context) { }
 
 public:
   void SetUp() override {
@@ -248,36 +245,33 @@ public:
   }
 
   void handle_wanted(Message *m) override {
-    lock.Lock();
+    std::lock_guard l{lock};
     // caller will put() after they call us, so hold on to a ref
     m->get();
     reply_msg = m;
-    cond.Signal();
-    lock.Unlock();
+    cond.notify_all();
   }
 
   Message *send_wait_reply(Message *m, int t, double timeout=30.0) {
-    lock.Lock();
+    std::unique_lock l{lock};
     reply_type = t;
     add_wanted(t);
     send_message(m);
 
-    int err = 0;
+    std::cv_status status = std::cv_status::no_timeout;
     if (timeout > 0) {
-      utime_t cond_timeout;
-      cond_timeout.set_from_double(timeout);
       utime_t s = ceph_clock_now();
-      err = cond.WaitInterval(lock, cond_timeout);
+      status = cond.wait_for(l, ceph::make_timespan(timeout));
       utime_t e = ceph_clock_now();
       dout(20) << __func__ << " took " << (e-s) << " seconds" << dendl;
     } else {
-      err = cond.Wait(lock);
+      cond.wait(l);
     }
     rm_wanted(t);
-    lock.Unlock();
-    if (err > 0) {
-      dout(20) << __func__ << " error: " << cpp_strerror(err) << dendl;
-      return (Message*)((long)-err);
+    l.unlock();
+    if (status == std::cv_status::timeout) {
+      dout(20) << __func__ << " error: " << cpp_strerror(ETIMEDOUT) << dendl;
+      return (Message*)((long)-ETIMEDOUT);
     }
 
     if (!reply_msg)
index 70b23dd3a9357988b51026845b0708a408b899a6..15792a63af5816f76ad338eda2c19126411f5f68 100644 (file)
@@ -46,8 +46,7 @@
 #include "common/config.h"
 #include "common/debug.h"
 #include "common/errno.h"
-#include "common/Cond.h"
-#include "common/Mutex.h"
+#include "common/ceph_mutex.h"
 #include "common/strtol.h"
 #include "common/LogEntry.h"
 #include "auth/KeyRing.h"
@@ -85,8 +84,8 @@ class TestStub : public Dispatcher
   MessengerRef messenger;
   MonClient monc;
 
-  Mutex lock;
-  Cond cond;
+  ceph::mutex lock;
+  ceph::condition_variable cond;
   SafeTimer timer;
 
   bool do_shutdown;
@@ -153,7 +152,7 @@ class TestStub : public Dispatcher
   virtual int init() = 0;
 
   virtual int shutdown() {
-    Mutex::Locker l(lock);
+    std::lock_guard l{lock};
     do_shutdown = true;
     int r = _shutdown();
     if (r < 0) {
@@ -179,7 +178,7 @@ class TestStub : public Dispatcher
   TestStub(CephContext *cct, string who)
     : Dispatcher(cct),
       monc(cct),
-      lock(who.append("::lock").c_str()),
+      lock(ceph::make_mutex(who.append("::lock"))),
       timer(cct, lock),
       do_shutdown(false),
       tick_seconds(0.0) { }
@@ -192,12 +191,12 @@ class ClientStub : public TestStub
 
  protected:
   bool ms_dispatch(Message *m) override {
-    Mutex::Locker l(lock);
+    std::lock_guard l{lock};
     dout(1) << "client::" << __func__ << " " << *m << dendl;
     switch (m->get_type()) {
     case CEPH_MSG_OSD_MAP:
       objecter->handle_osd_map((MOSDMap*)m);
-      cond.Signal();
+      cond.notify_all();
       break;
     }
     return true;
@@ -205,19 +204,19 @@ class ClientStub : public TestStub
 
   void ms_handle_connect(Connection *con) override {
     dout(1) << "client::" << __func__ << " " << con << dendl;
-    Mutex::Locker l(lock);
+    std::lock_guard l{lock};
     objecter->ms_handle_connect(con);
   }
 
   void ms_handle_remote_reset(Connection *con) override {
     dout(1) << "client::" << __func__ << " " << con << dendl;
-    Mutex::Locker l(lock);
+    std::lock_guard l{lock};
     objecter->ms_handle_remote_reset(con);
   }
 
   bool ms_handle_reset(Connection *con) override {
     dout(1) << "client::" << __func__ << dendl;
-    Mutex::Locker l(lock);
+    std::lock_guard l{lock};
     objecter->ms_handle_reset(con);
     return false;
   }
@@ -289,11 +288,11 @@ class ClientStub : public TestStub
     objecter->set_client_incarnation(0);
     objecter->start();
 
-    lock.Lock();
+    lock.lock();
     timer.init();
     monc.renew_subs();
 
-    lock.Unlock();
+    lock.unlock();
 
     objecter->wait_for_osd_map();
 
@@ -390,7 +389,7 @@ class OSDStub : public TestStub
 
   int init() override {
     dout(10) << __func__ << dendl;
-    Mutex::Locker l(lock);
+    std::lock_guard l{lock};
 
     dout(1) << __func__ << " fsid " << monc.monmap.fsid
            << " osd_fsid " << g_conf()->osd_uuid << dendl;
@@ -914,8 +913,8 @@ double const OSDStub::STUB_BOOT_INTERVAL = 10.0;
 
 const char *our_name = NULL;
 vector<TestStub*> stubs;
-Mutex shutdown_lock("main::shutdown_lock");
-Cond shutdown_cond;
+ceph::mutex shutdown_lock = ceph::make_mutex("main::shutdown_lock");
+ceph::condition_variable shutdown_cond;
 Context *shutdown_cb = NULL;
 SafeTimer *shutdown_timer = NULL;
 
@@ -923,7 +922,7 @@ struct C_Shutdown : public Context
 {
   void finish(int r) override {
     generic_dout(10) << "main::shutdown time has ran out" << dendl;
-    shutdown_cond.Signal();
+    shutdown_cond.notify_all();
   }
 };
 
@@ -933,10 +932,10 @@ void handle_test_signal(int signum)
     return;
 
   std::cerr << "*** Got signal " << sig_str(signum) << " ***" << std::endl;
-  Mutex::Locker l(shutdown_lock);
+  std::lock_guard l{shutdown_lock};
   if (shutdown_timer) {
     shutdown_timer->cancel_all_events();
-    shutdown_cond.Signal();
+    shutdown_cond.notify_all();
   }
 }
 
@@ -1067,21 +1066,20 @@ int main(int argc, const char *argv[])
   register_async_signal_handler_oneshot(SIGINT, handle_test_signal);
   register_async_signal_handler_oneshot(SIGTERM, handle_test_signal);
 
-  shutdown_lock.Lock();
-  shutdown_timer = new SafeTimer(g_ceph_context, shutdown_lock);
-  shutdown_timer->init();
-  if (duration != 0) {
-    std::cout << __func__
-           << " run test for " << duration << " seconds" << std::endl;
-    shutdown_timer->add_event_after((double) duration, new C_Shutdown);
+  {
+    unique_lock locker{shutdown_lock};
+    shutdown_timer = new SafeTimer(g_ceph_context, shutdown_lock);
+    shutdown_timer->init();
+    if (duration != 0) {
+      std::cout << __func__
+               << " run test for " << duration << " seconds" << std::endl;
+      shutdown_timer->add_event_after((double) duration, new C_Shutdown);
+    }
+    shutdown_cond.wait(locker);
+    shutdown_timer->shutdown();
+    delete shutdown_timer;
+    shutdown_timer = NULL;
   }
-  shutdown_cond.Wait(shutdown_lock);
-
-  shutdown_timer->shutdown();
-  delete shutdown_timer;
-  shutdown_timer = NULL;
-  shutdown_lock.Unlock();
-
   unregister_async_signal_handler(SIGINT, handle_test_signal);
   unregister_async_signal_handler(SIGTERM, handle_test_signal);