From 8342fcc144bd8f1d9a24c1a018c15440dd43481d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Bollo?= Date: Mon, 10 Apr 2017 21:35:57 +0200 Subject: [PATCH] Make HelloWord binding multi-thread MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Handling of event was not compatible with multi threaded. This implmentation solves this by protecting event access with mutexes. Change-Id: Ie52216289000f1ae6352c9dda442dfbda1ebe850 Signed-off-by: José Bollo --- bindings/samples/HelloWorld.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/bindings/samples/HelloWorld.c b/bindings/samples/HelloWorld.c index 3432d9f4..c16bb92a 100644 --- a/bindings/samples/HelloWorld.c +++ b/bindings/samples/HelloWorld.c @@ -17,11 +17,14 @@ #define _GNU_SOURCE #include #include +#include + #include #include const struct afb_binding_interface *interface; +static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; struct event { @@ -211,48 +214,56 @@ static void eventadd (struct afb_req request) const char *tag = afb_req_value(request, "tag"); const char *name = afb_req_value(request, "name"); + pthread_mutex_lock(&mutex); if (tag == NULL || name == NULL) afb_req_fail(request, "failed", "bad arguments"); else if (0 != event_add(tag, name)) afb_req_fail(request, "failed", "creation error"); else afb_req_success(request, NULL, NULL); + pthread_mutex_unlock(&mutex); } static void eventdel (struct afb_req request) { const char *tag = afb_req_value(request, "tag"); + pthread_mutex_lock(&mutex); if (tag == NULL) afb_req_fail(request, "failed", "bad arguments"); else if (0 != event_del(tag)) afb_req_fail(request, "failed", "deletion error"); else afb_req_success(request, NULL, NULL); + pthread_mutex_unlock(&mutex); } static void eventsub (struct afb_req request) { const char *tag = afb_req_value(request, "tag"); + pthread_mutex_lock(&mutex); if (tag == NULL) afb_req_fail(request, "failed", "bad arguments"); else if (0 != event_subscribe(request, tag)) afb_req_fail(request, "failed", "subscription error"); else afb_req_success(request, NULL, NULL); + pthread_mutex_unlock(&mutex); } static void eventunsub (struct afb_req request) { const char *tag = afb_req_value(request, "tag"); + pthread_mutex_lock(&mutex); if (tag == NULL) afb_req_fail(request, "failed", "bad arguments"); else if (0 != event_unsubscribe(request, tag)) afb_req_fail(request, "failed", "unsubscription error"); else afb_req_success(request, NULL, NULL); + pthread_mutex_unlock(&mutex); } static void eventpush (struct afb_req request) @@ -261,12 +272,14 @@ static void eventpush (struct afb_req request) const char *data = afb_req_value(request, "data"); json_object *object = data ? json_tokener_parse(data) : NULL; + pthread_mutex_lock(&mutex); if (tag == NULL) afb_req_fail(request, "failed", "bad arguments"); else if (0 > event_push(object, tag)) afb_req_fail(request, "failed", "push error"); else afb_req_success(request, NULL, NULL); + pthread_mutex_unlock(&mutex); } static void exitnow (struct afb_req request) -- 2.16.6