4 * timed_thread.c: Abstraction layer for timed threads
6 * Copyright (C) 2006-2007 Philip Kovacs pkovacs@users.sourceforge.net
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
31 #ifndef HAVE_CLOCK_GETTIME
34 #include "timed_thread.h"
36 /* Abstraction layer for timed threads */
38 static int now (struct timespec *);
43 pthread_t thread; /* thread itself */
44 pthread_attr_t thread_attr; /* thread attributes */
45 pthread_mutex_t cs_mutex; /* critical section mutex */
46 pthread_mutex_t runnable_mutex; /* only for the runnable_cond */
47 pthread_cond_t runnable_cond; /* signalled to stop the thread */
48 void *(*start_routine)(void*); /* thread function to run */
49 void *arg; /* thread function argument */
50 struct timespec absolute_time; /* absolute future time next timed_thread_test will wait until */
51 struct timespec interval_time; /* interval_usecs as a struct timespec */
54 /* linked list of created threads */
55 typedef struct _timed_thread_list
57 timed_thread *p_timed_thread;
58 timed_thread **addr_of_p_timed_thread;
59 struct _timed_thread_list *next;
60 } timed_thread_node, timed_thread_list;
62 static timed_thread_list *p_timed_thread_list_head = NULL;
63 static timed_thread_list *p_timed_thread_list_tail = NULL;
65 static int now (struct timespec *abstime)
70 #ifdef HAVE_CLOCK_GETTIME
71 return clock_gettime (CLOCK_REALTIME, &abstime);
73 /* fallback to gettimeofday () */
75 if (gettimeofday (&tv, NULL) != 0)
78 abstime->tv_sec = tv.tv_sec;
79 abstime->tv_nsec = tv.tv_usec * 1000;
85 /* create a timed thread (object creation only) */
87 timed_thread_create (void *(*start_routine)(void*), void *arg, unsigned int interval_usecs)
89 timed_thread *p_timed_thread;
91 assert ((start_routine != NULL) && (interval_usecs >= MINIMUM_INTERVAL_USECS));
93 if ((p_timed_thread = calloc (sizeof(timed_thread), 1)) == 0)
96 /* init attributes, e.g. joinable thread */
97 pthread_attr_init (&p_timed_thread->thread_attr);
98 pthread_attr_setdetachstate (&p_timed_thread->thread_attr, PTHREAD_CREATE_JOINABLE);
100 pthread_mutex_init (&p_timed_thread->cs_mutex, NULL);
101 pthread_mutex_init (&p_timed_thread->runnable_mutex, NULL);
103 pthread_cond_init (&p_timed_thread->runnable_cond, NULL);
105 p_timed_thread->start_routine = start_routine;
106 p_timed_thread->arg = arg;
108 p_timed_thread->absolute_time.tv_sec=0;
109 p_timed_thread->absolute_time.tv_nsec=0;
111 /* seconds portion of the microseconds interval */
112 p_timed_thread->interval_time.tv_sec = (time_t)(interval_usecs / 1000000);
113 /* remaining microseconds convert to nanoseconds */
114 p_timed_thread->interval_time.tv_nsec = (long)((interval_usecs % 1000000) * 1000);
116 return p_timed_thread;
119 /* run a timed thread (drop the thread and run it) */
121 timed_thread_run (timed_thread* p_timed_thread)
123 return pthread_create (&p_timed_thread->thread, &p_timed_thread->thread_attr,
124 p_timed_thread->start_routine, p_timed_thread->arg);
127 /* destroy a timed thread.
128 * optional addr_of_p_timed_thread to set callers pointer to NULL as a convenience. */
130 timed_thread_destroy (timed_thread* p_timed_thread, timed_thread** addr_of_p_timed_thread)
132 assert (p_timed_thread != NULL);
133 assert ((addr_of_p_timed_thread == NULL) || (*addr_of_p_timed_thread == p_timed_thread));
135 /* signal thread to stop */
136 pthread_mutex_lock (&p_timed_thread->runnable_mutex);
137 pthread_cond_signal (&p_timed_thread->runnable_cond);
138 pthread_mutex_unlock (&p_timed_thread->runnable_mutex);
140 /* join the terminating thread */
141 pthread_join (p_timed_thread->thread, NULL);
144 pthread_attr_destroy (&p_timed_thread->thread_attr);
145 pthread_mutex_destroy (&p_timed_thread->cs_mutex);
146 pthread_mutex_destroy (&p_timed_thread->runnable_mutex);
147 pthread_cond_destroy (&p_timed_thread->runnable_cond);
149 free (p_timed_thread);
150 if (addr_of_p_timed_thread)
151 *addr_of_p_timed_thread = NULL;
155 /* lock a timed thread for critical section activity */
157 timed_thread_lock (timed_thread* p_timed_thread)
159 assert (p_timed_thread != NULL);
161 return pthread_mutex_lock (&p_timed_thread->cs_mutex);
165 /* unlock a timed thread after critical section activity */
167 timed_thread_unlock (timed_thread* p_timed_thread)
169 assert (p_timed_thread != NULL);
171 return pthread_mutex_unlock (&p_timed_thread->cs_mutex);
175 /* thread waits interval_usecs for runnable_cond to be signaled. returns 1 if signaled,
176 * -1 on error, and 0 otherwise. caller should call timed_thread_exit() on any non-zero
179 timed_thread_test (timed_thread* p_timed_thread)
181 struct timespec nowtime;
184 assert (p_timed_thread != NULL);
186 /* adjust wait time to now if absolute_time drifted behind */
188 if ( ((nowtime.tv_sec * 1000000000) + nowtime.tv_nsec) >
189 ((p_timed_thread->absolute_time.tv_sec * 1000000000) + p_timed_thread->absolute_time.tv_nsec) )
191 p_timed_thread->absolute_time.tv_sec = nowtime.tv_sec;
192 p_timed_thread->absolute_time.tv_nsec = nowtime.tv_nsec;
195 /* acquire runnable_cond mutex */
196 if (pthread_mutex_lock (&p_timed_thread->runnable_mutex))
197 return (-1); /* could not acquire runnable_cond mutex, so tell caller to exit thread */
199 /* release mutex and wait until future time for runnable_cond to signal */
200 rc = pthread_cond_timedwait (&p_timed_thread->runnable_cond,
201 &p_timed_thread->runnable_mutex,
202 &p_timed_thread->absolute_time);
203 /* mutex re-acquired, so release it */
204 pthread_mutex_unlock (&p_timed_thread->runnable_mutex);
206 /* absolute future time for next pass */
207 p_timed_thread->absolute_time.tv_sec += p_timed_thread->interval_time.tv_sec;
208 p_timed_thread->absolute_time.tv_nsec += p_timed_thread->interval_time.tv_nsec;
211 return 1; /* runnable_cond was signaled, so tell caller to exit thread */
213 /* tell caller not to exit yet */
218 /* exit a timed thread */
220 timed_thread_exit (timed_thread* p_timed_thread)
222 assert (p_timed_thread != NULL);
228 /* register a timed thread for future destruction via timed_thread_destroy_registered_threads() */
230 timed_thread_register (timed_thread* p_timed_thread, timed_thread** addr_of_p_timed_thread)
232 timed_thread_node *p_node;
234 assert (p_timed_thread != NULL);
235 assert ((addr_of_p_timed_thread == NULL) || (*addr_of_p_timed_thread == p_timed_thread));
237 if ((p_node = calloc (sizeof (timed_thread_node), 1)) == 0)
240 p_node->p_timed_thread = p_timed_thread;
241 p_node->addr_of_p_timed_thread = addr_of_p_timed_thread;
244 if (!p_timed_thread_list_tail)
246 /* first node of empty list */
247 p_timed_thread_list_tail = p_node;
248 p_timed_thread_list_head = p_node;
252 /* add node to tail of non-empty list */
253 p_timed_thread_list_tail->next = p_node;
254 p_timed_thread_list_tail = p_node;
261 /* destroy all registered timed threads */
263 timed_thread_destroy_registered_threads (void)
265 timed_thread_node *p_node, *p_next;
267 for (p_node=p_timed_thread_list_head;
271 p_next = p_node->next;
272 timed_thread_destroy (p_node->p_timed_thread, p_node->addr_of_p_timed_thread);
277 p_timed_thread_list_head = NULL;
278 p_timed_thread_list_tail = NULL;