]>
Commit | Line | Data |
---|---|---|
d4697bc9 | 1 | /* Copyright (C) 2000-2014 Free Software Foundation, Inc. |
76a50749 UD |
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 | |
59ba27a6 PE |
16 | License along with the GNU C Library; see the file COPYING.LIB. If |
17 | not, see <http://www.gnu.org/licenses/>. */ | |
76a50749 UD |
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. */ | |
29 | int | |
30 | timer_create (clock_id, evp, timerid) | |
31 | clockid_t clock_id; | |
32 | struct sigevent *evp; | |
33 | timer_t *timerid; | |
34 | { | |
35 | int retval = -1; | |
36 | struct timer_node *newtimer = NULL; | |
37 | struct thread_node *thread = NULL; | |
38 | ||
5324b81c | 39 | if (0 |
2c03b6db | 40 | #if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0 |
5324b81c | 41 | || clock_id == CLOCK_PROCESS_CPUTIME_ID |
76a50749 | 42 | #endif |
2c03b6db | 43 | #if defined _POSIX_THREAD_CPUTIME && _POSIX_THREAD_CPUTIME >= 0 |
5324b81c | 44 | || clock_id == CLOCK_THREAD_CPUTIME_ID |
76a50749 UD |
45 | #endif |
46 | ) | |
5324b81c UD |
47 | { |
48 | /* We don't allow timers for CPU clocks. At least not in the | |
49 | moment. */ | |
50 | __set_errno (ENOTSUP); | |
51 | return -1; | |
52 | } | |
53 | ||
54 | if (clock_id != CLOCK_REALTIME) | |
76a50749 UD |
55 | { |
56 | __set_errno (EINVAL); | |
57 | return -1; | |
58 | } | |
59 | ||
60 | pthread_once (&__timer_init_once_control, __timer_init_once); | |
61 | ||
62 | if (__timer_init_failed) | |
63 | { | |
64 | __set_errno (ENOMEM); | |
65 | return -1; | |
66 | } | |
67 | ||
68 | pthread_mutex_lock (&__timer_mutex); | |
69 | ||
70 | newtimer = __timer_alloc (); | |
a1ffb40e | 71 | if (__glibc_unlikely (newtimer == NULL)) |
76a50749 UD |
72 | { |
73 | __set_errno (EAGAIN); | |
74 | goto unlock_bail; | |
75 | } | |
76 | ||
77 | if (evp != NULL) | |
78 | newtimer->event = *evp; | |
79 | else | |
80 | { | |
81 | newtimer->event.sigev_notify = SIGEV_SIGNAL; | |
82 | newtimer->event.sigev_signo = SIGALRM; | |
49b65043 | 83 | newtimer->event.sigev_value.sival_ptr = timer_ptr2id (newtimer); |
76a50749 UD |
84 | newtimer->event.sigev_notify_function = 0; |
85 | } | |
86 | ||
87 | newtimer->event.sigev_notify_attributes = &newtimer->attr; | |
88 | newtimer->creator_pid = getpid (); | |
89 | ||
90 | switch (__builtin_expect (newtimer->event.sigev_notify, SIGEV_SIGNAL)) | |
91 | { | |
92 | case SIGEV_NONE: | |
76a50749 UD |
93 | case SIGEV_SIGNAL: |
94 | /* We have a global thread for delivering timed signals. | |
95 | If it is not running, try to start it up. */ | |
bf7c04cd | 96 | thread = &__timer_signal_thread_rclk; |
76a50749 UD |
97 | if (! thread->exists) |
98 | { | |
99 | if (__builtin_expect (__timer_thread_start (thread), | |
100 | 1) < 0) | |
101 | { | |
102 | __set_errno (EAGAIN); | |
103 | goto unlock_bail; | |
104 | } | |
105 | } | |
106 | break; | |
107 | ||
108 | case SIGEV_THREAD: | |
109 | /* Copy over thread attributes or set up default ones. */ | |
110 | if (evp->sigev_notify_attributes) | |
111 | newtimer->attr = *(pthread_attr_t *) evp->sigev_notify_attributes; | |
112 | else | |
113 | pthread_attr_init (&newtimer->attr); | |
114 | ||
115 | /* Ensure thread attributes call for deatched thread. */ | |
116 | pthread_attr_setdetachstate (&newtimer->attr, PTHREAD_CREATE_DETACHED); | |
117 | ||
118 | /* Try to find existing thread having the right attributes. */ | |
119 | thread = __timer_thread_find_matching (&newtimer->attr, clock_id); | |
120 | ||
121 | /* If no existing thread has these attributes, try to allocate one. */ | |
122 | if (thread == NULL) | |
123 | thread = __timer_thread_alloc (&newtimer->attr, clock_id); | |
124 | ||
125 | /* Out of luck; no threads are available. */ | |
a1ffb40e | 126 | if (__glibc_unlikely (thread == NULL)) |
76a50749 UD |
127 | { |
128 | __set_errno (EAGAIN); | |
129 | goto unlock_bail; | |
130 | } | |
131 | ||
132 | /* If the thread is not running already, try to start it. */ | |
133 | if (! thread->exists | |
134 | && __builtin_expect (! __timer_thread_start (thread), 0)) | |
135 | { | |
136 | __set_errno (EAGAIN); | |
137 | goto unlock_bail; | |
138 | } | |
139 | break; | |
140 | ||
141 | default: | |
142 | __set_errno (EINVAL); | |
143 | goto unlock_bail; | |
144 | } | |
145 | ||
146 | newtimer->clock = clock_id; | |
147 | newtimer->abstime = 0; | |
148 | newtimer->armed = 0; | |
149 | newtimer->thread = thread; | |
150 | ||
151 | *timerid = timer_ptr2id (newtimer); | |
152 | retval = 0; | |
153 | ||
154 | if (__builtin_expect (retval, 0) == -1) | |
155 | { | |
156 | unlock_bail: | |
157 | if (thread != NULL) | |
158 | __timer_thread_dealloc (thread); | |
159 | if (newtimer != NULL) | |
a7f6c66e RM |
160 | { |
161 | timer_delref (newtimer); | |
162 | __timer_dealloc (newtimer); | |
163 | } | |
76a50749 UD |
164 | } |
165 | ||
166 | pthread_mutex_unlock (&__timer_mutex); | |
167 | ||
168 | return retval; | |
169 | } |