123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #include <errno.h>
- #include <setjmp.h>
- #include <signal.h>
- #include <stdbool.h>
- #include <sysdep.h>
- #include <bits/kernel-features.h>
- #include <pthreadP.h>
- #include "kernel-posix-timers.h"
- struct timer *__active_timer_sigev_thread;
- pthread_mutex_t __active_timer_sigev_thread_lock = PTHREAD_MUTEX_INITIALIZER;
- struct thread_start_data
- {
- void (*thrfunc) (sigval_t);
- sigval_t sival;
- };
- #ifdef __NR_timer_create
- static void *
- timer_sigev_thread (void *arg)
- {
-
- sigset_t ss;
- __sigemptyset (&ss);
- INTERNAL_SYSCALL_DECL (err);
- INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &ss, NULL, _NSIG / 8);
- struct thread_start_data *td = (struct thread_start_data *) arg;
- void (*thrfunc) (sigval_t) = td->thrfunc;
- sigval_t sival = td->sival;
-
- free (td);
-
- thrfunc (sival);
- return NULL;
- }
- static attribute_noreturn void *
- timer_helper_thread (void *arg)
- {
-
- sigset_t ss;
- __sigemptyset (&ss);
- __sigaddset (&ss, SIGTIMER);
-
- while (1)
- {
- siginfo_t si;
-
- int oldtype = LIBC_CANCEL_ASYNC ();
-
- int result = INLINE_SYSCALL (rt_sigtimedwait, 4, &ss, &si, NULL,
- _NSIG / 8);
- LIBC_CANCEL_RESET (oldtype);
- if (result > 0)
- {
- if (si.si_code == SI_TIMER)
- {
- struct timer *tk = (struct timer *) si.si_ptr;
-
- pthread_mutex_lock (&__active_timer_sigev_thread_lock);
- struct timer *runp = __active_timer_sigev_thread;
- while (runp != NULL)
- if (runp == tk)
- break;
- else
- runp = runp->next;
- if (runp != NULL)
- {
- struct thread_start_data *td = malloc (sizeof (*td));
-
- if (td != NULL)
- {
-
- td->thrfunc = tk->thrfunc;
- td->sival = tk->sival;
- pthread_t th;
- (void) pthread_create (&th, &tk->attr,
- timer_sigev_thread, td);
- }
- }
- pthread_mutex_unlock (&__active_timer_sigev_thread_lock);
- }
- else if (si.si_code == SI_TKILL)
-
- pthread_exit (NULL);
- }
- }
- }
- pthread_once_t __helper_once attribute_hidden;
- pid_t __helper_tid attribute_hidden;
- static void
- reset_helper_control (void)
- {
- __helper_once = PTHREAD_ONCE_INIT;
- __helper_tid = 0;
- }
- void
- attribute_hidden
- __start_helper_thread (void)
- {
-
- pthread_attr_t attr;
- (void) pthread_attr_init (&attr);
- (void) pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
-
- sigset_t ss;
- sigset_t oss;
- sigfillset (&ss);
- __sigaddset (&ss, SIGCANCEL);
- INTERNAL_SYSCALL_DECL (err);
- INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &ss, &oss, _NSIG / 8);
-
- pthread_t th;
- int res = pthread_create (&th, &attr, timer_helper_thread, NULL);
- if (res == 0)
-
- __helper_tid = ((struct pthread *) th)->tid;
-
- INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &oss, NULL,
- _NSIG / 8);
-
- (void) pthread_attr_destroy (&attr);
-
- pthread_atfork (NULL, NULL, reset_helper_control);
- }
- #endif
- #ifndef __ASSUME_POSIX_TIMERS
- # include <nptl/sysdeps/pthread/timer_routines.c>
- #endif
|