Removing old svn keywords.
[monky] / src / timed_thread.c
1 /* timed_thread.c: Abstraction layer for timed threads
2  *
3  * Copyright (C) 2006-2007 Philip Kovacs pkovacs@users.sourceforge.net
4  *
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.
9  *
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.
14  *
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
18  * USA. */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <pthread.h>
25 #include <assert.h>
26 #include <errno.h>
27 #include <time.h>
28 #include <unistd.h>
29 #ifndef HAVE_CLOCK_GETTIME
30 #include <sys/time.h>
31 #endif
32 #include "timed_thread.h"
33
34 /* Abstraction layer for timed threads */
35
36 static int now(struct timespec *);
37
38 /* private */
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 */
49         int pipefd[2];
50 };
51
52 /* linked list of created threads */
53 typedef struct _timed_thread_list {
54         timed_thread *p_timed_thread;
55         timed_thread **addr_of_p_timed_thread;
56         struct _timed_thread_list *next;
57 } timed_thread_node, timed_thread_list;
58
59 static timed_thread_list *p_timed_thread_list_head = NULL;
60 static timed_thread_list *p_timed_thread_list_tail = NULL;
61
62 int timed_thread_readfd(timed_thread *p_timed_thread)
63 {
64         return p_timed_thread->pipefd[0];
65 }
66
67 static int now(struct timespec *abstime)
68 {
69 #ifndef HAVE_CLOCK_GETTIME
70         struct timeval tv;
71 #endif
72
73         if (!abstime) {
74                 return -1;
75         }
76
77 #ifdef HAVE_CLOCK_GETTIME
78         return clock_gettime(CLOCK_REALTIME, abstime);
79 #else
80         /* fallback to gettimeofday () */
81         if (gettimeofday(&tv, NULL) != 0) {
82                 return -1;
83         }
84
85         abstime->tv_sec = tv.tv_sec;
86         abstime->tv_nsec = tv.tv_usec * 1000;
87         return 0;
88 #endif
89 }
90
91 /* create a timed thread (object creation only) */
92 timed_thread *timed_thread_create(void *start_routine(void *), void *arg,
93                 unsigned int interval_usecs)
94 {
95         timed_thread *p_timed_thread;
96
97         assert(start_routine != NULL);
98         assert(interval_usecs >= MINIMUM_INTERVAL_USECS);
99
100         if ((p_timed_thread = calloc(sizeof(timed_thread), 1)) == 0) {
101                 return NULL;
102         }
103
104         /* create thread pipe (used to tell threads to die) */
105         if (pipe(p_timed_thread->pipefd)) {
106                 return NULL;
107         }
108
109         /* init attributes, e.g. joinable thread */
110         pthread_attr_init(&p_timed_thread->thread_attr);
111         pthread_attr_setdetachstate(&p_timed_thread->thread_attr,
112                         PTHREAD_CREATE_JOINABLE);
113         /* init mutexes */
114         pthread_mutex_init(&p_timed_thread->cs_mutex, NULL);
115         pthread_mutex_init(&p_timed_thread->runnable_mutex, NULL);
116         /* init cond */
117         pthread_cond_init(&p_timed_thread->runnable_cond, NULL);
118
119         p_timed_thread->start_routine = start_routine;
120         p_timed_thread->arg = arg;
121
122         /* set wait time to current time */
123         if (now(&p_timed_thread->wait_time)) {
124                 return NULL;
125         }
126
127         /* seconds portion of the microseconds interval */
128         p_timed_thread->interval_time.tv_sec = (time_t) (interval_usecs / 1000000);
129         /* remaining microseconds convert to nanoseconds */
130         p_timed_thread->interval_time.tv_nsec =
131                 (long) ((interval_usecs % 1000000) * 1000);
132
133         /* printf("interval_time.tv_sec = %li, .tv_nsec = %li\n",
134            p_timed_thread->interval_time.tv_sec,
135            p_timed_thread->interval_time.tv_nsec); */
136         return p_timed_thread;
137 }
138
139 /* run a timed thread (drop the thread and run it) */
140 int timed_thread_run(timed_thread *p_timed_thread)
141 {
142         return pthread_create(&p_timed_thread->thread, &p_timed_thread->thread_attr,
143                         p_timed_thread->start_routine, p_timed_thread->arg);
144 }
145
146 /* destroy a timed thread.
147  * optional addr_of_p_timed_thread to set callers pointer to NULL as a
148  * convenience. */
149 void timed_thread_destroy(timed_thread *p_timed_thread,
150                 timed_thread **addr_of_p_timed_thread)
151 {
152         assert(p_timed_thread != NULL);
153         assert((addr_of_p_timed_thread == NULL)
154                         || (*addr_of_p_timed_thread == p_timed_thread));
155
156         /* signal thread to stop */
157         pthread_mutex_lock(&p_timed_thread->runnable_mutex);
158         pthread_cond_signal(&p_timed_thread->runnable_cond);
159         pthread_mutex_unlock(&p_timed_thread->runnable_mutex);
160         write(p_timed_thread->pipefd[1], "die", 3);
161
162         /* join the terminating thread */
163         if (p_timed_thread->thread) {
164                 pthread_join(p_timed_thread->thread, NULL);
165         }
166
167         /* clean up */
168         pthread_attr_destroy(&p_timed_thread->thread_attr);
169         pthread_mutex_destroy(&p_timed_thread->cs_mutex);
170         pthread_mutex_destroy(&p_timed_thread->runnable_mutex);
171         pthread_cond_destroy(&p_timed_thread->runnable_cond);
172
173         free(p_timed_thread);
174         if (addr_of_p_timed_thread) {
175                 *addr_of_p_timed_thread = NULL;
176         }
177 }
178
179 /* lock a timed thread for critical section activity */
180 int timed_thread_lock(timed_thread *p_timed_thread)
181 {
182         assert(p_timed_thread != NULL);
183
184         return pthread_mutex_lock(&p_timed_thread->cs_mutex);
185 }
186
187 /* unlock a timed thread after critical section activity */
188 int timed_thread_unlock(timed_thread *p_timed_thread)
189 {
190         assert(p_timed_thread != NULL);
191
192         return pthread_mutex_unlock(&p_timed_thread->cs_mutex);
193 }
194
195 /* thread waits interval_usecs for runnable_cond to be signaled.
196  * returns 1 if signaled, -1 on error, and 0 otherwise.
197  * caller should call timed_thread_exit() on any non-zero return value. */
198 int timed_thread_test(timed_thread *p_timed_thread, int override_wait_time)
199 {
200         struct timespec now_time;
201         int rc;
202
203         assert(p_timed_thread != NULL);
204
205         /* acquire runnable_cond mutex */
206         if (pthread_mutex_lock(&p_timed_thread->runnable_mutex)) {
207                 /* could not acquire runnable_cond mutex,
208                  * so tell caller to exit thread */
209                 return -1;
210         }
211
212         if (override_wait_time && now(&p_timed_thread->wait_time)) {
213                 return -1;
214         }
215
216         /* release mutex and wait until future time for runnable_cond to signal */
217         rc = pthread_cond_timedwait(&p_timed_thread->runnable_cond,
218                         &p_timed_thread->runnable_mutex, &p_timed_thread->wait_time);
219         /* mutex re-acquired, so release it */
220         pthread_mutex_unlock(&p_timed_thread->runnable_mutex);
221         
222         if (now(&now_time)) {
223                 return -1;
224         }
225
226         if (rc == 0) {
227                 /* runnable_cond was signaled, so tell caller to exit thread */
228                 return 1;
229         }
230
231         /* absolute future time for next pass */
232         p_timed_thread->wait_time.tv_sec += p_timed_thread->interval_time.tv_sec;
233         p_timed_thread->wait_time.tv_nsec += p_timed_thread->interval_time.tv_nsec;
234         p_timed_thread->wait_time.tv_sec += p_timed_thread->wait_time.tv_nsec / 1000000000;
235         p_timed_thread->wait_time.tv_nsec = p_timed_thread->wait_time.tv_nsec % 1000000000;
236
237         /* ensure our future wait time is sane */
238         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) {
239                 p_timed_thread->wait_time.tv_sec = now_time.tv_sec + p_timed_thread->interval_time.tv_sec;
240                 p_timed_thread->wait_time.tv_nsec = now_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;
243         }
244
245         /* tell caller not to exit yet */
246         return 0;
247 }
248
249 /* exit a timed thread */
250 void timed_thread_exit(timed_thread *p_timed_thread)
251 {
252         assert(p_timed_thread != NULL);
253
254         close(p_timed_thread->pipefd[0]);
255         close(p_timed_thread->pipefd[1]);
256
257         pthread_exit(NULL);
258 }
259
260 /* register a timed thread for future destruction via
261  * timed_thread_destroy_registered_threads() */
262 int timed_thread_register(timed_thread *p_timed_thread,
263                 timed_thread **addr_of_p_timed_thread)
264 {
265         timed_thread_node *p_node;
266
267         assert((addr_of_p_timed_thread == NULL)
268                         || (*addr_of_p_timed_thread == p_timed_thread));
269
270         if ((p_node = calloc(sizeof(timed_thread_node), 1)) == 0) {
271                 return 0;
272         }
273
274         p_node->p_timed_thread = p_timed_thread;
275         p_node->addr_of_p_timed_thread = addr_of_p_timed_thread;
276         p_node->next = NULL;
277
278         if (!p_timed_thread_list_tail) {
279                 /* first node of empty list */
280                 p_timed_thread_list_tail = p_node;
281                 p_timed_thread_list_head = p_node;
282         } else {
283                 /* add node to tail of non-empty list */
284                 p_timed_thread_list_tail->next = p_node;
285                 p_timed_thread_list_tail = p_node;
286         }
287
288         return 0;
289 }
290
291 /* destroy all registered timed threads */
292 void timed_thread_destroy_registered_threads(void)
293 {
294         timed_thread_node *p_node, *p_next;
295
296         for (p_node = p_timed_thread_list_head; p_node; p_node = p_next) {
297                 p_next = p_node->next;
298                 timed_thread_destroy(p_node->p_timed_thread,
299                                 p_node->addr_of_p_timed_thread);
300                 free(p_node);
301                 p_node = NULL;
302         }
303
304         p_timed_thread_list_head = NULL;
305         p_timed_thread_list_tail = NULL;
306 }