X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fjobs.c;h=93e864f20cc33ac1d56a7c7562cff66be96c2cf8;hb=766876ed2e18e7ab281871b890e4a2003006ad20;hp=8725d00e6cdd4246e5014ab7b6124ad6bb74ca12;hpb=b48b736097a222fdf8c6f1a33c922ee7d7bcd633;p=src%2Fapp-framework-binder.git diff --git a/src/jobs.c b/src/jobs.c index 8725d00e..93e864f2 100644 --- a/src/jobs.c +++ b/src/jobs.c @@ -50,7 +50,7 @@ typedef void (*job_cb_t)(int, void*); struct job { struct job *next; /**< link to the next job enqueued */ - void *group; /**< group of the request */ + const void *group; /**< group of the request */ job_cb_t callback; /**< processing callback */ void *arg; /**< argument */ int timeout; /**< timeout in second for processing the request */ @@ -64,6 +64,7 @@ struct events struct events *next; struct sd_event *event; uint64_t timeout; + unsigned used: 1; unsigned runs: 1; }; @@ -124,7 +125,7 @@ static struct job *free_jobs; * @return the created job unblock or NULL when no more memory */ static struct job *job_create( - void *group, + const void *group, int timeout, job_cb_t callback, void *arg) @@ -163,7 +164,7 @@ end: */ static void job_add(struct job *job) { - void *group; + const void *group; struct job *ijob, **pjob; /* prepare to add */ @@ -203,7 +204,7 @@ static inline struct job *job_get() static inline struct events *events_get() { struct events *events = first_events; - while (events && events->runs) + while (events && events->used) events = events->next; return events; } @@ -217,7 +218,7 @@ static inline struct events *events_get() static inline void job_release(struct job *job) { struct job *ijob, **pjob; - void *group; + const void *group; /* first unqueue the job */ pjob = &first_job; @@ -285,7 +286,7 @@ static void events_call(int signum, void *arg) */ static void thread_run(volatile struct thread *me) { - struct thread **prv; + struct thread **prv, *thr; struct job *job; struct events *events; uint64_t evto; @@ -299,17 +300,18 @@ static void thread_run(volatile struct thread *me) if (current) { current->lowered = 1; evto = EVENT_TIMEOUT_CHILD; + me->events = current->events; } else { started++; sig_monitor_init_timeouts(); evto = EVENT_TIMEOUT_TOP; + me->events = NULL; } me->next = threads; threads = (struct thread*)me; current = (struct thread*)me; /* loop until stopped */ - me->events = NULL; while (!me->stop) { /* get a job */ job = job_get(first_job); @@ -330,22 +332,31 @@ static void thread_run(volatile struct thread *me) /* release event if any */ events = me->events; if (events) { - events->runs = 0; + events->used = 0; me->events = NULL; } } else { /* no job, check events */ - events = events_get(); + events = me->events; + if (!events || events->runs) + events = events_get(); if (events) { /* run the events */ + events->used = 1; events->runs = 1; events->timeout = evto; me->events = events; pthread_mutex_unlock(&mutex); sig_monitor(0, events_call, events); pthread_mutex_lock(&mutex); + events->used = 0; events->runs = 0; me->events = NULL; + thr = me->upper; + while (thr && thr->events == events) { + thr->events = NULL; + thr = thr->upper; + } } else { /* no job and not events */ waiting++; @@ -422,7 +433,7 @@ static int start_one_thread() * @return 0 in case of success or -1 in case of error */ int jobs_queue( - void *group, + const void *group, int timeout, void (*callback)(int, void*), void *arg) @@ -504,7 +515,7 @@ static void call_cb(int signum, void *closure) * @see jobs_call, jobs_enter, jobs_leave */ static int do_sync( - void *group, + const void *group, int timeout, void (*sync_cb)(int signum, void *closure), struct sync *sync @@ -548,7 +559,7 @@ static int do_sync( * @return 0 on success or -1 in case of error */ int jobs_enter( - void *group, + const void *group, int timeout, void (*callback)(int signum, void *closure, struct jobloop *jobloop), void *closure @@ -600,7 +611,7 @@ int jobs_leave(struct jobloop *jobloop) * @return 0 in case of success or -1 in case of error */ int jobs_call( - void *group, + const void *group, int timeout, void (*callback)(int, void*), void *arg) @@ -644,6 +655,7 @@ struct sd_event *jobs_get_sd_event() events = malloc(sizeof *events); if (events && (rc = sd_event_new(&events->event)) >= 0) { if (nevents < started || start_one_thread() >= 0) { + events->used = 0; events->runs = 0; events->next = first_events; first_events = events; @@ -662,15 +674,14 @@ struct sd_event *jobs_get_sd_event() ERROR("creation of sd_event failed: %m"); events = NULL; errno = -rc; - } + } } } } if (events) { - /* */ me = current; if (me) { - events->runs = 1; + events->used = 1; me->events = events; } else { WARNING("event returned for unknown thread!"); @@ -689,7 +700,7 @@ struct sd_event *jobs_get_sd_event() * @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_start(int allowed_count, int start_count, int waiter_count, void (*start)()) +int jobs_start(int allowed_count, int start_count, int waiter_count, void (*start)(int signum)) { int rc, launched; struct thread me;