2 * Copyright (C) 2016, 2017 "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.
25 #include <sys/syscall.h>
30 #include <systemd/sd-event.h>
33 #include "sig-monitor.h"
37 #define _alert_ "do you really want to remove monitoring?"
38 #define sig_monitor_init_timeouts() ((void)0)
39 #define sig_monitor_clean_timeouts() ((void)0)
40 #define sig_monitor(to,cb,arg) (cb(0,arg))
43 #define EVENT_TIMEOUT_TOP ((uint64_t)-1)
44 #define EVENT_TIMEOUT_CHILD ((uint64_t)10000)
46 /** Internal shortcut for callback */
47 typedef void (*job_cb_t)(int, void*);
49 /** Description of a pending job */
52 struct job *next; /**< link to the next job enqueued */
53 const void *group; /**< group of the request */
54 job_cb_t callback; /**< processing callback */
55 void *arg; /**< argument */
56 int timeout; /**< timeout in second for processing the request */
57 unsigned blocked: 1; /**< is an other request blocking this one ? */
58 unsigned dropped: 1; /**< is removed ? */
61 /** Description of handled event loops */
65 struct sd_event *event;
74 /** Description of threads */
77 struct thread *next; /**< next thread of the list */
78 struct thread *upper; /**< upper same thread */
79 struct job *job; /**< currently processed job */
80 pthread_t tid; /**< the thread id */
81 unsigned stop: 1; /**< stop requested */
82 unsigned waits: 1; /**< is waiting? */
86 * Description of synchonous callback
90 struct thread thread; /**< thread loop data */
92 void (*callback)(int, void*); /**< the synchronous callback */
93 void (*enter)(int signum, void *closure, struct jobloop *jobloop);
94 /**< the entering synchronous routine */
96 void *arg; /**< the argument of the callback */
100 /* synchronisation of threads */
101 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
102 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
104 /* count allowed, started and waiting threads */
105 static int allowed = 0; /** allowed count of threads */
106 static int started = 0; /** started count of threads */
107 static int waiting = 0; /** waiting count of threads */
108 static int remains = 0; /** allowed count of waiting jobs */
109 static int nevents = 0; /** count of events */
111 /* list of threads */
112 static struct thread *threads;
113 static _Thread_local struct thread *current_thread;
114 static _Thread_local struct events *current_events;
116 /* queue of pending jobs */
117 static struct job *first_job;
118 static struct events *first_events;
119 static struct job *free_jobs;
122 * Create a new job with the given parameters
123 * @param group the group of the job
124 * @param timeout the timeout of the job (0 if none)
125 * @param callback the function that achieves the job
126 * @param arg the argument of the callback
127 * @return the created job unblock or NULL when no more memory
129 static struct job *job_create(
137 /* try recyle existing job */
140 free_jobs = job->next;
142 /* allocation without blocking */
143 pthread_mutex_unlock(&mutex);
144 job = malloc(sizeof *job);
145 pthread_mutex_lock(&mutex);
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 */
180 if (group && ijob->group == group)
191 * Get the next job to process or NULL if none.
192 * @return the first job that isn't blocked or NULL
194 static inline struct job *job_get()
196 struct job *job = first_job;
197 while (job && job->blocked)
203 * Get the next events to process or NULL if none.
204 * @return the first events that isn't running or NULL
206 static inline struct events *events_get()
208 struct events *events = first_events;
209 while (events && events->state != Available)
210 events = events->next;
215 * Releases the processed 'job': removes it
216 * from the list of jobs and unblock the first
217 * pending job of the same group if any.
218 * @param job the job to release
220 static inline void job_release(struct job *job)
222 struct job *ijob, **pjob;
225 /* first unqueue the job */
228 while (ijob != job) {
234 /* then unblock jobs of the same group */
238 while (ijob && ijob->group != group)
244 /* recycle the job */
245 job->next = free_jobs;
250 * Monitored cancel callback for a job.
251 * This function is called by the monitor
252 * to cancel the job when the safe environment
254 * @param signum 0 on normal flow or the number
255 * of the signal that interrupted the normal
257 * @param arg the job to run
259 static void job_cancel(int signum, void *arg)
261 struct job *job = arg;
262 job->callback(SIGABRT, job->arg);
266 * Monitored normal callback for events.
267 * This function is called by the monitor
268 * to run the event loop when the safe environment
270 * @param signum 0 on normal flow or the number
271 * of the signal that interrupted the normal
273 * @param arg the events to run
275 static void events_call(int signum, void *arg)
279 struct events *events = arg;
283 rc = sd_event_prepare(se);
286 ERROR("sd_event_prepare returned an error (state: %d): %m", sd_event_get_state(events->event));
289 rc = sd_event_wait(se, events->timeout);
292 ERROR("sd_event_wait returned an error (state: %d): %m", sd_event_get_state(events->event));
297 rc = sd_event_dispatch(se);
300 ERROR("sd_event_dispatch returned an error (state: %d): %m", sd_event_get_state(events->event));
308 * Main processing loop of threads processing jobs.
309 * The loop must be called with the mutex locked
310 * and it returns with the mutex locked.
311 * @param me the description of the thread to use
312 * TODO: how are timeout handled when reentering?
314 static void thread_run(volatile struct thread *me)
318 struct events *events;
321 /* initialize description of itself and link it in the list */
322 me->tid = pthread_self();
325 me->upper = current_thread;
326 if (current_thread) {
327 evto = EVENT_TIMEOUT_CHILD;
330 sig_monitor_init_timeouts();
331 evto = EVENT_TIMEOUT_TOP;
334 threads = (struct thread*)me;
335 current_thread = (struct thread*)me;
337 /* loop until stopped */
340 job = job_get(first_job);
342 /* prepare running the job */
343 remains++; /* increases count of job that can wait */
344 job->blocked = 1; /* mark job as blocked */
345 me->job = job; /* record the job (only for terminate) */
348 pthread_mutex_unlock(&mutex);
349 sig_monitor(job->timeout, job->callback, job->arg);
350 pthread_mutex_lock(&mutex);
352 /* release event if any */
353 events = current_events;
354 if (events && events->state == Modifiable) {
355 current_events = NULL;
356 events->state = Available;
359 /* release the run job */
362 /* no job, check events */
363 events = current_events;
365 events = events_get();
366 else if (events->state == Locked)
370 events->state = Locked;
371 events->timeout = evto;
372 current_events = events;
373 pthread_mutex_unlock(&mutex);
374 sig_monitor(0, events_call, events);
375 pthread_mutex_lock(&mutex);
376 current_events = NULL;
377 events->state = Available;
379 /* no job and not events */
382 pthread_cond_wait(&cond, &mutex);
389 /* unlink the current thread and cleanup */
394 current_thread = me->upper;
395 if (!current_thread) {
396 sig_monitor_clean_timeouts();
402 * Entry point for created threads.
403 * @param data not used
406 static void *thread_main(void *data)
410 pthread_mutex_lock(&mutex);
412 pthread_mutex_unlock(&mutex);
417 * Starts a new thread
418 * @return 0 in case of success or -1 in case of error
420 static int start_one_thread()
425 rc = pthread_create(&tid, NULL, thread_main, NULL);
428 WARNING("not able to start thread: %m");
435 * Queues a new asynchronous job represented by 'callback' and 'arg'
436 * for the 'group' and the 'timeout'.
437 * Jobs are queued FIFO and are possibly executed in parallel
438 * concurrently except for job of the same group that are
439 * executed sequentially in FIFO order.
440 * @param group The group of the job or NULL when no group.
441 * @param timeout The maximum execution time in seconds of the job
442 * or 0 for unlimited time.
443 * @param callback The function to execute for achieving the job.
444 * Its first parameter is either 0 on normal flow
445 * or the signal number that broke the normal flow.
446 * The remaining parameter is the parameter 'arg1'
448 * @param arg The second argument for 'callback'
449 * @return 0 in case of success or -1 in case of error
454 void (*callback)(int, void*),
461 pthread_mutex_lock(&mutex);
463 /* allocates the job */
464 job = job_create(group, timeout, callback, arg);
467 info = "out of memory";
471 /* check availability */
474 info = "too many jobs";
478 /* start a thread if needed */
479 if (waiting == 0 && started < allowed) {
480 /* all threads are busy and a new can be started */
481 rc = start_one_thread();
482 if (rc < 0 && started == 0) {
483 info = "can't start first thread";
492 /* signal an existing job */
493 pthread_cond_signal(&cond);
494 pthread_mutex_unlock(&mutex);
498 job->next = free_jobs;
501 ERROR("can't process job with threads: %s, %m", info);
502 pthread_mutex_unlock(&mutex);
507 * Internal helper function for 'jobs_enter'.
508 * @see jobs_enter, jobs_leave
510 static void enter_cb(int signum, void *closure)
512 struct sync *sync = closure;
513 sync->enter(signum, sync->arg, (void*)&sync->thread);
517 * Internal helper function for 'jobs_call'.
520 static void call_cb(int signum, void *closure)
522 struct sync *sync = closure;
523 sync->callback(signum, sync->arg);
524 jobs_leave((void*)&sync->thread);
528 * Internal helper for synchronous jobs. It enters
529 * a new thread loop for evaluating the given job
530 * as recorded by the couple 'sync_cb' and 'sync'.
531 * @see jobs_call, jobs_enter, jobs_leave
536 void (*sync_cb)(int signum, void *closure),
542 pthread_mutex_lock(&mutex);
544 /* allocates the job */
545 job = job_create(group, timeout, sync_cb, sync);
547 ERROR("out of memory");
549 pthread_mutex_unlock(&mutex);
556 /* run until stopped */
557 thread_run(&sync->thread);
558 pthread_mutex_unlock(&mutex);
563 * Enter a synchronisation point: activates the job given by 'callback'
564 * and 'closure' using 'group' and 'timeout' to control sequencing and
566 * @param group the group for sequencing jobs
567 * @param timeout the time in seconds allocated to the job
568 * @param callback the callback that will handle the job.
569 * it receives 3 parameters: 'signum' that will be 0
570 * on normal flow or the catched signal number in case
571 * of interrupted flow, the context 'closure' as given and
572 * a 'jobloop' reference that must be used when the job is
573 * terminated to unlock the current execution flow.
574 * @param arg the argument to the callback
575 * @return 0 on success or -1 in case of error
580 void (*callback)(int signum, void *closure, struct jobloop *jobloop),
586 sync.enter = callback;
588 return do_sync(group, timeout, enter_cb, &sync);
592 * Unlocks the execution flow designed by 'jobloop'.
593 * @param jobloop indication of the flow to unlock
594 * @return 0 in case of success of -1 on error
596 int jobs_leave(struct jobloop *jobloop)
600 pthread_mutex_lock(&mutex);
602 while (t && t != (struct thread*)jobloop)
609 pthread_cond_broadcast(&cond);
611 pthread_mutex_unlock(&mutex);
616 * Calls synchronously the job represented by 'callback' and 'arg1'
617 * for the 'group' and the 'timeout' and waits for its completion.
618 * @param group The group of the job or NULL when no group.
619 * @param timeout The maximum execution time in seconds of the job
620 * or 0 for unlimited time.
621 * @param callback The function to execute for achieving the job.
622 * Its first parameter is either 0 on normal flow
623 * or the signal number that broke the normal flow.
624 * The remaining parameter is the parameter 'arg1'
626 * @param arg The second argument for 'callback'
627 * @return 0 in case of success or -1 in case of error
632 void (*callback)(int, void*),
637 sync.callback = callback;
640 return do_sync(group, timeout, call_cb, &sync);
644 * Gets a sd_event item for the current thread.
645 * @return a sd_event or NULL in case of error
647 struct sd_event *jobs_get_sd_event()
649 struct events *events;
652 pthread_mutex_lock(&mutex);
654 /* search events on stack */
655 events = current_events;
657 /* search an available events */
658 events = events_get();
660 /* not found, check if creation possible */
661 if (nevents >= allowed) {
662 ERROR("not possible to add a new event");
665 events = malloc(sizeof *events);
666 if (events && (rc = sd_event_new(&events->event)) >= 0) {
667 if (nevents < started || start_one_thread() >= 0) {
668 events->state = Available;
669 events->next = first_events;
670 first_events = events;
672 ERROR("can't start thread for events");
673 sd_event_unref(events->event);
679 ERROR("out of memory");
683 ERROR("creation of sd_event failed: %m");
691 events->state = Modifiable;
693 WARNING("event returned for unknown thread!");
694 current_events = events;
697 pthread_mutex_unlock(&mutex);
698 return events ? events->event : NULL;
702 * Enter the jobs processing loop.
703 * @param allowed_count Maximum count of thread for jobs including this one
704 * @param start_count Count of thread to start now, must be lower.
705 * @param waiter_count Maximum count of jobs that can be waiting.
706 * @param start The start routine to activate (can't be NULL)
707 * @return 0 in case of success or -1 in case of error.
709 int jobs_start(int allowed_count, int start_count, int waiter_count, void (*start)(int signum))
715 assert(allowed_count >= 1);
716 assert(start_count >= 0);
717 assert(waiter_count > 0);
718 assert(start_count <= allowed_count);
721 pthread_mutex_lock(&mutex);
723 /* check whether already running */
724 if (current_thread || allowed) {
725 ERROR("thread already started");
731 if (sig_monitor_init() < 0) {
732 ERROR("failed to initialise signal handlers");
736 /* records the allowed count */
737 allowed = allowed_count;
740 remains = waiter_count;
742 /* start at least one thread */
744 while ((launched + 1) < start_count) {
745 if (start_one_thread() != 0) {
746 ERROR("Not all threads can be started");
752 /* queue the start job */
753 job = job_create(NULL, 0, (job_cb_t)start, NULL);
755 ERROR("out of memory");
766 pthread_mutex_unlock(&mutex);
771 * Terminate all the threads and cancel all pending jobs.
773 void jobs_terminate()
775 struct job *job, *head, *tail;
776 pthread_t me, *others;
783 /* request all threads to stop */
784 pthread_mutex_lock(&mutex);
787 /* count the number of threads */
791 if (!t->upper && !pthread_equal(t->tid, me))
796 /* fill the array of threads */
797 others = alloca(count * sizeof *others);
801 if (!t->upper && !pthread_equal(t->tid, me))
802 others[count++] = t->tid;
806 /* stops the threads */
813 /* wait the threads */
814 pthread_cond_broadcast(&cond);
815 pthread_mutex_unlock(&mutex);
817 pthread_join(others[--count], NULL);
818 pthread_mutex_lock(&mutex);
820 /* cancel pending jobs of other threads */
830 /* search if job is stacked for current */
832 while (t && t->job != job)
835 /* yes, relink it at end */
843 /* no cancel the job */
844 pthread_mutex_unlock(&mutex);
845 sig_monitor(0, job_cancel, job);
847 pthread_mutex_lock(&mutex);
850 pthread_mutex_unlock(&mutex);