timer_create.c 4.3 KB

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