Guard dispatch using a mutex, remove dipatch() timeout parameter
authorMarcus Fritzsch <marcus_fritzsch@mentor.com>
Fri, 1 Sep 2017 12:39:28 +0000 (14:39 +0200)
committerMarcus Fritzsch <marcus_fritzsch@mentor.com>
Thu, 14 Sep 2017 12:04:51 +0000 (14:04 +0200)
Signed-off-by: Marcus Fritzsch <marcus_fritzsch@mentor.com>
AFBClient.cpp
AFBClient.h

index d3e3a5e..0452d16 100644 (file)
@@ -7,6 +7,8 @@
 #include <cstdlib>
 #include <cstring>
 
+#include <mutex>
+
 #include <unistd.h>
 
 #include <systemd/sd-event.h>
@@ -87,13 +89,16 @@ static struct afb_wsj1_itf itf = {
     onHangup, onCall, onEvent,
 };
 
-void dispatch_internal(AFBClient *c, uint64_t timeout) {
+std::recursive_mutex dispatch_mutex;
+
+void dispatch_internal(struct sd_event *loop) {
+    std::lock_guard<std::recursive_mutex> guard(dispatch_mutex);
     TRACE();
-    c->dispatch(timeout);
+    sd_event_run(loop, -1);
 }
 
 /// object will be json_object_put
-int api_call(AFBClient *c, struct afb_wsj1 *wsj1, const char *verb,
+int api_call(struct sd_event *loop, struct afb_wsj1 *wsj1, const char *verb,
              json_object *object,
              std::function<void(bool, json_object *)> onReply) {
     TRACE();
@@ -142,7 +147,7 @@ int api_call(AFBClient *c, struct afb_wsj1 *wsj1, const char *verb,
         // if events get triggered by the call (and would be dispatched before
         // the actual call-reply).
         while (!returned) {
-            dispatch_internal(c, -1);
+            dispatch_internal(loop);
         }
 
         // return the actual API call result
@@ -219,8 +224,9 @@ fail:
     return rc;
 }
 
-int AFBClient::dispatch(uint64_t timeout) {
-    return sd_event_run(loop, timeout);
+int AFBClient::dispatch() {
+    std::lock_guard<std::recursive_mutex> guard(dispatch_mutex);
+    return sd_event_run(loop, 1);
 }
 
 int AFBClient::requestSurface(const char *label) {
@@ -230,7 +236,7 @@ int AFBClient::requestSurface(const char *label) {
     int rc = -1;
     /* send the request */
     int rc2 = api_call(
-        this, wsj1, "request_surface", jp, [&rc](bool ok, json_object *j) {
+        loop, wsj1, "request_surface", jp, [&rc](bool ok, json_object *j) {
             if (ok) {
                 int id =
                     json_object_get_int(json_object_object_get(j, "response"));
@@ -259,7 +265,7 @@ int AFBClient::activateSurface(const char *label) {
     TRACE();
     json_object *j = json_object_new_object();
     json_object_object_add(j, "drawing_name", json_object_new_string(label));
-    return api_call(this, wsj1, "activate_surface", j, [](bool ok,
+    return api_call(loop, wsj1, "activate_surface", j, [](bool ok,
                                                           json_object *j) {
         if (!ok) {
             fprintf(
@@ -274,7 +280,7 @@ int AFBClient::deactivateSurface(const char *label) {
     TRACE();
     json_object *j = json_object_new_object();
     json_object_object_add(j, "drawing_name", json_object_new_string(label));
-    return api_call(this, wsj1, "deactivate_surface", j, [](bool ok,
+    return api_call(loop, wsj1, "deactivate_surface", j, [](bool ok,
                                                             json_object *j) {
         if (!ok) {
             fprintf(
@@ -289,7 +295,7 @@ int AFBClient::endDraw(const char *label) {
     TRACE();
     json_object *j = json_object_new_object();
     json_object_object_add(j, "drawing_name", json_object_new_string(label));
-    return api_call(this, wsj1, "enddraw", j, [](bool ok, json_object *j) {
+    return api_call(loop, wsj1, "enddraw", j, [](bool ok, json_object *j) {
         if (!ok) {
             fprintf(
                 stderr, "API Call endDraw() failed: %s\n",
index 6625c44..b1694b8 100644 (file)
@@ -3,12 +3,9 @@
 
 #include <functional>
 
-extern "C"
-{
-   struct json_object;
-   struct afb_wsj1;
-   struct sd_event;
-}
+struct json_object;
+struct afb_wsj1;
+struct sd_event;
 
 class AFBClient
 {
@@ -31,7 +28,7 @@ public:
     static AFBClient &instance();
 
     int init(int port, char const *token);
-    int dispatch(uint64_t timeout);
+    int dispatch();
 
     // WM API
     int requestSurface(const char *label);