From: José Bollo Date: Thu, 14 Feb 2019 08:37:26 +0000 (+0100) Subject: system & jobs: Reverse link and acquiring events X-Git-Tag: 7.99.1~30 X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?p=src%2Fapp-framework-binder.git;a=commitdiff_plain;h=c330ecf92fef81fdb0471715326b8bc6b4d52bc6 system & jobs: Reverse link and acquiring events - 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 --- diff --git a/src/afb-api-dbus.c b/src/afb-api-dbus.c index 8c55ed01..dccfbbd8 100644 --- a/src/afb-api-dbus.c +++ b/src/afb-api-dbus.c @@ -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; diff --git a/src/afb-export.c b/src/afb-export.c index b5a4b9e3..37089566 100644 --- a/src/afb-export.c +++ b/src/afb-export.c @@ -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, diff --git a/src/afb-fdev.c b/src/afb-fdev.c index fa1a402f..a5f95660 100644 --- a/src/afb-fdev.c +++ b/src/afb-fdev.c @@ -17,10 +17,12 @@ #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); } diff --git a/src/jobs.c b/src/jobs.c index f30904a8..217c5391 100644 --- a/src/jobs.c +++ b/src/jobs.c @@ -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; } /** diff --git a/src/jobs.h b/src/jobs.h index 03d52796..a99c9622 100644 --- a/src/jobs.h +++ b/src/jobs.h @@ -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(); + diff --git a/src/systemd.c b/src/systemd.c index 1adb75ab..29d626bc 100644 --- a/src/systemd.c +++ b/src/systemd.c @@ -25,41 +25,53 @@ #include #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); } diff --git a/src/watchdog.c b/src/watchdog.c index f9afd640..92144ba0 100644 --- a/src/watchdog.c +++ b/src/watchdog.c @@ -27,12 +27,15 @@ #include #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; }