Fix timed thread race condition, seen esp. on new kernel scheduler (cfs).
[monky] / src / timed_thread.h
1 /* $Id$ */
2
3 /* 
4  * timed_thread.h: Abstraction layer for timed threads
5  *
6  * Copyright (C) 2006-2007 Philip Kovacs pkovacs@users.sourceforge.net
7  *
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.
12  *
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.
17  *
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
21  * USA.
22  *
23  */
24
25 #ifndef _TIMED_THREAD_H_
26 #define _TIMED_THREAD_H_
27
28 #define MINIMUM_INTERVAL_USECS 50000  /* 50000 microseconds = 50 ms =  0.05 sec */
29
30 /* opaque structure for clients */
31 typedef struct _timed_thread timed_thread;
32
33 /* create a timed thread (object creation only) */
34 timed_thread* timed_thread_create (void *(*start_routine)(void*), void *arg, unsigned int interval_usecs);
35
36 /* run a timed thread (drop the thread and run it) */
37 int timed_thread_run (timed_thread* p_timed_thread);
38
39 /* destroy a timed thread */
40 void timed_thread_destroy (timed_thread* p_timed_thread, timed_thread** addr_of_p_timed_thread);
41
42 /* lock a timed thread for critical section activity */
43 int timed_thread_lock (timed_thread* p_timed_thread);
44
45 /* unlock a timed thread after critical section activity */
46 int timed_thread_unlock (timed_thread* p_timed_thread);
47
48 /* waits required interval for termination signal and returns 1 if got it, 0 otherwise */
49 int timed_thread_test (timed_thread* p_timed_thread);
50
51 /* exit a timed thread */
52 void timed_thread_exit (timed_thread* p_timed_thread);
53
54 /* register a timed thread for future destruction via timed_thread_destroy_registered_threads() */
55 int timed_thread_register (timed_thread* p_timed_thread, timed_thread** addr_of_p_timed_thread);
56
57 /* destroy all registered timed threads */
58 void timed_thread_destroy_registered_threads (void);
59
60 #endif /* #ifdef _TIMED_THREAD_H_ */