update copywrite dates where appropriate
[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 /* private */
39 struct _timed_thread 
40 {
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   unsigned int interval_usecs;    /* timed_thread_test() wait interval in microseconds */
47 };
48
49 /* linked list of created threads */
50 typedef struct _timed_thread_list
51 {
52   timed_thread *p_timed_thread;
53   timed_thread **addr_of_p_timed_thread;
54   struct _timed_thread_list *next;
55 } timed_thread_node, timed_thread_list;
56
57 static timed_thread_list *p_timed_thread_list_head = NULL;
58 static timed_thread_list *p_timed_thread_list_tail = NULL;
59
60
61 /* create a timed thread */
62 timed_thread* 
63 timed_thread_create (void *(*start_routine)(void*), void *arg, unsigned int interval_usecs)
64 {
65   timed_thread *p_timed_thread;
66
67   assert ((start_routine != NULL) && (interval_usecs >= MINIMUM_INTERVAL_USECS));
68
69   if ((p_timed_thread = calloc (sizeof(timed_thread), 1)) == 0)
70     return NULL;
71
72   /* init attributes, e.g. joinable thread */
73   pthread_attr_init (&p_timed_thread->thread_attr);
74   pthread_attr_setdetachstate (&p_timed_thread->thread_attr, PTHREAD_CREATE_JOINABLE);
75   /* init mutexes */
76   pthread_mutex_init (&p_timed_thread->cs_mutex, NULL);
77   pthread_mutex_init (&p_timed_thread->runnable_mutex, NULL);
78   /* init cond */
79   pthread_cond_init (&p_timed_thread->runnable_cond, NULL);
80
81   p_timed_thread->interval_usecs = interval_usecs;
82
83   /* create thread */
84   if (pthread_create (&p_timed_thread->thread, &p_timed_thread->thread_attr, start_routine, arg))
85   {
86     timed_thread_destroy (p_timed_thread, NULL);
87     return NULL;
88   }
89
90   /*fprintf (stderr, "created timed thread 0x%08X\n", (unsigned)p_timed_thread);*/
91   return p_timed_thread;
92 }
93
94
95 /* destroy a timed thread.
96  * optional addr_of_p_timed_thread to set callers pointer to NULL as a convenience. */
97 void 
98 timed_thread_destroy (timed_thread* p_timed_thread, timed_thread** addr_of_p_timed_thread)
99 {
100   assert (p_timed_thread != NULL);
101   assert ((addr_of_p_timed_thread == NULL) || (*addr_of_p_timed_thread == p_timed_thread));
102
103   /* signal thread to stop */
104   pthread_mutex_lock (&p_timed_thread->runnable_mutex);
105   pthread_cond_signal (&p_timed_thread->runnable_cond);
106   pthread_mutex_unlock (&p_timed_thread->runnable_mutex);
107
108   /* join the terminating thread */
109   pthread_join (p_timed_thread->thread, NULL);
110
111   /* clean up */
112   pthread_attr_destroy (&p_timed_thread->thread_attr);
113   pthread_mutex_destroy (&p_timed_thread->cs_mutex);
114   pthread_mutex_destroy (&p_timed_thread->runnable_mutex);
115   pthread_cond_destroy (&p_timed_thread->runnable_cond);
116
117   /*fprintf (stderr, "Conky: destroying thread 0x%08X\n", (unsigned)p_timed_thread);*/
118   free (p_timed_thread);
119   if (addr_of_p_timed_thread)
120     *addr_of_p_timed_thread = NULL;
121 }
122
123
124 /* lock a timed thread for critical section activity */
125 int
126 timed_thread_lock (timed_thread* p_timed_thread)
127 {
128   assert (p_timed_thread != NULL);
129
130   return pthread_mutex_lock (&p_timed_thread->cs_mutex);
131 }
132
133
134 /* unlock a timed thread after critical section activity */
135 int
136 timed_thread_unlock (timed_thread* p_timed_thread)
137 {
138   assert (p_timed_thread != NULL);
139
140   return pthread_mutex_unlock (&p_timed_thread->cs_mutex);
141 }
142
143
144 /* thread waits interval_usecs for runnable_cond to be signaled.  returns 1 if signaled, 
145  * -1 on error, and 0 otherwise.  caller should call timed_thread_exit() on any non-zero 
146  * return value. */
147 int 
148 timed_thread_test (timed_thread* p_timed_thread)
149 {
150   struct timespec abstime, reltime;
151   int rc;
152
153   assert (p_timed_thread != NULL);
154
155   /* acquire runnable_cond mutex */
156   if (pthread_mutex_lock (&p_timed_thread->runnable_mutex))
157     return (-1);  /* could not acquire runnable_cond mutex, so tell caller to exit thread */
158
159   /* get the absolute time in the future we stop waiting for condition to signal */
160 #ifdef HAVE_CLOCK_GETTIME
161   clock_gettime (CLOCK_REALTIME, &abstime);
162 #else
163   {
164     /* fallback to gettimeofday () */
165     struct timeval tv;
166     if (gettimeofday (&tv, NULL) != 0)
167     {
168       pthread_mutex_unlock (&p_timed_thread->runnable_mutex);
169       return (-1);
170     }
171
172     abstime.tv_sec = tv.tv_sec;
173     abstime.tv_nsec = tv.tv_usec * 1000;
174   }
175 #endif
176   /* seconds portion of the microseconds interval */
177   reltime.tv_sec = (time_t)(p_timed_thread->interval_usecs / 1000000);
178   /* remaining microseconds convert to nanoseconds */
179   reltime.tv_nsec = (long)((p_timed_thread->interval_usecs % 1000000) * 1000);
180   /* absolute future time */
181   abstime.tv_sec += reltime.tv_sec;
182   abstime.tv_nsec += reltime.tv_nsec;
183
184   /* release mutex and wait until future time for runnable_cond to signal */
185   rc = pthread_cond_timedwait (&p_timed_thread->runnable_cond,
186                                &p_timed_thread->runnable_mutex,
187                                &abstime);
188   /* mutex re-acquired, so release it */
189   pthread_mutex_unlock (&p_timed_thread->runnable_mutex);
190
191   if (rc==0)
192     return 1; /* runnable_cond was signaled, so tell caller to exit thread */
193
194   /* tell caller not to exit yet */
195   return 0;
196 }
197
198
199 /* exit a timed thread */
200 void 
201 timed_thread_exit (timed_thread* p_timed_thread)
202 {
203   assert (p_timed_thread != NULL);
204
205   pthread_exit (NULL);
206 }
207
208
209 /* register a timed thread for future destruction via timed_thread_destroy_registered_threads() */
210 int 
211 timed_thread_register (timed_thread* p_timed_thread, timed_thread** addr_of_p_timed_thread)
212 {
213   timed_thread_node *p_node;
214
215   assert (p_timed_thread != NULL);
216   assert ((addr_of_p_timed_thread == NULL) || (*addr_of_p_timed_thread == p_timed_thread));
217
218   if ((p_node = calloc (sizeof (timed_thread_node), 1)) == 0)
219     return 0;
220     
221   p_node->p_timed_thread = p_timed_thread;
222   p_node->addr_of_p_timed_thread = addr_of_p_timed_thread;
223   p_node->next = NULL;
224
225   if (!p_timed_thread_list_tail)
226   {
227     /* first node of empty list */
228     p_timed_thread_list_tail = p_node;
229     p_timed_thread_list_head = p_node;
230   }
231   else
232   {
233     /* add node to tail of non-empty list */
234     p_timed_thread_list_tail->next = p_node;
235     p_timed_thread_list_tail = p_node;
236   }
237
238   return 0;
239 }
240
241
242 /* destroy all registered timed threads */
243 void 
244 timed_thread_destroy_registered_threads (void)
245 {
246   timed_thread_node *p_node, *p_next;
247
248   for (p_node=p_timed_thread_list_head;
249        p_node;
250        p_node=p_next)
251   {
252     p_next = p_node->next;
253     timed_thread_destroy (p_node->p_timed_thread, p_node->addr_of_p_timed_thread);
254     free (p_node);
255     p_node = NULL;
256   }
257
258   p_timed_thread_list_head = NULL;
259   p_timed_thread_list_tail = NULL;
260 }
261