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*, void *, void*);
49 /** Description of a pending job */
52 struct job *next; /**< link to the next job enqueued */
53 void *group; /**< group of the request */
54 job_cb_t callback; /**< processing callback */
55 void *arg1; /**< first arg */
56 void *arg2; /**< second arg */
57 void *arg3; /**< third arg */
58 int timeout; /**< timeout in second for processing the request */
59 unsigned blocked: 1; /**< is an other request blocking this one ? */
60 unsigned dropped: 1; /**< is removed ? */
63 /** Description of handled event loops */
67 struct sd_event *event;
72 /** Description of threads */
75 struct thread *next; /**< next thread of the list */
76 struct thread *upper; /**< upper same thread */
77 struct job *job; /**< currently processed job */
78 struct events *events; /**< currently processed job */
79 pthread_t tid; /**< the thread id */
80 unsigned stop: 1; /**< stop requested */
81 unsigned lowered: 1; /**< has a lower same thread */
82 unsigned waits: 1; /**< is waiting? */
86 * Description of synchonous callback
90 void (*callback)(int, void*); /**< the synchrnous callback */
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 /* count allowed, started and waiting threads */
99 static int allowed = 0; /** allowed count of threads */
100 static int started = 0; /** started count of threads */
101 static int waiting = 0; /** waiting count of threads */
102 static int remains = 0; /** allowed count of waiting jobs */
103 static int nevents = 0; /** count of events */
105 /* list of threads */
106 static struct thread *threads;
107 static _Thread_local struct thread *current;
109 /* queue of pending jobs */
110 static struct job *first_job;
111 static struct events *first_events;
112 static struct job *free_jobs;
115 * Create a new job with the given parameters
116 * @param group the group of the job
117 * @param timeout the timeout of the job (0 if none)
118 * @param callback the function that achieves the job
119 * @param arg1 the first argument of the callback
120 * @param arg2 the second argument of the callback
121 * @param arg3 the third argument of the callback
122 * @return the created job unblock or NULL when no more memory
124 static struct job *job_create(
134 /* try recyle existing job */
137 free_jobs = job->next;
139 /* allocation without blocking */
140 pthread_mutex_unlock(&mutex);
141 job = malloc(sizeof *job);
142 pthread_mutex_lock(&mutex);
148 /* initialises the job */
150 job->timeout = timeout;
151 job->callback = callback;
162 * Adds 'job' at the end of the list of jobs, marking it
163 * as blocked if an other job with the same group is pending.
164 * @param job the job to add
166 static void job_add(struct job *job)
169 struct job *ijob, **pjob;
175 /* search end and blockers */
179 if (group && ijob->group == group)
190 * Get the next job to process or NULL if none.
191 * @return the first job that isn't blocked or NULL
193 static inline struct job *job_get()
195 struct job *job = first_job;
196 while (job && job->blocked)
202 * Get the next events to process or NULL if none.
203 * @return the first events that isn't running or NULL
205 static inline struct events *events_get()
207 struct events *events = first_events;
208 while (events && events->runs)
209 events = events->next;
214 * Releases the processed 'job': removes it
215 * from the list of jobs and unblock the first
216 * pending job of the same group if any.
217 * @param job the job to release
219 static inline void job_release(struct job *job)
221 struct job *ijob, **pjob;
224 /* first unqueue the job */
227 while (ijob != job) {
233 /* then unblock jobs of the same group */
237 while (ijob && ijob->group != group)
243 /* recycle the job */
244 job->next = free_jobs;
249 * Monitored normal callback for a job.
250 * This function is called by the monitor
251 * to run the job when the safe environment
253 * @param signum 0 on normal flow or the number
254 * of the signal that interrupted the normal
256 * @param arg the job to run
258 static void job_call(int signum, void *arg)
260 struct job *job = arg;
261 job->callback(signum, job->arg1, job->arg2, job->arg3);
265 * Monitored cancel callback for a job.
266 * This function is called by the monitor
267 * to cancel the job when the safe environment
269 * @param signum 0 on normal flow or the number
270 * of the signal that interrupted the normal
272 * @param arg the job to run
274 static void job_cancel(int signum, void *arg)
276 job_call(SIGABRT, arg);
280 * Monitored normal callback for events.
281 * This function is called by the monitor
282 * to run the event loop when the safe environment
284 * @param signum 0 on normal flow or the number
285 * of the signal that interrupted the normal
287 * @param arg the events to run
289 static void events_call(int signum, void *arg)
291 struct events *events = arg;
293 sd_event_run(events->event, events->timeout);
297 * Main processing loop of threads processing jobs.
298 * The loop must be called with the mutex locked
299 * and it returns with the mutex locked.
300 * @param me the description of the thread to use
301 * TODO: how are timeout handled when reentering?
303 static void thread_run(volatile struct thread *me)
307 struct events *events;
310 /* initialize description of itself and link it in the list */
311 me->tid = pthread_self();
317 current->lowered = 1;
318 evto = EVENT_TIMEOUT_CHILD;
321 sig_monitor_init_timeouts();
322 evto = EVENT_TIMEOUT_TOP;
325 threads = (struct thread*)me;
326 current = (struct thread*)me;
328 NOTICE("job thread starting %d(/%d) %s", started, allowed, me->upper ? "child" : "parent");
330 /* loop until stopped */
334 job = job_get(first_job);
336 /* prepare running the job */
337 remains++; /* increases count of job that can wait */
338 job->blocked = 1; /* mark job as blocked */
339 me->job = job; /* record the job (only for terminate) */
342 pthread_mutex_unlock(&mutex);
343 sig_monitor(job->timeout, job_call, job);
344 pthread_mutex_lock(&mutex);
346 /* release the run job */
349 /* release event if any */
356 /* no job, check events */
357 events = events_get();
361 events->timeout = evto;
363 pthread_mutex_unlock(&mutex);
364 sig_monitor(0, events_call, events);
365 pthread_mutex_lock(&mutex);
369 /* no job and not events */
372 pthread_cond_wait(&cond, &mutex);
378 NOTICE("job thread stoping %d(/%d) %s", started, allowed, me->upper ? "child" : "parent");
380 /* unlink the current thread and cleanup */
387 current->lowered = 0;
389 sig_monitor_clean_timeouts();
395 * Entry point for created threads.
396 * @param data not used
399 static void *thread_main(void *data)
403 pthread_mutex_lock(&mutex);
405 pthread_mutex_unlock(&mutex);
410 * Starts a new thread
411 * @return 0 in case of success or -1 in case of error
413 static int start_one_thread()
418 rc = pthread_create(&tid, NULL, thread_main, NULL);
421 WARNING("not able to start thread: %m");
428 * Queues a new asynchronous job represented by 'callback'
429 * for the 'group' and the 'timeout'.
430 * Jobs are queued FIFO and are possibly executed in parallel
431 * concurrently except for job of the same group that are
432 * executed sequentially in FIFO order.
433 * @param group The group of the job or NULL when no group.
434 * @param timeout The maximum execution time in seconds of the job
435 * or 0 for unlimited time.
436 * @param callback The function to execute for achieving the job.
437 * Its first parameter is either 0 on normal flow
438 * or the signal number that broke the normal flow.
439 * @return 0 in case of success or -1 in case of error
444 void (*callback)(int signum))
446 return jobs_queue3(group, timeout, (job_cb_t)callback, NULL, NULL, NULL);
450 * Queues a new asynchronous job represented by 'callback' and 'arg1'
451 * for the 'group' and the 'timeout'.
452 * Jobs are queued FIFO and are possibly executed in parallel
453 * concurrently except for job of the same group that are
454 * executed sequentially in FIFO order.
455 * @param group The group of the job or NULL when no group.
456 * @param timeout The maximum execution time in seconds of the job
457 * or 0 for unlimited time.
458 * @param callback The function to execute for achieving the job.
459 * Its first parameter is either 0 on normal flow
460 * or the signal number that broke the normal flow.
461 * The remaining parameter is the parameter 'arg1'
463 * @param arg The second argument for 'callback'
464 * @return 0 in case of success or -1 in case of error
469 void (*callback)(int, void*),
472 return jobs_queue3(group, timeout, (job_cb_t)callback, arg, NULL, NULL);
476 * Queues a new asynchronous job represented by 'callback' and 'arg[12]'
477 * for the 'group' and the 'timeout'.
478 * Jobs are queued FIFO and are possibly executed in parallel
479 * concurrently except for job of the same group that are
480 * executed sequentially in FIFO order.
481 * @param group The group of the job or NULL when no group.
482 * @param timeout The maximum execution time in seconds of the job
483 * or 0 for unlimited time.
484 * @param callback The function to execute for achieving the job.
485 * Its first parameter is either 0 on normal flow
486 * or the signal number that broke the normal flow.
487 * The remaining parameters are the parameters 'arg[12]'
489 * @param arg1 The second argument for 'callback'
490 * @param arg2 The third argument for 'callback'
491 * @return 0 in case of success or -1 in case of error
496 void (*callback)(int, void*, void*),
500 return jobs_queue3(group, timeout, (job_cb_t)callback, arg1, arg2, NULL);
504 * Queues a new asynchronous job represented by 'callback' and 'arg[123]'
505 * for the 'group' and the 'timeout'.
506 * Jobs are queued FIFO and are possibly executed in parallel
507 * concurrently except for job of the same group that are
508 * executed sequentially in FIFO order.
509 * @param group The group of the job or NULL when no group.
510 * @param timeout The maximum execution time in seconds of the job
511 * or 0 for unlimited time.
512 * @param callback The function to execute for achieving the job.
513 * Its first parameter is either 0 on normal flow
514 * or the signal number that broke the normal flow.
515 * The remaining parameters are the parameters 'arg[123]'
517 * @param arg1 The second argument for 'callback'
518 * @param arg2 The third argument for 'callback'
519 * @param arg3 The forth argument for 'callback'
520 * @return 0 in case of success or -1 in case of error
525 void (*callback)(int, void*, void *, void*),
534 pthread_mutex_lock(&mutex);
536 /* allocates the job */
537 job = job_create(group, timeout, callback, arg1, arg2, arg3);
540 info = "out of memory";
544 /* check availability */
547 info = "too many jobs";
551 /* start a thread if needed */
552 if (waiting == 0 && started < allowed) {
553 /* all threads are busy and a new can be started */
554 rc = start_one_thread();
555 if (rc < 0 && started == 0) {
556 info = "can't start first thread";
565 /* signal an existing job */
566 pthread_cond_signal(&cond);
567 pthread_mutex_unlock(&mutex);
571 job->next = free_jobs;
574 ERROR("can't process job with threads: %s, %m", info);
575 pthread_mutex_unlock(&mutex);
580 * Enter a synchronisation point: activates the job given by 'callback'
581 * and 'closure' using 'group' and 'timeout' to control sequencing and
583 * @param group the group for sequencing jobs
584 * @param timeout the time in seconds allocated to the job
585 * @param callback the callback that will handle the job.
586 * it receives 3 parameters: 'signum' that will be 0
587 * on normal flow or the catched signal number in case
588 * of interrupted flow, the context 'closure' as given and
589 * a 'jobloop' reference that must be used when the job is
590 * terminated to unlock the current execution flow.
591 * @param closure the context completion closure for the callback
592 * @return 0 on success or -1 in case of error
597 void (*callback)(int signum, void *closure, struct jobloop *jobloop),
605 pthread_mutex_lock(&mutex);
607 /* allocates the job */
608 job = job_create(group, timeout, (job_cb_t)callback, closure, &me, NULL);
610 ERROR("out of memory");
612 pthread_mutex_unlock(&mutex);
619 /* run until stopped */
621 pthread_mutex_unlock(&mutex);
626 * Unlocks the execution flow designed by 'jobloop'.
627 * @param jobloop indication of the flow to unlock
628 * @return 0 in case of success of -1 on error
630 int jobs_leave(struct jobloop *jobloop)
634 pthread_mutex_lock(&mutex);
636 while (t && t != (struct thread*)jobloop)
643 pthread_cond_broadcast(&cond);
645 pthread_mutex_unlock(&mutex);
650 * Internal helper function for 'jobs_call'.
651 * @see jobs_call, jobs_enter, jobs_leave
653 static void call_cb(int signum, void *closure, struct jobloop *jobloop)
655 struct sync *sync = closure;
656 sync->callback(signum, sync->arg);
661 * Calls synchronously the job represented by 'callback' and 'arg1'
662 * for the 'group' and the 'timeout' and waits for its completion.
663 * @param group The group of the job or NULL when no group.
664 * @param timeout The maximum execution time in seconds of the job
665 * or 0 for unlimited time.
666 * @param callback The function to execute for achieving the job.
667 * Its first parameter is either 0 on normal flow
668 * or the signal number that broke the normal flow.
669 * The remaining parameter is the parameter 'arg1'
671 * @param arg The second argument for 'callback'
672 * @return 0 in case of success or -1 in case of error
677 void (*callback)(int, void*),
682 sync.callback = callback;
684 return jobs_enter(group, timeout, call_cb, &sync);
688 * Gets a sd_event item for the current thread.
689 * @return a sd_event or NULL in case of error
691 struct sd_event *jobs_get_sd_event()
693 struct events *events;
697 pthread_mutex_lock(&mutex);
699 /* search events on stack */
701 while (me && !me->events)
704 /* return the stacked events */
707 /* search an available events */
708 events = events_get();
710 /* not found, check if creation possible */
711 if (nevents >= allowed) {
712 ERROR("not possible to add a new event");
715 events = malloc(sizeof *events);
716 if (events && (rc = sd_event_new(&events->event)) >= 0) {
717 if (nevents < started || start_one_thread() >= 0) {
719 events->next = first_events;
720 first_events = events;
722 ERROR("can't start thread for events");
723 sd_event_unref(events->event);
729 ERROR("out of memory");
733 ERROR("creation of sd_event failed: %m");
747 WARNING("event returned for unknown thread!");
751 pthread_mutex_unlock(&mutex);
752 return events ? events->event : NULL;
756 * Enter the jobs processing loop.
757 * @param allowed_count Maximum count of thread for jobs including this one
758 * @param start_count Count of thread to start now, must be lower.
759 * @param waiter_count Maximum count of jobs that can be waiting.
760 * @param start The start routine to activate (can't be NULL)
761 * @return 0 in case of success or -1 in case of error.
763 int jobs_start(int allowed_count, int start_count, int waiter_count, void (*start)())
769 assert(allowed_count >= 1);
770 assert(start_count >= 0);
771 assert(waiter_count > 0);
772 assert(start_count <= allowed_count);
775 pthread_mutex_lock(&mutex);
777 /* check whether already running */
778 if (current || allowed) {
779 ERROR("thread already started");
785 if (sig_monitor_init() < 0) {
786 ERROR("failed to initialise signal handlers");
790 /* records the allowed count */
791 allowed = allowed_count;
794 remains = waiter_count;
796 /* start at least one thread */
798 while ((launched + 1) < start_count) {
799 if (start_one_thread() != 0) {
800 ERROR("Not all threads can be started");
806 /* queue the start job */
807 job = job_create(NULL, 0, (job_cb_t)start, NULL, NULL, NULL);
809 ERROR("out of memory");
820 pthread_mutex_unlock(&mutex);
825 * Terminate all the threads and cancel all pending jobs.
827 void jobs_terminate()
829 struct job *job, *head, *tail;
830 pthread_t me, *others;
837 /* request all threads to stop */
838 pthread_mutex_lock(&mutex);
841 /* count the number of threads */
845 if (!t->upper && !pthread_equal(t->tid, me))
850 /* fill the array of threads */
851 others = alloca(count * sizeof *others);
855 if (!t->upper && !pthread_equal(t->tid, me))
856 others[count++] = t->tid;
860 /* stops the threads */
867 /* wait the threads */
868 pthread_cond_broadcast(&cond);
869 pthread_mutex_unlock(&mutex);
871 pthread_join(others[--count], NULL);
872 pthread_mutex_lock(&mutex);
874 /* cancel pending jobs of other threads */
884 /* search if job is stacked for current */
886 while (t && t->job != job)
889 /* yes, relink it at end */
897 /* no cancel the job */
898 pthread_mutex_unlock(&mutex);
899 sig_monitor(0, job_cancel, job);
901 pthread_mutex_lock(&mutex);
904 pthread_mutex_unlock(&mutex);