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