Back to index

glibc  2.9
posix-timer.h
Go to the documentation of this file.
00001 /* Definitions for POSIX timer implementation on top of LinuxThreads.
00002    Copyright (C) 2000, 2002, 2004 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Kaz Kylheku <kaz@ashi.footprints.net>.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public License as
00008    published by the Free Software Foundation; either version 2.1 of the
00009    License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; see the file COPYING.LIB.  If not,
00018    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019    Boston, MA 02111-1307, USA.  */
00020 
00021 #include <limits.h>
00022 #include <signal.h>
00023 
00024 /* Double linked list.  */
00025 struct list_links
00026 {
00027   struct list_links *next;
00028   struct list_links *prev;
00029 };
00030 
00031 
00032 /* Forward declaration.  */
00033 struct timer_node;
00034 
00035 
00036 /* Definitions for an internal thread of the POSIX timer implementation.  */
00037 struct thread_node
00038 {
00039   struct list_links links;
00040   pthread_attr_t attr;
00041   pthread_t id;
00042   unsigned int exists;
00043   struct list_links timer_queue;
00044   pthread_cond_t cond;
00045   struct timer_node *current_timer;
00046   pthread_t captured;
00047   clockid_t clock_id;
00048 };
00049 
00050 
00051 /* Internal representation of a timer.  */
00052 struct timer_node
00053 {
00054   struct list_links links;
00055   struct sigevent event;
00056   clockid_t clock;
00057   struct itimerspec value;
00058   struct timespec expirytime;
00059   pthread_attr_t attr;
00060   unsigned int abstime;
00061   unsigned int armed;
00062   enum {
00063     TIMER_FREE, TIMER_INUSE, TIMER_DELETED
00064   } inuse;
00065   struct thread_node *thread;
00066   pid_t creator_pid;
00067   int refcount;
00068   int overrun_count;
00069 };
00070 
00071 
00072 /* Static array with the structures for all the timers.  */
00073 extern struct timer_node __timer_array[TIMER_MAX];
00074 
00075 /* Global lock to protect operation on the lists.  */
00076 extern pthread_mutex_t __timer_mutex;
00077 
00078 /* Variable to protext initialization.  */
00079 extern pthread_once_t __timer_init_once_control;
00080 
00081 /* Nonzero if initialization of timer implementation failed.  */
00082 extern int __timer_init_failed;
00083 
00084 /* Nodes for the threads used to deliver signals.  */
00085 /* A distinct thread is used for each clock type.  */
00086 
00087 extern struct thread_node __timer_signal_thread_rclk;
00088 
00089 
00090 /* Return pointer to timer structure corresponding to ID.  */
00091 static inline struct timer_node *
00092 timer_id2ptr (timer_t timerid)
00093 {
00094   if (timerid >= 0 && timerid < TIMER_MAX)
00095     return &__timer_array[timerid];
00096 
00097   return NULL;
00098 }
00099 
00100 /* Return ID of TIMER.  */
00101 static inline int
00102 timer_ptr2id (struct timer_node *timer)
00103 {
00104   return timer - __timer_array;
00105 }
00106 
00107 /* Check whether timer is valid; global mutex must be held. */
00108 static inline int
00109 timer_valid (struct timer_node *timer)
00110 {
00111   return timer && timer->inuse == TIMER_INUSE;
00112 }
00113 
00114 /* Timer refcount functions; need global mutex. */
00115 extern void __timer_dealloc (struct timer_node *timer);
00116 
00117 static inline void
00118 timer_addref (struct timer_node *timer)
00119 {
00120   timer->refcount++;
00121 }
00122 
00123 static inline void
00124 timer_delref (struct timer_node *timer)
00125 {
00126   if (--timer->refcount == 0)
00127     __timer_dealloc (timer);
00128 }
00129 
00130 /* Timespec helper routines.  */
00131 static inline int
00132 timespec_compare (const struct timespec *left, const struct timespec *right)
00133 {
00134   if (left->tv_sec < right->tv_sec)
00135     return -1;
00136   if (left->tv_sec > right->tv_sec)
00137     return 1;
00138 
00139   if (left->tv_nsec < right->tv_nsec)
00140     return -1;
00141   if (left->tv_nsec > right->tv_nsec)
00142     return 1;
00143 
00144   return 0;
00145 }
00146 
00147 static inline void
00148 timespec_add (struct timespec *sum, const struct timespec *left,
00149              const struct timespec *right)
00150 {
00151   sum->tv_sec = left->tv_sec + right->tv_sec;
00152   sum->tv_nsec = left->tv_nsec + right->tv_nsec;
00153 
00154   if (sum->tv_nsec >= 1000000000)
00155     {
00156       ++sum->tv_sec;
00157       sum->tv_nsec -= 1000000000;
00158     }
00159 }
00160 
00161 static inline void
00162 timespec_sub (struct timespec *diff, const struct timespec *left,
00163              const struct timespec *right)
00164 {
00165   diff->tv_sec = left->tv_sec - right->tv_sec;
00166   diff->tv_nsec = left->tv_nsec - right->tv_nsec;
00167 
00168   if (diff->tv_nsec < 0)
00169     {
00170       --diff->tv_sec;
00171       diff->tv_nsec += 1000000000;
00172     }
00173 }
00174 
00175 
00176 /* We need one of the list functions in the other modules.  */
00177 static inline void
00178 list_unlink_ip (struct list_links *list)
00179 {
00180   struct list_links *lnext = list->next, *lprev = list->prev;
00181 
00182   lnext->prev = lprev;
00183   lprev->next = lnext;
00184 
00185   /* The suffix ip means idempotent; list_unlink_ip can be called
00186    * two or more times on the same node.
00187    */
00188 
00189   list->next = list;
00190   list->prev = list;
00191 }
00192 
00193 
00194 /* Functions in the helper file.  */
00195 extern void __timer_mutex_cancel_handler (void *arg);
00196 extern void __timer_init_once (void);
00197 extern struct timer_node *__timer_alloc (void);
00198 extern int __timer_thread_start (struct thread_node *thread);
00199 extern struct thread_node *__timer_thread_find_matching (const pthread_attr_t *desired_attr, clockid_t);
00200 extern struct thread_node *__timer_thread_alloc (const pthread_attr_t *desired_attr, clockid_t);
00201 extern void __timer_thread_dealloc (struct thread_node *thread);
00202 extern int __timer_thread_queue_timer (struct thread_node *thread,
00203                                    struct timer_node *insert);
00204 extern void __timer_thread_wakeup (struct thread_node *thread);