system & jobs: Reverse link and acquiring events 44/20844/1
authorJosé Bollo <jose.bollo@iot.bzh>
Thu, 14 Feb 2019 08:37:26 +0000 (09:37 +0100)
committerJose Bollo <jose.bollo@iot.bzh>
Wed, 27 Mar 2019 09:48:40 +0000 (10:48 +0100)
- The new version of 'systemd_get_event_loop' dont depends
anymore on jobs but returns a unique systemd event loop
for any threads.

- The event loop of jobs now use system.h function instead
of the opposite.

- The function 'jobs_get_sd_event' is removed

- The function 'jobs_acquire_event_manager' is introduced.
It is designed to that the current thread can manipulate
the single event manager

Change-Id: I31fe48dfe0f2cfa4d468e49338d36fea6e7e8081
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/afb-api-dbus.c
src/afb-export.c
src/afb-fdev.c
src/jobs.c
src/jobs.h
src/systemd.c
src/watchdog.c

index 8c55ed0..dccfbbd 100644 (file)
@@ -45,6 +45,7 @@
 
 #include "verbose.h"
 #include "systemd.h"
+#include "jobs.h"
 
 static const char DEFAULT_PATH_PREFIX[] = "/org/agl/afb/api/";
 
@@ -129,6 +130,7 @@ static struct api_dbus *make_api_dbus_3(int system, const char *path, size_t pat
        }
 
        /* choose the bus */
+       jobs_acquire_event_manager();
        sdbus = (system ? systemd_get_system_bus : systemd_get_user_bus)();
        if (sdbus == NULL)
                goto error2;
index b5a4b9e..3708956 100644 (file)
@@ -315,6 +315,24 @@ static int event_broadcast_cb(struct afb_api_x3 *closure, const char *name, stru
        return afb_evt_broadcast(event, object);
 }
 
