-
-
-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;
-}
-
-/**
- * run the jobs as
- * @param allowed_count Maximum count of thread for jobs including this one
- * @param start_count Count of thread to start now, must be lower.
- * @param waiter_count Maximum count of jobs that can be waiting.
- * @param start The start routine to activate (can't be NULL)
- * @return 0 in case of success or -1 in case of error.
- */
-int jobs_enter(int allowed_count, int start_count, int waiter_count, void (*start)())
-{
- /* start */
- if (sig_monitor_init() < 0) {
- ERROR("failed to initialise signal handlers");
- return -1;
- }
-
- /* init job processing */
- if (jobs_init(allowed_count, start_count, waiter_count) < 0) {
- ERROR("failed to initialise threading");
- return -1;
- }
-
- /* queue the start job */
- if (jobs_queue0(NULL, 0, (void(*)(int))start) < 0) {
- ERROR("failed to start runnning jobs");
- return -1;
- }
-
- /* turn as processing thread */
- return jobs_add_me();
-};