1 /*****************************************************************************
2 * vlc_threads_funcs.h : threads implementation for the VideoLAN client
3 * This header provides a portable threads implementation.
4 *****************************************************************************
5 * Copyright (C) 1999-2007 the VideoLAN team
8 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
9 * Samuel Hocevar <sam@via.ecp.fr>
10 * Gildas Bazin <gbazin@netcourrier.com>
11 * Christophe Massiot <massiot@via.ecp.fr>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
28 #if !defined( __LIBVLC__ )
29 #error You are not libvlc or one of its plugins. You cannot include this file
32 #ifndef _VLC_THREADFUNCS_H_
33 #define _VLC_THREADFUNCS_H_
35 #if defined( WIN32 ) && !defined ETIMEDOUT
36 # define ETIMEDOUT 10060 /* This is the value in winsock.h. */
39 /*****************************************************************************
40 * Function definitions
41 *****************************************************************************/
42 VLC_EXPORT( int, vlc_mutex_init, ( vlc_mutex_t * ) );
43 VLC_EXPORT( int, vlc_mutex_init_recursive, ( vlc_mutex_t * ) );
44 VLC_EXPORT( void, __vlc_mutex_destroy, ( const char *, int, vlc_mutex_t * ) );
45 VLC_EXPORT( int, __vlc_cond_init, ( vlc_cond_t * ) );
46 VLC_EXPORT( void, __vlc_cond_destroy, ( const char *, int, vlc_cond_t * ) );
47 VLC_EXPORT( int, __vlc_threadvar_create, (vlc_threadvar_t * ) );
48 VLC_EXPORT( int, __vlc_thread_create, ( vlc_object_t *, const char *, int, const char *, void * ( * ) ( void * ), int, bool ) );
49 VLC_EXPORT( int, __vlc_thread_set_priority, ( vlc_object_t *, const char *, int, int ) );
50 VLC_EXPORT( void, __vlc_thread_ready, ( vlc_object_t * ) );
51 VLC_EXPORT( void, __vlc_thread_join, ( vlc_object_t *, const char *, int ) );
53 /*****************************************************************************
54 * vlc_mutex_lock: lock a mutex
55 *****************************************************************************/
56 #define vlc_mutex_lock( P_MUTEX ) \
57 __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
59 #if defined(LIBVLC_USE_PTHREAD)
60 VLC_EXPORT(void, vlc_pthread_fatal, (const char *action, int error, const char *file, unsigned line));
62 # define VLC_THREAD_ASSERT( action ) \
64 vlc_pthread_fatal (action, val, psz_file, i_line)
66 # define VLC_THREAD_ASSERT (void)0
69 static inline void __vlc_mutex_lock( const char * psz_file, int i_line,
70 vlc_mutex_t * p_mutex )
72 #if defined( UNDER_CE )
73 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
75 EnterCriticalSection( &p_mutex->csection );
77 #elif defined( WIN32 )
78 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
80 WaitForSingleObject( *p_mutex, INFINITE );
82 #elif defined( HAVE_KERNEL_SCHEDULER_H )
83 acquire_sem( p_mutex->lock );
85 #elif defined(LIBVLC_USE_PTHREAD)
86 # define vlc_assert_locked( m ) \
87 assert (pthread_mutex_lock (m) == EDEADLK)
88 int val = pthread_mutex_lock( p_mutex );
89 VLC_THREAD_ASSERT ("locking mutex");
94 #ifndef vlc_assert_locked
95 # define vlc_assert_locked( m ) (void)0
98 /*****************************************************************************
99 * vlc_mutex_unlock: unlock a mutex
100 *****************************************************************************/
101 #define vlc_mutex_unlock( P_MUTEX ) \
102 __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
104 static inline void __vlc_mutex_unlock( const char * psz_file, int i_line,
105 vlc_mutex_t *p_mutex )
107 #if defined( UNDER_CE )
108 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
110 LeaveCriticalSection( &p_mutex->csection );
112 #elif defined( WIN32 )
113 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
115 ReleaseMutex( *p_mutex );
117 #elif defined( HAVE_KERNEL_SCHEDULER_H )
118 release_sem( p_mutex->lock );
120 #elif defined(LIBVLC_USE_PTHREAD)
121 int val = pthread_mutex_unlock( p_mutex );
122 VLC_THREAD_ASSERT ("unlocking mutex");
127 /*****************************************************************************
128 * vlc_mutex_destroy: destroy a mutex
129 *****************************************************************************/
130 #define vlc_mutex_destroy( P_MUTEX ) \
131 __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
133 /*****************************************************************************
134 * vlc_cond_init: initialize a condition
135 *****************************************************************************/
136 #define vlc_cond_init( P_THIS, P_COND ) \
137 __vlc_cond_init( P_COND )
139 /*****************************************************************************
140 * vlc_cond_signal: start a thread on condition completion
141 *****************************************************************************/
142 #define vlc_cond_signal( P_COND ) \
143 __vlc_cond_signal( __FILE__, __LINE__, P_COND )
145 static inline void __vlc_cond_signal( const char * psz_file, int i_line,
146 vlc_cond_t *p_condvar )
148 #if defined( UNDER_CE ) || defined( WIN32 )
149 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
151 /* Release one waiting thread if one is available. */
152 /* For this trick to work properly, the vlc_cond_signal must be surrounded
153 * by a mutex. This will prevent another thread from stealing the signal */
154 /* PulseEvent() only works if none of the waiting threads is suspended.
155 * This is particularily problematic under a debug session.
156 * as documented in http://support.microsoft.com/kb/q173260/ */
157 PulseEvent( p_condvar->event );
159 #elif defined( HAVE_KERNEL_SCHEDULER_H )
160 while( p_condvar->thread != -1 )
163 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
166 if( info.state != B_THREAD_SUSPENDED )
168 /* The waiting thread is not suspended so it could
169 * have been interrupted beetwen the unlock and the
170 * suspend_thread line. That is why we sleep a little
171 * before retesting p_condver->thread. */
176 /* Ok, we have to wake up that thread */
177 resume_thread( p_condvar->thread );
181 #elif defined(LIBVLC_USE_PTHREAD)
182 int val = pthread_cond_signal( p_condvar );
183 VLC_THREAD_ASSERT ("signaling condition variable");
188 /*****************************************************************************
189 * vlc_cond_wait: wait until condition completion
190 *****************************************************************************/
191 #define vlc_cond_wait( P_COND, P_MUTEX ) \
192 __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX )
194 static inline void __vlc_cond_wait( const char * psz_file, int i_line,
195 vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
197 #if defined( UNDER_CE )
198 p_condvar->i_waiting_threads++;
199 LeaveCriticalSection( &p_mutex->csection );
200 WaitForSingleObject( p_condvar->event, INFINITE );
201 p_condvar->i_waiting_threads--;
203 /* Reacquire the mutex before returning. */
204 vlc_mutex_lock( p_mutex );
206 #elif defined( WIN32 )
207 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
209 /* Increase our wait count */
210 p_condvar->i_waiting_threads++;
211 SignalObjectAndWait( *p_mutex, p_condvar->event, INFINITE, FALSE );
212 p_condvar->i_waiting_threads--;
214 /* Reacquire the mutex before returning. */
215 vlc_mutex_lock( p_mutex );
217 #elif defined( HAVE_KERNEL_SCHEDULER_H )
218 /* The p_condvar->thread var is initialized before the unlock because
219 * it enables to identify when the thread is interrupted beetwen the
220 * unlock line and the suspend_thread line */
221 p_condvar->thread = find_thread( NULL );
222 vlc_mutex_unlock( p_mutex );
223 suspend_thread( p_condvar->thread );
224 p_condvar->thread = -1;
226 vlc_mutex_lock( p_mutex );
228 #elif defined(LIBVLC_USE_PTHREAD)
229 int val = pthread_cond_wait( p_condvar, p_mutex );
230 VLC_THREAD_ASSERT ("waiting on condition");
236 /*****************************************************************************
237 * vlc_cond_timedwait: wait until condition completion or expiration
238 *****************************************************************************
239 * Returns 0 if object signaled, an error code in case of timeout or error.
240 *****************************************************************************/
241 #define vlc_cond_timedwait( P_COND, P_MUTEX, DEADLINE ) \
242 __vlc_cond_timedwait( __FILE__, __LINE__, P_COND, P_MUTEX, DEADLINE )
244 static inline int __vlc_cond_timedwait( const char * psz_file, int i_line,
245 vlc_cond_t *p_condvar,
246 vlc_mutex_t *p_mutex,
249 #if defined( UNDER_CE )
250 mtime_t delay_ms = (deadline - mdate())/1000;
256 p_condvar->i_waiting_threads++;
257 LeaveCriticalSection( &p_mutex->csection );
258 result = WaitForSingleObject( p_condvar->event, delay_ms );
259 p_condvar->i_waiting_threads--;
261 /* Reacquire the mutex before returning. */
262 vlc_mutex_lock( p_mutex );
264 if(result == WAIT_TIMEOUT)
265 return ETIMEDOUT; /* this error is perfectly normal */
267 #elif defined( WIN32 )
268 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
272 mtime_t delay_ms = (deadline - mdate())/1000;
276 /* Increase our wait count */
277 p_condvar->i_waiting_threads++;
278 result = SignalObjectAndWait( *p_mutex, p_condvar->event,
280 p_condvar->i_waiting_threads--;
282 /* Reacquire the mutex before returning. */
283 vlc_mutex_lock( p_mutex );
284 if(result == WAIT_TIMEOUT)
285 return ETIMEDOUT; /* this error is perfectly normal */
287 #elif defined( HAVE_KERNEL_SCHEDULER_H )
288 # error Unimplemented
290 #elif defined(LIBVLC_USE_PTHREAD)
291 lldiv_t d = lldiv( deadline, 1000000 );
292 struct timespec ts = { d.quot, d.rem * 1000 };
294 int val = pthread_cond_timedwait (p_condvar, p_mutex, &ts);
295 if (val == ETIMEDOUT)
296 return ETIMEDOUT; /* this error is perfectly normal */
297 VLC_THREAD_ASSERT ("timed-waiting on condition");
304 /*****************************************************************************
305 * vlc_cond_destroy: destroy a condition
306 *****************************************************************************/
307 #define vlc_cond_destroy( P_COND ) \
308 __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
310 /*****************************************************************************
311 * vlc_threadvar_create: create a thread-local variable
312 *****************************************************************************/
313 #define vlc_threadvar_create( PTHIS, P_TLS ) \
314 __vlc_threadvar_create( P_TLS )
316 /*****************************************************************************
317 * vlc_threadvar_set: create: set the value of a thread-local variable
318 *****************************************************************************/
319 static inline int vlc_threadvar_set( vlc_threadvar_t * p_tls, void *p_value )
323 #if defined( HAVE_KERNEL_SCHEDULER_H )
326 #elif defined( UNDER_CE ) || defined( WIN32 )
327 i_ret = TlsSetValue( *p_tls, p_value ) ? EINVAL : 0;
329 #elif defined(LIBVLC_USE_PTHREAD)
330 i_ret = pthread_setspecific( *p_tls, p_value );
337 /*****************************************************************************
338 * vlc_threadvar_get: create: get the value of a thread-local variable
339 *****************************************************************************/
340 static inline void* vlc_threadvar_get( vlc_threadvar_t * p_tls )
344 #if defined( HAVE_KERNEL_SCHEDULER_H )
347 #elif defined( UNDER_CE ) || defined( WIN32 )
348 p_ret = TlsGetValue( *p_tls );
350 #elif defined(LIBVLC_USE_PTHREAD)
351 p_ret = pthread_getspecific( *p_tls );
358 # if defined (_POSIX_SPIN_LOCKS) && ((_POSIX_SPIN_LOCKS - 0) > 0)
361 pthread_spinlock_t spin;
365 * Initializes a spinlock.
367 static inline int vlc_spin_init (vlc_spinlock_t *spin)
369 return pthread_spin_init (&spin->spin, PTHREAD_PROCESS_PRIVATE);
373 * Acquires a spinlock.
375 static inline void vlc_spin_lock (vlc_spinlock_t *spin)
377 int val = pthread_spin_lock (&spin->spin);
383 * Releases a spinlock.
385 static inline void vlc_spin_unlock (vlc_spinlock_t *spin)
387 int val = pthread_spin_unlock (&spin->spin);
393 * Deinitializes a spinlock.
395 static inline void vlc_spin_destroy (vlc_spinlock_t *spin)
397 int val = pthread_spin_destroy (&spin->spin);
402 #elif defined( WIN32 )
404 typedef CRITICAL_SECTION vlc_spinlock_t;
407 * Initializes a spinlock.
409 static inline int vlc_spin_init (vlc_spinlock_t *spin)
411 return !InitializeCriticalSectionAndSpinCount(spin, 4000);
415 * Acquires a spinlock.
417 static inline void vlc_spin_lock (vlc_spinlock_t *spin)
419 EnterCriticalSection(spin);
423 * Releases a spinlock.
425 static inline void vlc_spin_unlock (vlc_spinlock_t *spin)
427 LeaveCriticalSection(spin);
431 * Deinitializes a spinlock.
433 static inline void vlc_spin_destroy (vlc_spinlock_t *spin)
435 DeleteCriticalSection(spin);
441 /* Fallback to plain mutexes if spinlocks are not available */
442 typedef vlc_mutex_t vlc_spinlock_t;
444 static inline int vlc_spin_init (vlc_spinlock_t *spin)
446 return vlc_mutex_init (spin);
449 # define vlc_spin_lock vlc_mutex_lock
450 # define vlc_spin_unlock vlc_mutex_unlock
451 # define vlc_spin_destroy vlc_mutex_destroy
454 /*****************************************************************************
455 * vlc_thread_create: create a thread
456 *****************************************************************************/
457 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT ) \
458 __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, PRIORITY, WAIT )
460 /*****************************************************************************
461 * vlc_thread_set_priority: set the priority of the calling thread
462 *****************************************************************************/
463 #define vlc_thread_set_priority( P_THIS, PRIORITY ) \
464 __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
466 /*****************************************************************************
467 * vlc_thread_ready: tell the parent thread we were successfully spawned
468 *****************************************************************************/
469 #define vlc_thread_ready( P_THIS ) \
470 __vlc_thread_ready( VLC_OBJECT(P_THIS) )
472 /*****************************************************************************
473 * vlc_thread_join: wait until a thread exits
474 *****************************************************************************/
475 #define vlc_thread_join( P_THIS ) \
476 __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )