system & jobs: Reverse link and acquiring events
[src/app-framework-binder.git] / src / jobs.c
index f5c9dde..217c539 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016, 2017, 2018 "IoT.bzh"
+ * Copyright (C) 2016-2019 "IoT.bzh"
  * Author José Bollo <jose.bollo@iot.bzh>
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
 
 #define _GNU_SOURCE
 
-#if defined(NO_JOBS_WATCHDOG)
-#   define HAS_WATCHDOG 0
-#else
-#   define HAS_WATCHDOG 1
-#endif
-
 #include <stdlib.h>
 #include <stdint.h>
 #include <unistd.h>
 #include <sys/eventfd.h>
 
 #include <systemd/sd-event.h>
-#include "fdev.h"
-#if HAS_WATCHDOG
-#include <systemd/sd-daemon.h>
-#endif
 
 #include "jobs.h"
 #include "sig-monitor.h"
 #include "verbose.h"
-
-#if defined(REMOVE_SYSTEMD_EVENT)
-#include "fdev-epoll.h"
-#endif
+#include "systemd.h"
 
 #define EVENT_TIMEOUT_TOP      ((uint64_t)-1)
 #define EVENT_TIMEOUT_CHILD    ((uint64_t)10000)
@@ -75,7 +62,6 @@ struct evloop
        unsigned state;        /**< encoded state */
        int efd;               /**< event notification */
        struct sd_event *sdev; /**< the systemd event loop */
-       struct fdev *fdev;     /**< handling of events */
        struct thread *holder; /**< holder of the evloop */
 };
 
@@ -131,11 +117,6 @@ static struct job *free_jobs;
 /* event loop */
 static struct evloop evloop;
 
-#if defined(REMOVE_SYSTEMD_EVENT)
-static struct fdev_epoll *fdevepoll;
-static int waitevt;
-#endif
-
 /**
  * Create a new job with the given parameters
  * @param group    the group of the job
@@ -272,23 +253,6 @@ static void job_cancel(int signum, void *arg)
        job->callback(SIGABRT, job->arg);
 }
 
-#if defined(REMOVE_SYSTEMD_EVENT)
-/**
- * Gets a fdev_epoll item.
- * @return a fdev_epoll or NULL in case of error
- */
-static struct fdev_epoll *get_fdevepoll()
-{
-       struct fdev_epoll *result;
-
-       result = fdevepoll;
-       if (!result)
-               result = fdevepoll = fdev_epoll_create();
-
-       return result;
-}
-#endif
-
 /**
  * Monitored normal callback for events.
  * This function is called by the monitor
@@ -363,7 +327,7 @@ static void evloop_release()
 {
        struct thread *nh, *ct = current_thread;
 
-       if (evloop.holder == ct) {
+       if (ct && evloop.holder == ct) {
                nh = ct->nholder;
                evloop.holder = nh;
                if (nh)
@@ -381,6 +345,9 @@ static int evloop_get()
        if (evloop.holder)
                return evloop.holder == ct;
 
+       if (!evloop.sdev)
+               return 0;
+
        ct->nholder = NULL;
        evloop.holder = ct;
        return 1;
@@ -417,29 +384,13 @@ static void evloop_acquire()
        }
 }
 
-#if defined(REMOVE_SYSTEMD_EVENT)
-/**
- * Monitored normal loop for waiting events.
- * @param signum 0 on normal flow or the number
- *               of the signal that interrupted the normal
- *               flow
- * @param arg     the events to run
- */
-static void monitored_wait_and_dispatch(int signum, void *arg)
-{
-       struct fdev_epoll *fdev_epoll = arg;
-       if (!signum) {
-               fdev_epoll_wait_and_dispatch(fdev_epoll, -1);
-       }
-}
-#endif
-
 /**
  * Enter the thread
  * @param me the description of the thread to enter
  */
 static void thread_enter(volatile struct thread *me)
 {
+       evloop_release();
        /* initialize description of itself and link it in the list */
        me->tid = pthread_self();
        me->stop = 0;
@@ -501,7 +452,6 @@ static void thread_run_internal(volatile struct thread *me)
 
                        /* release the run job */
                        job_release(job);
-#if !defined(REMOVE_SYSTEMD_EVENT)
                /* no job, check event loop wait */
                } else if (evloop_get()) {
                        if (evloop.state != 0) {
@@ -524,24 +474,6 @@ static void thread_run_internal(volatile struct thread *me)
                        pthread_cond_wait(&cond, &mutex);
                        me->waits = 0;
                        running++;
-#else
-               } else if (waitevt) {
-                       /* no job and not events */
-                       running--;
-                       if (!running)
-                               ERROR("Entering job deep sleep! Check your bindings.");
-                       me->waits = 1;
-                       pthread_cond_wait(&cond, &mutex);
-                       me->waits = 0;
-                       running++;
-               } else {
-                       /* wait for events */
-                       waitevt = 1;
-                       pthread_mutex_unlock(&mutex);
-                       sig_monitor(0, monitored_wait_and_dispatch, get_fdevepoll());
-                       pthread_mutex_lock(&mutex);
-                       waitevt = 0;
-#endif
                }
        }
        /* cleanup */
