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