timer_create.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* Copyright (C) 2000, 2003, 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Kaz Kylheku <kaz@ashi.footprints.net>.
  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
  14. not, see <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <signal.h>
  17. #include <pthread.h>
  18. #include <time.h>
  19. #include <unistd.h>
  20. #include "posix-timer.h"
  21. /* Create new per-process timer using CLOCK. */
  22. int
  23. timer_create (
  24. clockid_t clock_id,
  25. struct sigevent *evp,
  26. timer_t *timerid)
  27. {
  28. int retval = -1;
  29. struct timer_node *newtimer = NULL;
  30. struct thread_node *thread = NULL;
  31. if (0
  32. #if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
  33. || clock_id == CLOCK_PROCESS_CPUTIME_ID
  34. #endif
  35. #if defined _POSIX_THREAD_CPUTIME && _POSIX_THREAD_CPUTIME >= 0
  36. || clock_id == CLOCK_THREAD_CPUTIME_ID
  37. #endif
  38. )
  39. {
  40. /* We don't allow timers for CPU clocks. At least not in the
  41. moment. */
  42. __set_errno (ENOTSUP);
  43. return -1;
  44. }
  45. if (clock_id != CLOCK_REALTIME)
  46. {
  47. __set_errno (EINVAL);
  48. return -1;
  49. }
  50. pthread_once (&__timer_init_once_control, __timer_init_once);
  51. if (__timer_init_failed)
  52. {
  53. __set_errno (ENOMEM);
  54. return -1;
  55. }
  56. pthread_mutex_lock (&__timer_mutex);
  57. newtimer = __timer_alloc ();
  58. if (__builtin_expect (newtimer == NULL, 0))
  59. {
  60. __set_errno (EAGAIN);
  61. goto unlock_bail;
  62. }
  63. if (evp != NULL)
  64. newtimer->event = *evp;
  65. else
  66. {
  67. newtimer->event.sigev_notify = SIGEV_SIGNAL;
  68. newtimer->event.sigev_signo = SIGALRM;
  69. newtimer->event.sigev_value.sival_ptr = timer_ptr2id (newtimer);
  70. newtimer->event.sigev_notify_function = 0;
  71. }
  72. newtimer->event.sigev_notify_attributes = &newtimer->attr;
  73. newtimer->creator_pid = getpid ();
  74. switch (__builtin_expect (newtimer->event.sigev_notify, SIGEV_SIGNAL))
  75. {
  76. case SIGEV_NONE:
  77. case SIGEV_SIGNAL:
  78. /* We have a global thread for delivering timed signals.
  79. If it is not running, try to start it up. */
  80. thread = &__timer_signal_thread_rclk;
  81. if (! thread->exists)
  82. {
  83. if (__builtin_expect (__timer_thread_start (thread),
  84. 1) < 0)
  85. {
  86. __set_errno (EAGAIN);
  87. goto unlock_bail;
  88. }
  89. }
  90. break;
  91. case SIGEV_THREAD:
  92. /* Copy over thread attributes or set up default ones. */
  93. if (evp->sigev_notify_attributes)
  94. newtimer->attr = *(pthread_attr_t *) evp->sigev_notify_attributes;
  95. else
  96. pthread_attr_init (&newtimer->attr);
  97. /* Ensure thread attributes call for deatched thread. */
  98. pthread_attr_setdetachstate (&newtimer->attr, PTHREAD_CREATE_DETACHED);
  99. /* Try to find existing thread having the right attributes. */
  100. thread = __timer_thread_find_matching (&newtimer->attr, clock_id);
  101. /* If no existing thread has these attributes, try to allocate one. */
  102. if (thread == NULL)
  103. thread = __timer_thread_alloc (&newtimer->attr, clock_id);
  104. /* Out of luck; no threads are available. */
  105. if (__builtin_expect (thread == NULL, 0))
  106. {
  107. __set_errno (EAGAIN);
  108. goto unlock_bail;
  109. }
  110. /* If the thread is not running already, try to start it. */
  111. if (! thread->exists
  112. && __builtin_expect (! __timer_thread_start (thread), 0))
  113. {
  114. __set_errno (EAGAIN);
  115. goto unlock_bail;
  116. }
  117. break;
  118. default:
  119. __set_errno (EINVAL);
  120. goto unlock_bail;
  121. }
  122. newtimer->clock = clock_id;
  123. newtimer->abstime = 0;
  124. newtimer->armed = 0;
  125. newtimer->thread = thread;
  126. *timerid = timer_ptr2id (newtimer);
  127. retval = 0;
  128. if (__builtin_expect (retval, 0) == -1)
  129. {
  130. unlock_bail:
  131. if (thread != NULL)
  132. __timer_thread_dealloc (thread);
  133. if (newtimer != NULL)
  134. {
  135. timer_delref (newtimer);
  136. __timer_dealloc (newtimer);
  137. }
  138. }
  139. pthread_mutex_unlock (&__timer_mutex);
  140. return retval;
  141. }