1 /* Copyright (C) 2003-2016 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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.
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.
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 <http://www.gnu.org/licenses/>. */
26 #include <internaltypes.h>
27 #include <nptl/pthreadP.h>
28 #include "kernel-posix-timers.h"
29 #include "kernel-posix-cpu-timers.h"
32 #ifdef timer_create_alias
33 # define timer_create timer_create_alias
38 timer_create (clockid_t clock_id
, struct sigevent
*evp
, timer_t
*timerid
)
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
)
48 /* If the user wants notification via a thread we need to handle
51 || __builtin_expect (evp
->sigev_notify
!= SIGEV_THREAD
, 1))
53 struct sigevent local_evp
;
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
,
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
;
77 kernel_timer_t ktimerid
;
78 int retval
= INLINE_SYSCALL (timer_create
, 3, syscall_clockid
, evp
,
83 newp
->sigev_notify
= (evp
!= NULL
84 ? evp
->sigev_notify
: SIGEV_SIGNAL
);
85 newp
->ktimerid
= ktimerid
;
87 *timerid
= (timer_t
) newp
;
91 /* Cannot allocate the timer, fail. */
100 /* Create the helper thread. */
101 pthread_once (&__helper_once
, __start_helper_thread
);
102 if (__helper_tid
== 0)
104 /* No resources to start the helper thread. */
105 __set_errno (EAGAIN
);
110 newp
= (struct timer
*) malloc (sizeof (struct timer
));
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
;
119 /* We cannot simply copy the thread attributes since the
120 implementation might keep internal information for
122 (void) pthread_attr_init (&newp
->attr
);
123 if (evp
->sigev_notify_attributes
!= NULL
)
125 struct pthread_attr
*nattr
;
126 struct pthread_attr
*oattr
;
128 nattr
= (struct pthread_attr
*) &newp
->attr
;
129 oattr
= (struct pthread_attr
*) evp
->sigev_notify_attributes
;
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
;
139 /* In any case set the detach flag. */
140 (void) pthread_attr_setdetachstate (&newp
->attr
,
141 PTHREAD_CREATE_DETACHED
);
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
} } };
150 /* Create the timer. */
151 INTERNAL_SYSCALL_DECL (err
);
153 res
= INTERNAL_SYSCALL (timer_create
, err
, 3,
154 syscall_clockid
, &sev
, &newp
->ktimerid
);
155 if (! INTERNAL_SYSCALL_ERROR_P (res
, err
))
157 /* Add to the queue of active timers with thread
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
);
164 *timerid
= (timer_t
) newp
;
168 /* Free the resources. */
171 __set_errno (INTERNAL_SYSCALL_ERRNO (res
, err
));