4 * timed_thread.c: Abstraction layer for timed threads
6 * Copyright (C) 2006-2007 Philip Kovacs pkovacs@users.sourceforge.net
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
31 #ifndef HAVE_CLOCK_GETTIME
34 #include "timed_thread.h"
36 /* Abstraction layer for timed threads */
38 static int now (struct timespec *);
43 pthread_t thread; /* thread itself */
44 pthread_attr_t thread_attr; /* thread attributes */
45 pthread_mutex_t cs_mutex; /* critical section mutex */
46 pthread_mutex_t runnable_mutex; /* only for the runnable_cond */
47 pthread_cond_t runnable_cond; /* signalled to stop the thread */
48 void *(*start_routine)(void*); /* thread function to run */
49 void *arg; /* thread function argument */
50 struct timespec interval_time; /* interval_usecs as a struct timespec */
53 /* linked list of created threads */
54 typedef struct _timed_thread_list
56 timed_thread *p_timed_thread;
57 timed_thread **addr_of_p_timed_thread;
58 struct _timed_thread_list *next;
59 } timed_thread_node, timed_thread_list;
61 static timed_thread_list *p_timed_thread_list_head = NULL;
62 static timed_thread_list *p_timed_thread_list_tail = NULL;
64 static int now (struct timespec *abstime)
69 #ifdef HAVE_CLOCK_GETTIME
70 return clock_gettime (CLOCK_REALTIME, &abstime);
72 /* fallback to gettimeofday () */
74 if (gettimeofday (&tv, NULL) != 0)
77 abstime->tv_sec = tv.tv_sec;
78 abstime->tv_nsec = tv.tv_usec * 1000;
84 /* create a timed thread (object creation only) */
86 timed_thread_create (void *(*start_routine)(void*), void *arg, unsigned int interval_usecs)
88 timed_thread *p_timed_thread;
90 assert ((start_routine != NULL) && (interval_usecs >= MINIMUM_INTERVAL_USECS));
92 if ((p_timed_thread = calloc (sizeof(timed_thread), 1)) == 0)
95 /* init attributes, e.g. joinable thread */
96 pthread_attr_init (&p_timed_thread->thread_attr);
97 pthread_attr_setdetachstate (&p_timed_thread->thread_attr, PTHREAD_CREATE_JOINABLE);
99 pthread_mutex_init (&p_timed_thread->cs_mutex, NULL);
100 pthread_mutex_init (&p_timed_thread->runnable_mutex, NULL);
102 pthread_cond_init (&p_timed_thread->runnable_cond, NULL);
104 p_timed_thread->start_routine = start_routine;
105 p_timed_thread->arg = arg;
107 /* seconds portion of the microseconds interval */
108 p_timed_thread->interval_time.tv_sec = (time_t)(interval_usecs / 1000000);
109 /* remaining microseconds convert to nanoseconds */
110 p_timed_thread->interval_time.tv_nsec = (long)((interval_usecs % 1000000) * 1000);
112 return p_timed_thread;
115 /* run a timed thread (drop the thread and run it) */
117 timed_thread_run (timed_thread* p_timed_thread)
119 return pthread_create (&p_timed_thread->thread, &p_timed_thread->thread_attr,
120 p_timed_thread->start_routine, p_timed_thread->arg);
123 /* destroy a timed thread.
124 * optional addr_of_p_timed_thread to set callers pointer to NULL as a convenience. */
126 timed_thread_destroy (timed_thread* p_timed_thread, timed_thread** addr_of_p_timed_thread)
128 assert (p_timed_thread != NULL);
129 assert ((addr_of_p_timed_thread == NULL) || (*addr_of_p_timed_thread == p_timed_thread));
131 /* signal thread to stop */
132 pthread_mutex_lock (&p_timed_thread->runnable_mutex);
133 pthread_cond_signal (&p_timed_thread->runnable_cond);
134 pthread_mutex_unlock (&p_timed_thread->runnable_mutex);
136 /* join the terminating thread */
137 pthread_join (p_timed_thread->thread, NULL);
140 pthread_attr_destroy (&p_timed_thread->thread_attr);
141 pthread_mutex_destroy (&p_timed_thread->cs_mutex);
142 pthread_mutex_destroy (&p_timed_thread->runnable_mutex);
143 pthread_cond_destroy (&p_timed_thread->runnable_cond);
145 free (p_timed_thread);
146 if (addr_of_p_timed_thread)
147 *addr_of_p_timed_thread = NULL;
151 /* lock a timed thread for critical section activity */
153 timed_thread_lock (timed_thread* p_timed_thread)
155 assert (p_timed_thread != NULL);
157 return pthread_mutex_lock (&p_timed_thread->cs_mutex);
161 /* unlock a timed thread after critical section activity */
163 timed_thread_unlock (timed_thread* p_timed_thread)
165 assert (p_timed_thread != NULL);
167 return pthread_mutex_unlock (&p_timed_thread->cs_mutex);
171 /* thread waits interval_usecs for runnable_cond to be signaled. returns 1 if signaled,
172 * -1 on error, and 0 otherwise. caller should call timed_thread_exit() on any non-zero
175 timed_thread_test (timed_thread* p_timed_thread)
177 struct timespec wait_time;
180 assert (p_timed_thread != NULL);
182 if (now (&wait_time)) return (-1);
183 /* move the clock backward 1 sec to shift sync slightly earlier than UI thread */
184 wait_time.tv_sec -= 1;
185 /* now add in the wait interval */
186 wait_time.tv_sec += p_timed_thread->interval_time.tv_sec;
187 wait_time.tv_nsec += p_timed_thread->interval_time.tv_nsec;
189 /* acquire runnable_cond mutex */
190 if (pthread_mutex_lock (&p_timed_thread->runnable_mutex))
191 return (-1); /* could not acquire runnable_cond mutex, so tell caller to exit thread */
193 /* release mutex and wait until future time for runnable_cond to signal */
194 rc = pthread_cond_timedwait (&p_timed_thread->runnable_cond,
195 &p_timed_thread->runnable_mutex,
197 /* mutex re-acquired, so release it */
198 pthread_mutex_unlock (&p_timed_thread->runnable_mutex);
201 return 1; /* runnable_cond was signaled, so tell caller to exit thread */
203 /* tell caller not to exit yet */
208 /* exit a timed thread */
210 timed_thread_exit (timed_thread* p_timed_thread)
212 assert (p_timed_thread != NULL);
218 /* register a timed thread for future destruction via timed_thread_destroy_registered_threads() */
220 timed_thread_register (timed_thread* p_timed_thread, timed_thread** addr_of_p_timed_thread)
222 timed_thread_node *p_node;
224 assert ((addr_of_p_timed_thread == NULL) || (*addr_of_p_timed_thread == p_timed_thread));
226 if ((p_node = calloc (sizeof (timed_thread_node), 1)) == 0)
229 p_node->p_timed_thread = p_timed_thread;
230 p_node->addr_of_p_timed_thread = addr_of_p_timed_thread;
233 if (!p_timed_thread_list_tail)
235 /* first node of empty list */
236 p_timed_thread_list_tail = p_node;
237 p_timed_thread_list_head = p_node;
241 /* add node to tail of non-empty list */
242 p_timed_thread_list_tail->next = p_node;
243 p_timed_thread_list_tail = p_node;
250 /* destroy all registered timed threads */
252 timed_thread_destroy_registered_threads (void)
254 timed_thread_node *p_node, *p_next;
256 for (p_node=p_timed_thread_list_head;
260 p_next = p_node->next;
261 timed_thread_destroy (p_node->p_timed_thread, p_node->addr_of_p_timed_thread);
266 p_timed_thread_list_head = NULL;
267 p_timed_thread_list_tail = NULL;