4 * timed_thread.c: Abstraction layer for timed threads
6 * Copyright (C) 2006 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 #include "timed_thread.h"
33 /* Abstraction layer for timed threads */
38 pthread_t thread; /* thread itself */
39 pthread_attr_t thread_attr; /* thread attributes */
40 pthread_mutex_t cs_mutex; /* critical section mutex */
41 pthread_mutex_t runnable_mutex; /* only for the runnable_cond */
42 pthread_cond_t runnable_cond; /* signalled to stop the thread */
43 unsigned int interval_usecs; /* timed_thread_test() wait interval in microseconds */
46 /* linked list of created threads */
47 typedef struct _timed_thread_list
49 timed_thread *p_timed_thread;
50 timed_thread **addr_of_p_timed_thread;
51 struct _timed_thread_list *next;
52 } timed_thread_node, timed_thread_list;
54 static timed_thread_list *p_timed_thread_list_head = NULL;
55 static timed_thread_list *p_timed_thread_list_tail = NULL;
58 /* create a timed thread */
60 timed_thread_create (void *(*start_routine)(void*), void *arg, unsigned int interval_usecs)
62 timed_thread *p_timed_thread;
64 assert ((start_routine != NULL) && (interval_usecs >= MINIMUM_INTERVAL_USECS));
66 if ((p_timed_thread = calloc (sizeof(timed_thread), 1)) == 0)
69 /* init attributes, e.g. joinable thread */
70 pthread_attr_init (&p_timed_thread->thread_attr);
71 pthread_attr_setdetachstate (&p_timed_thread->thread_attr, PTHREAD_CREATE_JOINABLE);
73 pthread_mutex_init (&p_timed_thread->cs_mutex, NULL);
74 pthread_mutex_init (&p_timed_thread->runnable_mutex, NULL);
76 pthread_cond_init (&p_timed_thread->runnable_cond, NULL);
78 p_timed_thread->interval_usecs = interval_usecs;
81 if (pthread_create (&p_timed_thread->thread, &p_timed_thread->thread_attr, start_routine, arg))
83 timed_thread_destroy (p_timed_thread, NULL);
87 /*fprintf (stderr, "created timed thread 0x%08X\n", (unsigned)p_timed_thread);*/
88 return p_timed_thread;
92 /* destroy a timed thread.
93 * optional addr_of_p_timed_thread to set callers pointer to NULL as a convenience. */
95 timed_thread_destroy (timed_thread* p_timed_thread, timed_thread** addr_of_p_timed_thread)
97 assert (p_timed_thread != NULL);
98 assert ((addr_of_p_timed_thread == NULL) || (*addr_of_p_timed_thread == p_timed_thread));
100 /* signal thread to stop */
101 pthread_mutex_lock (&p_timed_thread->runnable_mutex);
102 pthread_cond_signal (&p_timed_thread->runnable_cond);
103 pthread_mutex_unlock (&p_timed_thread->runnable_mutex);
105 /* join the terminating thread */
106 pthread_join (p_timed_thread->thread, NULL);
109 pthread_attr_destroy (&p_timed_thread->thread_attr);
110 pthread_mutex_destroy (&p_timed_thread->cs_mutex);
111 pthread_mutex_destroy (&p_timed_thread->runnable_mutex);
112 pthread_cond_destroy (&p_timed_thread->runnable_cond);
114 /*fprintf (stderr, "Conky: destroying thread 0x%08X\n", (unsigned)p_timed_thread);*/
115 free (p_timed_thread);
116 if (addr_of_p_timed_thread)
117 *addr_of_p_timed_thread = NULL;
121 /* lock a timed thread for critical section activity */
123 timed_thread_lock (timed_thread* p_timed_thread)
125 assert (p_timed_thread != NULL);
127 return pthread_mutex_lock (&p_timed_thread->cs_mutex);
131 /* unlock a timed thread after critical section activity */
133 timed_thread_unlock (timed_thread* p_timed_thread)
135 assert (p_timed_thread != NULL);
137 return pthread_mutex_unlock (&p_timed_thread->cs_mutex);
141 /* thread waits interval_usecs for runnable_cond to be signaled. returns 1 if signaled,
142 * -1 on error, and 0 otherwise. caller should call timed_thread_exit() on any non-zero
145 timed_thread_test (timed_thread* p_timed_thread)
147 struct timespec abstime, reltime;
150 assert (p_timed_thread != NULL);
152 /* acquire runnable_cond mutex */
153 if (pthread_mutex_lock (&p_timed_thread->runnable_mutex))
154 return (-1); /* could not acquire runnable_cond mutex, so tell caller to exit thread */
156 /* get the absolute time in the future we stop waiting for condition to signal */
157 clock_gettime (CLOCK_REALTIME, &abstime);
158 /* seconds portion of the microseconds interval */
159 reltime.tv_sec = (time_t)(p_timed_thread->interval_usecs / 1000000);
160 /* remaining microseconds convert to nanoseconds */
161 reltime.tv_nsec = (long)((p_timed_thread->interval_usecs % 1000000) * 1000);
162 /* absolute future time */
163 abstime.tv_sec += reltime.tv_sec;
164 abstime.tv_nsec += reltime.tv_nsec;
166 /* release mutex and wait until future time for runnable_cond to signal */
167 rc = pthread_cond_timedwait (&p_timed_thread->runnable_cond,
168 &p_timed_thread->runnable_mutex,
170 /* mutex re-acquired, so release it */
171 pthread_mutex_unlock (&p_timed_thread->runnable_mutex);
174 return 1; /* runnable_cond was signaled, so tell caller to exit thread */
176 /* tell caller not to exit yet */
181 /* exit a timed thread */
183 timed_thread_exit (timed_thread* p_timed_thread)
185 assert (p_timed_thread != NULL);
191 /* register a timed thread for future destruction via timed_thread_destroy_registered_threads() */
193 timed_thread_register (timed_thread* p_timed_thread, timed_thread** addr_of_p_timed_thread)
195 timed_thread_node *p_node;
197 assert (p_timed_thread != NULL);
198 assert ((addr_of_p_timed_thread == NULL) || (*addr_of_p_timed_thread == p_timed_thread));
200 if ((p_node = calloc (sizeof (timed_thread_node), 1)) == 0)
203 p_node->p_timed_thread = p_timed_thread;
204 p_node->addr_of_p_timed_thread = addr_of_p_timed_thread;
207 if (!p_timed_thread_list_tail)
209 /* first node of empty list */
210 p_timed_thread_list_tail = p_node;
211 p_timed_thread_list_head = p_node;
215 /* add node to tail of non-empty list */
216 p_timed_thread_list_tail->next = p_node;
217 p_timed_thread_list_tail = p_node;
224 /* destroy all registered timed threads */
226 timed_thread_destroy_registered_threads (void)
228 timed_thread_node *p_node, *p_next;
230 for (p_node=p_timed_thread_list_head;
234 p_next = p_node->next;
235 timed_thread_destroy (p_node->p_timed_thread, p_node->addr_of_p_timed_thread);
240 p_timed_thread_list_head = NULL;
241 p_timed_thread_list_tail = NULL;