4 * Author: Philip Kovacs
6 * Abstraction layer for timed threads
15 #include "timed_thread.h"
17 /* Abstraction layer for timed threads */
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 */
30 /* linked list of created threads */
31 typedef struct _timed_thread_list
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;
38 static timed_thread_list *p_timed_thread_list_head = NULL;
39 static timed_thread_list *p_timed_thread_list_tail = NULL;
42 /* create a timed thread */
44 timed_thread_create (void *(*start_routine)(void*), void *arg, unsigned int interval_usecs)
46 timed_thread *p_timed_thread;
48 assert ((start_routine != NULL) && (interval_usecs >= MINIMUM_INTERVAL_USECS));
50 if ((p_timed_thread = calloc (sizeof(timed_thread), 1)) == 0)
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);
57 pthread_mutex_init (&p_timed_thread->cs_mutex, NULL);
58 pthread_mutex_init (&p_timed_thread->runnable_mutex, NULL);
60 pthread_cond_init (&p_timed_thread->runnable_cond, NULL);
62 p_timed_thread->interval_usecs = interval_usecs;
65 if (pthread_create (&p_timed_thread->thread, &p_timed_thread->thread_attr, start_routine, arg))
67 timed_thread_destroy (p_timed_thread, NULL);
71 /*fprintf (stderr, "created timed thread 0x%08X\n", (unsigned)p_timed_thread);*/
72 return p_timed_thread;
76 /* destroy a timed thread.
77 * optional addr_of_p_timed_thread to set callers pointer to NULL as a convenience. */
79 timed_thread_destroy (timed_thread* p_timed_thread, timed_thread** addr_of_p_timed_thread)
81 assert (p_timed_thread != NULL);
82 assert ((addr_of_p_timed_thread == NULL) || (*addr_of_p_timed_thread == p_timed_thread));
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);
89 /* join the terminating thread */
90 pthread_join (p_timed_thread->thread, NULL);
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);
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;
105 /* lock a timed thread for critical section activity */
107 timed_thread_lock (timed_thread* p_timed_thread)
109 assert (p_timed_thread != NULL);
111 return pthread_mutex_lock (&p_timed_thread->cs_mutex);
115 /* unlock a timed thread after critical section activity */
117 timed_thread_unlock (timed_thread* p_timed_thread)
119 assert (p_timed_thread != NULL);
121 return pthread_mutex_unlock (&p_timed_thread->cs_mutex);
125 /* waits required interval for termination signal and returns 1 if got it, 0 otherwise */
127 timed_thread_test (timed_thread* p_timed_thread)
129 struct timespec abstime, reltime;
131 assert (p_timed_thread != NULL);
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;
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)
148 /* runnable_cond was signalled or some error occured */
149 pthread_mutex_unlock (&p_timed_thread->runnable_mutex);
150 /* tell caller to exit */
154 /* tell caller not to exit yet */
159 /* exit a timed thread */
161 timed_thread_exit (timed_thread* p_timed_thread)
163 assert (p_timed_thread != NULL);
169 /* register a timed thread for future destruction via timed_thread_destroy_registered_threads() */
171 timed_thread_register (timed_thread* p_timed_thread, timed_thread** addr_of_p_timed_thread)
173 timed_thread_node *p_node;
175 assert (p_timed_thread != NULL);
176 assert ((addr_of_p_timed_thread == NULL) || (*addr_of_p_timed_thread == p_timed_thread));
178 if ((p_node = calloc (sizeof (timed_thread_node), 1)) == 0)
181 p_node->p_timed_thread = p_timed_thread;
182 p_node->addr_of_p_timed_thread = addr_of_p_timed_thread;
185 if (!p_timed_thread_list_tail)
187 /* first node of empty list */
188 p_timed_thread_list_tail = p_node;
189 p_timed_thread_list_head = p_node;
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;
202 /* destroy all registered timed threads */
204 timed_thread_destroy_registered_threads (void)
206 timed_thread_node *p_node, *p_next;
208 for (p_node=p_timed_thread_list_head;
212 p_next = p_node->next;
213 timed_thread_destroy (p_node->p_timed_thread, p_node->addr_of_p_timed_thread);
218 p_timed_thread_list_head = NULL;
219 p_timed_thread_list_tail = NULL;