fix regression from a745c4bfc8a9b5db4e48387170da0dc1d39e3abe
[uclibc-ng.git] / librt / timer_create.c
blobf52a36ff9066be3fb08cf392ec5622fcd6072e63
1 /*
2 * timer_create.c - create a per-process timer.
3 */
5 #include <stddef.h>
6 #include <errno.h>
7 #include <signal.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <time.h>
11 #include <sys/syscall.h>
13 #include "kernel-posix-timers.h"
15 #ifdef __NR_timer_create
17 #define __NR___syscall_timer_create __NR_timer_create
18 static __inline__ _syscall3(int, __syscall_timer_create, clockid_t, clock_id,
19 struct sigevent *, evp, kernel_timer_t *, ktimerid);
21 /* Create a per-process timer */
22 int timer_create(clockid_t clock_id, struct sigevent *evp, timer_t * timerid)
24 int retval;
25 kernel_timer_t ktimerid;
26 struct sigevent default_evp;
27 struct timer *newp;
29 if (evp == NULL) {
31 * The kernel has to pass up the timer ID which is a userlevel object.
32 * Therefore we cannot leave it up to the kernel to determine it.
34 default_evp.sigev_notify = SIGEV_SIGNAL;
35 default_evp.sigev_signo = SIGALRM;
36 evp = &default_evp;
39 /* Notification via a thread is not supported yet */
40 if (__builtin_expect(evp->sigev_notify == SIGEV_THREAD, 1))
41 return -1;
44 * We avoid allocating too much memory by basically using
45 * struct timer as a derived class with the first two elements
46 * being in the superclass. We only need these two elements here.
48 newp = malloc(offsetof(struct timer, thrfunc));
49 if (newp == NULL)
50 return -1; /* No memory */
51 default_evp.sigev_value.sival_ptr = newp;
53 retval = __syscall_timer_create(clock_id, evp, &ktimerid);
54 if (retval != -1) {
55 newp->sigev_notify = evp->sigev_notify;
56 newp->ktimerid = ktimerid;
58 *timerid = (timer_t) newp;
59 } else {
60 /* Cannot allocate the timer, fail */
61 free(newp);
62 retval = -1;
65 return retval;
68 #endif