Lots of clean-ups and simplifications
[invirt/third/libt4.git] / rsm_client.cc
index ee25564..eed356f 100644 (file)
@@ -3,20 +3,20 @@
 #include <arpa/inet.h>
 #include <stdio.h>
 #include <handle.h>
+#include <unistd.h>
 #include "lang/verify.h"
-
+#include "lock.h"
 
 rsm_client::rsm_client(std::string dst) {
     printf("create rsm_client\n");
     std::vector<std::string> mems;
 
-    pthread_mutex_init(&rsm_client_mutex, NULL);
     sockaddr_in dstsock;
     make_sockaddr(dst.c_str(), &dstsock);
     primary = dst;
 
     {
-        ScopedLock ml(&rsm_client_mutex);
+        lock ml(rsm_client_mutex);
         VERIFY (init_members());
     }
     printf("rsm_client: done\n");
@@ -29,17 +29,17 @@ void rsm_client::primary_failure() {
 }
 
 rsm_protocol::status rsm_client::invoke(int proc, std::string req, std::string &rep) {
-    int ret;
-    ScopedLock ml(&rsm_client_mutex);
+    int ret = 0;
+    lock ml(rsm_client_mutex);
     while (1) {
         printf("rsm_client::invoke proc %x primary %s\n", proc, primary.c_str());
         handle h(primary);
 
-        VERIFY(pthread_mutex_unlock(&rsm_client_mutex)==0);
+        ml.unlock();
         rpcc *cl = h.safebind();
         if (cl)
             ret = cl->call(rsm_client_protocol::invoke, proc, req, rep, rpcc::to(5000));
-        VERIFY(pthread_mutex_lock(&rsm_client_mutex)==0);
+        ml.lock();
 
         if (!cl)
             goto prim_fail;
@@ -70,14 +70,18 @@ prim_fail:
 bool rsm_client::init_members() {
     printf("rsm_client::init_members get members!\n");
     handle h(primary);
-    VERIFY(pthread_mutex_unlock(&rsm_client_mutex)==0);
     int ret;
-    rpcc *cl = h.safebind();
-    if (cl) {
-        ret = cl->call(rsm_client_protocol::members, 0, known_mems,
-                rpcc::to(1000));
+    rpcc *cl;
+    {
+        adopt_lock ml(rsm_client_mutex);
+        ml.unlock();
+        cl = h.safebind();
+        if (cl) {
+            ret = cl->call(rsm_client_protocol::members, 0, known_mems,
+                    rpcc::to(1000));
+        }
+        ml.lock();
     }
-    VERIFY(pthread_mutex_lock(&rsm_client_mutex)==0);
     if (cl == 0 || ret != rsm_protocol::OK)
         return false;
     if (known_mems.size() < 1) {