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
30 #ifndef HAVE_CLOCK_GETTIME
33 #include "timed_thread.h"
35 /* Abstraction layer for timed threads */
37 static int now(struct timespec *);
40 struct _timed_thread {
41 pthread_t thread; /* thread itself */
42 pthread_attr_t thread_attr; /* thread attributes */
43 pthread_mutex_t cs_mutex; /* critical section mutex */
44 pthread_mutex_t runnable_mutex; /* only for the runnable_cond */
45 pthread_cond_t runnable_cond; /* signalled to stop the thread */
46 void *(*start_routine)(void *); /* thread function to run */
47 void *arg; /* thread function argument */
48 struct timespec interval_time; /* interval_usecs as a struct timespec */
51 /* linked list of created threads */
52 typedef struct _timed_thread_list {
53 timed_thread *p_timed_thread;
54 timed_thread **addr_of_p_timed_thread;
55 struct _timed_thread_list *next;
56 } timed_thread_node, timed_thread_list;
58 static timed_thread_list *p_timed_thread_list_head = NULL;
59 static timed_thread_list *p_timed_thread_list_tail = NULL;
61 static int now(struct timespec *abstime)
63 #ifndef HAVE_CLOCK_GETTIME
71 #ifdef HAVE_CLOCK_GETTIME
72 return clock_gettime(CLOCK_REALTIME, abstime);
74 /* fallback to gettimeofday () */
75 if (gettimeofday(&tv, NULL) != 0) {
79 abstime->tv_sec = tv.tv_sec;
80 abstime->tv_nsec = tv.tv_usec * 1000;
85 /* create a timed thread (object creation only) */
86 timed_thread *timed_thread_create(void *start_routine(void *), void *arg,
87 unsigned int interval_usecs)
89 timed_thread *p_timed_thread;
91 assert(start_routine != NULL);
92 assert(interval_usecs >= MINIMUM_INTERVAL_USECS);
94 if ((p_timed_thread = calloc(sizeof(timed_thread), 1)) == 0) {
98 /* init attributes, e.g. joinable thread */
99 pthread_attr_init(&p_timed_thread->thread_attr);
100 pthread_attr_setdetachstate(&p_timed_thread->thread_attr,
101 PTHREAD_CREATE_JOINABLE);
103 pthread_mutex_init(&p_timed_thread->cs_mutex, NULL);
104 pthread_mutex_init(&p_timed_thread->runnable_mutex, NULL);
106 pthread_cond_init(&p_timed_thread->runnable_cond, NULL);
108 p_timed_thread->start_routine = start_routine;
109 p_timed_thread->arg = arg;
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 =
115 (long) ((interval_usecs % 1000000) * 1000);
117 /* printf("interval_time.tv_sec = %li, .tv_nsec = %li\n",
118 p_timed_thread->interval_time.tv_sec,
119 p_timed_thread->interval_time.tv_nsec); */
120 return p_timed_thread;
123 /* run a timed thread (drop the thread and run it) */
124 int timed_thread_run(timed_thread *p_timed_thread)
126 return pthread_create(&p_timed_thread->thread, &p_timed_thread->thread_attr,
127 p_timed_thread->start_routine, p_timed_thread->arg);
130 /* destroy a timed thread.
131 * optional addr_of_p_timed_thread to set callers pointer to NULL as a
133 void timed_thread_destroy(timed_thread *p_timed_thread,
134 timed_thread **addr_of_p_timed_thread)
136 assert(p_timed_thread != NULL);
137 assert((addr_of_p_timed_thread == NULL)
138 || (*addr_of_p_timed_thread == p_timed_thread));
140 /* signal thread to stop */
141 pthread_mutex_lock(&p_timed_thread->runnable_mutex);
142 pthread_cond_signal(&p_timed_thread->runnable_cond);
143 pthread_mutex_unlock(&p_timed_thread->runnable_mutex);
145 /* join the terminating thread */
146 if (p_timed_thread->thread) {
147 pthread_join(p_timed_thread->thread, NULL);
151 pthread_attr_destroy(&p_timed_thread->thread_attr);
152 pthread_mutex_destroy(&p_timed_thread->cs_mutex);
153 pthread_mutex_destroy(&p_timed_thread->runnable_mutex);
154 pthread_cond_destroy(&p_timed_thread->runnable_cond);
156 free(p_timed_thread);
157 if (addr_of_p_timed_thread) {
158 *addr_of_p_timed_thread = NULL;
162 /* lock a timed thread for critical section activity */
163 int timed_thread_lock(timed_thread *p_timed_thread)
165 assert(p_timed_thread != NULL);
167 return pthread_mutex_lock(&p_timed_thread->cs_mutex);
170 /* unlock a timed thread after critical section activity */
171 int timed_thread_unlock(timed_thread *p_timed_thread)
173 assert(p_timed_thread != NULL);
175 return pthread_mutex_unlock(&p_timed_thread->cs_mutex);
178 /* thread waits interval_usecs for runnable_cond to be signaled.
179 * returns 1 if signaled, -1 on error, and 0 otherwise.
180 * caller should call timed_thread_exit() on any non-zero return value. */
181 int timed_thread_test(timed_thread *p_timed_thread)
183 struct timespec wait_time;
186 assert(p_timed_thread != NULL);
188 if (now(&wait_time)) {
191 /* printf("PRE:wait_time.tv_secs = %li, .tv_nsecs = %li\n",
192 wait_time.tv_sec, wait_time.tv_nsec); */
194 /* add in the wait interval */
195 if (1000000000 - wait_time.tv_nsec
196 <= p_timed_thread->interval_time.tv_nsec) {
197 /* perform nsec->sec carry operation */
198 wait_time.tv_sec += p_timed_thread->interval_time.tv_sec + 1;
199 wait_time.tv_nsec -= 1000000000 - p_timed_thread->interval_time.tv_nsec;
200 /* printf("001:wait_time.tv_secs = %li, .tv_nsecs = %li\n",
201 wait_time.tv_sec, wait_time.tv_nsec); */
203 /* no carry needed, just add respective components */
204 wait_time.tv_sec += p_timed_thread->interval_time.tv_sec;
205 wait_time.tv_nsec += p_timed_thread->interval_time.tv_nsec;
206 /* printf("002:wait_time.tv_secs = %li, .tv_nsecs = %li\n",
207 wait_time.tv_sec, wait_time.tv_nsec); */
210 /* acquire runnable_cond mutex */
211 if (pthread_mutex_lock(&p_timed_thread->runnable_mutex)) {
212 /* could not acquire runnable_cond mutex,
213 * so tell caller to exit thread */
217 /* release mutex and wait until future time for runnable_cond to signal */
218 rc = pthread_cond_timedwait(&p_timed_thread->runnable_cond,
219 &p_timed_thread->runnable_mutex, &wait_time);
220 /* mutex re-acquired, so release it */
221 pthread_mutex_unlock(&p_timed_thread->runnable_mutex);
224 /* runnable_cond was signaled, so tell caller to exit thread */
228 /* tell caller not to exit yet */
232 /* exit a timed thread */
233 void timed_thread_exit(timed_thread *p_timed_thread)
235 assert(p_timed_thread != NULL);
240 /* register a timed thread for future destruction via
241 * timed_thread_destroy_registered_threads() */
242 int timed_thread_register(timed_thread *p_timed_thread,
243 timed_thread **addr_of_p_timed_thread)
245 timed_thread_node *p_node;
247 assert((addr_of_p_timed_thread == NULL)
248 || (*addr_of_p_timed_thread == p_timed_thread));
250 if ((p_node = calloc(sizeof(timed_thread_node), 1)) == 0) {
254 p_node->p_timed_thread = p_timed_thread;
255 p_node->addr_of_p_timed_thread = addr_of_p_timed_thread;
258 if (!p_timed_thread_list_tail) {
259 /* first node of empty list */
260 p_timed_thread_list_tail = p_node;
261 p_timed_thread_list_head = p_node;
263 /* add node to tail of non-empty list */
264 p_timed_thread_list_tail->next = p_node;
265 p_timed_thread_list_tail = p_node;
271 /* destroy all registered timed threads */
272 void timed_thread_destroy_registered_threads(void)
274 timed_thread_node *p_node, *p_next;
276 for (p_node = p_timed_thread_list_head; p_node; p_node = p_next) {
277 p_next = p_node->next;
278 timed_thread_destroy(p_node->p_timed_thread,
279 p_node->addr_of_p_timed_thread);
284 p_timed_thread_list_head = NULL;
285 p_timed_thread_list_tail = NULL;