1 | /* Copyright (C) 2003-2021 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2003. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public License as |
7 | published by the Free Software Foundation; either version 2.1 of the |
8 | License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; see the file COPYING.LIB. If |
17 | not, see <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <errno.h> |
20 | #include <pthread.h> |
21 | #include <signal.h> |
22 | #include <stdlib.h> |
23 | #include <string.h> |
24 | #include <time.h> |
25 | #include <sysdep.h> |
26 | #include <internaltypes.h> |
27 | #include <nptl/pthreadP.h> |
28 | #include "kernel-posix-timers.h" |
29 | #include "kernel-posix-cpu-timers.h" |
30 | |
31 | |
32 | #ifdef timer_create_alias |
33 | # define timer_create timer_create_alias |
34 | #endif |
35 | |
36 | |
37 | int |
38 | timer_create (clockid_t clock_id, struct sigevent *evp, timer_t *timerid) |
39 | { |
40 | #undef timer_create |
41 | { |
42 | clockid_t syscall_clockid = (clock_id == CLOCK_PROCESS_CPUTIME_ID |
43 | ? MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED) |
44 | : clock_id == CLOCK_THREAD_CPUTIME_ID |
45 | ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED) |
46 | : clock_id); |
47 | |
48 | /* If the user wants notification via a thread we need to handle |
49 | this special. */ |
50 | if (evp == NULL |
51 | || __builtin_expect (evp->sigev_notify != SIGEV_THREAD, 1)) |
52 | { |
53 | struct sigevent local_evp; |
54 | |
55 | if (evp == NULL) |
56 | { |
57 | /* The kernel has to pass up the timer ID which is a |
58 | userlevel object. Therefore we cannot leave it up to |
59 | the kernel to determine it. */ |
60 | local_evp.sigev_notify = SIGEV_SIGNAL; |
61 | local_evp.sigev_signo = SIGALRM; |
62 | local_evp.sigev_value.sival_ptr = NULL; |
63 | |
64 | evp = &local_evp; |
65 | } |
66 | |
67 | kernel_timer_t ktimerid; |
68 | if (INLINE_SYSCALL_CALL (timer_create, syscall_clockid, evp, |
69 | &ktimerid) == -1) |
70 | return -1; |
71 | |
72 | *timerid = kernel_timer_to_timerid (ktimerid); |
73 | } |
74 | else |
75 | { |
76 | /* Create the helper thread. */ |
77 | pthread_once (&__helper_once, __start_helper_thread); |
78 | if (__helper_tid == 0) |
79 | { |
80 | /* No resources to start the helper thread. */ |
81 | __set_errno (EAGAIN); |
82 | return -1; |
83 | } |
84 | |
85 | struct timer *newp = malloc (sizeof (struct timer)); |
86 | if (newp == NULL) |
87 | return -1; |
88 | |
89 | /* Copy the thread parameters the user provided. */ |
90 | newp->sival = evp->sigev_value; |
91 | newp->thrfunc = evp->sigev_notify_function; |
92 | |
93 | /* We cannot simply copy the thread attributes since the |
94 | implementation might keep internal information for |
95 | each instance. */ |
96 | pthread_attr_init (&newp->attr); |
97 | if (evp->sigev_notify_attributes != NULL) |
98 | { |
99 | struct pthread_attr *nattr; |
100 | struct pthread_attr *oattr; |
101 | |
102 | nattr = (struct pthread_attr *) &newp->attr; |
103 | oattr = (struct pthread_attr *) evp->sigev_notify_attributes; |
104 | |
105 | nattr->schedparam = oattr->schedparam; |
106 | nattr->schedpolicy = oattr->schedpolicy; |
107 | nattr->flags = oattr->flags; |
108 | nattr->guardsize = oattr->guardsize; |
109 | nattr->stackaddr = oattr->stackaddr; |
110 | nattr->stacksize = oattr->stacksize; |
111 | } |
112 | |
113 | /* In any case set the detach flag. */ |
114 | pthread_attr_setdetachstate (&newp->attr, PTHREAD_CREATE_DETACHED); |
115 | |
116 | /* Create the event structure for the kernel timer. */ |
117 | struct sigevent sev = |
118 | { .sigev_value.sival_ptr = newp, |
119 | .sigev_signo = SIGTIMER, |
120 | .sigev_notify = SIGEV_SIGNAL | SIGEV_THREAD_ID, |
121 | ._sigev_un = { ._pad = { [0] = __helper_tid } } }; |
122 | |
123 | /* Create the timer. */ |
124 | int res; |
125 | res = INTERNAL_SYSCALL_CALL (timer_create, syscall_clockid, &sev, |
126 | &newp->ktimerid); |
127 | if (INTERNAL_SYSCALL_ERROR_P (res)) |
128 | { |
129 | free (newp); |
130 | __set_errno (INTERNAL_SYSCALL_ERRNO (res)); |
131 | return -1; |
132 | } |
133 | |
134 | /* Add to the queue of active timers with thread delivery. */ |
135 | pthread_mutex_lock (&__active_timer_sigev_thread_lock); |
136 | newp->next = __active_timer_sigev_thread; |
137 | __active_timer_sigev_thread = newp; |
138 | pthread_mutex_unlock (&__active_timer_sigev_thread_lock); |
139 | |
140 | *timerid = timer_to_timerid (newp); |
141 | } |
142 | } |
143 | |
144 | return 0; |
145 | } |
146 | |