1 /*****************************************************************************
2 * threads.h : thread implementation for VideoLAN client
3 * This header is supposed to provide a portable threads implementation.
4 * Currently, it is a wrapper to either the POSIX pthreads library, or
5 * the Mach cthreads (for the GNU/Hurd).
6 *****************************************************************************
7 * Copyright (C) 1999, 2000 VideoLAN
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
25 *****************************************************************************/
33 /*****************************************************************************
35 *****************************************************************************
36 * These constants are used by all threads in *_CreateThread() and
37 * *_DestroyThreads() functions. Since those calls are non-blocking, an integer
38 * value is used as a shared flag to represent the status of the thread.
39 *****************************************************************************/
41 /* Void status - this value can be used to be sure, in an array of recorded
42 * threads, that no operation is currently in progress on the concerned thread */
43 #define THREAD_NOP 0 /* nothing happened */
46 #define THREAD_CREATE 10 /* thread is initializing */
47 #define THREAD_START 11 /* thread has forked */
48 #define THREAD_READY 19 /* thread is ready */
50 /* Destructions status */
51 #define THREAD_DESTROY 20 /* destruction order has been sent */
52 #define THREAD_END 21 /* destruction order has been received */
53 #define THREAD_OVER 29 /* thread does not exist any more */
56 #define THREAD_ERROR 30 /* an error occured */
57 #define THREAD_FATAL 31 /* an fatal error occured - program must end */
59 /*****************************************************************************
61 *****************************************************************************/
65 typedef cthread_t vlc_thread_t;
67 /* those structs are the ones defined in /include/cthreads.h but we need
68 * * to handle *foo where foo is a mutex_t */
69 typedef struct s_mutex {
73 struct cthread_queue queue;
76 typedef struct s_condition {
78 struct cthread_queue queue;
80 struct cond_imp *implications;
85 typedef pthread_t vlc_thread_t;
86 typedef pthread_mutex_t vlc_mutex_t;
87 typedef pthread_cond_t vlc_cond_t;
91 typedef void *(*vlc_thread_func_t)(void *p_data);
93 /*****************************************************************************
95 *****************************************************************************/
97 static __inline__ int vlc_thread_create( vlc_thread_t *p_thread, char *psz_name,
98 vlc_thread_func_t func, void *p_data );
99 static __inline__ void vlc_thread_exit ( void );
100 static __inline__ void vlc_thread_join ( vlc_thread_t thread );
102 static __inline__ int vlc_mutex_init ( vlc_mutex_t *p_mutex );
103 static __inline__ int vlc_mutex_lock ( vlc_mutex_t *p_mutex );
104 static __inline__ int vlc_mutex_unlock ( vlc_mutex_t *p_mutex );
106 static __inline__ int vlc_cond_init ( vlc_cond_t *p_condvar );
107 static __inline__ int vlc_cond_signal ( vlc_cond_t *p_condvar );
108 static __inline__ int vlc_cond_wait ( vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex );
111 static _inline__ int vlc_cond_timedwait ( vlc_cond_t * condvar, vlc_mutex_t * mutex,
112 mtime_t absoute_timeout_time );
115 /*****************************************************************************
116 * vlc_thread_create: create a thread
117 *****************************************************************************/
118 static __inline__ int vlc_thread_create( vlc_thread_t *p_thread,
119 char *psz_name, vlc_thread_func_t func,
123 *p_thread = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
126 return pthread_create( p_thread, NULL, func, p_data );
130 /*****************************************************************************
131 * vlc_thread_exit: terminate a thread
132 *****************************************************************************/
133 static __inline__ void vlc_thread_exit( void )
137 cthread_exit( &result );
143 /*****************************************************************************
144 * vlc_thread_join: wait until a thread exits
145 *****************************************************************************/
146 static __inline__ void vlc_thread_join( vlc_thread_t thread )
149 cthread_join( thread );
151 pthread_join( thread, NULL );
155 /*****************************************************************************
156 * vlc_mutex_init: initialize a mutex
157 *****************************************************************************/
158 static __inline__ int vlc_mutex_init( vlc_mutex_t *p_mutex )
161 mutex_init( p_mutex );
164 return pthread_mutex_init( p_mutex, NULL );
168 /*****************************************************************************
169 * vlc_mutex_lock: lock a mutex
170 *****************************************************************************/
171 static __inline__ int vlc_mutex_lock( vlc_mutex_t *p_mutex )
174 mutex_lock( p_mutex );
177 return pthread_mutex_lock( p_mutex );
181 /*****************************************************************************
182 * vlc_mutex_unlock: unlock a mutex
183 *****************************************************************************/
184 static __inline__ int vlc_mutex_unlock( vlc_mutex_t *p_mutex )
187 mutex_unlock( p_mutex );
190 return pthread_mutex_unlock( p_mutex );
194 /*****************************************************************************
195 * vlc_cond_init: initialize a condition
196 *****************************************************************************/
197 static __inline__ int vlc_cond_init( vlc_cond_t *p_condvar )
200 /* condition_init() */
201 spin_lock_init( &p_condvar->lock );
202 cthread_queue_init( &p_condvar->queue );
204 p_condvar->implications = 0;
208 return pthread_cond_init( p_condvar, NULL );
212 /*****************************************************************************
213 * vlc_cond_signal: start a thread on condition completion
214 *****************************************************************************/
215 static __inline__ int vlc_cond_signal( vlc_cond_t *p_condvar )
218 /* condition_signal() */
219 if ( p_condvar->queue.head || p_condvar->implications )
221 cond_signal( (condition_t)p_condvar );
225 return pthread_cond_signal( p_condvar );
229 /*****************************************************************************
230 * vlc_cond_wait: wait until condition completion
231 *****************************************************************************/
232 static __inline__ int vlc_cond_wait( vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
235 condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
238 return pthread_cond_wait( p_condvar, p_mutex );