]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/librt/timer_create.c
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / librt / timer_create.c
1 /*
2  * timer_create.c - create a per-process timer.
3  */
4
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>
12
13 #include "kernel-posix-timers.h"
14
15 #ifdef __NR_timer_create
16
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);
20
21 /* Create a per-process timer */
22 int timer_create(clockid_t clock_id, struct sigevent *evp, timer_t * timerid)
23 {
24         int retval;
25         kernel_timer_t ktimerid;
26         struct sigevent default_evp;
27         struct timer *newp;
28
29         if (evp == NULL) {
30                 /*
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.
33                  */
34                 default_evp.sigev_notify = SIGEV_SIGNAL;
35                 default_evp.sigev_signo = SIGALRM;
36                 evp = &default_evp;
37         }
38
39         /* Notification via a thread is not supported yet */
40         if (__builtin_expect(evp->sigev_notify == SIGEV_THREAD, 1))
41                 return -1;
42
43         /*
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.
47          */
48         newp = malloc(offsetof(struct timer, thrfunc));
49         if (newp == NULL)
50                 return -1;      /* No memory */
51         default_evp.sigev_value.sival_ptr = newp;
52
53         retval = __syscall_timer_create(clock_id, evp, &ktimerid);
54         if (retval != -1) {
55                 newp->sigev_notify = evp->sigev_notify;
56                 newp->ktimerid = ktimerid;
57
58                 *timerid = (timer_t) newp;
59         } else {
60                 /* Cannot allocate the timer, fail */
61                 free(newp);
62                 retval = -1;
63         }
64
65         return retval;
66 }
67
68 #endif