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