1 /* Copyright (C) 2003-2015 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 (clock_id
, evp
, timerid
)
45 clockid_t syscall_clockid
= (clock_id
== CLOCK_PROCESS_CPUTIME_ID
46 ? MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED
)
47 : clock_id
== CLOCK_THREAD_CPUTIME_ID
48 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED
)
51 /* If the user wants notification via a thread we need to handle
54 || __builtin_expect (evp
->sigev_notify
!= SIGEV_THREAD
, 1))
56 struct sigevent local_evp
;
58 /* We avoid allocating too much memory by basically
59 using struct timer as a derived class with the
60 first two elements being in the superclass. We only
61 need these two elements here. */
62 struct timer
*newp
= (struct timer
*) malloc (offsetof (struct timer
,
70 /* The kernel has to pass up the timer ID which is a
71 userlevel object. Therefore we cannot leave it up to
72 the kernel to determine it. */
73 local_evp
.sigev_notify
= SIGEV_SIGNAL
;
74 local_evp
.sigev_signo
= SIGALRM
;
75 local_evp
.sigev_value
.sival_ptr
= newp
;
80 kernel_timer_t ktimerid
;
81 int retval
= INLINE_SYSCALL (timer_create
, 3, syscall_clockid
, evp
,
86 newp
->sigev_notify
= (evp
!= NULL
87 ? evp
->sigev_notify
: SIGEV_SIGNAL
);
88 newp
->ktimerid
= ktimerid
;
90 *timerid
= (timer_t
) newp
;
94 /* Cannot allocate the timer, fail. */
103 /* Create the helper thread. */
104 pthread_once (&__helper_once
, __start_helper_thread
);
105 if (__helper_tid
== 0)
107 /* No resources to start the helper thread. */
108 __set_errno (EAGAIN
);
113 newp
= (struct timer
*) malloc (sizeof (struct timer
));
117 /* Copy the thread parameters the user provided. */
118 newp
->sival
= evp
->sigev_value
;
119 newp
->thrfunc
= evp
->sigev_notify_function
;
120 newp
->sigev_notify
= SIGEV_THREAD
;
122 /* We cannot simply copy the thread attributes since the
123 implementation might keep internal information for
125 (void) pthread_attr_init (&newp
->attr
);
126 if (evp
->sigev_notify_attributes
!= NULL
)
128 struct pthread_attr
*nattr
;
129 struct pthread_attr
*oattr
;
131 nattr
= (struct pthread_attr
*) &newp
->attr
;
132 oattr
= (struct pthread_attr
*) evp
->sigev_notify_attributes
;
134 nattr
->schedparam
= oattr
->schedparam
;
135 nattr
->schedpolicy
= oattr
->schedpolicy
;
136 nattr
->flags
= oattr
->flags
;
137 nattr
->guardsize
= oattr
->guardsize
;
138 nattr
->stackaddr
= oattr
->stackaddr
;
139 nattr
->stacksize
= oattr
->stacksize
;
142 /* In any case set the detach flag. */
143 (void) pthread_attr_setdetachstate (&newp
->attr
,
144 PTHREAD_CREATE_DETACHED
);
146 /* Create the event structure for the kernel timer. */
147 struct sigevent sev
=
148 { .sigev_value
.sival_ptr
= newp
,
149 .sigev_signo
= SIGTIMER
,
150 .sigev_notify
= SIGEV_SIGNAL
| SIGEV_THREAD_ID
,
151 ._sigev_un
= { ._pad
= { [0] = __helper_tid
} } };
153 /* Create the timer. */
154 INTERNAL_SYSCALL_DECL (err
);
156 res
= INTERNAL_SYSCALL (timer_create
, err
, 3,
157 syscall_clockid
, &sev
, &newp
->ktimerid
);
158 if (! INTERNAL_SYSCALL_ERROR_P (res
, err
))
160 /* Add to the queue of active timers with thread
162 pthread_mutex_lock (&__active_timer_sigev_thread_lock
);
163 newp
->next
= __active_timer_sigev_thread
;
164 __active_timer_sigev_thread
= newp
;
165 pthread_mutex_unlock (&__active_timer_sigev_thread_lock
);
167 *timerid
= (timer_t
) newp
;
171 /* Free the resources. */
174 __set_errno (INTERNAL_SYSCALL_ERRNO (res
, err
));