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 'job' 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 job the job to add
152 static void job_add(struct job *job)
155 struct job *ijob, **pjob;
161 /* search end and blockers */
165 if (group && ijob->group == group)
176 * Get the next job to process or NULL if none.
177 * @return the first job that isn't blocked or NULL
179 static inline struct job *job_get()
181 struct job *job = first_job;
182 while (job && job->blocked)
188 * Get the next events to process or NULL if none.
189 * @return the first events that isn't running or NULL
191 static inline struct events *events_get()
193 struct events *events = first_events;
194 while (events && events->runs)
195 events = events->next;
200 * Releases the processed 'job': removes it
201 * from the list of jobs and unblock the first
202 * pending job of the same group if any.
203 * @param job the job to release
205 static inline void job_release(struct job *job)
207 struct job *ijob, **pjob;
210 /* first unqueue the job */
213 while (ijob != job) {
219 /* then unblock jobs of the same group */
223 while (ijob && ijob->group != group)
229 /* recycle the job */
230 job->next = free_jobs;
235 * Monitored normal callback for a job.
236 * This function is called by the monitor
237 * to run the job when the safe environment
239 * @param signum 0 on normal flow or the number
240 * of the signal that interrupted the normal
242 * @param arg the job to run
244 static void job_call(int signum, void *arg)
246 struct job *job = arg;
247 job->callback(signum, job->arg1, job->arg2, job->arg3);
251 * Monitored cancel callback for a job.
252 * This function is called by the monitor
253 * to cancel the job when the safe environment
255 * @param signum 0 on normal flow or the number
256 * of the signal that interrupted the normal
258 * @param arg the job to run
260 static void job_cancel(int signum, void *arg)
262 job_call(SIGABRT, 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)
277 struct events *events = arg;
279 sd_event_run(events->event, (uint64_t) -1);
283 * Main processing loop of threads processing jobs.
284 * The loop must be called with the mutex locked
285 * and it returns with the mutex locked.
286 * @param me the description of the thread to use
287 * TODO: how are timeout handled when reentering?
289 static void thread_run(volatile struct thread *me)
293 struct events *events;
295 /* initialize description of itself and link it in the list */
296 me->tid = pthread_self();
302 current->lowered = 1;
304 sig_monitor_init_timeouts();
305 current = (struct thread*)me;
307 threads = (struct thread*)me;
310 NOTICE("job thread starting %d(/%d) %s", started, allowed, me->upper ? "child" : "parent");
312 /* loop until stopped */
316 job = job_get(first_job);
318 /* prepare running the job */
319 remains++; /* increases count of job that can wait */
320 job->blocked = 1; /* mark job as blocked */
321 me->job = job; /* record the job (only for terminate) */
324 pthread_mutex_unlock(&mutex);
325 sig_monitor(job->timeout, job_call, job);
326 pthread_mutex_lock(&mutex);
328 /* release the run job */
331 /* release event if any */
338 /* no job, check events */
339 events = events_get();
344 pthread_mutex_unlock(&mutex);
345 sig_monitor(0, events_call, events);
346 pthread_mutex_lock(&mutex);
350 /* no job and not events */
353 pthread_cond_wait(&cond, &mutex);
359 NOTICE("job thread stoping %d(/%d) %s", started, allowed, me->upper ? "child" : "parent");
361 /* unlink the current thread and cleanup */
369 current->lowered = 0;
371 sig_monitor_clean_timeouts();
375 * Entry point for created threads.
376 * @param data not used
379 static void *thread_main(void *data)
383 pthread_mutex_lock(&mutex);
385 pthread_mutex_unlock(&mutex);
390 * Starts a new thread
391 * @return 0 in case of success or -1 in case of error
393 static int start_one_thread()
398 rc = pthread_create(&tid, NULL, thread_main, NULL);
401 WARNING("not able to start thread: %m");
408 * Queues a new asynchronous job represented by 'callback'
409 * for the 'group' and the 'timeout'.
410 * Jobs are queued FIFO and are possibly executed in parallel
411 * concurrently except for job of the same group that are
412 * executed sequentially in FIFO order.
413 * @param group The group of the job or NULL when no group.
414 * @param timeout The maximum execution time in seconds of the job
415 * or 0 for unlimited time.
416 * @param callback The function to execute for achieving the job.
417 * Its first parameter is either 0 on normal flow
418 * or the signal number that broke the normal flow.
419 * @return 0 in case of success or -1 in case of error
424 void (*callback)(int signum))
426 return jobs_queue3(group, timeout, (job_cb_t)callback, NULL, NULL, NULL);
430 * Queues a new asynchronous job represented by 'callback' and 'arg1'
431 * for the 'group' and the 'timeout'.
432 * Jobs are queued FIFO and are possibly executed in parallel
433 * concurrently except for job of the same group that are
434 * executed sequentially in FIFO order.
435 * @param group The group of the job or NULL when no group.
436 * @param timeout The maximum execution time in seconds of the job
437 * or 0 for unlimited time.
438 * @param callback The function to execute for achieving the job.
439 * Its first parameter is either 0 on normal flow
440 * or the signal number that broke the normal flow.
441 * The remaining parameter is the parameter 'arg1'
443 * @param arg1 The second argument for 'callback'
444 * @return 0 in case of success or -1 in case of error
449 void (*callback)(int, void*),
452 return jobs_queue3(group, timeout, (job_cb_t)callback, arg, NULL, NULL);
456 * Queues a new asynchronous job represented by 'callback' and 'arg[12]'
457 * for the 'group' and the 'timeout'.
458 * Jobs are queued FIFO and are possibly executed in parallel
459 * concurrently except for job of the same group that are
460 * executed sequentially in FIFO order.
461 * @param group The group of the job or NULL when no group.
462 * @param timeout The maximum execution time in seconds of the job
463 * or 0 for unlimited time.
464 * @param callback The function to execute for achieving the job.
465 * Its first parameter is either 0 on normal flow
466 * or the signal number that broke the normal flow.
467 * The remaining parameters are the parameters 'arg[12]'
469 * @param arg1 The second argument for 'callback'
470 * @param arg2 The third argument for 'callback'
471 * @return 0 in case of success or -1 in case of error
476 void (*callback)(int, void*, void*),
480 return jobs_queue3(group, timeout, (job_cb_t)callback, arg1, arg2, NULL);
484 * Queues a new asynchronous job represented by 'callback' and 'arg[123]'
485 * for the 'group' and the 'timeout'.
486 * Jobs are queued FIFO and are possibly executed in parallel
487 * concurrently except for job of the same group that are
488 * executed sequentially in FIFO order.
489 * @param group The group of the job or NULL when no group.
490 * @param timeout The maximum execution time in seconds of the job
491 * or 0 for unlimited time.
492 * @param callback The function to execute for achieving the job.
493 * Its first parameter is either 0 on normal flow
494 * or the signal number that broke the normal flow.
495 * The remaining parameters are the parameters 'arg[123]'
497 * @param arg1 The second argument for 'callback'
498 * @param arg2 The third argument for 'callback'
499 * @param arg3 The forth argument for 'callback'
500 * @return 0 in case of success or -1 in case of error
505 void (*callback)(int, void*, void *, void*),
514 pthread_mutex_lock(&mutex);
516 /* allocates the job */
517 job = job_create(group, timeout, callback, arg1, arg2, arg3);
520 info = "out of memory";
524 /* check availability */
527 info = "too many jobs";
531 /* start a thread if needed */
532 if (waiting == 0 && started < allowed) {
533 /* all threads are busy and a new can be started */
534 rc = start_one_thread();
535 if (rc < 0 && started == 0) {
536 info = "can't start first thread";
545 /* signal an existing job */
546 pthread_cond_signal(&cond);
547 pthread_mutex_unlock(&mutex);
551 job->next = free_jobs;
554 ERROR("can't process job with threads: %s, %m", info);
555 pthread_mutex_unlock(&mutex);
560 * Enter a synchronisation point: activates the job given by 'callback'
561 * @param group the gro
566 void (*callback)(int signum, void *closure, struct jobloop *jobloop),
573 pthread_mutex_lock(&mutex);
575 /* allocates the job */
576 job = job_create(group, timeout, (job_cb_t)callback, closure, &me, NULL);
578 ERROR("out of memory");
580 pthread_mutex_unlock(&mutex);
587 /* run until stopped */
589 pthread_mutex_unlock(&mutex);
593 int jobs_leave(struct jobloop *jobloop)
596 pthread_mutex_lock(&mutex);
599 while (t && t != (struct thread*)jobloop)
606 pthread_cond_broadcast(&cond);
608 pthread_mutex_unlock(&mutex);
613 * Gets a sd_event item for the current thread.
614 * @return a sd_event or NULL in case of error
616 struct sd_event *jobs_get_sd_event()
618 struct events *events;
622 pthread_mutex_lock(&mutex);
624 /* search events on stack */
626 while (me && !me->events)
629 /* return the stacked events */
632 /* search an available events */
633 events = events_get();
635 /* not found, check if creation possible */
636 if (nevents >= allowed) {
637 ERROR("not possible to add a new event");
640 events = malloc(sizeof *events);
641 if (events && (rc = sd_event_new(&events->event)) >= 0) {
642 if (nevents < started || start_one_thread() >= 0) {
644 events->next = first_events;
645 first_events = events;
647 ERROR("can't start thread for events");
648 sd_event_unref(events->event);
654 ERROR("out of memory");
658 ERROR("creation of sd_event failed: %m");
672 WARNING("event returned for unknown thread!");
676 pthread_mutex_unlock(&mutex);
677 return events ? events->event : NULL;
681 * Enter the jobs processing loop.
682 * @param allowed_count Maximum count of thread for jobs including this one
683 * @param start_count Count of thread to start now, must be lower.
684 * @param waiter_count Maximum count of jobs that can be waiting.
685 * @param start The start routine to activate (can't be NULL)
686 * @return 0 in case of success or -1 in case of error.
688 int jobs_start(int allowed_count, int start_count, int waiter_count, void (*start)())
694 assert(allowed_count >= 1);
695 assert(start_count >= 0);
696 assert(waiter_count > 0);
697 assert(start_count <= allowed_count);
700 pthread_mutex_lock(&mutex);
702 /* check whether already running */
703 if (current || allowed) {
704 ERROR("thread already started");
710 if (sig_monitor_init() < 0) {
711 ERROR("failed to initialise signal handlers");
715 /* records the allowed count */
716 allowed = allowed_count;
719 remains = waiter_count;
721 /* start at least one thread */
723 while ((launched + 1) < start_count) {
724 if (start_one_thread() != 0) {
725 ERROR("Not all threads can be started");
731 /* queue the start job */
732 job = job_create(NULL, 0, (job_cb_t)start, NULL, NULL, NULL);
734 ERROR("out of memory");
745 pthread_mutex_unlock(&mutex);
750 * Terminate all the threads and cancel all pending jobs.
752 void jobs_terminate()
754 struct job *job, *head, *tail;
755 pthread_t me, *others;
762 /* request all threads to stop */
763 pthread_mutex_lock(&mutex);
766 /* count the number of threads */
770 if (!t->upper && !pthread_equal(t->tid, me))
775 /* fill the array of threads */
776 others = alloca(count * sizeof *others);
780 if (!t->upper && !pthread_equal(t->tid, me))
781 others[count++] = t->tid;
785 /* stops the threads */
792 /* wait the threads */
793 pthread_cond_broadcast(&cond);
794 pthread_mutex_unlock(&mutex);
796 pthread_join(others[--count], NULL);
797 pthread_mutex_lock(&mutex);
799 /* cancel pending jobs of other threads */
809 /* search if job is stacked for current */
811 while (t && t->job != job)
814 /* yes, relink it at end */
822 /* no cancel the job */
823 pthread_mutex_unlock(&mutex);
824 sig_monitor(0, job_cancel, job);
826 pthread_mutex_lock(&mutex);
829 pthread_mutex_unlock(&mutex);