+static struct sd_event *get_event_loop(struct afb_api_x3 *closure)
+{
+       jobs_acquire_event_manager();
+       return systemd_get_event_loop();
+}
+
+static struct sd_bus *get_user_bus(struct afb_api_x3 *closure)
+{
+       jobs_acquire_event_manager();
+       return systemd_get_user_bus();
+}
+
+static struct sd_bus *get_system_bus(struct afb_api_x3 *closure)
+{
+       jobs_acquire_event_manager();
+       return systemd_get_system_bus();
+}
+
 static int rootdir_open_locale_cb(struct afb_api_x3 *closure, const char *filename, int flags, const char *locale)
 {
        return afb_common_rootdir_open_locale(filename, flags, locale);
@@ -451,21 +469,30 @@ static int hooked_event_broadcast_cb(struct afb_api_x3 *closure, const char *nam
 static struct sd_event *hooked_get_event_loop(struct afb_api_x3 *closure)
 {
        struct afb_export *export = from_api_x3(closure);
-       struct sd_event *r = systemd_get_event_loop();
+       struct sd_event *r;
+
+       jobs_acquire_event_manager();
+       r = get_event_loop(closure);
        return afb_hook_api_get_event_loop(export, r);
 }
 
 static struct sd_bus *hooked_get_user_bus(struct afb_api_x3 *closure)
 {
        struct afb_export *export = from_api_x3(closure);
-       struct sd_bus *r = systemd_get_user_bus();
+       struct sd_bus *r;
+
+       jobs_acquire_event_manager();
+       r = get_user_bus(closure);
        return afb_hook_api_get_user_bus(export, r);
 }
 
 static struct sd_bus *hooked_get_system_bus(struct afb_api_x3 *closure)
 {
        struct afb_export *export = from_api_x3(closure);
-       struct sd_bus *r = systemd_get_system_bus();
+       struct sd_bus *r;
+
+       jobs_acquire_event_manager();
+       r = get_system_bus(closure);
        return afb_hook_api_get_system_bus(export, r);
 }
 
@@ -537,9 +564,9 @@ static const struct afb_daemon_itf_x1 daemon_itf = {
        .vverbose_v2 = vverbose_cb,
        .event_make = legacy_event_x1_make_cb,
        .event_broadcast = event_broadcast_cb,
-       .get_event_loop = systemd_get_event_loop,
-       .get_user_bus = systemd_get_user_bus,
-       .get_system_bus = systemd_get_system_bus,
+       .get_event_loop = get_event_loop,
+       .get_user_bus = get_user_bus,
+       .get_system_bus = get_system_bus,
        .rootdir_get_fd = afb_common_rootdir_get_fd,
        .rootdir_open_locale = rootdir_open_locale_cb,
        .queue_job = queue_job_cb,
@@ -1052,9 +1079,9 @@ static const struct afb_api_x3_itf api_x3_itf = {
 
        .vverbose = (void*)vverbose_cb,
 
-       .get_event_loop = systemd_get_event_loop,
-       .get_user_bus = systemd_get_user_bus,
-       .get_system_bus = systemd_get_system_bus,
+       .get_event_loop = get_event_loop,
+       .get_user_bus = get_user_bus,
+       .get_system_bus = get_system_bus,
        .rootdir_get_fd = afb_common_rootdir_get_fd,
        .rootdir_open_locale = rootdir_open_locale_cb,
        .queue_job = queue_job_cb,
index fa1a402..a5f9566 100644 (file)
 
 #include "fdev.h"
 
+#include "jobs.h"
 #include "systemd.h"
 #include "fdev-systemd.h"
 
 struct fdev *afb_fdev_create(int fd)
 {
+       jobs_acquire_event_manager();
        return fdev_systemd_create(systemd_get_event_loop(), fd);
 }
index f30904a..217c539 100644 (file)
@@ -34,6 +34,7 @@
 #include "jobs.h"
 #include "sig-monitor.h"
 #include "verbose.h"
+#include "systemd.h"
 
 #define EVENT_TIMEOUT_TOP      ((uint64_t)-1)
 #define EVENT_TIMEOUT_CHILD    ((uint64_t)10000)
@@ -783,16 +784,15 @@ static struct sd_event *get_sd_event_locked()
                        goto error1;
                }
                /* create the systemd event loop */
-               rc = sd_event_new(&evloop.sdev);
-               if (rc < 0) {
-                       ERROR("can't make new event loop");
+               evloop.sdev = systemd_get_event_loop();
+               if (!evloop.sdev) {
+                       ERROR("can't make event loop");
                        goto error2;
                }
                /* put the eventfd in the event loop */
                rc = sd_event_add_io(evloop.sdev, NULL, evloop.efd, EPOLLIN, on_evloop_efd, NULL);
                if (rc < 0) {
                        ERROR("can't register eventfd");
-                       sd_event_unref(evloop.sdev);
                        evloop.sdev = NULL;
 error2:
                        close(evloop.efd);
@@ -808,12 +808,10 @@ error1:
 }
 
 /**
- * Gets a sd_event item for the current thread.
- * @return a sd_event or NULL in case of error
+ * Ensure that the current running thread can control the event loop.
  */
-struct sd_event *jobs_get_sd_event()
+void jobs_acquire_event_manager()
 {
-       struct sd_event *result;
        struct thread lt;
 
        /* ensure an existing thread environment */
@@ -824,7 +822,7 @@ struct sd_event *jobs_get_sd_event()
 
        /* process */
        pthread_mutex_lock(&mutex);
-       result = get_sd_event_locked();
+       get_sd_event_locked();
        pthread_mutex_unlock(&mutex);
 
        /* release the faked thread environment if needed */
@@ -843,8 +841,6 @@ struct sd_event *jobs_get_sd_event()
                evloop_release();
                current_thread = NULL;
        }
-
-       return result;
 }
 
 /**
index 03d5279..a99c962 100644 (file)
@@ -48,5 +48,5 @@ extern int jobs_start(
                void (*start)(int signum, void* arg),
                void *arg);
 
-struct sd_event;
-extern struct sd_event *jobs_get_sd_event();
+extern void jobs_acquire_event_manager();
+
index 1adb75a..29d626b 100644 (file)
 #include <systemd/sd-daemon.h>
 
 #include "systemd.h"
-#include "jobs.h"
 
 static struct sd_bus *sdbusopen(struct sd_bus **p, int (*f)(struct sd_bus **))
 {
-       if (*p == NULL) {
-               int rc = f(p);
-               if (rc < 0) {
-                       errno = -rc;
-                       *p = NULL;
-               } else {
-                       rc = sd_bus_attach_event(*p, systemd_get_event_loop(), 0);
+       int rc;
+       struct sd_bus *r;
+
+       r = *p;
+       if (!r) {
+               rc = f(&r);
+               if (rc >= 0) {
+                       rc = sd_bus_attach_event(r, systemd_get_event_loop(), 0);
                        if (rc < 0) {
-                               sd_bus_unref(*p);
-                               errno = -rc;
-                               *p = NULL;
+                               sd_bus_unref(r);
+                               r = 0;
                        }
                }
+               if (rc < 0)
+                       errno = -rc;
+               *p = r;
        }
-       return *p;
+       return r;
 }
 
 struct sd_event *systemd_get_event_loop()
 {
-       return jobs_get_sd_event();
+       static struct sd_event *result = 0;
+       int rc;
+
+       if (!result) {
+               rc = sd_event_new(&result);
+               if (rc < 0) {
+                       errno = -rc;
+                       result = NULL;
+               }
+       }
+       return result;
 }
 
 struct sd_bus *systemd_get_user_bus()
 {
-       static struct sd_bus *result = NULL;
+       static struct sd_bus *result = 0;
        return sdbusopen((void*)&result, (void*)sd_bus_open_user);
 }
 
 struct sd_bus *systemd_get_system_bus()
 {
-       static struct sd_bus *result = NULL;
+       static struct sd_bus *result = 0;
        return sdbusopen((void*)&result, (void*)sd_bus_open_system);
 }
 
index f9afd64..92144ba 100644 (file)
 #include <systemd/sd-daemon.h>
 
 #include "jobs.h"
+#include "systemd.h"
 
 int watchdog_activate()
 {
        /* set the watchdog */
-       if (sd_watchdog_enabled(0, NULL))
-               sd_event_set_watchdog(jobs_get_sd_event(), 1);
+       if (sd_watchdog_enabled(0, NULL)) {
+               jobs_acquire_event_manager();
+               sd_event_set_watchdog(systemd_get_event_loop(), 1);
+       }
        return 0;
 }