4 * Author: Philip Kovacs
6 * Abstraction layer for timed threads
14 #include "timed_thread.h"
16 /* Abstraction layer for timed threads */
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 */
29 /* linked list of created threadsa */
30 typedef struct _timed_thread_list
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;
37 static timed_thread_list *p_timed_thread_list_head = NULL;
38 static timed_thread_list *p_timed_thread_list_tail = NULL;
41 /* create a timed thread */
43 timed_thread_create (void *(*start_routine)(void*), void *arg, unsigned int interval_usecs)
45 timed_thread *p_timed_thread;
47 assert ((start_routine != NULL) && (interval_usecs >= MINIMUM_INTERVAL_USECS));
49 if ((p_timed_thread = calloc (sizeof(timed_thread), 1)) == 0)
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);
56 pthread_mutex_init (&p_timed_thread->cs_mutex, NULL);
57 pthread_mutex_init (&p_timed_thread->runnable_mutex, NULL);
59 pthread_cond_init (&p_timed_thread->runnable_cond, NULL);
61 p_timed_thread->interval_usecs = interval_usecs;
64 if (pthread_create (&p_timed_thread->thread, &p_timed_thread->thread_attr, start_routine, arg))
66 timed_thread_destroy (p_timed_thread, NULL);
70 return p_timed_thread;
74 /* destroy a timed thread.
75 * optional addr_of_p_timed_thread to set callers pointer to NULL as a convenience. */
77 timed_thread_destroy (timed_thread* p_timed_thread, timed_thread** addr_of_p_timed_thread)
79 assert (p_timed_thread != NULL);
80 assert ((addr_of_p_timed_thread == NULL) || (*addr_of_p_timed_thread == p_timed_thread));
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);
87 /* join the terminating thread */
88 pthread_join (p_timed_thread->thread, NULL);
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);
96 free (p_timed_thread);
97 if (addr_of_p_timed_thread)
98 *addr_of_p_timed_thread = NULL;
102 /* lock a timed thread for critical section activity */
104 timed_thread_lock (timed_thread* p_timed_thread)
106 assert (p_timed_thread != NULL);
108 return pthread_mutex_lock (&p_timed_thread->cs_mutex);
112 /* unlock a timed thread after critical section activity */
114 timed_thread_unlock (timed_thread* p_timed_thread)
116 assert (p_timed_thread != NULL);
118 return pthread_mutex_unlock (&p_timed_thread->cs_mutex);
122 /* waits required interval for termination signal and returns 1 if got it, 0 otherwise */
124 timed_thread_test (timed_thread* p_timed_thread)
126 struct timespec abstime, reltime;
128 assert (p_timed_thread != NULL);
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;
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)
145 /* runnable_cond was signalled or some error occured */
146 pthread_mutex_unlock (&p_timed_thread->runnable_mutex);
147 /* tell caller to exit */
151 /* tell caller not to exit yet */
156 /* exit a timed thread */
158 timed_thread_exit (timed_thread* p_timed_thread)
160 assert (p_timed_thread != NULL);
166 /* register a timed thread for future destruction via timed_thread_destroy_registered_threads() */
168 timed_thread_register (timed_thread* p_timed_thread, timed_thread** addr_of_p_timed_thread)
170 timed_thread_node *p_node;
172 assert (p_timed_thread != NULL);
173 assert ((addr_of_p_timed_thread == NULL) || (*addr_of_p_timed_thread == p_timed_thread));
175 if ((p_node = calloc (sizeof (timed_thread_node), 1)) == 0)
178 p_node->p_timed_thread = p_timed_thread;
179 p_node->addr_of_p_timed_thread = addr_of_p_timed_thread;
182 if (!p_timed_thread_list_tail)
184 /* first node of empty list */
185 p_timed_thread_list_tail = p_node;
186 p_timed_thread_list_head = p_node;
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;
199 /* destroy all registered timed threads */
201 timed_thread_destroy_registered_threads (void)
203 timed_thread_node *p_node, *p_next;
205 for (p_node=p_timed_thread_list_head;
209 p_next = p_node->next;
210 timed_thread_destroy (p_node->p_timed_thread, p_node->addr_of_p_timed_thread);
214 p_timed_thread_list_head = NULL;
215 p_timed_thread_list_tail = NULL;