set ptr to NULL after free
[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;       /* used to time and stop the thread */
27     unsigned int interval_usecs;        /* firing 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, "destroying timed 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
131     assert (p_timed_thread != NULL);
132
133     /* get the absolute time in the future we stop waiting for condition to signal */
134     clock_gettime (CLOCK_REALTIME, &abstime);
135     /* seconds portion of the microseconds interval */
136     reltime.tv_sec = (time_t)(p_timed_thread->interval_usecs / 1000000);
137     /* remaining microseconds convert to nanoseconds */
138     reltime.tv_nsec = (long)((p_timed_thread->interval_usecs % 1000000) * 1000);
139     /* absolute future time */
140     abstime.tv_sec += reltime.tv_sec;
141     abstime.tv_nsec += reltime.tv_nsec;
142
143     /* wait until future time for runnable_cond to signal */
144     if (pthread_cond_timedwait (&p_timed_thread->runnable_cond,
145                                 &p_timed_thread->runnable_mutex,
146                                 &abstime) != ETIMEDOUT)
147     {
148         /* runnable_cond was signalled or some error occured */
149         pthread_mutex_unlock (&p_timed_thread->runnable_mutex);
150         /* tell caller to exit */
151         return 1;
152     }
153
154     /* tell caller not to exit yet */
155     return 0;
156 }
157
158
159 /* exit a timed thread */
160 void 
161 timed_thread_exit (timed_thread* p_timed_thread)
162 {
163     assert (p_timed_thread != NULL);
164
165     pthread_exit (NULL);
166 }
167
168
169 /* register a timed thread for future destruction via timed_thread_destroy_registered_threads() */
170 int 
171 timed_thread_register (timed_thread* p_timed_thread, timed_thread** addr_of_p_timed_thread)
172 {
173     timed_thread_node *p_node;
174
175     assert (p_timed_thread != NULL);
176     assert ((addr_of_p_timed_thread == NULL) || (*addr_of_p_timed_thread == p_timed_thread));
177
178     if ((p_node = calloc (sizeof (timed_thread_node), 1)) == 0)
179         return 0;
180     
181     p_node->p_timed_thread = p_timed_thread;
182     p_node->addr_of_p_timed_thread = addr_of_p_timed_thread;
183     p_node->next = NULL;
184
185     if (!p_timed_thread_list_tail)
186     {
187         /* first node of empty list */
188         p_timed_thread_list_tail = p_node;
189         p_timed_thread_list_head = p_node;
190     }
191     else
192     {
193         /* add node to tail of non-empty list */
194         p_timed_thread_list_tail->next = p_node;
195         p_timed_thread_list_tail = p_node;
196     }
197
198     return 0;
199 }
200
201
202 /* destroy all registered timed threads */
203 void 
204 timed_thread_destroy_registered_threads (void)
205 {
206     timed_thread_node *p_node, *p_next;
207
208     for (p_node=p_timed_thread_list_head;
209          p_node;
210          p_node=p_next)
211     {
212         p_next = p_node->next;
213         timed_thread_destroy (p_node->p_timed_thread, p_node->addr_of_p_timed_thread);
214         free (p_node);
215         p_node=NULL:
216     }
217
218     p_timed_thread_list_head = NULL;
219     p_timed_thread_list_tail = NULL;
220 }
221