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