Warning: That file was not part of the compilation database. It may have many parsing errors.

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

Warning: That file was not part of the compilation database. It may have many parsing errors.