-/**
- * Adds the current thread to the pool of threads
- * processing the jobs. Returns normally when the threads are
- * terminated or immediately with an error if the thread is
- * already in the pool.
- * @return 0 in case of success or -1 in case of error
- */
-int jobs_add_me()
-{
- struct thread me;
-
- /* check whether already running */
- if (current) {
- ERROR("thread already running");
- errno = EINVAL;
- return -1;
- }
-
- /* allowed... */
- pthread_mutex_lock(&mutex);
- allowed++;
- thread_run(&me);
- allowed--;
- pthread_mutex_unlock(&mutex);
- return 0;
-}
-
-
-struct sd_event *jobs_get_sd_event()
-{
- struct events *events;
- struct thread *me;
- int rc;
-
- pthread_mutex_lock(&mutex);
-
- /* search events on stack */
- me = current;
- while (me && !me->events)
- me = me->upper;
- if (me)
- /* return the stacked events */
- events = me->events;
- else {
- /* search an available events */
- events = events_get();
- if (!events) {
- /* not found, check if creation possible */
- if (nevents >= allowed) {
- ERROR("not possible to add a new event");
- events = NULL;
- } else {
- events = malloc(sizeof *events);
- if (events && (rc = sd_event_new(&events->event)) >= 0) {
- if (nevents < started || start_one_thread() >= 0) {
- events->runs = 0;
- events->next = first_events;
- first_events = events;
- } else {
- ERROR("can't start thread for events");
- sd_event_unref(events->event);
- free(events);
- events = NULL;
- }
- } else {
- if (!events)
- ERROR("out of memory");
- else {
- free(events);
- ERROR("creation of sd_event failed: %m");
- events = NULL;
- errno = -rc;
- }
- }
- }
- }
- if (events) {
- /* */
- me = current;
- if (me) {
- events->runs = 1;
- me->events = events;
- } else {
- WARNING("event returned for unknown thread!");
- }
- }
- }
- pthread_mutex_unlock(&mutex);
- return events ? events->event : NULL;
-}
-