| 1 |
148 |
jeremybenn |
/* Definitions for POSIX timer implementation on top of LinuxThreads.
|
| 2 |
|
|
Copyright (C) 2000 Free Software Foundation, Inc.
|
| 3 |
|
|
This file is part of the GNU C Library.
|
| 4 |
|
|
Contributed by Kaz Kylheku <kaz@ashi.footprints.net>.
|
| 5 |
|
|
|
| 6 |
|
|
The GNU C Library is free software; you can redistribute it and/or
|
| 7 |
|
|
modify it under the terms of the GNU Library General Public License as
|
| 8 |
|
|
published by the Free Software Foundation; either version 2 of the
|
| 9 |
|
|
License, or (at your option) any later version.
|
| 10 |
|
|
|
| 11 |
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
| 12 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 14 |
|
|
Library General Public License for more details.
|
| 15 |
|
|
|
| 16 |
|
|
You should have received a copy of the GNU Library General Public
|
| 17 |
|
|
License along with the GNU C Library; see the file COPYING.LIB. If not,
|
| 18 |
|
|
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
| 19 |
|
|
Boston, MA 02111-1307, USA. */
|
| 20 |
|
|
|
| 21 |
|
|
#include <limits.h>
|
| 22 |
|
|
#include <signal.h>
|
| 23 |
|
|
#include <linux/time.h>
|
| 24 |
|
|
|
| 25 |
|
|
/* Double linked list. */
|
| 26 |
|
|
struct list_links
|
| 27 |
|
|
{
|
| 28 |
|
|
struct list_links *next;
|
| 29 |
|
|
struct list_links *prev;
|
| 30 |
|
|
};
|
| 31 |
|
|
|
| 32 |
|
|
|
| 33 |
|
|
/* Forward declaration. */
|
| 34 |
|
|
struct timer_node;
|
| 35 |
|
|
|
| 36 |
|
|
|
| 37 |
|
|
/* Definitions for an internal thread of the POSIX timer implementation. */
|
| 38 |
|
|
struct thread_node
|
| 39 |
|
|
{
|
| 40 |
|
|
struct list_links links;
|
| 41 |
|
|
pthread_attr_t attr;
|
| 42 |
|
|
pthread_t id;
|
| 43 |
|
|
unsigned int exists;
|
| 44 |
|
|
struct list_links timer_queue;
|
| 45 |
|
|
pthread_cond_t cond;
|
| 46 |
|
|
struct timer_node *current_timer;
|
| 47 |
|
|
pthread_t captured;
|
| 48 |
|
|
clockid_t clock_id;
|
| 49 |
|
|
};
|
| 50 |
|
|
|
| 51 |
|
|
|
| 52 |
|
|
/* Internal representation of a timer. */
|
| 53 |
|
|
struct timer_node
|
| 54 |
|
|
{
|
| 55 |
|
|
struct list_links links;
|
| 56 |
|
|
struct sigevent event;
|
| 57 |
|
|
clockid_t clock;
|
| 58 |
|
|
struct itimerspec value;
|
| 59 |
|
|
struct timespec expirytime;
|
| 60 |
|
|
pthread_attr_t attr;
|
| 61 |
|
|
unsigned int abstime;
|
| 62 |
|
|
unsigned int armed;
|
| 63 |
|
|
enum {
|
| 64 |
|
|
TIMER_FREE, TIMER_INUSE, TIMER_DELETED
|
| 65 |
|
|
} inuse;
|
| 66 |
|
|
struct thread_node *thread;
|
| 67 |
|
|
pid_t creator_pid;
|
| 68 |
|
|
int refcount;
|
| 69 |
|
|
};
|
| 70 |
|
|
|
| 71 |
|
|
|
| 72 |
|
|
/* Static array with the structures for all the timers. */
|
| 73 |
|
|
extern struct timer_node __timer_array[TIMER_MAX];
|
| 74 |
|
|
|
| 75 |
|
|
/* Global lock to protect operation on the lists. */
|
| 76 |
|
|
extern pthread_mutex_t __timer_mutex;
|
| 77 |
|
|
|
| 78 |
|
|
/* Variable to protext initialization. */
|
| 79 |
|
|
extern pthread_once_t __timer_init_once_control;
|
| 80 |
|
|
|
| 81 |
|
|
/* Nonzero if initialization of timer implementation failed. */
|
| 82 |
|
|
extern int __timer_init_failed;
|
| 83 |
|
|
|
| 84 |
|
|
/* Nodes for the threads used to deliver signals. */
|
| 85 |
|
|
/* A distinct thread is used for each clock type. */
|
| 86 |
|
|
|
| 87 |
|
|
extern struct thread_node __timer_signal_thread_rclk;
|
| 88 |
|
|
#ifdef _POSIX_CPUTIME
|
| 89 |
|
|
extern struct thread_node __timer_signal_thread_pclk;
|
| 90 |
|
|
#endif
|
| 91 |
|
|
#ifdef _POSIX_THREAD_CPUTIME
|
| 92 |
|
|
extern struct thread_node __timer_signal_thread_tclk;
|
| 93 |
|
|
#endif
|
| 94 |
|
|
|
| 95 |
|
|
|
| 96 |
|
|
/* Return pointer to timer structure corresponding to ID. */
|
| 97 |
|
|
static inline struct timer_node *
|
| 98 |
|
|
timer_id2ptr (timer_t timerid)
|
| 99 |
|
|
{
|
| 100 |
|
|
if (timerid >= 0 && timerid < TIMER_MAX)
|
| 101 |
|
|
return &__timer_array[timerid];
|
| 102 |
|
|
|
| 103 |
|
|
return NULL;
|
| 104 |
|
|
}
|
| 105 |
|
|
|
| 106 |
|
|
/* Return ID of TIMER. */
|
| 107 |
|
|
static inline int
|
| 108 |
|
|
timer_ptr2id (struct timer_node *timer)
|
| 109 |
|
|
{
|
| 110 |
|
|
return timer - __timer_array;
|
| 111 |
|
|
}
|
| 112 |
|
|
|
| 113 |
|
|
/* Check whether timer is valid; global mutex must be held. */
|
| 114 |
|
|
static inline int
|
| 115 |
|
|
timer_valid (struct timer_node *timer)
|
| 116 |
|
|
{
|
| 117 |
|
|
return timer && timer->inuse == TIMER_INUSE;
|
| 118 |
|
|
}
|
| 119 |
|
|
|
| 120 |
|
|
/* Timer refcount functions; need global mutex. */
|
| 121 |
|
|
extern void __timer_dealloc (struct timer_node *timer);
|
| 122 |
|
|
|
| 123 |
|
|
static inline void
|
| 124 |
|
|
timer_addref (struct timer_node *timer)
|
| 125 |
|
|
{
|
| 126 |
|
|
timer->refcount++;
|
| 127 |
|
|
}
|
| 128 |
|
|
|
| 129 |
|
|
static inline void
|
| 130 |
|
|
timer_delref (struct timer_node *timer)
|
| 131 |
|
|
{
|
| 132 |
|
|
if (--timer->refcount == 0)
|
| 133 |
|
|
__timer_dealloc (timer);
|
| 134 |
|
|
}
|
| 135 |
|
|
|
| 136 |
|
|
/* Timespec helper routines. */
|
| 137 |
|
|
static inline int
|
| 138 |
|
|
timespec_compare (const struct timespec *left, const struct timespec *right)
|
| 139 |
|
|
{
|
| 140 |
|
|
if (left->tv_sec < right->tv_sec)
|
| 141 |
|
|
return -1;
|
| 142 |
|
|
if (left->tv_sec > right->tv_sec)
|
| 143 |
|
|
return 1;
|
| 144 |
|
|
|
| 145 |
|
|
if (left->tv_nsec < right->tv_nsec)
|
| 146 |
|
|
return -1;
|
| 147 |
|
|
if (left->tv_nsec > right->tv_nsec)
|
| 148 |
|
|
return 1;
|
| 149 |
|
|
|
| 150 |
|
|
return 0;
|
| 151 |
|
|
}
|
| 152 |
|
|
|
| 153 |
|
|
static inline void
|
| 154 |
|
|
timespec_add (struct timespec *sum, const struct timespec *left,
|
| 155 |
|
|
const struct timespec *right)
|
| 156 |
|
|
{
|
| 157 |
|
|
sum->tv_sec = left->tv_sec + right->tv_sec;
|
| 158 |
|
|
sum->tv_nsec = left->tv_nsec + right->tv_nsec;
|
| 159 |
|
|
|
| 160 |
|
|
if (sum->tv_nsec >= 1000000000)
|
| 161 |
|
|
{
|
| 162 |
|
|
++sum->tv_sec;
|
| 163 |
|
|
sum->tv_nsec -= 1000000000;
|
| 164 |
|
|
}
|
| 165 |
|
|
}
|
| 166 |
|
|
|
| 167 |
|
|
static inline void
|
| 168 |
|
|
timespec_sub (struct timespec *diff, const struct timespec *left,
|
| 169 |
|
|
const struct timespec *right)
|
| 170 |
|
|
{
|
| 171 |
|
|
diff->tv_sec = left->tv_sec - right->tv_sec;
|
| 172 |
|
|
diff->tv_nsec = left->tv_nsec - right->tv_nsec;
|
| 173 |
|
|
|
| 174 |
|
|
if (diff->tv_nsec < 0)
|
| 175 |
|
|
{
|
| 176 |
|
|
--diff->tv_sec;
|
| 177 |
|
|
diff->tv_nsec += 1000000000;
|
| 178 |
|
|
}
|
| 179 |
|
|
}
|
| 180 |
|
|
|
| 181 |
|
|
|
| 182 |
|
|
/* We need one of the list functions in the other modules. */
|
| 183 |
|
|
static inline void
|
| 184 |
|
|
list_unlink_ip (struct list_links *list)
|
| 185 |
|
|
{
|
| 186 |
|
|
struct list_links *lnext = list->next, *lprev = list->prev;
|
| 187 |
|
|
|
| 188 |
|
|
lnext->prev = lprev;
|
| 189 |
|
|
lprev->next = lnext;
|
| 190 |
|
|
|
| 191 |
|
|
/* The suffix ip means idempotent; list_unlink_ip can be called
|
| 192 |
|
|
* two or more times on the same node.
|
| 193 |
|
|
*/
|
| 194 |
|
|
|
| 195 |
|
|
list->next = list;
|
| 196 |
|
|
list->prev = list;
|
| 197 |
|
|
}
|
| 198 |
|
|
|
| 199 |
|
|
|
| 200 |
|
|
/* Functions in the helper file. */
|
| 201 |
|
|
extern void __timer_mutex_cancel_handler (void *arg);
|
| 202 |
|
|
extern void __timer_init_once (void);
|
| 203 |
|
|
extern struct timer_node *__timer_alloc (void);
|
| 204 |
|
|
extern int __timer_thread_start (struct thread_node *thread);
|
| 205 |
|
|
extern struct thread_node *__timer_thread_find_matching (const pthread_attr_t *desired_attr, clockid_t);
|
| 206 |
|
|
extern struct thread_node *__timer_thread_alloc (const pthread_attr_t *desired_attr, clockid_t);
|
| 207 |
|
|
extern void __timer_thread_dealloc (struct thread_node *thread);
|
| 208 |
|
|
extern int __timer_thread_queue_timer (struct thread_node *thread,
|
| 209 |
|
|
struct timer_node *insert);
|
| 210 |
|
|
extern void __timer_thread_wakeup (struct thread_node *thread);
|