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