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)
43 /** Internal shortcut for callback */
44 typedef void (*job_cb_t)(int, void*);
46 /** starting mode for jobs */
49 Start_Default, /**< Start a thread if more than one jobs is pending */
50 Start_Urgent, /**< Always start a thread */
51 Start_Lazy /**< Never start a thread */
54 /** Description of a pending job */
57 struct job *next; /**< link to the next job enqueued */
58 const void *group; /**< group of the request */
59 job_cb_t callback; /**< processing callback */
60 void *arg; /**< argument */
61 int timeout; /**< timeout in second for processing the request */
62 unsigned blocked: 1; /**< is an other request blocking this one ? */
63 unsigned dropped: 1; /**< is removed ? */
66 /** Description of threads */
69 struct thread *next; /**< next thread of the list */
70 struct thread *upper; /**< upper same thread */
71 struct thread *nholder;/**< next holder for evloop */
72 pthread_cond_t *cwhold;/**< condition wait for holding */
73 struct job *job; /**< currently processed job */
74 pthread_t tid; /**< the thread id */
75 volatile unsigned stop: 1; /**< stop requested */
76 volatile unsigned waits: 1; /**< is waiting? */
77 volatile unsigned leaved: 1; /**< was leaved? */
81 * Description of synchronous callback
85 struct thread thread; /**< thread loop data */
87 void (*callback)(int, void*); /**< the synchronous callback */
88 void (*enter)(int signum, void *closure, struct jobloop *jobloop);
89 /**< the entering synchronous routine */
91 void *arg; /**< the argument of the callback */
94 /* synchronisation of threads */
95 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
96 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
98 /* counts for threads */
99 static int allowed_thread_count = 0; /** allowed count of threads */
100 static int started_thread_count = 0; /** started count of threads */
101 static int busy_thread_count = 0; /** count of busy threads */
103 /* list of threads */
104 static struct thread *threads;
105 static _Thread_local struct thread *current_thread;
107 /* counts for jobs */
108 static int remaining_job_count = 0; /** count of job that can be created */
109 static int allowed_job_count = 0; /** allowed count of pending jobs */
111 /* queue of pending jobs */
112 static struct job *first_pending_job;
113 static struct job *first_free_job;
116 static struct evmgr *evmgr;
118 static void (*exit_handler)();
121 * Create a new job with the given parameters
122 * @param group the group of the job
123 * @param timeout the timeout of the job (0 if none)
124 * @param callback the function that achieves the job
125 * @param arg the argument of the callback
126 * @return the created job unblock or NULL when no more memory
128 static struct job *job_create(
136 /* try recyle existing job */
137 job = first_free_job;
139 first_free_job = job->next;
141 /* allocation without blocking */
142 pthread_mutex_unlock(&mutex);
143 job = malloc(sizeof *job);
144 pthread_mutex_lock(&mutex);
146 ERROR("out of memory");
151 /* initialises the job */
153 job->timeout = timeout;
154 job->callback = callback;
163 * Adds 'job' at the end of the list of jobs, marking it
164 * as blocked if an other job with the same group is pending.
165 * @param job the job to add
167 static void job_add(struct job *job)
170 struct job *ijob, **pjob;
176 /* search end and blockers */
177 pjob = &first_pending_job;
178 ijob = first_pending_job;
180 if (group && ijob->group == group)
188 remaining_job_count--;
192 * Get the next job to process or NULL if none.
193 * @return the first job that isn't blocked or NULL
195 static inline struct job *job_get()
197 struct job *job = first_pending_job;
198 while (job && job->blocked)
201 remaining_job_count++;
206 * Releases the processed 'job': removes it
207 * from the list of jobs and unblock the first
208 * pending job of the same group if any.
209 * @param job the job to release
211 static inline void job_release(struct job *job)
213 struct job *ijob, **pjob;
216 /* first unqueue the job */
217 pjob = &first_pending_job;
218 ijob = first_pending_job;
219 while (ijob != job) {
225 /* then unblock jobs of the same group */
229 while (ijob && ijob->group != group)
235 /* recycle the job */
236 job->next = first_free_job;
237 first_free_job = job;
241 * Monitored cancel callback for a job.
242 * This function is called by the monitor
243 * to cancel the job when the safe environment
245 * @param signum 0 on normal flow or the number
246 * of the signal that interrupted the normal
248 * @param arg the job to run
250 __attribute__((unused))
251 static void job_cancel(int signum, void *arg)
253 struct job *job = arg;
254 job->callback(SIGABRT, job->arg);
258 * wakeup the event loop if needed by sending
261 static void evloop_wakeup()
268 * Release the currently held event loop
270 static void evloop_release()
272 struct thread *nh, *ct = current_thread;
274 if (ct && evmgr && evmgr_release_if(evmgr, ct)) {
278 evmgr_try_hold(evmgr, nh);
279 pthread_cond_signal(nh->cwhold);
285 * get the eventloop for the current thread
287 static int evloop_get()
289 return evmgr && evmgr_try_hold(evmgr, current_thread);
293 * acquire the eventloop for the current thread
295 static void evloop_acquire()
297 struct thread *pwait, *ct;
300 /* try to get the evloop */
302 /* failed, init waiting state */
306 pthread_cond_init(&cond, NULL);
308 /* queue current thread in holder list */
309 pwait = evmgr_holder(evmgr);
310 while (pwait->nholder)
311 pwait = pwait->nholder;
314 /* wake up the evloop */
317 /* wait to acquire the evloop */
318 pthread_cond_wait(&cond, &mutex);
319 pthread_cond_destroy(&cond);
325 * @param me the description of the thread to enter
327 static void thread_enter(volatile struct thread *me)
330 /* initialize description of itself and link it in the list */
331 me->tid = pthread_self();
336 me->upper = current_thread;
338 threads = (struct thread*)me;
339 current_thread = (struct thread*)me;
344 * @param me the description of the thread to leave
346 static void thread_leave()
348 struct thread **prv, *me;
350 /* unlink the current thread and cleanup */
357 current_thread = me->upper;
361 * Main processing loop of internal threads with processing jobs.
362 * The loop must be called with the mutex locked
363 * and it returns with the mutex locked.
364 * @param me the description of the thread to use
365 * TODO: how are timeout handled when reentering?
367 static void thread_run_internal(volatile struct thread *me)
374 /* loop until stopped */
376 /* release the current event loop */
382 /* prepare running the job */
383 job->blocked = 1; /* mark job as blocked */
384 me->job = job; /* record the job (only for terminate) */
387 pthread_mutex_unlock(&mutex);
388 sig_monitor(job->timeout, job->callback, job->arg);
389 pthread_mutex_lock(&mutex);
391 /* release the run job */
393 /* no job, check event loop wait */
394 } else if (evloop_get()) {
395 if (!evmgr_can_run(evmgr)) {
397 CRITICAL("Can't enter dispatch while in dispatch!");
401 evmgr_prepare_run(evmgr);
402 pthread_mutex_unlock(&mutex);
403 sig_monitor(0, (void(*)(int,void*))evmgr_job_run, evmgr);
404 pthread_mutex_lock(&mutex);
406 /* no job and no event loop */
408 if (!busy_thread_count)
409 ERROR("Entering job deep sleep! Check your bindings.");
411 pthread_cond_wait(&cond, &mutex);
422 * Main processing loop of external threads.
423 * The loop must be called with the mutex locked
424 * and it returns with the mutex locked.
425 * @param me the description of the thread to use
427 static void thread_run_external(volatile struct thread *me)
432 /* loop until stopped */
435 pthread_cond_wait(&cond, &mutex);
441 * Root for created threads.
443 static void thread_main()
448 started_thread_count++;
449 sig_monitor_init_timeouts();
450 thread_run_internal(&me);
451 sig_monitor_clean_timeouts();
452 started_thread_count--;
457 * Entry point for created threads.
458 * @param data not used
461 static void *thread_starter(void *data)
463 pthread_mutex_lock(&mutex);
465 pthread_mutex_unlock(&mutex);
470 * Starts a new thread
471 * @return 0 in case of success or -1 in case of error
473 static int start_one_thread()
478 rc = pthread_create(&tid, NULL, thread_starter, NULL);
481 WARNING("not able to start thread: %m");
488 * Queues a new asynchronous job represented by 'callback' and 'arg'
489 * for the 'group' and the 'timeout'.
490 * Jobs are queued FIFO and are possibly executed in parallel
491 * concurrently except for job of the same group that are
492 * executed sequentially in FIFO order.
493 * @param group The group of the job or NULL when no group.
494 * @param timeout The maximum execution time in seconds of the job
495 * or 0 for unlimited time.
496 * @param callback The function to execute for achieving the job.
497 * Its first parameter is either 0 on normal flow
498 * or the signal number that broke the normal flow.
499 * The remaining parameter is the parameter 'arg1'
501 * @param arg The second argument for 'callback'
502 * @param start The start mode for threads
503 * @return 0 in case of success or -1 in case of error
505 static int queue_job(
508 void (*callback)(int, void*),
510 enum start_mode start_mode)
515 pthread_mutex_lock(&mutex);
517 /* check availability */
518 if (remaining_job_count <= 0) {
519 ERROR("can't process job with threads: too many jobs");
524 /* allocates the job */
525 job = job_create(group, timeout, callback, arg);
529 /* start a thread if needed */
530 busy = busy_thread_count == started_thread_count;
531 if (start_mode != Start_Lazy
533 && (start_mode == Start_Urgent || remaining_job_count + started_thread_count < allowed_job_count)
534 && started_thread_count < allowed_thread_count) {
535 /* all threads are busy and a new can be started */
536 rc = start_one_thread();
537 if (rc < 0 && started_thread_count == 0) {
538 ERROR("can't start initial thread: %m");
547 /* wakeup an evloop if needed */
551 /* signal an existing job */
552 pthread_cond_signal(&cond);
553 pthread_mutex_unlock(&mutex);
557 job->next = first_free_job;
558 first_free_job = job;
560 pthread_mutex_unlock(&mutex);
565 * Queues a new asynchronous job represented by 'callback' and 'arg'
566 * for the 'group' and the 'timeout'.
567 * Jobs are queued FIFO and are possibly executed in parallel
568 * concurrently except for job of the same group that are
569 * executed sequentially in FIFO order.
570 * @param group The group of the job or NULL when no group.
571 * @param timeout The maximum execution time in seconds of the job
572 * or 0 for unlimited time.
573 * @param callback The function to execute for achieving the job.
574 * Its first parameter is either 0 on normal flow
575 * or the signal number that broke the normal flow.
576 * The remaining parameter is the parameter 'arg1'
578 * @param arg The second argument for 'callback'
579 * @return 0 in case of success or -1 in case of error
584 void (*callback)(int, void*),
587 return queue_job(group, timeout, callback, arg, Start_Default);
591 * Queues lazyly a new asynchronous job represented by 'callback' and 'arg'
592 * for the 'group' and the 'timeout'.
593 * Jobs are queued FIFO and are possibly executed in parallel
594 * concurrently except for job of the same group that are
595 * executed sequentially in FIFO order.
596 * @param group The group of the job or NULL when no group.
597 * @param timeout The maximum execution time in seconds of the job
598 * or 0 for unlimited time.
599 * @param callback The function to execute for achieving the job.
600 * Its first parameter is either 0 on normal flow
601 * or the signal number that broke the normal flow.
602 * The remaining parameter is the parameter 'arg1'
604 * @param arg The second argument for 'callback'
605 * @return 0 in case of success or -1 in case of error
610 void (*callback)(int, void*),
613 return queue_job(group, timeout, callback, arg, Start_Lazy);
617 * Queues urgently a new asynchronous job represented by 'callback' and 'arg'
618 * for the 'group' and the 'timeout'.
619 * Jobs are queued FIFO and are possibly executed in parallel
620 * concurrently except for job of the same group that are
621 * executed sequentially in FIFO order.
622 * @param group The group of the job or NULL when no group.
623 * @param timeout The maximum execution time in seconds of the job
624 * or 0 for unlimited time.
625 * @param callback The function to execute for achieving the job.
626 * Its first parameter is either 0 on normal flow
627 * or the signal number that broke the normal flow.
628 * The remaining parameter is the parameter 'arg1'
630 * @param arg The second argument for 'callback'
631 * @return 0 in case of success or -1 in case of error
633 int jobs_queue_urgent(
636 void (*callback)(int, void*),
639 return queue_job(group, timeout, callback, arg, Start_Urgent);
643 * Internal helper function for 'jobs_enter'.
644 * @see jobs_enter, jobs_leave
646 static void enter_cb(int signum, void *closure)
648 struct sync *sync = closure;
649 sync->enter(signum, sync->arg, (void*)&sync->thread);
653 * Internal helper function for 'jobs_call'.
656 static void call_cb(int signum, void *closure)
658 struct sync *sync = closure;
659 sync->callback(signum, sync->arg);
660 jobs_leave((void*)&sync->thread);
664 * Internal helper for synchronous jobs. It enters
665 * a new thread loop for evaluating the given job
666 * as recorded by the couple 'sync_cb' and 'sync'.
667 * @see jobs_call, jobs_enter, jobs_leave
672 void (*sync_cb)(int signum, void *closure),
678 pthread_mutex_lock(&mutex);
680 /* allocates the job */
681 job = job_create(group, timeout, sync_cb, sync);
683 pthread_mutex_unlock(&mutex);
690 /* run until stopped */
692 thread_run_internal(&sync->thread);
694 thread_run_external(&sync->thread);
695 pthread_mutex_unlock(&mutex);
696 if (sync->thread.leaved)
703 * Enter a synchronisation point: activates the job given by 'callback'
704 * and 'closure' using 'group' and 'timeout' to control sequencing and
706 * @param group the group for sequencing jobs
707 * @param timeout the time in seconds allocated to the job
708 * @param callback the callback that will handle the job.
709 * it receives 3 parameters: 'signum' that will be 0
710 * on normal flow or the catched signal number in case
711 * of interrupted flow, the context 'closure' as given and
712 * a 'jobloop' reference that must be used when the job is
713 * terminated to unlock the current execution flow.
714 * @param closure the argument to the callback
715 * @return 0 on success or -1 in case of error
720 void (*callback)(int signum, void *closure, struct jobloop *jobloop),
726 sync.enter = callback;
728 return do_sync(group, timeout, enter_cb, &sync);
732 * Unlocks the execution flow designed by 'jobloop'.
733 * @param jobloop indication of the flow to unlock
734 * @return 0 in case of success of -1 on error
736 int jobs_leave(struct jobloop *jobloop)
740 pthread_mutex_lock(&mutex);
742 while (t && t != (struct thread*)jobloop)
750 pthread_cond_broadcast(&cond);
754 pthread_mutex_unlock(&mutex);
759 * Calls synchronously the job represented by 'callback' and 'arg1'
760 * for the 'group' and the 'timeout' and waits for its completion.
761 * @param group The group of the job or NULL when no group.
762 * @param timeout The maximum execution time in seconds of the job
763 * or 0 for unlimited time.
764 * @param callback The function to execute for achieving the job.
765 * Its first parameter is either 0 on normal flow
766 * or the signal number that broke the normal flow.
767 * The remaining parameter is the parameter 'arg1'
769 * @param arg The second argument for 'callback'
770 * @return 0 in case of success or -1 in case of error
775 void (*callback)(int, void*),
780 sync.callback = callback;
783 return do_sync(group, timeout, call_cb, &sync);
787 * Ensure that the current running thread can control the event loop.
789 void jobs_acquire_event_manager()
793 /* ensure an existing thread environment */
794 if (!current_thread) {
795 memset(<, 0, sizeof lt);
796 current_thread = <
800 pthread_mutex_lock(&mutex);
802 /* creates the evloop on need */
804 evmgr_create(&evmgr);
806 /* acquire the event loop under lock */
811 pthread_mutex_unlock(&mutex);
813 /* release the faked thread environment if needed */
814 if (current_thread == <) {
816 * Releasing it is needed because there is no way to guess
817 * when it has to be released really. But here is where it is
818 * hazardous: if the caller modifies the eventloop when it
819 * is waiting, there is no way to make the change effective.
820 * A workaround to achieve that goal is for the caller to
821 * require the event loop a second time after having modified it.
823 NOTICE("Requiring event manager/loop from outside of binder's callback is hazardous!");
824 if (verbose_wants(Log_Level_Info))
825 sig_monitor_dumpstack();
827 current_thread = NULL;
832 * Enter the jobs processing loop.
833 * @param allowed_count Maximum count of thread for jobs including this one
834 * @param start_count Count of thread to start now, must be lower.
835 * @param waiter_count Maximum count of jobs that can be waiting.
836 * @param start The start routine to activate (can't be NULL)
837 * @return 0 in case of success or -1 in case of error.
843 void (*start)(int signum, void* arg),
849 assert(allowed_count >= 1);
850 assert(start_count >= 0);
851 assert(waiter_count > 0);
852 assert(start_count <= allowed_count);
855 pthread_mutex_lock(&mutex);
857 /* check whether already running */
858 if (current_thread || allowed_thread_count) {
859 ERROR("thread already started");
864 /* records the allowed count */
865 allowed_thread_count = allowed_count;
866 started_thread_count = 0;
867 busy_thread_count = 0;
868 remaining_job_count = waiter_count;
869 allowed_job_count = waiter_count;
871 /* start at least one thread: the current one */
873 while (launched < start_count) {
874 if (start_one_thread() != 0) {
875 ERROR("Not all threads can be started");
881 /* queue the start job */
882 job = job_create(NULL, 0, start, arg);
891 pthread_mutex_unlock(&mutex);
898 * Exit jobs threads and call handler if not NULL.
900 void jobs_exit(void (*handler)())
904 /* request all threads to stop */
905 pthread_mutex_lock(&mutex);
907 /* set the handler */
908 exit_handler = handler;
910 /* stops the threads */
917 /* wake up the threads */
919 pthread_cond_broadcast(&cond);
922 pthread_mutex_unlock(&mutex);