2 * Copyright (C) 2016-2019 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
26 #include <sys/syscall.h>
30 #include <sys/eventfd.h>
32 #include <systemd/sd-event.h>
36 #include "sig-monitor.h"
40 #define EVENT_TIMEOUT_TOP ((uint64_t)-1)
41 #define EVENT_TIMEOUT_CHILD ((uint64_t)10000)
45 /** Internal shortcut for callback */
46 typedef void (*job_cb_t)(int, void*);
48 /** Description of a pending job */
51 struct job *next; /**< link to the next job enqueued */
52 const void *group; /**< group of the request */
53 job_cb_t callback; /**< processing callback */
54 void *arg; /**< argument */
55 int timeout; /**< timeout in second for processing the request */
56 unsigned blocked: 1; /**< is an other request blocking this one ? */
57 unsigned dropped: 1; /**< is removed ? */
60 /** Description of threads */
63 struct thread *next; /**< next thread of the list */
64 struct thread *upper; /**< upper same thread */
65 struct thread *nholder;/**< next holder for evloop */
66 pthread_cond_t *cwhold;/**< condition wait for holding */
67 struct job *job; /**< currently processed job */
68 pthread_t tid; /**< the thread id */
69 volatile unsigned stop: 1; /**< stop requested */
70 volatile unsigned waits: 1; /**< is waiting? */
71 volatile unsigned leaved: 1; /**< was leaved? */
75 * Description of synchronous callback
79 struct thread thread; /**< thread loop data */
81 void (*callback)(int, void*); /**< the synchronous callback */
82 void (*enter)(int signum, void *closure, struct jobloop *jobloop);
83 /**< the entering synchronous routine */
85 void *arg; /**< the argument of the callback */
88 /* synchronisation of threads */
89 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
90 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
92 /* count allowed, started and running threads */
93 static int allowed = 0; /** allowed count of threads */
94 static int started = 0; /** started count of threads */
95 static int running = 0; /** running count of threads */
96 static int remains = 0; /** allowed count of waiting jobs */
99 static struct thread *threads;
100 static _Thread_local struct thread *current_thread;
102 /* queue of pending jobs */
103 static struct job *first_job;
104 static struct job *free_jobs;
107 static struct evmgr *evmgr;
109 static void (*exit_handler)();
112 * Create a new job with the given parameters
113 * @param group the group of the job
114 * @param timeout the timeout of the job (0 if none)
115 * @param callback the function that achieves the job
116 * @param arg the argument of the callback
117 * @return the created job unblock or NULL when no more memory
119 static struct job *job_create(
127 /* try recyle existing job */
130 free_jobs = job->next;
132 /* allocation without blocking */
133 pthread_mutex_unlock(&mutex);
134 job = malloc(sizeof *job);
135 pthread_mutex_lock(&mutex);
137 ERROR("out of memory");
142 /* initialises the job */
144 job->timeout = timeout;
145 job->callback = callback;
154 * Adds 'job' at the end of the list of jobs, marking it
155 * as blocked if an other job with the same group is pending.
156 * @param job the job to add
158 static void job_add(struct job *job)
161 struct job *ijob, **pjob;
167 /* search end and blockers */
171 if (group && ijob->group == group)
183 * Get the next job to process or NULL if none.
184 * @return the first job that isn't blocked or NULL
186 static inline struct job *job_get()
188 struct job *job = first_job;
189 while (job && job->blocked)
197 * Releases the processed 'job': removes it
198 * from the list of jobs and unblock the first
199 * pending job of the same group if any.
200 * @param job the job to release
202 static inline void job_release(struct job *job)
204 struct job *ijob, **pjob;
207 /* first unqueue the job */
210 while (ijob != job) {
216 /* then unblock jobs of the same group */
220 while (ijob && ijob->group != group)
226 /* recycle the job */
227 job->next = free_jobs;
232 * Monitored cancel callback for a job.
233 * This function is called by the monitor
234 * to cancel the job when the safe environment
236 * @param signum 0 on normal flow or the number
237 * of the signal that interrupted the normal
239 * @param arg the job to run
241 __attribute__((unused))
242 static void job_cancel(int signum, void *arg)
244 struct job *job = arg;
245 job->callback(SIGABRT, job->arg);
249 * wakeup the event loop if needed by sending
252 static void evloop_wakeup()
259 * Release the currently held event loop
261 static void evloop_release()
263 struct thread *nh, *ct = current_thread;
265 if (ct && evmgr && evmgr_release_if(evmgr, ct)) {
269 evmgr_try_hold(evmgr, nh);
270 pthread_cond_signal(nh->cwhold);
276 * get the eventloop for the current thread
278 static int evloop_get()
280 return evmgr && evmgr_try_hold(evmgr, current_thread);
284 * acquire the eventloop for the current thread
286 static void evloop_acquire()
288 struct thread *pwait, *ct;
291 /* try to get the evloop */
293 /* failed, init waiting state */
297 pthread_cond_init(&cond, NULL);
299 /* queue current thread in holder list */
300 pwait = evmgr_holder(evmgr);
301 while (pwait->nholder)
302 pwait = pwait->nholder;
305 /* wake up the evloop */
308 /* wait to acquire the evloop */
309 pthread_cond_wait(&cond, &mutex);
310 pthread_cond_destroy(&cond);
316 * @param me the description of the thread to enter
318 static void thread_enter(volatile struct thread *me)
321 /* initialize description of itself and link it in the list */
322 me->tid = pthread_self();
327 me->upper = current_thread;
329 threads = (struct thread*)me;
330 current_thread = (struct thread*)me;
335 * @param me the description of the thread to leave
337 static void thread_leave()
339 struct thread **prv, *me;
341 /* unlink the current thread and cleanup */
348 current_thread = me->upper;
352 * Main processing loop of internal threads with processing jobs.
353 * The loop must be called with the mutex locked
354 * and it returns with the mutex locked.
355 * @param me the description of the thread to use
356 * TODO: how are timeout handled when reentering?
358 static void thread_run_internal(volatile struct thread *me)
365 /* loop until stopped */
367 /* release the current event loop */
373 /* prepare running the job */
374 job->blocked = 1; /* mark job as blocked */
375 me->job = job; /* record the job (only for terminate) */
378 pthread_mutex_unlock(&mutex);
379 sig_monitor(job->timeout, job->callback, job->arg);
380 pthread_mutex_lock(&mutex);
382 /* release the run job */
384 /* no job, check event loop wait */
385 } else if (evloop_get()) {
386 if (!evmgr_can_run(evmgr)) {
388 CRITICAL("Can't enter dispatch while in dispatch!");
392 evmgr_prepare_run(evmgr);
393 pthread_mutex_unlock(&mutex);
394 sig_monitor(0, (void(*)(int,void*))evmgr_job_run, evmgr);
395 pthread_mutex_lock(&mutex);
397 /* no job and no event loop */
400 ERROR("Entering job deep sleep! Check your bindings.");
402 pthread_cond_wait(&cond, &mutex);
413 * Main processing loop of external threads.
414 * The loop must be called with the mutex locked
415 * and it returns with the mutex locked.
416 * @param me the description of the thread to use
418 static void thread_run_external(volatile struct thread *me)
423 /* loop until stopped */
426 pthread_cond_wait(&cond, &mutex);
432 * Root for created threads.
434 static void thread_main()
440 sig_monitor_init_timeouts();
441 thread_run_internal(&me);
442 sig_monitor_clean_timeouts();
448 * Entry point for created threads.
449 * @param data not used
452 static void *thread_starter(void *data)
454 pthread_mutex_lock(&mutex);
456 pthread_mutex_unlock(&mutex);
461 * Starts a new thread
462 * @return 0 in case of success or -1 in case of error
464 static int start_one_thread()
469 rc = pthread_create(&tid, NULL, thread_starter, NULL);
472 WARNING("not able to start thread: %m");
479 * Queues a new asynchronous job represented by 'callback' and 'arg'
480 * for the 'group' and the 'timeout'.
481 * Jobs are queued FIFO and are possibly executed in parallel
482 * concurrently except for job of the same group that are
483 * executed sequentially in FIFO order.
484 * @param group The group of the job or NULL when no group.
485 * @param timeout The maximum execution time in seconds of the job
486 * or 0 for unlimited time.
487 * @param callback The function to execute for achieving the job.
488 * Its first parameter is either 0 on normal flow
489 * or the signal number that broke the normal flow.
490 * The remaining parameter is the parameter 'arg1'
492 * @param arg The second argument for 'callback'
493 * @param start Allow to start a thread if not zero
494 * @return 0 in case of success or -1 in case of error
496 static int queue_job(
499 void (*callback)(int, void*),
506 pthread_mutex_lock(&mutex);
508 /* allocates the job */
509 job = job_create(group, timeout, callback, arg);
513 /* check availability */
515 ERROR("can't process job with threads: too many jobs");
520 /* start a thread if needed */
521 if (start && running == started && started < allowed) {
522 /* all threads are busy and a new can be started */
523 rc = start_one_thread();
524 if (rc < 0 && started == 0) {
525 ERROR("can't start initial thread: %m");
533 /* signal an existing job */
534 pthread_cond_signal(&cond);
535 pthread_mutex_unlock(&mutex);
539 job->next = free_jobs;
542 pthread_mutex_unlock(&mutex);
547 * Queues a new asynchronous job represented by 'callback' and 'arg'
548 * for the 'group' and the 'timeout'.
549 * Jobs are queued FIFO and are possibly executed in parallel
550 * concurrently except for job of the same group that are
551 * executed sequentially in FIFO order.
552 * @param group The group of the job or NULL when no group.
553 * @param timeout The maximum execution time in seconds of the job
554 * or 0 for unlimited time.
555 * @param callback The function to execute for achieving the job.
556 * Its first parameter is either 0 on normal flow
557 * or the signal number that broke the normal flow.
558 * The remaining parameter is the parameter 'arg1'
560 * @param arg The second argument for 'callback'
561 * @return 0 in case of success or -1 in case of error
566 void (*callback)(int, void*),
569 return queue_job(group, timeout, callback, arg, 1);
573 * Internal helper function for 'jobs_enter'.
574 * @see jobs_enter, jobs_leave
576 static void enter_cb(int signum, void *closure)
578 struct sync *sync = closure;
579 sync->enter(signum, sync->arg, (void*)&sync->thread);
583 * Internal helper function for 'jobs_call'.
586 static void call_cb(int signum, void *closure)
588 struct sync *sync = closure;
589 sync->callback(signum, sync->arg);
590 jobs_leave((void*)&sync->thread);
594 * Internal helper for synchronous jobs. It enters
595 * a new thread loop for evaluating the given job
596 * as recorded by the couple 'sync_cb' and 'sync'.
597 * @see jobs_call, jobs_enter, jobs_leave
602 void (*sync_cb)(int signum, void *closure),
608 pthread_mutex_lock(&mutex);
610 /* allocates the job */
611 job = job_create(group, timeout, sync_cb, sync);
613 pthread_mutex_unlock(&mutex);
620 /* run until stopped */
622 thread_run_internal(&sync->thread);
624 thread_run_external(&sync->thread);
625 pthread_mutex_unlock(&mutex);
626 if (sync->thread.leaved)
633 * Enter a synchronisation point: activates the job given by 'callback'
634 * and 'closure' using 'group' and 'timeout' to control sequencing and
636 * @param group the group for sequencing jobs
637 * @param timeout the time in seconds allocated to the job
638 * @param callback the callback that will handle the job.
639 * it receives 3 parameters: 'signum' that will be 0
640 * on normal flow or the catched signal number in case
641 * of interrupted flow, the context 'closure' as given and
642 * a 'jobloop' reference that must be used when the job is
643 * terminated to unlock the current execution flow.
644 * @param closure the argument to the callback
645 * @return 0 on success or -1 in case of error
650 void (*callback)(int signum, void *closure, struct jobloop *jobloop),
656 sync.enter = callback;
658 return do_sync(group, timeout, enter_cb, &sync);
662 * Unlocks the execution flow designed by 'jobloop'.
663 * @param jobloop indication of the flow to unlock
664 * @return 0 in case of success of -1 on error
666 int jobs_leave(struct jobloop *jobloop)
670 pthread_mutex_lock(&mutex);
672 while (t && t != (struct thread*)jobloop)
680 pthread_cond_broadcast(&cond);
684 pthread_mutex_unlock(&mutex);
689 * Calls synchronously the job represented by 'callback' and 'arg1'
690 * for the 'group' and the 'timeout' and waits for its completion.
691 * @param group The group of the job or NULL when no group.
692 * @param timeout The maximum execution time in seconds of the job
693 * or 0 for unlimited time.
694 * @param callback The function to execute for achieving the job.
695 * Its first parameter is either 0 on normal flow
696 * or the signal number that broke the normal flow.
697 * The remaining parameter is the parameter 'arg1'
699 * @param arg The second argument for 'callback'
700 * @return 0 in case of success or -1 in case of error
705 void (*callback)(int, void*),
710 sync.callback = callback;
713 return do_sync(group, timeout, call_cb, &sync);
717 * Ensure that the current running thread can control the event loop.
719 void jobs_acquire_event_manager()
723 /* ensure an existing thread environment */
724 if (!current_thread) {
725 memset(<, 0, sizeof lt);
726 current_thread = <
730 pthread_mutex_lock(&mutex);
732 /* creates the evloop on need */
734 evmgr_create(&evmgr);
736 /* acquire the event loop under lock */
741 pthread_mutex_unlock(&mutex);
743 /* release the faked thread environment if needed */
744 if (current_thread == <) {
746 * Releasing it is needed because there is no way to guess
747 * when it has to be released really. But here is where it is
748 * hazardous: if the caller modifies the eventloop when it
749 * is waiting, there is no way to make the change effective.
750 * A workaround to achieve that goal is for the caller to
751 * require the event loop a second time after having modified it.
753 NOTICE("Requiring event manager/loop from outside of binder's callback is hazardous!");
754 if (verbose_wants(Log_Level_Info))
755 sig_monitor_dumpstack();
757 current_thread = NULL;
762 * Enter the jobs processing loop.
763 * @param allowed_count Maximum count of thread for jobs including this one
764 * @param start_count Count of thread to start now, must be lower.
765 * @param waiter_count Maximum count of jobs that can be waiting.
766 * @param start The start routine to activate (can't be NULL)
767 * @return 0 in case of success or -1 in case of error.
769 int jobs_start(int allowed_count, int start_count, int waiter_count, void (*start)(int signum, void* arg), void *arg)
774 assert(allowed_count >= 1);
775 assert(start_count >= 0);
776 assert(waiter_count > 0);
777 assert(start_count <= allowed_count);
780 pthread_mutex_lock(&mutex);
782 /* check whether already running */
783 if (current_thread || allowed) {
784 ERROR("thread already started");
789 /* records the allowed count */
790 allowed = allowed_count;
793 remains = waiter_count;
795 /* start at least one thread: the current one */
797 while (launched < start_count) {
798 if (start_one_thread() != 0) {
799 ERROR("Not all threads can be started");
805 /* queue the start job */
806 job = job_create(NULL, 0, start, arg);
815 pthread_mutex_unlock(&mutex);
822 * Exit jobs threads and call handler if not NULL.
824 void jobs_exit(void (*handler)())
828 /* request all threads to stop */
829 pthread_mutex_lock(&mutex);
831 /* set the handler */
832 exit_handler = handler;
834 /* stops the threads */
841 /* wait the threads */
842 pthread_cond_broadcast(&cond);
845 pthread_mutex_unlock(&mutex);