1 /* timed_thread.c: Abstraction layer for timed threads
3 * Copyright (C) 2006-2007 Philip Kovacs pkovacs@users.sourceforge.net
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This 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 this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
29 #ifndef HAVE_CLOCK_GETTIME
32 #include "timed_thread.h"
34 /* Abstraction layer for timed threads */
36 static int now(struct timespec *);
39 struct _timed_thread {
40 pthread_t thread; /* thread itself */
41 pthread_attr_t thread_attr; /* thread attributes */
42 pthread_mutex_t cs_mutex; /* critical section mutex */
43 pthread_mutex_t runnable_mutex; /* only for the runnable_cond */
44 pthread_cond_t runnable_cond; /* signalled to stop the thread */
45 void *(*start_routine)(void *); /* thread function to run */
46 void *arg; /* thread function argument */
47 struct timespec interval_time; /* interval_usecs as a struct timespec */
48 struct timespec wait_time; /* absolute future time next timed_thread_test will wait until */
53 /* linked list of created threads */
54 typedef struct _timed_thread_list {
55 timed_thread *p_timed_thread;
56 timed_thread **addr_of_p_timed_thread;
57 struct _timed_thread_list *next;
58 } timed_thread_node, timed_thread_list;
60 static timed_thread_list *p_timed_thread_list_head = NULL;
61 static timed_thread_list *p_timed_thread_list_tail = NULL;
63 int timed_thread_readfd(timed_thread *p_timed_thread)
65 return p_timed_thread->pipefd[0];
68 static int now(struct timespec *abstime)
70 #ifndef HAVE_CLOCK_GETTIME
78 #ifdef HAVE_CLOCK_GETTIME
79 return clock_gettime(CLOCK_REALTIME, abstime);
81 /* fallback to gettimeofday () */
82 if (gettimeofday(&tv, NULL) != 0) {
86 abstime->tv_sec = tv.tv_sec;
87 abstime->tv_nsec = tv.tv_usec * 1000;
92 /* create a timed thread (object creation only) */
93 timed_thread *timed_thread_create(void *start_routine(void *), void *arg,
94 unsigned int interval_usecs)
96 timed_thread *p_timed_thread;
98 assert(start_routine != NULL);
99 assert(interval_usecs >= MINIMUM_INTERVAL_USECS);
101 if ((p_timed_thread = calloc(sizeof(timed_thread), 1)) == 0) {
105 /* create thread pipe (used to tell threads to die) */
106 if (pipe(p_timed_thread->pipefd)) {
110 /* init attributes, e.g. joinable thread */
111 pthread_attr_init(&p_timed_thread->thread_attr);
112 pthread_attr_setdetachstate(&p_timed_thread->thread_attr,
113 PTHREAD_CREATE_JOINABLE);
115 pthread_mutex_init(&p_timed_thread->cs_mutex, NULL);
116 pthread_mutex_init(&p_timed_thread->runnable_mutex, NULL);
118 pthread_cond_init(&p_timed_thread->runnable_cond, NULL);
120 p_timed_thread->start_routine = start_routine;
121 p_timed_thread->arg = arg;
123 /* set wait time to current time */
124 if (now(&p_timed_thread->wait_time)) {
128 /* seconds portion of the microseconds interval */
129 p_timed_thread->interval_time.tv_sec = (time_t) (interval_usecs / 1000000);
130 /* remaining microseconds convert to nanoseconds */
131 p_timed_thread->interval_time.tv_nsec =
132 (long) ((interval_usecs % 1000000) * 1000);
134 /* printf("interval_time.tv_sec = %li, .tv_nsec = %li\n",
135 p_timed_thread->interval_time.tv_sec,
136 p_timed_thread->interval_time.tv_nsec); */
137 return p_timed_thread;
140 /* run a timed thread (drop the thread and run it) */
141 int timed_thread_run(timed_thread *p_timed_thread)
143 return pthread_create(&p_timed_thread->thread, &p_timed_thread->thread_attr,
144 p_timed_thread->start_routine, p_timed_thread->arg);
147 /* destroy a timed thread.
148 * optional addr_of_p_timed_thread to set callers pointer to NULL as a
150 void timed_thread_destroy(timed_thread *p_timed_thread,
151 timed_thread **addr_of_p_timed_thread)
153 assert(p_timed_thread != NULL);
154 assert((addr_of_p_timed_thread == NULL)
155 || (*addr_of_p_timed_thread == p_timed_thread));
157 /* signal thread to stop */
158 pthread_mutex_lock(&p_timed_thread->runnable_mutex);
159 pthread_cond_signal(&p_timed_thread->runnable_cond);
160 p_timed_thread->die = 1;
161 pthread_mutex_unlock(&p_timed_thread->runnable_mutex);
162 write(p_timed_thread->pipefd[1], "die", 3);
164 /* join the terminating thread */
165 if (p_timed_thread->thread) {
166 pthread_join(p_timed_thread->thread, NULL);
170 pthread_attr_destroy(&p_timed_thread->thread_attr);
171 pthread_mutex_destroy(&p_timed_thread->cs_mutex);
172 pthread_mutex_destroy(&p_timed_thread->runnable_mutex);
173 pthread_cond_destroy(&p_timed_thread->runnable_cond);
175 free(p_timed_thread);
176 if (addr_of_p_timed_thread) {
177 *addr_of_p_timed_thread = NULL;
181 /* lock a timed thread for critical section activity */
182 int timed_thread_lock(timed_thread *p_timed_thread)
184 assert(p_timed_thread != NULL);
186 return pthread_mutex_lock(&p_timed_thread->cs_mutex);
189 /* unlock a timed thread after critical section activity */
190 int timed_thread_unlock(timed_thread *p_timed_thread)
192 assert(p_timed_thread != NULL);
194 return pthread_mutex_unlock(&p_timed_thread->cs_mutex);
197 /* thread waits interval_usecs for runnable_cond to be signaled.
198 * returns 1 if signaled, -1 on error, and 0 otherwise.
199 * caller should call timed_thread_exit() on any non-zero return value. */
200 int timed_thread_test(timed_thread *p_timed_thread, int override_wait_time)
202 struct timespec now_time;
205 assert(p_timed_thread != NULL);
207 /* acquire runnable_cond mutex */
208 if (pthread_mutex_lock(&p_timed_thread->runnable_mutex)) {
209 /* could not acquire runnable_cond mutex,
210 * so tell caller to exit thread */
214 if (p_timed_thread->die) {
215 /* if we were kindly asked to die, then die */
219 if (override_wait_time && now(&p_timed_thread->wait_time)) {
223 /* release mutex and wait until future time for runnable_cond to signal */
224 rc = pthread_cond_timedwait(&p_timed_thread->runnable_cond,
225 &p_timed_thread->runnable_mutex, &p_timed_thread->wait_time);
226 /* mutex re-acquired, so release it */
227 pthread_mutex_unlock(&p_timed_thread->runnable_mutex);
229 if (now(&now_time)) {
234 /* runnable_cond was signaled, so tell caller to exit thread */
238 /* absolute future time for next pass */
239 p_timed_thread->wait_time.tv_sec += p_timed_thread->interval_time.tv_sec;
240 p_timed_thread->wait_time.tv_nsec += p_timed_thread->interval_time.tv_nsec;
241 p_timed_thread->wait_time.tv_sec += p_timed_thread->wait_time.tv_nsec / 1000000000;
242 p_timed_thread->wait_time.tv_nsec = p_timed_thread->wait_time.tv_nsec % 1000000000;
244 /* ensure our future wait time is sane */
245 if (p_timed_thread->wait_time.tv_sec > (now_time.tv_sec + p_timed_thread->interval_time.tv_sec) || p_timed_thread->wait_time.tv_sec < now_time.tv_sec) {
246 p_timed_thread->wait_time.tv_sec = now_time.tv_sec + p_timed_thread->interval_time.tv_sec;
247 p_timed_thread->wait_time.tv_nsec = now_time.tv_nsec + p_timed_thread->interval_time.tv_nsec;
248 p_timed_thread->wait_time.tv_sec += p_timed_thread->wait_time.tv_nsec / 1000000000;
249 p_timed_thread->wait_time.tv_nsec = p_timed_thread->wait_time.tv_nsec % 1000000000;
252 /* tell caller not to exit yet */
256 /* exit a timed thread */
257 void timed_thread_exit(timed_thread *p_timed_thread)
259 assert(p_timed_thread != NULL);
261 close(p_timed_thread->pipefd[0]);
262 close(p_timed_thread->pipefd[1]);
267 /* register a timed thread for future destruction via
268 * timed_thread_destroy_registered_threads() */
269 int timed_thread_register(timed_thread *p_timed_thread,
270 timed_thread **addr_of_p_timed_thread)
272 timed_thread_node *p_node;
274 assert((addr_of_p_timed_thread == NULL)
275 || (*addr_of_p_timed_thread == p_timed_thread));
277 if ((p_node = calloc(sizeof(timed_thread_node), 1)) == 0) {
281 p_node->p_timed_thread = p_timed_thread;
282 p_node->addr_of_p_timed_thread = addr_of_p_timed_thread;
285 if (!p_timed_thread_list_tail) {
286 /* first node of empty list */
287 p_timed_thread_list_tail = p_node;
288 p_timed_thread_list_head = p_node;
290 /* add node to tail of non-empty list */
291 p_timed_thread_list_tail->next = p_node;
292 p_timed_thread_list_tail = p_node;
298 /* destroy all registered timed threads */
299 void timed_thread_destroy_registered_threads(void)
301 timed_thread_node *p_node, *p_next;
303 for (p_node = p_timed_thread_list_head; p_node; p_node = p_next) {
304 p_next = p_node->next;
305 timed_thread_destroy(p_node->p_timed_thread,
306 p_node->addr_of_p_timed_thread);
311 p_timed_thread_list_head = NULL;
312 p_timed_thread_list_tail = NULL;