3 /* timed_thread.c: Abstraction layer for timed threads
5 * Copyright (C) 2006-2007 Philip Kovacs pkovacs@users.sourceforge.net
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * 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 *);
41 struct _timed_thread {
42 pthread_t thread; /* thread itself */
43 pthread_attr_t thread_attr; /* thread attributes */
44 pthread_mutex_t cs_mutex; /* critical section mutex */
45 pthread_mutex_t runnable_mutex; /* only for the runnable_cond */
46 pthread_cond_t runnable_cond; /* signalled to stop the thread */
47 void *(*start_routine)(void *); /* thread function to run */
48 void *arg; /* thread function argument */
49 struct timespec interval_time; /* interval_usecs as a struct timespec */
50 struct timespec wait_time; /* absolute future time next timed_thread_test will wait until */
54 /* linked list of created threads */
55 typedef struct _timed_thread_list {
56 timed_thread *p_timed_thread;
57 timed_thread **addr_of_p_timed_thread;
58 struct _timed_thread_list *next;
59 } timed_thread_node, timed_thread_list;
61 static timed_thread_list *p_timed_thread_list_head = NULL;
62 static timed_thread_list *p_timed_thread_list_tail = NULL;
64 int timed_thread_readfd(timed_thread *p_timed_thread)
66 return p_timed_thread->pipefd[0];
69 static int now(struct timespec *abstime)
71 #ifndef HAVE_CLOCK_GETTIME
79 #ifdef HAVE_CLOCK_GETTIME
80 return clock_gettime(CLOCK_REALTIME, abstime);
82 /* fallback to gettimeofday () */
83 if (gettimeofday(&tv, NULL) != 0) {
87 abstime->tv_sec = tv.tv_sec;
88 abstime->tv_nsec = tv.tv_usec * 1000;
93 /* create a timed thread (object creation only) */
94 timed_thread *timed_thread_create(void *start_routine(void *), void *arg,
95 unsigned int interval_usecs)
97 timed_thread *p_timed_thread;
99 assert(start_routine != NULL);
100 assert(interval_usecs >= MINIMUM_INTERVAL_USECS);
102 if ((p_timed_thread = calloc(sizeof(timed_thread), 1)) == 0) {
106 /* create thread pipe (used to tell threads to die) */
107 if (pipe(p_timed_thread->pipefd)) {
111 /* init attributes, e.g. joinable thread */
112 pthread_attr_init(&p_timed_thread->thread_attr);
113 pthread_attr_setdetachstate(&p_timed_thread->thread_attr,
114 PTHREAD_CREATE_JOINABLE);
116 pthread_mutex_init(&p_timed_thread->cs_mutex, NULL);
117 pthread_mutex_init(&p_timed_thread->runnable_mutex, NULL);
119 pthread_cond_init(&p_timed_thread->runnable_cond, NULL);
121 p_timed_thread->start_routine = start_routine;
122 p_timed_thread->arg = arg;
124 /* set wait time to current time */
125 if (now(&p_timed_thread->wait_time)) {
129 /* seconds portion of the microseconds interval */
130 p_timed_thread->interval_time.tv_sec = (time_t) (interval_usecs / 1000000);
131 /* remaining microseconds convert to nanoseconds */
132 p_timed_thread->interval_time.tv_nsec =
133 (long) ((interval_usecs % 1000000) * 1000);
135 /* printf("interval_time.tv_sec = %li, .tv_nsec = %li\n",
136 p_timed_thread->interval_time.tv_sec,
137 p_timed_thread->interval_time.tv_nsec); */
138 return p_timed_thread;
141 /* run a timed thread (drop the thread and run it) */
142 int timed_thread_run(timed_thread *p_timed_thread)
144 return pthread_create(&p_timed_thread->thread, &p_timed_thread->thread_attr,
145 p_timed_thread->start_routine, p_timed_thread->arg);
148 /* destroy a timed thread.
149 * optional addr_of_p_timed_thread to set callers pointer to NULL as a
151 void timed_thread_destroy(timed_thread *p_timed_thread,
152 timed_thread **addr_of_p_timed_thread)
154 assert(p_timed_thread != NULL);
155 assert((addr_of_p_timed_thread == NULL)
156 || (*addr_of_p_timed_thread == p_timed_thread));
158 /* signal thread to stop */
159 pthread_mutex_lock(&p_timed_thread->runnable_mutex);
160 pthread_cond_signal(&p_timed_thread->runnable_cond);
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 (override_wait_time && now(&p_timed_thread->wait_time)) {
218 /* release mutex and wait until future time for runnable_cond to signal */
219 rc = pthread_cond_timedwait(&p_timed_thread->runnable_cond,
220 &p_timed_thread->runnable_mutex, &p_timed_thread->wait_time);
221 /* mutex re-acquired, so release it */
222 pthread_mutex_unlock(&p_timed_thread->runnable_mutex);
224 if (now(&now_time)) {
229 /* runnable_cond was signaled, so tell caller to exit thread */
233 /* absolute future time for next pass */
234 p_timed_thread->wait_time.tv_sec += p_timed_thread->interval_time.tv_sec;
235 p_timed_thread->wait_time.tv_nsec += p_timed_thread->interval_time.tv_nsec;
236 p_timed_thread->wait_time.tv_sec += p_timed_thread->wait_time.tv_nsec / 1000000000;
237 p_timed_thread->wait_time.tv_nsec = p_timed_thread->wait_time.tv_nsec % 1000000000;
239 /* ensure our future wait time is sane */
240 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) {
241 p_timed_thread->wait_time.tv_sec = now_time.tv_sec + p_timed_thread->interval_time.tv_sec;
242 p_timed_thread->wait_time.tv_nsec = now_time.tv_nsec + p_timed_thread->interval_time.tv_nsec;
243 p_timed_thread->wait_time.tv_sec += p_timed_thread->wait_time.tv_nsec / 1000000000;
244 p_timed_thread->wait_time.tv_nsec = p_timed_thread->wait_time.tv_nsec % 1000000000;
247 /* tell caller not to exit yet */
251 /* exit a timed thread */
252 void timed_thread_exit(timed_thread *p_timed_thread)
254 assert(p_timed_thread != NULL);
256 close(p_timed_thread->pipefd[0]);
257 close(p_timed_thread->pipefd[1]);
262 /* register a timed thread for future destruction via
263 * timed_thread_destroy_registered_threads() */
264 int timed_thread_register(timed_thread *p_timed_thread,
265 timed_thread **addr_of_p_timed_thread)
267 timed_thread_node *p_node;
269 assert((addr_of_p_timed_thread == NULL)
270 || (*addr_of_p_timed_thread == p_timed_thread));
272 if ((p_node = calloc(sizeof(timed_thread_node), 1)) == 0) {
276 p_node->p_timed_thread = p_timed_thread;
277 p_node->addr_of_p_timed_thread = addr_of_p_timed_thread;
280 if (!p_timed_thread_list_tail) {
281 /* first node of empty list */
282 p_timed_thread_list_tail = p_node;
283 p_timed_thread_list_head = p_node;
285 /* add node to tail of non-empty list */
286 p_timed_thread_list_tail->next = p_node;
287 p_timed_thread_list_tail = p_node;
293 /* destroy all registered timed threads */
294 void timed_thread_destroy_registered_threads(void)
296 timed_thread_node *p_node, *p_next;
298 for (p_node = p_timed_thread_list_head; p_node; p_node = p_next) {
299 p_next = p_node->next;
300 timed_thread_destroy(p_node->p_timed_thread,
301 p_node->addr_of_p_timed_thread);
306 p_timed_thread_list_head = NULL;
307 p_timed_thread_list_tail = NULL;