timed thread work
[monky] / src / timed_thread.c
1 /* $Id$ */
2
3 /* 
4  * timed_thread.c: Abstraction layer for timed threads
5  *
6  * Copyright (C) 2006-2007 Philip Kovacs pkovacs@users.sourceforge.net
7  *
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.
12  *
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.
17  *
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
21  * USA.
22  *
23  */
24
25 #include <pthread.h>
26 #include <assert.h>
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <time.h>
31 #ifndef HAVE_CLOCK_GETTIME
32 #include <sys/time.h>
33 #endif
34 #include "timed_thread.h"
35
36 /* Abstraction layer for timed threads */
37
38 static int now (struct timespec *);
39
40 /* private */
41 struct _timed_thread 
42 {
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 */
52 };
53
54 /* linked list of created threads */
55 typedef struct _timed_thread_list
56 {
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;
61
62 static timed_thread_list *p_timed_thread_list_head = NULL;
63 static timed_thread_list *p_timed_thread_list_tail = NULL;
64
65 static int now (struct timespec *abstime)
66 {
67   if (!abstime)
68     return (-1);
69
70 #ifdef HAVE_CLOCK_GETTIME
71   return clock_gettime (CLOCK_REALTIME, &abstime);
72 #else
73   /* fallback to gettimeofday () */
74   struct timeval tv;
75   if (gettimeofday (&tv, NULL) != 0)
76     return (-1);
77
78   abstime->tv_sec = tv.tv_sec;
79   abstime->tv_nsec = tv.tv_usec * 1000;
80   return 0;
81 #endif
82 }
83
84
85 /* create a timed thread (object creation only) */
86 timed_thread* 
87 timed_thread_create (void *(*start_routine)(void*), void *arg, unsigned int interval_usecs)
88 {
89   timed_thread *p_timed_thread;
90
91   assert ((start_routine != NULL) && (interval_usecs >= MINIMUM_INTERVAL_USECS));
92
93   if ((p_timed_thread = calloc (sizeof(timed_thread), 1)) == 0)
94     return NULL;
95
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);
99   /* init mutexes */
100   pthread_mutex_init (&p_timed_thread->cs_mutex, NULL);
101   pthread_mutex_init (&p_timed_thread->runnable_mutex, NULL);
102   /* init cond */
103   pthread_cond_init (&p_timed_thread->runnable_cond, NULL);
104
105   p_timed_thread->start_routine = start_routine;
106   p_timed_thread->arg = arg;
107
108   p_timed_thread->absolute_time.tv_sec=0;
109   p_timed_thread->absolute_time.tv_nsec=0;
110
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);
115
116   return p_timed_thread;
117 }
118
119 /* run a timed thread (drop the thread and run it) */
120 int 
121 timed_thread_run (timed_thread* p_timed_thread)
122 {
123   return pthread_create (&p_timed_thread->thread, &p_timed_thread->thread_attr,
124                          p_timed_thread->start_routine, p_timed_thread->arg);
125 }
126
127 /* destroy a timed thread.
128  * optional addr_of_p_timed_thread to set callers pointer to NULL as a convenience. */
129 void 
130 timed_thread_destroy (timed_thread* p_timed_thread, timed_thread** addr_of_p_timed_thread)
131 {
132   assert (p_timed_thread != NULL);
133   assert ((addr_of_p_timed_thread == NULL) || (*addr_of_p_timed_thread == p_timed_thread));
134
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);
139
140   /* join the terminating thread */
141   pthread_join (p_timed_thread->thread, NULL);
142
143   /* clean up */
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);
148
149   free (p_timed_thread);
150   if (addr_of_p_timed_thread)
151     *addr_of_p_timed_thread = NULL;
152 }
153
154
155 /* lock a timed thread for critical section activity */
156 int
157 timed_thread_lock (timed_thread* p_timed_thread)
158 {
159   assert (p_timed_thread != NULL);
160
161   return pthread_mutex_lock (&p_timed_thread->cs_mutex);
162 }
163
164
165 /* unlock a timed thread after critical section activity */
166 int
167 timed_thread_unlock (timed_thread* p_timed_thread)
168 {
169   assert (p_timed_thread != NULL);
170
171   return pthread_mutex_unlock (&p_timed_thread->cs_mutex);
172 }
173
174
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 
177  * return value. */
178 int 
179 timed_thread_test (timed_thread* p_timed_thread)
180 {
181   struct timespec nowtime;
182   int rc;
183
184   assert (p_timed_thread != NULL);
185
186   /* adjust wait time to now if absolute_time drifted behind */
187   now (&nowtime);
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) )
190   {
191     p_timed_thread->absolute_time.tv_sec = nowtime.tv_sec;
192     p_timed_thread->absolute_time.tv_nsec = nowtime.tv_nsec;
193   }
194
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 */
198
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);
205
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;
209
210   if (rc==0)
211     return 1; /* runnable_cond was signaled, so tell caller to exit thread */
212
213   /* tell caller not to exit yet */
214   return 0;
215 }
216
217
218 /* exit a timed thread */
219 void 
220 timed_thread_exit (timed_thread* p_timed_thread)
221 {
222   assert (p_timed_thread != NULL);
223
224   pthread_exit (NULL);
225 }
226
227
228 /* register a timed thread for future destruction via timed_thread_destroy_registered_threads() */
229 int 
230 timed_thread_register (timed_thread* p_timed_thread, timed_thread** addr_of_p_timed_thread)
231 {
232   timed_thread_node *p_node;
233
234   assert (p_timed_thread != NULL);
235   assert ((addr_of_p_timed_thread == NULL) || (*addr_of_p_timed_thread == p_timed_thread));
236
237   if ((p_node = calloc (sizeof (timed_thread_node), 1)) == 0)
238     return 0;
239     
240   p_node->p_timed_thread = p_timed_thread;
241   p_node->addr_of_p_timed_thread = addr_of_p_timed_thread;
242   p_node->next = NULL;
243
244   if (!p_timed_thread_list_tail)
245   {
246     /* first node of empty list */
247     p_timed_thread_list_tail = p_node;
248     p_timed_thread_list_head = p_node;
249   }
250   else
251   {
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;
255   }
256
257   return 0;
258 }
259
260
261 /* destroy all registered timed threads */
262 void 
263 timed_thread_destroy_registered_threads (void)
264 {
265   timed_thread_node *p_node, *p_next;
266
267   for (p_node=p_timed_thread_list_head;
268        p_node;
269        p_node=p_next)
270   {
271     p_next = p_node->next;
272     timed_thread_destroy (p_node->p_timed_thread, p_node->addr_of_p_timed_thread);
273     free (p_node);
274     p_node = NULL;
275   }
276
277   p_timed_thread_list_head = NULL;
278   p_timed_thread_list_tail = NULL;
279 }
280