@@ -833,15 +765,6 @@ static int on_evloop_efd(sd_event_source *s, int fd, uint32_t revents, void *use
        return 1;
 }
 
-/* temporary hack */
-#if !defined(REMOVE_SYSTEMD_EVENT)
-__attribute__((unused))
-#endif
-static void evloop_callback(void *arg, uint32_t event, struct fdev *fdev)
-{
-       sig_monitor(0, evloop_run, arg);
-}
-
 /**
  * Gets a sd_event item for the current thread.
  * @return a sd_event or NULL in case of error
@@ -861,42 +784,21 @@ 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");
-#if !defined(REMOVE_SYSTEMD_EVENT)
-                       sd_event_unref(evloop.sdev);
                        evloop.sdev = NULL;
 error2:
                        close(evloop.efd);
 error1:
                        return NULL;
                }
-#else
-                       goto error3;
-               }
-               /* handle the event loop */
-               evloop.fdev = fdev_epoll_add(get_fdevepoll(), sd_event_get_fd(evloop.sdev));
-               if (!evloop.fdev) {
-                       ERROR("can't create fdev");
-error3:
-                       sd_event_unref(evloop.sdev);
-error2:
-                       close(evloop.efd);
-error1:
-                       memset(&evloop, 0, sizeof evloop);
-                       return NULL;
-               }
-               fdev_set_autoclose(evloop.fdev, 0);
-               fdev_set_events(evloop.fdev, EPOLLIN);
-               fdev_set_callback(evloop.fdev, evloop_callback, NULL);
-#endif
        }
 
        /* acquire the event loop */
@@ -906,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 */
@@ -922,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 */
@@ -941,26 +841,7 @@ struct sd_event *jobs_get_sd_event()
                evloop_release();
                current_thread = NULL;
        }
-
-       return result;
-}
-
-#if defined(REMOVE_SYSTEMD_EVENT)
-/**
- * Gets the fdev_epoll item.
- * @return a fdev_epoll or NULL in case of error
- */
-struct fdev_epoll *jobs_get_fdev_epoll()
-{
-       struct fdev_epoll *result;
-
-       pthread_mutex_lock(&mutex);
-       result = get_fdevepoll();
-       pthread_mutex_unlock(&mutex);
-
-       return result;
 }
-#endif
 
 /**
  * Enter the jobs processing loop.
@@ -996,12 +877,6 @@ int jobs_start(int allowed_count, int start_count, int waiter_count, void (*star
        running = 0;
        remains = waiter_count;
 
-#if HAS_WATCHDOG
-       /* set the watchdog */
-       if (sd_watchdog_enabled(0, NULL))
-               sd_event_set_watchdog(get_sd_event_locked(), 1);
-#endif
-
        /* start at least one thread: the current one */
        launched = 1;
        while (launched < start_count) {