Add 'afb_daemon_queue_job' for bindings
authorJosé Bollo <jose.bollo@iot.bzh>
Tue, 25 Apr 2017 15:21:28 +0000 (17:21 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Tue, 25 Apr 2017 15:21:28 +0000 (17:21 +0200)
This allow bindings to queue asynchronous jobs.

Change-Id: I2a228388c9defde23adb672d579c4a299f212850
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
include/afb/afb-binding.h
src/afb-ditf.c
src/afb-hook.c
src/afb-hook.h

index fe347af..9c84515 100644 (file)
@@ -67,6 +67,7 @@ struct afb_daemon_itf {
        struct afb_event (*event_make)(void *closure, const char *name); /* creates an event of 'name' */
        int (*rootdir_get_fd)(void *closure);
        int (*rootdir_open_locale)(void *closure, const char *filename, int flags, const char *locale);
+       int (*queue_job)(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout);
 };
 
 /*
@@ -193,4 +194,20 @@ static inline int afb_daemon_rootdir_open_locale(struct afb_daemon daemon, const
        return daemon.itf->rootdir_open_locale(daemon.closure, filename, flags, locale);
 }
 
-
+/*
+ * Queue the job defined by 'callback' and 'argument' for being executed asynchronously
+ * in this thread (later) or in an other thread.
+ * If 'group' is not NUL, the jobs queued with a same value (as the pointer value 'group')
+ * are executed in sequence in the order of there submission.
+ * If 'timeout' is not 0, it represent the maximum execution time for the job in seconds.
+ * At first, the job is called with 0 as signum and the given argument.
+ * The job is executed with the monitoring of its time and some signals like SIGSEGV and
+ * SIGFPE. When a such signal is catched, the job is terminated and reexecuted but with
+ * signum being the signal number (SIGALRM when timeout expired).
+ *
+ * Returns 0 in case of success or -1 in case of error.
+ */
+static inline int afb_daemon_queue_job(struct afb_daemon daemon, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
+{
+       return daemon.itf->queue_job(daemon.closure, callback, argument, group, timeout);
+}
index 012ef02..9d65d30 100644 (file)
@@ -29,6 +29,7 @@
 #include "afb-evt.h"
 #include "afb-common.h"
 #include "afb-hook.h"
+#include "jobs.h"
 #include "verbose.h"
 
 /**********************************************
@@ -93,6 +94,11 @@ static int rootdir_open_locale_cb(void *closure, const char *filename, int flags
        return afb_common_rootdir_open_locale(filename, flags, locale);
 }
 
+static int queue_job_cb(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
+{
+       return jobs_queue(group, timeout, callback, argument);
+}
+
 /**********************************************
 * hooked flow
 **********************************************/
@@ -165,6 +171,13 @@ static int hooked_rootdir_open_locale_cb(void *closure, const char *filename, in
        return afb_hook_ditf_rootdir_open_locale(ditf, filename, flags, locale, r);
 }
 
+static int hooked_queue_job_cb(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
+{
+       struct afb_ditf *ditf = closure;
+       int r = queue_job_cb(closure, callback, argument, group, timeout);
+       return afb_hook_ditf_queue_job(ditf, callback, argument, group, timeout, r);
+}
+
 static const struct afb_daemon_itf daemon_itf = {
        .vverbose = old_vverbose_cb,
        .event_make = event_make_cb,
@@ -173,7 +186,8 @@ static const struct afb_daemon_itf daemon_itf = {
        .get_user_bus = afb_common_get_user_bus,
        .get_system_bus = afb_common_get_system_bus,
        .rootdir_get_fd = afb_common_rootdir_get_fd,
-       .rootdir_open_locale = rootdir_open_locale_cb
+       .rootdir_open_locale = rootdir_open_locale_cb,
+       .queue_job = queue_job_cb
 };
 
 static const struct afb_daemon_itf hooked_daemon_itf = {
@@ -184,7 +198,8 @@ static const struct afb_daemon_itf hooked_daemon_itf = {
        .get_user_bus = hooked_get_user_bus,
        .get_system_bus = hooked_get_system_bus,
        .rootdir_get_fd = hooked_rootdir_get_fd,
-       .rootdir_open_locale = hooked_rootdir_open_locale_cb
+       .rootdir_open_locale = hooked_rootdir_open_locale_cb,
+       .queue_job = hooked_queue_job_cb
 };
 
 void afb_ditf_init(struct afb_ditf *ditf, const char *prefix)
index 2e0255d..545673f 100644 (file)
@@ -554,6 +554,10 @@ static void hook_ditf_rootdir_open_locale_cb(void *closure, const struct afb_dit
        }
 }
 
+static void hook_ditf_queue_job(void *closure, const struct afb_ditf *ditf, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result)
+{
+       _hook_ditf_(ditf, "queue_job(%p, %p, %p, %d) -> %d", callback, argument, group, timeout, result);
+}
 
 static struct afb_hook_ditf_itf hook_ditf_default_itf = {
        .hook_ditf_event_broadcast_before = hook_ditf_event_broadcast_before_cb,
@@ -565,6 +569,7 @@ static struct afb_hook_ditf_itf hook_ditf_default_itf = {
        .hook_ditf_event_make = hook_ditf_event_make_cb,
        .hook_ditf_rootdir_get_fd = hook_ditf_rootdir_get_fd_cb,
        .hook_ditf_rootdir_open_locale = hook_ditf_rootdir_open_locale_cb,
+       .hook_ditf_queue_job = hook_ditf_queue_job
 };
 
 /******************************************************************************
@@ -637,7 +642,11 @@ int afb_hook_ditf_rootdir_open_locale(const struct afb_ditf *ditf, const char *f
        return result;
 }
 
-
+int afb_hook_ditf_queue_job(const struct afb_ditf *ditf, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result)
+{
+       _HOOK_DITF_(queue_job, ditf, callback, argument, group, timeout, result);
+       return result;
+}
 
 /******************************************************************************
  * section: 
index 46dd55b..d1f6371 100644 (file)
@@ -141,6 +141,7 @@ extern int afb_hook_xreq_subcallsync_result(const struct afb_xreq *xreq, int sta
 #define afb_hook_flag_ditf_get_system_bus              0x000040
 #define afb_hook_flag_ditf_rootdir_get_fd              0x000080
 #define afb_hook_flag_ditf_rootdir_open_locale         0x000100
+#define afb_hook_flag_ditf_queue_job                   0x000200
 
 #define afb_hook_flags_ditf_common     (afb_hook_flag_ditf_vverbose\
                                        |afb_hook_flag_ditf_event_make\
@@ -150,20 +151,22 @@ extern int afb_hook_xreq_subcallsync_result(const struct afb_xreq *xreq, int sta
                                        |afb_hook_flag_ditf_get_user_bus\
                                        |afb_hook_flag_ditf_get_system_bus\
                                        |afb_hook_flag_ditf_rootdir_get_fd\
-                                       |afb_hook_flag_ditf_rootdir_open_locale)
+                                       |afb_hook_flag_ditf_rootdir_open_locale\
+                                       |afb_hook_flag_ditf_queue_job)
 
 #define afb_hook_flags_ditf_all                (afb_hook_flags_ditf_common|afb_hook_flags_ditf_extra)
 
 struct afb_hook_ditf_itf {
-       void (*hook_ditf_event_broadcast_before)(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object);
-       void (*hook_ditf_event_broadcast_after)(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object, int result);
-       void (*hook_ditf_get_event_loop)(void *closure, const struct afb_ditf *ditf, struct sd_event *result);
-       void (*hook_ditf_get_user_bus)(void *closure, const struct afb_ditf *ditf, struct sd_bus *result);
-       void (*hook_ditf_get_system_bus)(void *closure, const struct afb_ditf *ditf, struct sd_bus *result);
-       void (*hook_ditf_vverbose)(void*closure, const struct afb_ditf *ditf, int level, const char *file, int line, const char *function, const char *fmt, va_list args);
-       void (*hook_ditf_event_make)(void *closure, const struct afb_ditf *ditf, const char *name, struct afb_event result);
-       void (*hook_ditf_rootdir_get_fd)(void *closure, const struct afb_ditf *ditf, int result);
-       void (*hook_ditf_rootdir_open_locale)(void *closure, const struct afb_ditf *ditf, const char *filename, int flags, const char *locale, int result);
+       void (*hook_ditf_event_broadcast_before)(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object);
+       void (*hook_ditf_event_broadcast_after)(void *closure, const struct afb_ditf *ditf, const char *name, struct json_object *object, int result);
+       void (*hook_ditf_get_event_loop)(void *closure, const struct afb_ditf *ditf, struct sd_event *result);
+       void (*hook_ditf_get_user_bus)(void *closure, const struct afb_ditf *ditf, struct sd_bus *result);
+       void (*hook_ditf_get_system_bus)(void *closure, const struct afb_ditf *ditf, struct sd_bus *result);
+       void (*hook_ditf_vverbose)(void*closure, const struct afb_ditf *ditf, int level, const char *file, int line, const char *function, const char *fmt, va_list args);
+       void (*hook_ditf_event_make)(void *closure, const struct afb_ditf *ditf, const char *name, struct afb_event result);
+       void (*hook_ditf_rootdir_get_fd)(void *closure, const struct afb_ditf *ditf, int result);
+       void (*hook_ditf_rootdir_open_locale)(void *closure, const struct afb_ditf *ditf, const char *filename, int flags, const char *locale, int result);
+       void (*hook_ditf_queue_job)(void *closure, const struct afb_ditf *ditf, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result);
 };
 
 extern void afb_hook_ditf_event_broadcast_before(const struct afb_ditf *ditf, const char *name, struct json_object *object);
@@ -175,6 +178,7 @@ extern void afb_hook_ditf_vverbose(const struct afb_ditf *ditf, int level, const
 extern struct afb_event afb_hook_ditf_event_make(const struct afb_ditf *ditf, const char *name, struct afb_event result);
 extern int afb_hook_ditf_rootdir_get_fd(const struct afb_ditf *ditf, int result);
 extern int afb_hook_ditf_rootdir_open_locale(const struct afb_ditf *ditf, const char *filename, int flags, const char *locale, int result);
+extern int afb_hook_ditf_queue_job(const struct afb_ditf *ditf, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout, int result);
 
 extern int afb_hook_flags_ditf(const char *api);
 extern struct afb_hook_ditf *afb_hook_create_ditf(const char *api, int flags, struct afb_hook_ditf_itf *itf, void *closure);