timer_create.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* Copyright (C) 2003,2004, 2007, 2009 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If not,
  14. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. #include <errno.h>
  17. #include <pthread.h>
  18. #include <signal.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <time.h>
  22. #include <sysdep.h>
  23. #include <bits/kernel-features.h>
  24. #include <internaltypes.h>
  25. #include <pthreadP.h>
  26. #include "kernel-posix-timers.h"
  27. #include "kernel-posix-cpu-timers.h"
  28. #ifdef __NR_timer_create
  29. # ifndef __ASSUME_POSIX_TIMERS
  30. static int compat_timer_create (clockid_t clock_id, struct sigevent *evp,
  31. timer_t *timerid);
  32. # define timer_create static compat_timer_create
  33. # include <nptl/sysdeps/pthread/timer_create.c>
  34. # undef timer_create
  35. /* Nonzero if the system calls are not available. */
  36. int __no_posix_timers attribute_hidden;
  37. # endif
  38. # ifdef timer_create_alias
  39. # define timer_create timer_create_alias
  40. # endif
  41. int
  42. timer_create (
  43. clockid_t clock_id,
  44. struct sigevent *evp,
  45. timer_t *timerid)
  46. {
  47. # undef timer_create
  48. # ifndef __ASSUME_POSIX_TIMERS
  49. if (__no_posix_timers >= 0)
  50. # endif
  51. {
  52. clockid_t syscall_clockid = (clock_id == CLOCK_PROCESS_CPUTIME_ID
  53. ? MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED)
  54. : clock_id == CLOCK_THREAD_CPUTIME_ID
  55. ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED)
  56. : clock_id);
  57. /* If the user wants notification via a thread we need to handle
  58. this special. */
  59. if (evp == NULL
  60. || __builtin_expect (evp->sigev_notify != SIGEV_THREAD, 1))
  61. {
  62. struct sigevent local_evp;
  63. /* We avoid allocating too much memory by basically
  64. using struct timer as a derived class with the
  65. first two elements being in the superclass. We only
  66. need these two elements here. */
  67. struct timer *newp = (struct timer *) malloc (offsetof (struct timer,
  68. thrfunc));
  69. if (newp == NULL)
  70. /* No more memory. */
  71. return -1;
  72. if (evp == NULL)
  73. {
  74. /* The kernel has to pass up the timer ID which is a
  75. userlevel object. Therefore we cannot leave it up to
  76. the kernel to determine it. */
  77. local_evp.sigev_notify = SIGEV_SIGNAL;
  78. local_evp.sigev_signo = SIGALRM;
  79. local_evp.sigev_value.sival_ptr = newp;
  80. evp = &local_evp;
  81. }
  82. kernel_timer_t ktimerid;
  83. int retval = INLINE_SYSCALL (timer_create, 3, syscall_clockid, evp,
  84. &ktimerid);
  85. # ifndef __ASSUME_POSIX_TIMERS
  86. if (retval != -1 || errno != ENOSYS)
  87. # endif
  88. {
  89. # ifndef __ASSUME_POSIX_TIMERS
  90. __no_posix_timers = 1;
  91. # endif
  92. if (retval != -1)
  93. {
  94. newp->sigev_notify = (evp != NULL
  95. ? evp->sigev_notify : SIGEV_SIGNAL);
  96. newp->ktimerid = ktimerid;
  97. *timerid = (timer_t) newp;
  98. }
  99. else
  100. {
  101. /* Cannot allocate the timer, fail. */
  102. free (newp);
  103. retval = -1;
  104. }
  105. return retval;
  106. }
  107. free (newp);
  108. # ifndef __ASSUME_POSIX_TIMERS
  109. /* When we come here the syscall does not exist. Make sure we
  110. do not try to use it again. */
  111. __no_posix_timers = -1;
  112. # endif
  113. }
  114. else
  115. {
  116. # ifndef __ASSUME_POSIX_TIMERS
  117. /* Make sure we have the necessary kernel support. */
  118. if (__no_posix_timers == 0)
  119. {
  120. INTERNAL_SYSCALL_DECL (err);
  121. struct timespec ts;
  122. int res;
  123. res = INTERNAL_SYSCALL (clock_getres, err, 2,
  124. CLOCK_REALTIME, &ts);
  125. __no_posix_timers = (INTERNAL_SYSCALL_ERROR_P (res, err)
  126. ? -1 : 1);
  127. }
  128. if (__no_posix_timers > 0)
  129. # endif
  130. {
  131. /* Create the helper thread. */
  132. pthread_once (&__helper_once, __start_helper_thread);
  133. if (__helper_tid == 0)
  134. {
  135. /* No resources to start the helper thread. */
  136. __set_errno (EAGAIN);
  137. return -1;
  138. }
  139. struct timer *newp;
  140. newp = (struct timer *) malloc (sizeof (struct timer));
  141. if (newp == NULL)
  142. return -1;
  143. /* Copy the thread parameters the user provided. */
  144. newp->sival = evp->sigev_value;
  145. newp->thrfunc = evp->sigev_notify_function;
  146. newp->sigev_notify = SIGEV_THREAD;
  147. /* We cannot simply copy the thread attributes since the
  148. implementation might keep internal information for
  149. each instance. */
  150. (void) pthread_attr_init (&newp->attr);
  151. if (evp->sigev_notify_attributes != NULL)
  152. {
  153. struct pthread_attr *nattr;
  154. struct pthread_attr *oattr;
  155. nattr = (struct pthread_attr *) &newp->attr;
  156. oattr = (struct pthread_attr *) evp->sigev_notify_attributes;
  157. nattr->schedparam = oattr->schedparam;
  158. nattr->schedpolicy = oattr->schedpolicy;
  159. nattr->flags = oattr->flags;
  160. nattr->guardsize = oattr->guardsize;
  161. nattr->stackaddr = oattr->stackaddr;
  162. nattr->stacksize = oattr->stacksize;
  163. }
  164. /* In any case set the detach flag. */
  165. (void) pthread_attr_setdetachstate (&newp->attr,
  166. PTHREAD_CREATE_DETACHED);
  167. /* Create the event structure for the kernel timer. */
  168. struct sigevent sev =
  169. { .sigev_value.sival_ptr = newp,
  170. .sigev_signo = SIGTIMER,
  171. .sigev_notify = SIGEV_SIGNAL | SIGEV_THREAD_ID,
  172. ._sigev_un = { ._pad = { [0] = __helper_tid } } };
  173. /* Create the timer. */
  174. INTERNAL_SYSCALL_DECL (err);
  175. int res;
  176. res = INTERNAL_SYSCALL (timer_create, err, 3,
  177. syscall_clockid, &sev, &newp->ktimerid);
  178. if (! INTERNAL_SYSCALL_ERROR_P (res, err))
  179. {
  180. /* Add to the queue of active timers with thread
  181. delivery. */
  182. pthread_mutex_lock (&__active_timer_sigev_thread_lock);
  183. newp->next = __active_timer_sigev_thread;
  184. __active_timer_sigev_thread = newp;
  185. pthread_mutex_unlock (&__active_timer_sigev_thread_lock);
  186. *timerid = (timer_t) newp;
  187. return 0;
  188. }
  189. /* Free the resources. */
  190. free (newp);
  191. __set_errno (INTERNAL_SYSCALL_ERRNO (res, err));
  192. return -1;
  193. }
  194. }
  195. }
  196. # ifndef __ASSUME_POSIX_TIMERS
  197. /* Compatibility code. */
  198. return compat_timer_create (clock_id, evp, timerid);
  199. # endif
  200. }
  201. #else
  202. # ifdef timer_create_alias
  203. # define timer_create timer_create_alias
  204. # endif
  205. /* The new system calls are not available. Use the userlevel
  206. implementation. */
  207. # include <nptl/sysdeps/pthread/timer_create.c>
  208. #endif