From: José Bollo Date: Mon, 10 Apr 2017 19:35:57 +0000 (+0200) Subject: Make HelloWord binding multi-thread X-Git-Tag: dab_3.99.1~76 X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?p=src%2Fapp-framework-binder.git;a=commitdiff_plain;h=8342fcc144bd8f1d9a24c1a018c15440dd43481d Make HelloWord binding multi-thread 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 --- 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)