1/* Copyright (C) 2003-2020 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
37int
38timer_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 /* We avoid allocating too much memory by basically
56 using struct timer as a derived class with the
57 first two elements being in the superclass. We only
58 need these two elements here. */
59 struct timer *newp = (struct timer *) malloc (offsetof (struct timer,
60 thrfunc));
61 if (newp == NULL)
62 /* No more memory. */
63 return -1;
64
65 if (evp == NULL)
66 {
67 /* The kernel has to pass up the timer ID which is a
68 userlevel object. Therefore we cannot leave it up to
69 the kernel to determine it. */
70 local_evp.sigev_notify = SIGEV_SIGNAL;
71 local_evp.sigev_signo = SIGALRM;
72 local_evp.sigev_value.sival_ptr = newp;
73
74 evp = &local_evp;
75 }
76
77 kernel_timer_t ktimerid;
78 int retval = INLINE_SYSCALL (timer_create, 3, syscall_clockid, evp,
79 &ktimerid);
80
81 if (retval != -1)
82 {
83 newp->sigev_notify = (evp != NULL
84 ? evp->sigev_notify : SIGEV_SIGNAL);
85 newp->ktimerid = ktimerid;
86
87 *timerid = (timer_t) newp;
88 }
89 else
90 {
91 /* Cannot allocate the timer, fail. */
92 free (newp);
93 retval = -1;
94 }
95
96 return retval;
97 }
98 else
99 {
100 /* Create the helper thread. */
101 pthread_once (&__helper_once, __start_helper_thread);
102 if (__helper_tid == 0)
103 {
104 /* No resources to start the helper thread. */
105 __set_errno (EAGAIN);
106 return -1;
107 }
108
109 struct timer *newp;
110 newp = (struct timer *) malloc (sizeof (struct timer));
111 if (newp == NULL)
112 return -1;
113
114 /* Copy the thread parameters the user provided. */
115 newp->sival = evp->sigev_value;
116 newp->thrfunc = evp->sigev_notify_function;
117 newp->sigev_notify = SIGEV_THREAD;
118
119 /* We cannot simply copy the thread attributes since the
120 implementation might keep internal information for
121 each instance. */
122 (void) pthread_attr_init (&newp->attr);
123 if (evp->sigev_notify_attributes != NULL)
124 {
125 struct pthread_attr *nattr;
126 struct pthread_attr *oattr;
127
128 nattr = (struct pthread_attr *) &newp->attr;
129 oattr = (struct pthread_attr *) evp->sigev_notify_attributes;
130
131 nattr->schedparam = oattr->schedparam;
132 nattr->schedpolicy = oattr->schedpolicy;
133 nattr->flags = oattr->flags;
134 nattr->guardsize = oattr->guardsize;
135 nattr->stackaddr = oattr->stackaddr;
136 nattr->stacksize = oattr->stacksize;
137 }
138
139 /* In any case set the detach flag. */
140 (void) pthread_attr_setdetachstate (&newp->attr,
141 PTHREAD_CREATE_DETACHED);
142
143 /* Create the event structure for the kernel timer. */
144 struct sigevent sev =
145 { .sigev_value.sival_ptr = newp,
146 .sigev_signo = SIGTIMER,
147 .sigev_notify = SIGEV_SIGNAL | SIGEV_THREAD_ID,
148 ._sigev_un = { ._pad = { [0] = __helper_tid } } };
149
150 /* Create the timer. */
151 INTERNAL_SYSCALL_DECL (err);
152 int res;
153 res = INTERNAL_SYSCALL (timer_create, err, 3,
154 syscall_clockid, &sev, &newp->ktimerid);
155 if (! INTERNAL_SYSCALL_ERROR_P (res, err))
156 {
157 /* Add to the queue of active timers with thread
158 delivery. */
159 pthread_mutex_lock (&__active_timer_sigev_thread_lock);
160 newp->next = __active_timer_sigev_thread;
161 __active_timer_sigev_thread = newp;
162 pthread_mutex_unlock (&__active_timer_sigev_thread_lock);
163
164 *timerid = (timer_t) newp;
165 return 0;
166 }
167
168 /* Free the resources. */
169 free (newp);
170
171 __set_errno (INTERNAL_SYSCALL_ERRNO (res, err));
172
173 return -1;
174 }
175 }
176}
177