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.
24 #include <sys/syscall.h>
29 #include <systemd/sd-event.h>
32 #include "sig-monitor.h"
36 #define _alert_ "do you really want to remove monitoring?"
37 #define sig_monitor_init_timeouts() ((void)0)
38 #define sig_monitor_clean_timeouts() ((void)0)
39 #define sig_monitor(to,cb,arg) (cb(0,arg))
42 /** Internal shortcut for callback */
43 typedef void (*job_cb_t)(int, void*, void *, void*);
45 /** Description of a pending job */
48 struct job *next; /**< link to the next job enqueued */
49 void *group; /**< group of the request */
50 job_cb_t callback; /**< processing callback */
51 void *arg1; /**< first arg */
52 void *arg2; /**< second arg */
53 void *arg3; /**< third arg */
54 int timeout; /**< timeout in second for processing the request */
55 unsigned blocked: 1; /**< is an other request blocking this one ? */
56 unsigned dropped: 1; /**< is removed ? */
59 /** Description of handled event loops */
63 struct sd_event *event;
67 /** Description of threads */
70 struct thread *next; /**< next thread of the list */
71 struct thread *upper; /**< upper same thread */
72 struct job *job; /**< currently processed job */
73 struct events *events; /**< currently processed job */
74 pthread_t tid; /**< the thread id */
75 unsigned stop: 1; /**< stop requested */
76 unsigned lowered: 1; /**< has a lower same thread */
77 unsigned waits: 1; /**< is waiting? */
80 /* synchronisation of threads */
81 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
82 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
84 /* count allowed, started and waiting threads */
85 static int allowed = 0; /** allowed count of threads */
86 static int started = 0; /** started count of threads */
87 static int waiting = 0; /** waiting count of threads */
88 static int remains = 0; /** allowed count of waiting jobs */
89 static int nevents = 0; /** count of events */
92 static struct thread *threads;
93 static _Thread_local struct thread *current;
95 /* queue of pending jobs */
96 static struct job *first_job;
97 static struct events *first_events;
98 static struct job *free_jobs;
101 * Create a new job with the given parameters
102 * @param group the group of the job
103 * @param timeout the timeout of the job (0 if none)
104 * @param callback the function that achieves the job
105 * @param arg1 the first argument of the callback
106 * @param arg2 the second argument of the callback
107 * @param arg3 the third argument of the callback
108 * @return the created job unblock or NULL when no more memory
110 static struct job *job_create(
120 /* try recyle existing job */
123 free_jobs = job->next;
125 /* allocation without blocking */
126 pthread_mutex_unlock(&mutex);
127 job = malloc(sizeof *job);
128 pthread_mutex_lock(&mutex);
134 /* initialises the job */
136 job->timeout = timeout;
137 job->callback = callback;
148 * Adds 'job1' and 'job2' at the end of the list of jobs, marking it
149 * as blocked if an other job with the same group is pending.
150 * @param job1 the first job to add
151 * @param job2 the second job to add or NULL
153 static void job_add2(struct job *job1, struct job *job2)
155 void *group1, *group2, *group;
156 struct job *ijob, **pjob;
159 group1 = job1->group;
165 group2 = job2->group;
166 if (group2 && group2 == group1)
170 /* search end and blockers */
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, (uint64_t) -1);
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;
309 /* initialize description of itself and link it in the list */
310 me->tid = pthread_self();
316 current->lowered = 1;
318 sig_monitor_init_timeouts();
319 current = (struct thread*)me;
321 threads = (struct thread*)me;
324 /* loop until stopped */
328 job = job_get(first_job);
330 /* prepare running the job */
331 remains++; /* increases count of job that can wait */
332 job->blocked = 1; /* mark job as blocked */
333 me->job = job; /* record the job (only for terminate) */
336 pthread_mutex_unlock(&mutex);
337 sig_monitor(job->timeout, job_call, job);
338 pthread_mutex_lock(&mutex);
340 /* release the run job */
343 /* release event if any */
350 /* no job, check events */
351 events = events_get();
356 pthread_mutex_unlock(&mutex);
357 sig_monitor(0, events_call, events);
358 pthread_mutex_lock(&mutex);
362 /* no job and not events */
365 pthread_cond_wait(&cond, &mutex);
372 /* unlink the current thread and cleanup */
380 current->lowered = 0;
382 sig_monitor_clean_timeouts();
386 * Entry point for created threads.
387 * @param data not used
390 static void *thread_main(void *data)
394 pthread_mutex_lock(&mutex);
396 pthread_mutex_unlock(&mutex);
401 * Starts a new thread
402 * @return 0 in case of success or -1 in case of error
404 static int start_one_thread()
409 rc = pthread_create(&tid, NULL, thread_main, NULL);
412 WARNING("not able to start thread: %m");
419 * Queues a new asynchronous job represented by 'callback'
420 * for the 'group' and the 'timeout'.
421 * Jobs are queued FIFO and are possibly executed in parallel
422 * concurrently except for job of the same group that are
423 * executed sequentially in FIFO order.
424 * @param group The group of the job or NULL when no group.
425 * @param timeout The maximum execution time in seconds of the job
426 * or 0 for unlimited time.
427 * @param callback The function to execute for achieving the job.
428 * Its first parameter is either 0 on normal flow
429 * or the signal number that broke the normal flow.
430 * @return 0 in case of success or -1 in case of error
435 void (*callback)(int signum))
437 return jobs_queue3(group, timeout, (job_cb_t)callback, NULL, NULL, NULL);
441 * Queues a new asynchronous job represented by 'callback' and 'arg1'
442 * for the 'group' and the 'timeout'.
443 * Jobs are queued FIFO and are possibly executed in parallel
444 * concurrently except for job of the same group that are
445 * executed sequentially in FIFO order.
446 * @param group The group of the job or NULL when no group.
447 * @param timeout The maximum execution time in seconds of the job
448 * or 0 for unlimited time.
449 * @param callback The function to execute for achieving the job.
450 * Its first parameter is either 0 on normal flow
451 * or the signal number that broke the normal flow.
452 * The remaining parameter is the parameter 'arg1'
454 * @param arg1 The second argument for 'callback'
455 * @return 0 in case of success or -1 in case of error
460 void (*callback)(int, void*),
463 return jobs_queue3(group, timeout, (job_cb_t)callback, arg, NULL, NULL);
467 * Queues a new asynchronous job represented by 'callback' and 'arg[12]'
468 * for the 'group' and the 'timeout'.
469 * Jobs are queued FIFO and are possibly executed in parallel
470 * concurrently except for job of the same group that are
471 * executed sequentially in FIFO order.
472 * @param group The group of the job or NULL when no group.
473 * @param timeout The maximum execution time in seconds of the job
474 * or 0 for unlimited time.
475 * @param callback The function to execute for achieving the job.
476 * Its first parameter is either 0 on normal flow
477 * or the signal number that broke the normal flow.
478 * The remaining parameters are the parameters 'arg[12]'
480 * @param arg1 The second argument for 'callback'
481 * @param arg2 The third argument for 'callback'
482 * @return 0 in case of success or -1 in case of error
487 void (*callback)(int, void*, void*),
491 return jobs_queue3(group, timeout, (job_cb_t)callback, arg1, arg2, NULL);
495 * Queues a new asynchronous job represented by 'callback' and 'arg[123]'
496 * for the 'group' and the 'timeout'.
497 * Jobs are queued FIFO and are possibly executed in parallel
498 * concurrently except for job of the same group that are
499 * executed sequentially in FIFO order.
500 * @param group The group of the job or NULL when no group.
501 * @param timeout The maximum execution time in seconds of the job
502 * or 0 for unlimited time.
503 * @param callback The function to execute for achieving the job.
504 * Its first parameter is either 0 on normal flow
505 * or the signal number that broke the normal flow.
506 * The remaining parameters are the parameters 'arg[123]'
508 * @param arg1 The second argument for 'callback'
509 * @param arg2 The third argument for 'callback'
510 * @param arg3 The forth argument for 'callback'
511 * @return 0 in case of success or -1 in case of error
516 void (*callback)(int, void*, void *, void*),
525 pthread_mutex_lock(&mutex);
527 /* allocates the job */
528 job = job_create(group, timeout, callback, arg1, arg2, arg3);
531 info = "out of memory";
535 /* check availability */
538 info = "too many jobs";
542 /* start a thread if needed */
543 if (waiting == 0 && started < allowed) {
544 /* all threads are busy and a new can be started */
545 rc = start_one_thread();
546 if (rc < 0 && started == 0) {
547 info = "can't start first thread";
556 /* signal an existing job */
557 pthread_cond_signal(&cond);
558 pthread_mutex_unlock(&mutex);
562 job->next = free_jobs;
565 ERROR("can't process job with threads: %s, %m", info);
566 pthread_mutex_unlock(&mutex);
571 * Run a asynchronous job represented by 'callback'
572 * with the 'timeout' but only returns after job completion.
573 * @param timeout The maximum execution time in seconds of the job
574 * or 0 for unlimited time.
575 * @param callback The function to execute for achieving the job.
576 * Its first parameter is either 0 on normal flow
577 * or the signal number that broke the normal flow.
578 * @return 0 in case of success or -1 in case of error
582 void (*callback)(int signum))
584 return jobs_invoke3(timeout, (job_cb_t)callback, NULL, NULL, NULL);
588 * Run a asynchronous job represented by 'callback' and 'arg1'
589 * with the 'timeout' but only returns after job completion.
590 * @param timeout The maximum execution time in seconds of the job
591 * or 0 for unlimited time.
592 * @param callback The function to execute for achieving the job.
593 * Its first parameter is either 0 on normal flow
594 * or the signal number that broke the normal flow.
595 * The remaining parameter is the parameter 'arg1'
597 * @param arg1 The second argument for 'callback'
598 * @return 0 in case of success or -1 in case of error
602 void (*callback)(int, void*),
605 return jobs_invoke3(timeout, (job_cb_t)callback, arg, NULL, NULL);
609 * Run a asynchronous job represented by 'callback' and 'arg[12]'
610 * with the 'timeout' but only returns after job completion.
611 * @param timeout The maximum execution time in seconds of the job
612 * or 0 for unlimited time.
613 * @param callback The function to execute for achieving the job.
614 * Its first parameter is either 0 on normal flow
615 * or the signal number that broke the normal flow.
616 * The remaining parameters are the parameters 'arg[12]'
618 * @param arg1 The second argument for 'callback'
619 * @param arg2 The third argument for 'callback'
620 * @return 0 in case of success or -1 in case of error
624 void (*callback)(int, void*, void*),
628 return jobs_invoke3(timeout, (job_cb_t)callback, arg1, arg2, NULL);
632 * Stops the thread pointed by 'arg1'. Used with
633 * invoke familly to return to the caller after completion.
634 * @param signum Unused
635 * @param arg1 The thread to stop
639 static void unlock_invoker(int signum, void *arg1, void *arg2, void *arg3)
641 struct thread *t = arg1;
642 pthread_mutex_lock(&mutex);
645 pthread_cond_broadcast(&cond);
646 pthread_mutex_unlock(&mutex);
650 * Run a asynchronous job represented by 'callback' and 'arg[123]'
651 * with the 'timeout' but only returns after job completion.
652 * @param timeout The maximum execution time in seconds of the job
653 * or 0 for unlimited time.
654 * @param callback The function to execute for achieving the job.
655 * Its first parameter is either 0 on normal flow
656 * or the signal number that broke the normal flow.
657 * The remaining parameters are the parameters 'arg[123]'
659 * @param arg1 The second argument for 'callback'
660 * @param arg2 The third argument for 'callback'
661 * @param arg3 The forth argument for 'callback'
662 * @return 0 in case of success or -1 in case of error
666 void (*callback)(int, void*, void *, void*),
671 struct job *job1, *job2;
674 pthread_mutex_lock(&mutex);
676 /* allocates the job */
677 job1 = job_create(&me, timeout, callback, arg1, arg2, arg3);
678 job2 = job_create(&me, 0, unlock_invoker, &me, NULL, NULL);
679 if (!job1 || !job2) {
680 ERROR("out of memory");
683 job1->next = free_jobs;
687 job2->next = free_jobs;
690 pthread_mutex_unlock(&mutex);
695 job_add2(job1, job2);
697 /* run until stopped */
699 pthread_mutex_unlock(&mutex);
704 * Initialise the job stuff.
705 * @param allowed_count Maximum count of thread for jobs (can be 0,
706 * see 'jobs_add_me' for merging new threads)
707 * @param start_count Count of thread to start now, must be lower.
708 * @param waiter_count Maximum count of jobs that can be waiting.
709 * @return 0 in case of success or -1 in case of error.
711 int jobs_init(int allowed_count, int start_count, int waiter_count)
715 assert(allowed_count >= 0);
716 assert(start_count >= 0);
717 assert(waiter_count > 0);
718 assert(start_count <= allowed_count);
720 /* records the allowed count */
721 allowed = allowed_count;
724 remains = waiter_count;
726 /* start at least one thread */
727 pthread_mutex_lock(&mutex);
729 while (launched < start_count && start_one_thread() == 0)
731 rc = -(launched != start_count);
732 pthread_mutex_unlock(&mutex);
736 ERROR("Not all threads can be started");
741 * Terminate all the threads and cancel all pending jobs.
743 void jobs_terminate()
745 struct job *job, *head, *tail;
746 pthread_t me, *others;
753 /* request all threads to stop */
754 pthread_mutex_lock(&mutex);
757 /* count the number of threads */
761 if (!t->upper && !pthread_equal(t->tid, me))
766 /* fill the array of threads */
767 others = alloca(count * sizeof *others);
771 if (!t->upper && !pthread_equal(t->tid, me))
772 others[count++] = t->tid;
776 /* stops the threads */
783 /* wait the threads */
784 pthread_cond_broadcast(&cond);
785 pthread_mutex_unlock(&mutex);
787 pthread_join(others[--count], NULL);
788 pthread_mutex_lock(&mutex);
790 /* cancel pending jobs of other threads */
800 /* search if job is stacked for current */
802 while (t && t->job != job)
805 /* yes, relink it at end */
813 /* no cancel the job */
814 pthread_mutex_unlock(&mutex);
815 sig_monitor(0, job_cancel, job);
817 pthread_mutex_lock(&mutex);
820 pthread_mutex_unlock(&mutex);
824 * Adds the current thread to the pool of threads
825 * processing the jobs. Returns normally when the threads are
826 * terminated or immediately with an error if the thread is
827 * already in the pool.
828 * @return 0 in case of success or -1 in case of error
834 /* check whether already running */
836 ERROR("thread already running");
842 pthread_mutex_lock(&mutex);
846 pthread_mutex_unlock(&mutex);
851 struct sd_event *jobs_get_sd_event()
853 struct events *events;
857 pthread_mutex_lock(&mutex);
859 /* search events on stack */
861 while (me && !me->events)
864 /* return the stacked events */
867 /* search an available events */
868 events = events_get();
870 /* not found, check if creation possible */
871 if (nevents >= allowed) {
872 ERROR("not possible to add a new event");
875 events = malloc(sizeof *events);
876 if (events && (rc = sd_event_new(&events->event)) >= 0) {
877 if (nevents < started || start_one_thread() >= 0) {
879 events->next = first_events;
880 first_events = events;
882 ERROR("can't start thread for events");
883 sd_event_unref(events->event);
889 ERROR("out of memory");
892 ERROR("creation of sd_event failed: %m");
906 WARNING("event returned for unknown thread!");
910 pthread_mutex_unlock(&mutex);
911 return events ? events->event : NULL;