This allow bindings to queue asynchronous jobs.
Change-Id: I2a228388c9defde23adb672d579c4a299f212850
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
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);
};
/*
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);
+}
#include "afb-evt.h"
#include "afb-common.h"
#include "afb-hook.h"
+#include "jobs.h"
#include "verbose.h"
/**********************************************
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
**********************************************/
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,
.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 = {
.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)
}
}
+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,
.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
};
/******************************************************************************
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:
#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\
|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);
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);