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) {
368 WARNING("Loosing an event loop because reentering");
372 events->state = Locked;
373 events->timeout = evto;
374 current_events = events;
375 pthread_mutex_unlock(&mutex);
376 sig_monitor(0, events_call, events);
377 pthread_mutex_lock(&mutex);
378 current_events = NULL;
379 events->state = Available;
381 /* no job and not events */
384 pthread_cond_wait(&cond, &mutex);
391 /* unlink the current thread and cleanup */
396 current_thread = me->upper;
397 if (!current_thread) {
398 sig_monitor_clean_timeouts();
404 * Entry point for created threads.
405 * @param data not used
408 static void *thread_main(void *data)
412 pthread_mutex_lock(&mutex);
414 pthread_mutex_unlock(&mutex);
419 * Starts a new thread
420 * @return 0 in case of success or -1 in case of error
422 static int start_one_thread()
427 rc = pthread_create(&tid, NULL, thread_main, NULL);
430 WARNING("not able to start thread: %m");
437 * Queues a new asynchronous job represented by 'callback' and 'arg'
438 * for the 'group' and the 'timeout'.
439 * Jobs are queued FIFO and are possibly executed in parallel
440 * concurrently except for job of the same group that are
441 * executed sequentially in FIFO order.
442 * @param group The group of the job or NULL when no group.
443 * @param timeout The maximum execution time in seconds of the job
444 * or 0 for unlimited time.
445 * @param callback The function to execute for achieving the job.
446 * Its first parameter is either 0 on normal flow
447 * or the signal number that broke the normal flow.
448 * The remaining parameter is the parameter 'arg1'
450 * @param arg The second argument for 'callback'
451 * @return 0 in case of success or -1 in case of error
456 void (*callback)(int, void*),
463 pthread_mutex_lock(&mutex);
465 /* allocates the job */
466 job = job_create(group, timeout, callback, arg);
469 info = "out of memory";
473 /* check availability */
476 info = "too many jobs";
480 /* start a thread if needed */
481 if (waiting == 0 && started < allowed) {
482 /* all threads are busy and a new can be started */
483 rc = start_one_thread();
484 if (rc < 0 && started == 0) {
485 info = "can't start first thread";
494 /* signal an existing job */
495 pthread_cond_signal(&cond);
496 pthread_mutex_unlock(&mutex);
500 job->next = free_jobs;
503 ERROR("can't process job with threads: %s, %m", info);
504 pthread_mutex_unlock(&mutex);
509 * Internal helper function for 'jobs_enter'.
510 * @see jobs_enter, jobs_leave
512 static void enter_cb(int signum, void *closure)
514 struct sync *sync = closure;
515 sync->enter(signum, sync->arg, (void*)&sync->thread);
519 * Internal helper function for 'jobs_call'.
522 static void call_cb(int signum, void *closure)
524 struct sync *sync = closure;
525 sync->callback(signum, sync->arg);
526 jobs_leave((void*)&sync->thread);
530 * Internal helper for synchronous jobs. It enters
531 * a new thread loop for evaluating the given job
532 * as recorded by the couple 'sync_cb' and 'sync'.
533 * @see jobs_call, jobs_enter, jobs_leave
538 void (*sync_cb)(int signum, void *closure),
544 pthread_mutex_lock(&mutex);
546 /* allocates the job */
547 job = job_create(group, timeout, sync_cb, sync);
549 ERROR("out of memory");
551 pthread_mutex_unlock(&mutex);
558 /* run until stopped */
559 thread_run(&sync->thread);
560 pthread_mutex_unlock(&mutex);
565 * Enter a synchronisation point: activates the job given by 'callback'
566 * and 'closure' using 'group' and 'timeout' to control sequencing and
568 * @param group the group for sequencing jobs
569 * @param timeout the time in seconds allocated to the job
570 * @param callback the callback that will handle the job.
571 * it receives 3 parameters: 'signum' that will be 0
572 * on normal flow or the catched signal number in case
573 * of interrupted flow, the context 'closure' as given and
574 * a 'jobloop' reference that must be used when the job is
575 * terminated to unlock the current execution flow.
576 * @param arg the argument to the callback
577 * @return 0 on success or -1 in case of error
582 void (*callback)(int signum, void *closure, struct jobloop *jobloop),
588 sync.enter = callback;
590 return do_sync(group, timeout, enter_cb, &sync);
594 * Unlocks the execution flow designed by 'jobloop'.
595 * @param jobloop indication of the flow to unlock
596 * @return 0 in case of success of -1 on error
598 int jobs_leave(struct jobloop *jobloop)
602 pthread_mutex_lock(&mutex);
604 while (t && t != (struct thread*)jobloop)
611 pthread_cond_broadcast(&cond);
613 pthread_mutex_unlock(&mutex);
618 * Calls synchronously the job represented by 'callback' and 'arg1'
619 * for the 'group' and the 'timeout' and waits for its completion.
620 * @param group The group of the job or NULL when no group.
621 * @param timeout The maximum execution time in seconds of the job
622 * or 0 for unlimited time.
623 * @param callback The function to execute for achieving the job.
624 * Its first parameter is either 0 on normal flow
625 * or the signal number that broke the normal flow.
626 * The remaining parameter is the parameter 'arg1'
628 * @param arg The second argument for 'callback'
629 * @return 0 in case of success or -1 in case of error
634 void (*callback)(int, void*),
639 sync.callback = callback;
642 return do_sync(group, timeout, call_cb, &sync);
646 * Gets a sd_event item for the current thread.
647 * @return a sd_event or NULL in case of error
649 struct sd_event *jobs_get_sd_event()
651 struct events *events;
654 pthread_mutex_lock(&mutex);
656 /* search events on stack */
657 events = current_events;
659 /* search an available events */
660 events = events_get();
662 /* not found, check if creation possible */
663 if (nevents >= allowed) {
664 ERROR("not possible to add a new event");
667 events = malloc(sizeof *events);
668 if (events && (rc = sd_event_new(&events->event)) >= 0) {
669 if (nevents < started || start_one_thread() >= 0) {
670 events->state = Available;
671 events->next = first_events;
672 first_events = events;
674 ERROR("can't start thread for events");
675 sd_event_unref(events->event);
681 ERROR("out of memory");
685 ERROR("creation of sd_event failed: %m");
693 events->state = Modifiable;
695 WARNING("event returned for unknown thread!");
696 current_events = events;
699 pthread_mutex_unlock(&mutex);
700 return events ? events->event : NULL;
704 * Enter the jobs processing loop.
705 * @param allowed_count Maximum count of thread for jobs including this one
706 * @param start_count Count of thread to start now, must be lower.
707 * @param waiter_count Maximum count of jobs that can be waiting.
708 * @param start The start routine to activate (can't be NULL)
709 * @return 0 in case of success or -1 in case of error.
711 int jobs_start(int allowed_count, int start_count, int waiter_count, void (*start)(int signum))
717 assert(allowed_count >= 1);
718 assert(start_count >= 0);
719 assert(waiter_count > 0);
720 assert(start_count <= allowed_count);
723 pthread_mutex_lock(&mutex);
725 /* check whether already running */
726 if (current_thread || allowed) {
727 ERROR("thread already started");
733 if (sig_monitor_init() < 0) {
734 ERROR("failed to initialise signal handlers");
738 /* records the allowed count */
739 allowed = allowed_count;
742 remains = waiter_count;
744 /* start at least one thread */
746 while ((launched + 1) < start_count) {
747 if (start_one_thread() != 0) {
748 ERROR("Not all threads can be started");
754 /* queue the start job */
755 job = job_create(NULL, 0, (job_cb_t)start, NULL);
757 ERROR("out of memory");
768 pthread_mutex_unlock(&mutex);
773 * Terminate all the threads and cancel all pending jobs.
775 void jobs_terminate()
777 struct job *job, *head, *tail;
778 pthread_t me, *others;
785 /* request all threads to stop */
786 pthread_mutex_lock(&mutex);
789 /* count the number of threads */
793 if (!t->upper && !pthread_equal(t->tid, me))
798 /* fill the array of threads */
799 others = alloca(count * sizeof *others);
803 if (!t->upper && !pthread_equal(t->tid, me))
804 others[count++] = t->tid;
808 /* stops the threads */
815 /* wait the threads */
816 pthread_cond_broadcast(&cond);
817 pthread_mutex_unlock(&mutex);
819 pthread_join(others[--count], NULL);
820 pthread_mutex_lock(&mutex);
822 /* cancel pending jobs of other threads */
832 /* search if job is stacked for current */
834 while (t && t->job != job)
837 /* yes, relink it at end */
845 /* no cancel the job */
846 pthread_mutex_unlock(&mutex);
847 sig_monitor(0, job_cancel, job);
849 pthread_mutex_lock(&mutex);
852 pthread_mutex_unlock(&mutex);