123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- #include <limits.h>
- #include <signal.h>
- struct list_links
- {
- struct list_links *next;
- struct list_links *prev;
- };
- struct timer_node;
- struct thread_node
- {
- struct list_links links;
- pthread_attr_t attr;
- pthread_t id;
- unsigned int exists;
- struct list_links timer_queue;
- pthread_cond_t cond;
- struct timer_node *current_timer;
- pthread_t captured;
- clockid_t clock_id;
- };
- struct timer_node
- {
- struct list_links links;
- struct sigevent event;
- clockid_t clock;
- struct itimerspec value;
- struct timespec expirytime;
- pthread_attr_t attr;
- unsigned int abstime;
- unsigned int armed;
- enum {
- TIMER_FREE, TIMER_INUSE, TIMER_DELETED
- } inuse;
- struct thread_node *thread;
- pid_t creator_pid;
- int refcount;
- int overrun_count;
- };
- #ifndef TIMER_MAX
- # define TIMER_MAX 256
- #endif
- extern struct timer_node __timer_array[TIMER_MAX];
- extern pthread_mutex_t __timer_mutex;
- extern pthread_once_t __timer_init_once_control;
- extern int __timer_init_failed;
- extern struct thread_node __timer_signal_thread_rclk;
- #define timer_id2ptr(timerid) ((struct timer_node *) timerid)
- #define timer_ptr2id(timerid) ((void *) timerid)
- static inline int
- timer_valid (struct timer_node *timer)
- {
- return timer && timer->inuse == TIMER_INUSE;
- }
- extern void __timer_dealloc (struct timer_node *timer);
- static inline void
- timer_addref (struct timer_node *timer)
- {
- timer->refcount++;
- }
- static inline void
- timer_delref (struct timer_node *timer)
- {
- if (--timer->refcount == 0)
- __timer_dealloc (timer);
- }
- static inline int
- __attribute ((always_inline))
- timespec_compare (const struct timespec *left, const struct timespec *right)
- {
- if (left->tv_sec < right->tv_sec)
- return -1;
- if (left->tv_sec > right->tv_sec)
- return 1;
- if (left->tv_nsec < right->tv_nsec)
- return -1;
- if (left->tv_nsec > right->tv_nsec)
- return 1;
- return 0;
- }
- static inline void
- timespec_add (struct timespec *sum, const struct timespec *left,
- const struct timespec *right)
- {
- sum->tv_sec = left->tv_sec + right->tv_sec;
- sum->tv_nsec = left->tv_nsec + right->tv_nsec;
- if (sum->tv_nsec >= 1000000000)
- {
- ++sum->tv_sec;
- sum->tv_nsec -= 1000000000;
- }
- }
- static inline void
- timespec_sub (struct timespec *diff, const struct timespec *left,
- const struct timespec *right)
- {
- diff->tv_sec = left->tv_sec - right->tv_sec;
- diff->tv_nsec = left->tv_nsec - right->tv_nsec;
- if (diff->tv_nsec < 0)
- {
- --diff->tv_sec;
- diff->tv_nsec += 1000000000;
- }
- }
- static inline void
- list_unlink_ip (struct list_links *list)
- {
- struct list_links *lnext = list->next, *lprev = list->prev;
- lnext->prev = lprev;
- lprev->next = lnext;
-
- list->next = list;
- list->prev = list;
- }
- extern void __timer_mutex_cancel_handler (void *arg);
- extern void __timer_init_once (void);
- extern struct timer_node *__timer_alloc (void);
- extern int __timer_thread_start (struct thread_node *thread);
- extern struct thread_node *__timer_thread_find_matching (const pthread_attr_t *desired_attr, clockid_t);
- extern struct thread_node *__timer_thread_alloc (const pthread_attr_t *desired_attr, clockid_t);
- extern void __timer_thread_dealloc (struct thread_node *thread);
- extern int __timer_thread_queue_timer (struct thread_node *thread,
- struct timer_node *insert);
- extern void __timer_thread_wakeup (struct thread_node *thread);
|