1 /* Copyright (C) 2000, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Kaz Kylheku <kaz@ashi.footprints.net>.
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.
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.
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 not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
26 #include "posix-timer.h"
29 /* Create new per-process timer using CLOCK. */
31 timer_create (clock_id
, evp
, timerid
)
37 struct timer_node
*newtimer
= NULL
;
38 struct thread_node
*thread
= NULL
;
42 || clock_id
== CLOCK_PROCESS_CPUTIME_ID
44 #ifdef _POSIX_THREAD_CPUTIME
45 || clock_id
== CLOCK_THREAD_CPUTIME_ID
49 /* We don't allow timers for CPU clocks. At least not in the
51 __set_errno (ENOTSUP
);
55 if (clock_id
!= CLOCK_REALTIME
)
61 pthread_once (&__timer_init_once_control
, __timer_init_once
);
63 if (__timer_init_failed
)
69 pthread_mutex_lock (&__timer_mutex
);
71 newtimer
= __timer_alloc ();
72 if (__builtin_expect (newtimer
== NULL
, 0))
79 newtimer
->event
= *evp
;
82 newtimer
->event
.sigev_notify
= SIGEV_SIGNAL
;
83 newtimer
->event
.sigev_signo
= SIGALRM
;
84 newtimer
->event
.sigev_value
.sival_ptr
= timer_ptr2id (newtimer
);
85 newtimer
->event
.sigev_notify_function
= 0;
88 newtimer
->event
.sigev_notify_attributes
= &newtimer
->attr
;
89 newtimer
->creator_pid
= getpid ();
91 switch (__builtin_expect (newtimer
->event
.sigev_notify
, SIGEV_SIGNAL
))
94 /* This is a strange choice! */
98 /* We have a global thread for delivering timed signals.
99 If it is not running, try to start it up. */
104 thread
= &__timer_signal_thread_rclk
;
106 #ifdef _POSIX_CPUTIME
107 case CLOCK_PROCESS_CPUTIME_ID
:
108 thread
= &__timer_signal_thread_pclk
;
111 #ifdef _POSIX_THREAD_CPUTIME
112 case CLOCK_THREAD_CPUTIME_ID
:
113 thread
= &__timer_signal_thread_tclk
;
118 if (! thread
->exists
)
120 if (__builtin_expect (__timer_thread_start (thread
),
123 __set_errno (EAGAIN
);
130 /* Copy over thread attributes or set up default ones. */
131 if (evp
->sigev_notify_attributes
)
132 newtimer
->attr
= *(pthread_attr_t
*) evp
->sigev_notify_attributes
;
134 pthread_attr_init (&newtimer
->attr
);
136 /* Ensure thread attributes call for deatched thread. */
137 pthread_attr_setdetachstate (&newtimer
->attr
, PTHREAD_CREATE_DETACHED
);
139 /* Try to find existing thread having the right attributes. */
140 thread
= __timer_thread_find_matching (&newtimer
->attr
, clock_id
);
142 /* If no existing thread has these attributes, try to allocate one. */
144 thread
= __timer_thread_alloc (&newtimer
->attr
, clock_id
);
146 /* Out of luck; no threads are available. */
147 if (__builtin_expect (thread
== NULL
, 0))
149 __set_errno (EAGAIN
);
153 /* If the thread is not running already, try to start it. */
155 && __builtin_expect (! __timer_thread_start (thread
), 0))
157 __set_errno (EAGAIN
);
163 __set_errno (EINVAL
);
167 newtimer
->clock
= clock_id
;
168 newtimer
->abstime
= 0;
170 newtimer
->thread
= thread
;
172 *timerid
= timer_ptr2id (newtimer
);
175 if (__builtin_expect (retval
, 0) == -1)
179 __timer_thread_dealloc (thread
);
180 if (newtimer
!= NULL
)
182 timer_delref (newtimer
);
183 __timer_dealloc (newtimer
);
187 pthread_mutex_unlock (&__timer_mutex
);