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; /* signalled to stop the thread */
27 unsigned int interval_usecs; /* timed_thread_test() wait 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, "Conky: destroying 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;
132 assert (p_timed_thread != NULL);
134 /* get the absolute time in the future we stop waiting for condition to signal */
135 clock_gettime (CLOCK_REALTIME, &abstime);
136 /* seconds portion of the microseconds interval */
137 reltime.tv_sec = (time_t)(p_timed_thread->interval_usecs / 1000000);
138 /* remaining microseconds convert to nanoseconds */
139 reltime.tv_nsec = (long)((p_timed_thread->interval_usecs % 1000000) * 1000);
140 /* absolute future time */
141 abstime.tv_sec += reltime.tv_sec;
142 abstime.tv_nsec += reltime.tv_nsec;
144 /* acquire runnable_cond mutex */
145 if (pthread_mutex_lock (&p_timed_thread->runnable_mutex))
146 return (-1); /* could not acquire runnable_cond mutex, so tell caller to exit thread */
148 /* release mutex and wait until future time for runnable_cond to signal */
149 rc = pthread_cond_timedwait (&p_timed_thread->runnable_cond,
150 &p_timed_thread->runnable_mutex,
152 /* mutex re-acquired, so release it */
153 pthread_mutex_unlock (&p_timed_thread->runnable_mutex);
156 return 1; /* runnable_cond was signaled, so tell caller to exit thread */
158 /* tell caller not to exit yet */
163 /* exit a timed thread */
165 timed_thread_exit (timed_thread* p_timed_thread)
167 assert (p_timed_thread != NULL);
173 /* register a timed thread for future destruction via timed_thread_destroy_registered_threads() */
175 timed_thread_register (timed_thread* p_timed_thread, timed_thread** addr_of_p_timed_thread)
177 timed_thread_node *p_node;
179 assert (p_timed_thread != NULL);
180 assert ((addr_of_p_timed_thread == NULL) || (*addr_of_p_timed_thread == p_timed_thread));
182 if ((p_node = calloc (sizeof (timed_thread_node), 1)) == 0)
185 p_node->p_timed_thread = p_timed_thread;
186 p_node->addr_of_p_timed_thread = addr_of_p_timed_thread;
189 if (!p_timed_thread_list_tail)
191 /* first node of empty list */
192 p_timed_thread_list_tail = p_node;
193 p_timed_thread_list_head = p_node;
197 /* add node to tail of non-empty list */
198 p_timed_thread_list_tail->next = p_node;
199 p_timed_thread_list_tail = p_node;
206 /* destroy all registered timed threads */
208 timed_thread_destroy_registered_threads (void)
210 timed_thread_node *p_node, *p_next;
212 for (p_node=p_timed_thread_list_head;
216 p_next = p_node->next;
217 timed_thread_destroy (p_node->p_timed_thread, p_node->addr_of_p_timed_thread);
222 p_timed_thread_list_head = NULL;
223 p_timed_thread_list_tail = NULL;