timer_create.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* Copyright (C) 2000-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If
  13. not, see <https://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <signal.h>
  16. #include <pthread.h>
  17. #include <time.h>
  18. #include <unistd.h>
  19. #include "posix-timer.h"
  20. /* Create new per-process timer using CLOCK. */
  21. int
  22. timer_create (clockid_t clock_id, struct sigevent *evp, timer_t *timerid)
  23. {
  24. int retval = -1;
  25. struct timer_node *newtimer = NULL;
  26. struct thread_node *thread = NULL;
  27. if (0
  28. #if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
  29. || clock_id == CLOCK_PROCESS_CPUTIME_ID
  30. #endif
  31. #if defined _POSIX_THREAD_CPUTIME && _POSIX_THREAD_CPUTIME >= 0
  32. || clock_id == CLOCK_THREAD_CPUTIME_ID
  33. #endif
  34. )
  35. {
  36. /* We don't allow timers for CPU clocks. At least not in the
  37. moment. */
  38. __set_errno (ENOTSUP);
  39. return -1;
  40. }
  41. if (clock_id != CLOCK_REALTIME)
  42. {
  43. __set_errno (EINVAL);
  44. return -1;
  45. }
  46. pthread_once (&__timer_init_once_control, __timer_init_once);
  47. if (__timer_init_failed)
  48. {
  49. __set_errno (ENOMEM);
  50. return -1;
  51. }
  52. pthread_mutex_lock (&__timer_mutex);
  53. newtimer = __timer_alloc ();
  54. if (__glibc_unlikely (newtimer == NULL))
  55. {
  56. __set_errno (EAGAIN);
  57. goto unlock_bail;
  58. }
  59. if (evp != NULL)
  60. newtimer->event = *evp;
  61. else
  62. {
  63. newtimer->event.sigev_notify = SIGEV_SIGNAL;
  64. newtimer->event.sigev_signo = SIGALRM;
  65. newtimer->event.sigev_value.sival_ptr = newtimer;
  66. newtimer->event.sigev_notify_function = 0;
  67. }
  68. newtimer->event.sigev_notify_attributes = &newtimer->attr;
  69. newtimer->creator_pid = getpid ();
  70. switch (__builtin_expect (newtimer->event.sigev_notify, SIGEV_SIGNAL))
  71. {
  72. case SIGEV_NONE:
  73. case SIGEV_SIGNAL:
  74. /* We have a global thread for delivering timed signals.
  75. If it is not running, try to start it up. */
  76. thread = &__timer_signal_thread_rclk;
  77. if (! thread->exists)
  78. {
  79. if (__builtin_expect (__timer_thread_start (thread),
  80. 1) < 0)
  81. {
  82. __set_errno (EAGAIN);
  83. goto unlock_bail;
  84. }
  85. }
  86. break;
  87. case SIGEV_THREAD:
  88. /* Copy over thread attributes or set up default ones. */
  89. if (evp->sigev_notify_attributes)
  90. newtimer->attr = *(pthread_attr_t *) evp->sigev_notify_attributes;
  91. else
  92. pthread_attr_init (&newtimer->attr);
  93. /* Ensure thread attributes call for detached thread. */
  94. pthread_attr_setdetachstate (&newtimer->attr, PTHREAD_CREATE_DETACHED);
  95. /* Try to find existing thread having the right attributes. */
  96. thread = __timer_thread_find_matching (&newtimer->attr, clock_id);
  97. /* If no existing thread has these attributes, try to allocate one. */
  98. if (thread == NULL)
  99. thread = __timer_thread_alloc (&newtimer->attr, clock_id);
  100. /* Out of luck; no threads are available. */
  101. if (__glibc_unlikely (thread == NULL))
  102. {
  103. __set_errno (EAGAIN);
  104. goto unlock_bail;
  105. }
  106. /* If the thread is not running already, try to start it. */
  107. if (! thread->exists
  108. && __builtin_expect (! __timer_thread_start (thread), 0))
  109. {
  110. __set_errno (EAGAIN);
  111. goto unlock_bail;
  112. }
  113. break;
  114. default:
  115. __set_errno (EINVAL);
  116. goto unlock_bail;
  117. }
  118. newtimer->clock = clock_id;
  119. newtimer->abstime = 0;
  120. newtimer->armed = 0;
  121. newtimer->thread = thread;
  122. *timerid = timer_ptr2id (newtimer);
  123. retval = 0;
  124. if (__builtin_expect (retval, 0) == -1)
  125. {
  126. unlock_bail:
  127. if (thread != NULL)
  128. __timer_thread_dealloc (thread);
  129. if (newtimer != NULL)
  130. {
  131. timer_delref (newtimer);
  132. __timer_dealloc (newtimer);
  133. }
  134. }
  135. pthread_mutex_unlock (&__timer_mutex);
  136. return retval;
  137. }