aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarcus Fritzsch <marcus_fritzsch@mentor.com>2017-09-01 14:39:28 +0200
committerMarcus Fritzsch <marcus_fritzsch@mentor.com>2017-09-14 14:04:51 +0200
commitb4cf01ec167f17c5df96906741fb4c6690ed9164 (patch)
tree21833e8eff442d5e607b3c02f430b835055d9858
parent49fe14f956c0bef44c047b7d6fc98412abaedace (diff)
Guard dispatch using a mutex, remove dipatch() timeout parameter
Signed-off-by: Marcus Fritzsch <marcus_fritzsch@mentor.com>
-rw-r--r--AFBClient.cpp26
-rw-r--r--AFBClient.h11
2 files changed, 20 insertions, 17 deletions
diff --git a/AFBClient.cpp b/AFBClient.cpp
index d3e3a5e..0452d16 100644
--- a/AFBClient.cpp
+++ b/AFBClient.cpp
@@ -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",
diff --git a/AFBClient.h b/AFBClient.h
index 6625c44..b1694b8 100644
--- a/AFBClient.h
+++ b/AFBClient.h
@@ -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);