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_init: initialize a mutex
55 *****************************************************************************/
56 #define vlc_mutex_init( P_MUTEX ) \
57 __vlc_mutex_init( P_MUTEX )
59 /*****************************************************************************
60 * vlc_mutex_init: initialize a recursive mutex (Don't use it)
61 *****************************************************************************/
62 #define vlc_mutex_init_recursive( P_THIS, P_MUTEX ) \
63 __vlc_mutex_init_recursive( P_MUTEX )
65 /*****************************************************************************
66 * vlc_mutex_lock: lock a mutex
67 *****************************************************************************/
68 #define vlc_mutex_lock( P_MUTEX ) \
69 __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
71 #if defined(LIBVLC_USE_PTHREAD)
72 VLC_EXPORT(void, vlc_pthread_fatal, (const char *action, int error, const char *file, unsigned line));
74 # define VLC_THREAD_ASSERT( action ) \
76 vlc_pthread_fatal (action, val, psz_file, i_line)
78 # define VLC_THREAD_ASSERT (void)0
81 static inline void __vlc_mutex_lock( const char * psz_file, int i_line,
82 vlc_mutex_t * p_mutex )
84 #if defined( UNDER_CE )
85 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
87 EnterCriticalSection( &p_mutex->csection );
89 #elif defined( WIN32 )
90 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
92 WaitForSingleObject( *p_mutex, INFINITE );
94 #elif defined( HAVE_KERNEL_SCHEDULER_H )
95 acquire_sem( p_mutex->lock );
97 #elif defined(LIBVLC_USE_PTHREAD)
98 # define vlc_assert_locked( m ) \
99 assert (pthread_mutex_lock (m) == EDEADLK)
100 int val = pthread_mutex_lock( p_mutex );
101 VLC_THREAD_ASSERT ("locking mutex");
106 #ifndef vlc_assert_locked
107 # define vlc_assert_locked( m ) (void)0
110 /*****************************************************************************
111 * vlc_mutex_unlock: unlock a mutex
112 *****************************************************************************/
113 #define vlc_mutex_unlock( P_MUTEX ) \
114 __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
116 static inline void __vlc_mutex_unlock( const char * psz_file, int i_line,
117 vlc_mutex_t *p_mutex )
119 #if defined( UNDER_CE )
120 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
122 LeaveCriticalSection( &p_mutex->csection );
124 #elif defined( WIN32 )
125 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
127 ReleaseMutex( *p_mutex );
129 #elif defined( HAVE_KERNEL_SCHEDULER_H )
130 release_sem( p_mutex->lock );
132 #elif defined(LIBVLC_USE_PTHREAD)
133 int val = pthread_mutex_unlock( p_mutex );
134 VLC_THREAD_ASSERT ("unlocking mutex");
139 /*****************************************************************************
140 * vlc_mutex_destroy: destroy a mutex
141 *****************************************************************************/
142 #define vlc_mutex_destroy( P_MUTEX ) \
143 __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
145 /*****************************************************************************
146 * vlc_cond_init: initialize a condition
147 *****************************************************************************/
148 #define vlc_cond_init( P_THIS, P_COND ) \
149 __vlc_cond_init( P_COND )
151 /*****************************************************************************
152 * vlc_cond_signal: start a thread on condition completion
153 *****************************************************************************/
154 #define vlc_cond_signal( P_COND ) \
155 __vlc_cond_signal( __FILE__, __LINE__, P_COND )
157 static inline void __vlc_cond_signal( const char * psz_file, int i_line,
158 vlc_cond_t *p_condvar )
160 #if defined( UNDER_CE ) || defined( WIN32 )
161 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
163 /* Release one waiting thread if one is available. */
164 /* For this trick to work properly, the vlc_cond_signal must be surrounded
165 * by a mutex. This will prevent another thread from stealing the signal */
166 /* PulseEvent() only works if none of the waiting threads is suspended.
167 * This is particularily problematic under a debug session.
168 * as documented in http://support.microsoft.com/kb/q173260/ */
169 PulseEvent( p_condvar->event );
171 #elif defined( HAVE_KERNEL_SCHEDULER_H )
172 while( p_condvar->thread != -1 )
175 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
178 if( info.state != B_THREAD_SUSPENDED )
180 /* The waiting thread is not suspended so it could
181 * have been interrupted beetwen the unlock and the
182 * suspend_thread line. That is why we sleep a little
183 * before retesting p_condver->thread. */
188 /* Ok, we have to wake up that thread */
189 resume_thread( p_condvar->thread );
193 #elif defined(LIBVLC_USE_PTHREAD)
194 int val = pthread_cond_signal( p_condvar );
195 VLC_THREAD_ASSERT ("signaling condition variable");
200 /*****************************************************************************
201 * vlc_cond_wait: wait until condition completion
202 *****************************************************************************/
203 #define vlc_cond_wait( P_COND, P_MUTEX ) \
204 __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX )
206 static inline void __vlc_cond_wait( const char * psz_file, int i_line,
207 vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
209 #if defined( UNDER_CE )
210 p_condvar->i_waiting_threads++;
211 LeaveCriticalSection( &p_mutex->csection );
212 WaitForSingleObject( p_condvar->event, INFINITE );
213 p_condvar->i_waiting_threads--;
215 /* Reacquire the mutex before returning. */
216 vlc_mutex_lock( p_mutex );
218 #elif defined( WIN32 )
219 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
221 /* Increase our wait count */
222 p_condvar->i_waiting_threads++;
223 SignalObjectAndWait( *p_mutex, p_condvar->event, INFINITE, FALSE );
224 p_condvar->i_waiting_threads--;
226 /* Reacquire the mutex before returning. */
227 vlc_mutex_lock( p_mutex );
229 #elif defined( HAVE_KERNEL_SCHEDULER_H )
230 /* The p_condvar->thread var is initialized before the unlock because
231 * it enables to identify when the thread is interrupted beetwen the
232 * unlock line and the suspend_thread line */
233 p_condvar->thread = find_thread( NULL );
234 vlc_mutex_unlock( p_mutex );
235 suspend_thread( p_condvar->thread );
236 p_condvar->thread = -1;
238 vlc_mutex_lock( p_mutex );
240 #elif defined(LIBVLC_USE_PTHREAD)
241 int val = pthread_cond_wait( p_condvar, p_mutex );
242 VLC_THREAD_ASSERT ("waiting on condition");
248 /*****************************************************************************
249 * vlc_cond_timedwait: wait until condition completion or expiration
250 *****************************************************************************
251 * Returns 0 if object signaled, an error code in case of timeout or error.
252 *****************************************************************************/
253 #define vlc_cond_timedwait( P_COND, P_MUTEX, DEADLINE ) \
254 __vlc_cond_timedwait( __FILE__, __LINE__, P_COND, P_MUTEX, DEADLINE )
256 static inline int __vlc_cond_timedwait( const char * psz_file, int i_line,
257 vlc_cond_t *p_condvar,
258 vlc_mutex_t *p_mutex,
261 #if defined( UNDER_CE )
262 mtime_t delay_ms = (deadline - mdate())/1000;
268 p_condvar->i_waiting_threads++;
269 LeaveCriticalSection( &p_mutex->csection );
270 result = WaitForSingleObject( p_condvar->event, delay_ms );
271 p_condvar->i_waiting_threads--;
273 /* Reacquire the mutex before returning. */
274 vlc_mutex_lock( p_mutex );
276 if(result == WAIT_TIMEOUT)
277 return ETIMEDOUT; /* this error is perfectly normal */
279 #elif defined( WIN32 )
280 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
284 mtime_t delay_ms = (deadline - mdate())/1000;
288 /* Increase our wait count */
289 p_condvar->i_waiting_threads++;
290 result = SignalObjectAndWait( *p_mutex, p_condvar->event,
292 p_condvar->i_waiting_threads--;
294 /* Reacquire the mutex before returning. */
295 vlc_mutex_lock( p_mutex );
296 if(result == WAIT_TIMEOUT)
297 return ETIMEDOUT; /* this error is perfectly normal */
299 #elif defined( HAVE_KERNEL_SCHEDULER_H )
300 # error Unimplemented
302 #elif defined(LIBVLC_USE_PTHREAD)
303 lldiv_t d = lldiv( deadline, 1000000 );
304 struct timespec ts = { d.quot, d.rem * 1000 };
306 int val = pthread_cond_timedwait (p_condvar, p_mutex, &ts);
307 if (val == ETIMEDOUT)
308 return ETIMEDOUT; /* this error is perfectly normal */
309 VLC_THREAD_ASSERT ("timed-waiting on condition");
316 /*****************************************************************************
317 * vlc_cond_destroy: destroy a condition
318 *****************************************************************************/
319 #define vlc_cond_destroy( P_COND ) \
320 __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
322 /*****************************************************************************
323 * vlc_threadvar_create: create a thread-local variable
324 *****************************************************************************/
325 #define vlc_threadvar_create( PTHIS, P_TLS ) \
326 __vlc_threadvar_create( P_TLS )
328 /*****************************************************************************
329 * vlc_threadvar_set: create: set the value of a thread-local variable
330 *****************************************************************************/
331 static inline int vlc_threadvar_set( vlc_threadvar_t * p_tls, void *p_value )
335 #if defined( HAVE_KERNEL_SCHEDULER_H )
338 #elif defined( UNDER_CE ) || defined( WIN32 )
339 i_ret = TlsSetValue( *p_tls, p_value ) ? EINVAL : 0;
341 #elif defined(LIBVLC_USE_PTHREAD)
342 i_ret = pthread_setspecific( *p_tls, p_value );
349 /*****************************************************************************
350 * vlc_threadvar_get: create: get the value of a thread-local variable
351 *****************************************************************************/
352 static inline void* vlc_threadvar_get( vlc_threadvar_t * p_tls )
356 #if defined( HAVE_KERNEL_SCHEDULER_H )
359 #elif defined( UNDER_CE ) || defined( WIN32 )
360 p_ret = TlsGetValue( *p_tls );
362 #elif defined(LIBVLC_USE_PTHREAD)
363 p_ret = pthread_getspecific( *p_tls );
370 # if defined (_POSIX_SPIN_LOCKS) && ((_POSIX_SPIN_LOCKS - 0) > 0)
373 pthread_spinlock_t spin;
377 * Initializes a spinlock.
379 static inline int vlc_spin_init (vlc_spinlock_t *spin)
381 return pthread_spin_init (&spin->spin, PTHREAD_PROCESS_PRIVATE);
385 * Acquires a spinlock.
387 static inline void vlc_spin_lock (vlc_spinlock_t *spin)
389 int val = pthread_spin_lock (&spin->spin);
395 * Releases a spinlock.
397 static inline void vlc_spin_unlock (vlc_spinlock_t *spin)
399 int val = pthread_spin_unlock (&spin->spin);
405 * Deinitializes a spinlock.
407 static inline void vlc_spin_destroy (vlc_spinlock_t *spin)
409 int val = pthread_spin_destroy (&spin->spin);
414 #elif defined( WIN32 )
416 typedef CRITICAL_SECTION vlc_spinlock_t;
419 * Initializes a spinlock.
421 static inline int vlc_spin_init (vlc_spinlock_t *spin)
423 return !InitializeCriticalSectionAndSpinCount(spin, 4000);
427 * Acquires a spinlock.
429 static inline void vlc_spin_lock (vlc_spinlock_t *spin)
431 EnterCriticalSection(spin);
435 * Releases a spinlock.
437 static inline void vlc_spin_unlock (vlc_spinlock_t *spin)
439 LeaveCriticalSection(spin);
443 * Deinitializes a spinlock.
445 static inline void vlc_spin_destroy (vlc_spinlock_t *spin)
447 DeleteCriticalSection(spin);
453 /* Fallback to plain mutexes if spinlocks are not available */
454 typedef vlc_mutex_t vlc_spinlock_t;
456 static inline int vlc_spin_init (vlc_spinlock_t *spin)
458 return __vlc_mutex_init (spin);
461 # define vlc_spin_lock vlc_mutex_lock
462 # define vlc_spin_unlock vlc_mutex_unlock
463 # define vlc_spin_destroy vlc_mutex_destroy
466 /*****************************************************************************
467 * vlc_thread_create: create a thread
468 *****************************************************************************/
469 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT ) \
470 __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, PRIORITY, WAIT )
472 /*****************************************************************************
473 * vlc_thread_set_priority: set the priority of the calling thread
474 *****************************************************************************/
475 #define vlc_thread_set_priority( P_THIS, PRIORITY ) \
476 __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
478 /*****************************************************************************
479 * vlc_thread_ready: tell the parent thread we were successfully spawned
480 *****************************************************************************/
481 #define vlc_thread_ready( P_THIS ) \
482 __vlc_thread_ready( VLC_OBJECT(P_THIS) )
484 /*****************************************************************************
485 * vlc_thread_join: wait until a thread exits
486 *****************************************************************************/
487 #define vlc_thread_join( P_THIS ) \
488 __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )