timer_create.c 1.7 KB

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