+ if (t->waits)
+ pthread_cond_broadcast(&cond);
+ else
+ evloop_wakeup();
+ }
+ pthread_mutex_unlock(&mutex);
+ return -!t;
+}
+
+/**
+ * Calls synchronously the job represented by 'callback' and 'arg1'
+ * for the 'group' and the 'timeout' and waits for its completion.
+ * @param group The group of the job or NULL when no group.
+ * @param timeout The maximum execution time in seconds of the job
+ * or 0 for unlimited time.
+ * @param callback The function to execute for achieving the job.
+ * Its first parameter is either 0 on normal flow
+ * or the signal number that broke the normal flow.
+ * The remaining parameter is the parameter 'arg1'
+ * given here.
+ * @param arg The second argument for 'callback'
+ * @return 0 in case of success or -1 in case of error
+ */
+int jobs_call(
+ const void *group,
+ int timeout,
+ void (*callback)(int, void*),
+ void *arg)
+{
+ struct sync sync;
+
+ sync.callback = callback;
+ sync.arg = arg;
+
+ return do_sync(group, timeout, call_cb, &sync);
+}
+
+/**
+ * Internal callback for evloop management.
+ * The effect of this function is hidden: it exits
+ * the waiting poll if any. Then it wakes up a thread
+ * awaiting the evloop using signal.
+ */
+static int on_evloop_efd(sd_event_source *s, int fd, uint32_t revents, void *userdata)
+{
+ evloop_on_efd_event();
+ 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
+ */
+static struct sd_event *get_sd_event_locked()
+{
+ int rc;
+
+ /* creates the evloop on need */
+ if (!evloop.sdev) {
+ /* start the creation */
+ evloop.state = 0;
+ /* creates the eventfd for waking up polls */
+ evloop.efd = eventfd(0, EFD_CLOEXEC|EFD_SEMAPHORE);
+ if (evloop.efd < 0) {
+ ERROR("can't make eventfd for events");
+ goto error1;
+ }
+ /* create the systemd event loop */
+ rc = sd_event_new(&evloop.sdev);
+ if (rc < 0) {
+ ERROR("can't make new 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