Meaningful Typo
[vlc.git] / include / vlc_threads_funcs.h
bloba3152589982094f5be71ca6d6b9e411ea3784478
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
6 * $Id$
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
30 #endif
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. */
37 #endif
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_threads_init: initialize threads system
55 *****************************************************************************/
56 #define vlc_threads_init( P_THIS ) \
57 __vlc_threads_init( VLC_OBJECT(P_THIS) )
59 /*****************************************************************************
60 * vlc_threads_end: deinitialize threads system
61 *****************************************************************************/
62 #define vlc_threads_end( P_THIS ) \
63 __vlc_threads_end( VLC_OBJECT(P_THIS) )
65 /*****************************************************************************
66 * vlc_mutex_init: initialize a mutex
67 *****************************************************************************/
68 #define vlc_mutex_init( P_THIS, P_MUTEX ) \
69 __vlc_mutex_init( P_MUTEX )
71 /*****************************************************************************
72 * vlc_mutex_init: initialize a recursive mutex (Don't use it)
73 *****************************************************************************/
74 #define vlc_mutex_init_recursive( P_THIS, P_MUTEX ) \
75 __vlc_mutex_init_recursive( P_MUTEX )
77 /*****************************************************************************
78 * vlc_mutex_lock: lock a mutex
79 *****************************************************************************/
80 #define vlc_mutex_lock( P_MUTEX ) \
81 __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
83 #if defined(LIBVLC_USE_PTHREAD)
84 VLC_EXPORT(void, vlc_pthread_fatal, (const char *action, int error, const char *file, unsigned line));
86 # define VLC_THREAD_ASSERT( action ) \
87 if (val) \
88 vlc_pthread_fatal (action, val, psz_file, i_line)
89 #else
90 # define VLC_THREAD_ASSERT (void)0
91 #endif
93 static inline void __vlc_mutex_lock( const char * psz_file, int i_line,
94 vlc_mutex_t * p_mutex )
96 #if defined( UNDER_CE )
97 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
99 EnterCriticalSection( &p_mutex->csection );
101 #elif defined( WIN32 )
102 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
104 WaitForSingleObject( *p_mutex, INFINITE );
106 #elif defined( HAVE_KERNEL_SCHEDULER_H )
107 acquire_sem( p_mutex->lock );
109 #elif defined(LIBVLC_USE_PTHREAD)
110 # define vlc_assert_locked( m ) \
111 assert (pthread_mutex_lock (m) == EDEADLK)
112 int val = pthread_mutex_lock( p_mutex );
113 VLC_THREAD_ASSERT ("locking mutex");
115 #endif
118 #ifndef vlc_assert_locked
119 # define vlc_assert_locked( m ) (void)0
120 #endif
122 /*****************************************************************************
123 * vlc_mutex_unlock: unlock a mutex
124 *****************************************************************************/
125 #define vlc_mutex_unlock( P_MUTEX ) \
126 __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
128 static inline void __vlc_mutex_unlock( const char * psz_file, int i_line,
129 vlc_mutex_t *p_mutex )
131 #if defined( UNDER_CE )
132 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
134 LeaveCriticalSection( &p_mutex->csection );
136 #elif defined( WIN32 )
137 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
139 ReleaseMutex( *p_mutex );
141 #elif defined( HAVE_KERNEL_SCHEDULER_H )
142 release_sem( p_mutex->lock );
144 #elif defined(LIBVLC_USE_PTHREAD)
145 int val = pthread_mutex_unlock( p_mutex );
146 VLC_THREAD_ASSERT ("unlocking mutex");
148 #endif
151 /*****************************************************************************
152 * vlc_mutex_destroy: destroy a mutex
153 *****************************************************************************/
154 #define vlc_mutex_destroy( P_MUTEX ) \
155 __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
157 /*****************************************************************************
158 * vlc_cond_init: initialize a condition
159 *****************************************************************************/
160 #define vlc_cond_init( P_THIS, P_COND ) \
161 __vlc_cond_init( P_COND )
163 /*****************************************************************************
164 * vlc_cond_signal: start a thread on condition completion
165 *****************************************************************************/
166 #define vlc_cond_signal( P_COND ) \
167 __vlc_cond_signal( __FILE__, __LINE__, P_COND )
169 static inline void __vlc_cond_signal( const char * psz_file, int i_line,
170 vlc_cond_t *p_condvar )
172 #if defined( UNDER_CE ) || defined( WIN32 )
173 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
175 /* Release one waiting thread if one is available. */
176 /* For this trick to work properly, the vlc_cond_signal must be surrounded
177 * by a mutex. This will prevent another thread from stealing the signal */
178 /* PulseEvent() only works if none of the waiting threads is suspended.
179 * This is particularily problematic under a debug session.
180 * as documented in http://support.microsoft.com/kb/q173260/ */
181 PulseEvent( p_condvar->event );
183 #elif defined( HAVE_KERNEL_SCHEDULER_H )
184 while( p_condvar->thread != -1 )
186 thread_info info;
187 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
188 return;
190 if( info.state != B_THREAD_SUSPENDED )
192 /* The waiting thread is not suspended so it could
193 * have been interrupted beetwen the unlock and the
194 * suspend_thread line. That is why we sleep a little
195 * before retesting p_condver->thread. */
196 snooze( 10000 );
198 else
200 /* Ok, we have to wake up that thread */
201 resume_thread( p_condvar->thread );
205 #elif defined(LIBVLC_USE_PTHREAD)
206 int val = pthread_cond_signal( p_condvar );
207 VLC_THREAD_ASSERT ("signaling condition variable");
209 #endif
212 /*****************************************************************************
213 * vlc_cond_wait: wait until condition completion
214 *****************************************************************************/
215 #define vlc_cond_wait( P_COND, P_MUTEX ) \
216 __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX )
218 static inline void __vlc_cond_wait( const char * psz_file, int i_line,
219 vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
221 #if defined( UNDER_CE )
222 p_condvar->i_waiting_threads++;
223 LeaveCriticalSection( &p_mutex->csection );
224 WaitForSingleObject( p_condvar->event, INFINITE );
225 p_condvar->i_waiting_threads--;
227 /* Reacquire the mutex before returning. */
228 vlc_mutex_lock( p_mutex );
230 #elif defined( WIN32 )
231 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
233 /* Increase our wait count */
234 p_condvar->i_waiting_threads++;
235 SignalObjectAndWait( *p_mutex, p_condvar->event, INFINITE, FALSE );
236 p_condvar->i_waiting_threads--;
238 /* Reacquire the mutex before returning. */
239 vlc_mutex_lock( p_mutex );
241 #elif defined( HAVE_KERNEL_SCHEDULER_H )
242 /* The p_condvar->thread var is initialized before the unlock because
243 * it enables to identify when the thread is interrupted beetwen the
244 * unlock line and the suspend_thread line */
245 p_condvar->thread = find_thread( NULL );
246 vlc_mutex_unlock( p_mutex );
247 suspend_thread( p_condvar->thread );
248 p_condvar->thread = -1;
250 vlc_mutex_lock( p_mutex );
252 #elif defined(LIBVLC_USE_PTHREAD)
253 int val = pthread_cond_wait( p_condvar, p_mutex );
254 VLC_THREAD_ASSERT ("waiting on condition");
256 #endif
260 /*****************************************************************************
261 * vlc_cond_timedwait: wait until condition completion or expiration
262 *****************************************************************************
263 * Returns 0 if object signaled, an error code in case of timeout or error.
264 *****************************************************************************/
265 #define vlc_cond_timedwait( P_COND, P_MUTEX, DEADLINE ) \
266 __vlc_cond_timedwait( __FILE__, __LINE__, P_COND, P_MUTEX, DEADLINE )
268 static inline int __vlc_cond_timedwait( const char * psz_file, int i_line,
269 vlc_cond_t *p_condvar,
270 vlc_mutex_t *p_mutex,
271 mtime_t deadline )
273 #if defined( UNDER_CE )
274 mtime_t delay_ms = (deadline - mdate())/1000;
276 DWORD result;
277 if( delay_ms < 0 )
278 delay_ms = 0;
280 p_condvar->i_waiting_threads++;
281 LeaveCriticalSection( &p_mutex->csection );
282 result = WaitForSingleObject( p_condvar->event, delay_ms );
283 p_condvar->i_waiting_threads--;
285 /* Reacquire the mutex before returning. */
286 vlc_mutex_lock( p_mutex );
288 if(result == WAIT_TIMEOUT)
289 return ETIMEDOUT; /* this error is perfectly normal */
291 #elif defined( WIN32 )
292 VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
294 DWORD result;
296 mtime_t delay_ms = (deadline - mdate())/1000;
297 if( delay_ms < 0 )
298 delay_ms = 0;
300 /* Increase our wait count */
301 p_condvar->i_waiting_threads++;
302 result = SignalObjectAndWait( *p_mutex, p_condvar->event,
303 delay_ms, FALSE );
304 p_condvar->i_waiting_threads--;
306 /* Reacquire the mutex before returning. */
307 vlc_mutex_lock( p_mutex );
308 if(result == WAIT_TIMEOUT)
309 return ETIMEDOUT; /* this error is perfectly normal */
311 #elif defined( HAVE_KERNEL_SCHEDULER_H )
312 # error Unimplemented
314 #elif defined(LIBVLC_USE_PTHREAD)
315 lldiv_t d = lldiv( deadline, 1000000 );
316 struct timespec ts = { d.quot, d.rem * 1000 };
318 int val = pthread_cond_timedwait (p_condvar, p_mutex, &ts);
319 if (val == ETIMEDOUT)
320 return ETIMEDOUT; /* this error is perfectly normal */
321 VLC_THREAD_ASSERT ("timed-waiting on condition");
323 #endif
325 return 0;
328 /*****************************************************************************
329 * vlc_cond_destroy: destroy a condition
330 *****************************************************************************/
331 #define vlc_cond_destroy( P_COND ) \
332 __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
334 /*****************************************************************************
335 * vlc_threadvar_create: create a thread-local variable
336 *****************************************************************************/
337 #define vlc_threadvar_create( PTHIS, P_TLS ) \
338 __vlc_threadvar_create( P_TLS )
340 /*****************************************************************************
341 * vlc_threadvar_set: create: set the value of a thread-local variable
342 *****************************************************************************/
343 static inline int vlc_threadvar_set( vlc_threadvar_t * p_tls, void *p_value )
345 int i_ret;
347 #if defined( HAVE_KERNEL_SCHEDULER_H )
348 i_ret = EINVAL;
350 #elif defined( UNDER_CE ) || defined( WIN32 )
351 i_ret = TlsSetValue( *p_tls, p_value ) ? EINVAL : 0;
353 #elif defined(LIBVLC_USE_PTHREAD)
354 i_ret = pthread_setspecific( *p_tls, p_value );
356 #endif
358 return i_ret;
361 /*****************************************************************************
362 * vlc_threadvar_get: create: get the value of a thread-local variable
363 *****************************************************************************/
364 static inline void* vlc_threadvar_get( vlc_threadvar_t * p_tls )
366 void *p_ret;
368 #if defined( HAVE_KERNEL_SCHEDULER_H )
369 p_ret = NULL;
371 #elif defined( UNDER_CE ) || defined( WIN32 )
372 p_ret = TlsGetValue( *p_tls );
374 #elif defined(LIBVLC_USE_PTHREAD)
375 p_ret = pthread_getspecific( *p_tls );
377 #endif
379 return p_ret;
382 # if defined (_POSIX_SPIN_LOCKS) && ((_POSIX_SPIN_LOCKS - 0) > 0)
383 typedef struct
385 pthread_spinlock_t spin;
386 } vlc_spinlock_t;
389 * Initializes a spinlock.
391 static inline int vlc_spin_init (vlc_spinlock_t *spin)
393 return pthread_spin_init (&spin->spin, PTHREAD_PROCESS_PRIVATE);
397 * Acquires a spinlock.
399 static inline void vlc_spin_lock (vlc_spinlock_t *spin)
401 int val = pthread_spin_lock (&spin->spin);
402 assert (val == 0);
403 (void)val;
407 * Releases a spinlock.
409 static inline void vlc_spin_unlock (vlc_spinlock_t *spin)
411 int val = pthread_spin_unlock (&spin->spin);
412 assert (val == 0);
413 (void)val;
417 * Deinitializes a spinlock.
419 static inline void vlc_spin_destroy (vlc_spinlock_t *spin)
421 int val = pthread_spin_destroy (&spin->spin);
422 assert (val == 0);
423 (void)val;
426 #elif defined( WIN32 )
428 typedef CRITICAL_SECTION vlc_spinlock_t;
431 * Initializes a spinlock.
433 static inline int vlc_spin_init (vlc_spinlock_t *spin)
435 return !InitializeCriticalSectionAndSpinCount(spin, 4000);
439 * Acquires a spinlock.
441 static inline void vlc_spin_lock (vlc_spinlock_t *spin)
443 EnterCriticalSection(spin);
447 * Releases a spinlock.
449 static inline void vlc_spin_unlock (vlc_spinlock_t *spin)
451 LeaveCriticalSection(spin);
455 * Deinitializes a spinlock.
457 static inline void vlc_spin_destroy (vlc_spinlock_t *spin)
459 DeleteCriticalSection(spin);
463 #else
465 /* Fallback to plain mutexes if spinlocks are not available */
466 typedef vlc_mutex_t vlc_spinlock_t;
468 static inline int vlc_spin_init (vlc_spinlock_t *spin)
470 return __vlc_mutex_init (spin);
473 # define vlc_spin_lock vlc_mutex_lock
474 # define vlc_spin_unlock vlc_mutex_unlock
475 # define vlc_spin_destroy vlc_mutex_destroy
476 #endif
478 /*****************************************************************************
479 * vlc_thread_create: create a thread
480 *****************************************************************************/
481 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT ) \
482 __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, PRIORITY, WAIT )
484 /*****************************************************************************
485 * vlc_thread_set_priority: set the priority of the calling thread
486 *****************************************************************************/
487 #define vlc_thread_set_priority( P_THIS, PRIORITY ) \
488 __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
490 /*****************************************************************************
491 * vlc_thread_ready: tell the parent thread we were successfully spawned
492 *****************************************************************************/
493 #define vlc_thread_ready( P_THIS ) \
494 __vlc_thread_ready( VLC_OBJECT(P_THIS) )
496 /*****************************************************************************
497 * vlc_thread_join: wait until a thread exits
498 *****************************************************************************/
499 #define vlc_thread_join( P_THIS ) \
500 __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )
502 #endif