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, 2002 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 /*****************************************************************************
29 * Function definitions
30 *****************************************************************************/
31 VLC_EXPORT( int, __vlc_threads_init, ( vlc_object_t * ) );
32 VLC_EXPORT( int, __vlc_threads_end, ( vlc_object_t * ) );
33 VLC_EXPORT( int, __vlc_mutex_init, ( vlc_object_t *, vlc_mutex_t * ) );
34 VLC_EXPORT( int, __vlc_mutex_destroy, ( char *, int, vlc_mutex_t * ) );
35 VLC_EXPORT( int, __vlc_cond_init, ( vlc_object_t *, vlc_cond_t * ) );
36 VLC_EXPORT( int, __vlc_cond_destroy, ( char *, int, vlc_cond_t * ) );
37 VLC_EXPORT( int, __vlc_thread_create, ( vlc_object_t *, char *, int, char *, void * ( * ) ( void * ), int, vlc_bool_t ) );
38 VLC_EXPORT( int, __vlc_thread_set_priority, ( vlc_object_t *, char *, int, int ) );
39 VLC_EXPORT( void, __vlc_thread_ready, ( vlc_object_t * ) );
40 VLC_EXPORT( void, __vlc_thread_join, ( vlc_object_t *, char *, int ) );
43 /*****************************************************************************
44 * vlc_threads_init: initialize threads system
45 *****************************************************************************/
46 #define vlc_threads_init( P_THIS ) \
47 __vlc_threads_init( VLC_OBJECT(P_THIS) )
49 /*****************************************************************************
50 * vlc_threads_end: deinitialize threads system
51 *****************************************************************************/
52 #define vlc_threads_end( P_THIS ) \
53 __vlc_threads_end( VLC_OBJECT(P_THIS) )
55 /*****************************************************************************
56 * vlc_mutex_init: initialize a mutex
57 *****************************************************************************/
58 #define vlc_mutex_init( P_THIS, P_MUTEX ) \
59 __vlc_mutex_init( VLC_OBJECT(P_THIS), P_MUTEX )
61 /*****************************************************************************
62 * vlc_mutex_lock: lock a mutex
63 *****************************************************************************/
64 #define vlc_mutex_lock( P_MUTEX ) \
65 __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
67 static inline int __vlc_mutex_lock( const char * psz_file, int i_line,
68 vlc_mutex_t * p_mutex )
71 /* In case of error : */
73 const char * psz_error = "";
75 #if defined( PTH_INIT_IN_PTH_H )
76 i_result = ( pth_mutex_acquire( &p_mutex->mutex, FALSE, NULL ) == FALSE );
78 #elif defined( ST_INIT_IN_ST_H )
79 i_result = st_mutex_lock( p_mutex->mutex );
81 #elif defined( UNDER_CE )
82 EnterCriticalSection( &p_mutex->csection );
85 #elif defined( WIN32 )
88 WaitForSingleObject( p_mutex->mutex, INFINITE );
92 EnterCriticalSection( &p_mutex->csection );
96 #elif defined( HAVE_KERNEL_SCHEDULER_H )
99 i_result = B_BAD_VALUE;
101 else if( p_mutex->init < 2000 )
103 i_result = B_NO_INIT;
107 i_result = acquire_sem( p_mutex->lock );
110 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
111 i_result = pthread_mutex_lock( &p_mutex->mutex );
114 i_thread = (int)pthread_self();
115 psz_error = strerror(i_result);
118 #elif defined( HAVE_CTHREADS_H )
119 mutex_lock( p_mutex->mutex );
126 msg_Err( p_mutex->p_this,
127 "thread %u: mutex_lock failed at %s:%d (%d:%s)",
128 i_thread, psz_file, i_line, i_result, psz_error );
133 /*****************************************************************************
134 * vlc_mutex_unlock: unlock a mutex
135 *****************************************************************************/
136 #define vlc_mutex_unlock( P_MUTEX ) \
137 __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
139 static inline int __vlc_mutex_unlock( const char * psz_file, int i_line,
140 vlc_mutex_t *p_mutex )
143 /* In case of error : */
145 const char * psz_error = "";
147 #if defined( PTH_INIT_IN_PTH_H )
148 i_result = ( pth_mutex_release( &p_mutex->mutex ) == FALSE );
150 #elif defined( ST_INIT_IN_ST_H )
151 i_result = st_mutex_unlock( p_mutex->mutex );
153 #elif defined( UNDER_CE )
154 LeaveCriticalSection( &p_mutex->csection );
157 #elif defined( WIN32 )
160 ReleaseMutex( p_mutex->mutex );
164 LeaveCriticalSection( &p_mutex->csection );
168 #elif defined( HAVE_KERNEL_SCHEDULER_H )
169 if( p_mutex == NULL )
171 i_result = B_BAD_VALUE;
173 else if( p_mutex->init < 2000 )
175 i_result = B_NO_INIT;
179 release_sem( p_mutex->lock );
183 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
184 i_result = pthread_mutex_unlock( &p_mutex->mutex );
187 i_thread = (int)pthread_self();
188 psz_error = strerror(i_result);
191 #elif defined( HAVE_CTHREADS_H )
192 mutex_unlock( p_mutex );
199 msg_Err( p_mutex->p_this,
200 "thread %u: mutex_unlock failed at %s:%d (%d:%s)",
201 i_thread, psz_file, i_line, i_result, psz_error );
207 /*****************************************************************************
208 * vlc_mutex_destroy: destroy a mutex
209 *****************************************************************************/
210 #define vlc_mutex_destroy( P_MUTEX ) \
211 __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
213 /*****************************************************************************
214 * vlc_cond_init: initialize a condition
215 *****************************************************************************/
216 #define vlc_cond_init( P_THIS, P_COND ) \
217 __vlc_cond_init( VLC_OBJECT(P_THIS), P_COND )
219 /*****************************************************************************
220 * vlc_cond_signal: start a thread on condition completion
221 *****************************************************************************/
222 #define vlc_cond_signal( P_COND ) \
223 __vlc_cond_signal( __FILE__, __LINE__, P_COND )
225 static inline int __vlc_cond_signal( const char * psz_file, int i_line,
226 vlc_cond_t *p_condvar )
229 /* In case of error : */
231 const char * psz_error = "";
233 #if defined( PTH_INIT_IN_PTH_H )
234 i_result = ( pth_cond_notify( &p_condvar->cond, FALSE ) == FALSE );
236 #elif defined( ST_INIT_IN_ST_H )
237 i_result = st_cond_signal( p_condvar->cond );
239 #elif defined( UNDER_CE )
240 PulseEvent( p_condvar->event );
243 #elif defined( WIN32 )
244 /* Release one waiting thread if one is available. */
245 /* For this trick to work properly, the vlc_cond_signal must be surrounded
246 * by a mutex. This will prevent another thread from stealing the signal */
247 if( !p_condvar->semaphore )
249 PulseEvent( p_condvar->event );
251 else if( p_condvar->i_win9x_cv == 1 )
253 /* Wait for the gate to be open */
254 WaitForSingleObject( p_condvar->event, INFINITE );
256 if( p_condvar->i_waiting_threads )
258 /* Using a semaphore exposes us to a race condition. It is
259 * possible for another thread to start waiting on the semaphore
260 * just after we signaled it and thus steal the signal.
261 * We have to prevent new threads from entering the cond_wait(). */
262 ResetEvent( p_condvar->event );
264 /* A semaphore is used here because Win9x doesn't have
265 * SignalObjectAndWait() and thus a race condition exists
266 * during the time we release the mutex and the time we start
267 * waiting on the event (more precisely, the signal can sometimes
268 * be missed by the waiting thread if we use PulseEvent()). */
269 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
274 if( p_condvar->i_waiting_threads )
276 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
278 /* Wait for the last thread to be awakened */
279 WaitForSingleObject( p_condvar->event, INFINITE );
284 #elif defined( HAVE_KERNEL_SCHEDULER_H )
285 if( p_condvar == NULL )
287 i_result = B_BAD_VALUE;
289 else if( p_condvar->init < 2000 )
291 i_result = B_NO_INIT;
295 while( p_condvar->thread != -1 )
298 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
303 if( info.state != B_THREAD_SUSPENDED )
305 /* The waiting thread is not suspended so it could
306 * have been interrupted beetwen the unlock and the
307 * suspend_thread line. That is why we sleep a little
308 * before retesting p_condver->thread. */
313 /* Ok, we have to wake up that thread */
314 resume_thread( p_condvar->thread );
321 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
322 i_result = pthread_cond_signal( &p_condvar->cond );
325 i_thread = (int)pthread_self();
326 psz_error = strerror(i_result);
329 #elif defined( HAVE_CTHREADS_H )
330 /* condition_signal() */
331 if ( p_condvar->queue.head || p_condvar->implications )
333 cond_signal( (condition_t)p_condvar );
341 msg_Err( p_condvar->p_this,
342 "thread %u: cond_signal failed at %s:%d (%d:%s)",
343 i_thread, psz_file, i_line, i_result, psz_error );
349 /*****************************************************************************
350 * vlc_cond_wait: wait until condition completion
351 *****************************************************************************/
352 #define vlc_cond_wait( P_COND, P_MUTEX ) \
353 __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX )
355 static inline int __vlc_cond_wait( const char * psz_file, int i_line,
356 vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
359 /* In case of error : */
361 const char * psz_error = "";
363 #if defined( PTH_INIT_IN_PTH_H )
364 i_result = ( pth_cond_await( &p_condvar->cond, &p_mutex->mutex, NULL )
367 #elif defined( ST_INIT_IN_ST_H )
368 st_mutex_unlock( p_mutex->mutex );
369 i_result = st_cond_wait( p_condvar->cond );
370 st_mutex_lock( p_mutex->mutex );
372 #elif defined( UNDER_CE )
373 p_condvar->i_waiting_threads++;
374 LeaveCriticalSection( &p_mutex->csection );
375 WaitForSingleObject( p_condvar->event, INFINITE );
376 p_condvar->i_waiting_threads--;
378 /* Reacquire the mutex before returning. */
379 vlc_mutex_lock( p_mutex );
383 #elif defined( WIN32 )
384 if( !p_condvar->semaphore )
386 /* Increase our wait count */
387 p_condvar->i_waiting_threads++;
391 /* It is only possible to atomically release the mutex and
392 * initiate the waiting on WinNT/2K/XP. Win9x doesn't have
393 * SignalObjectAndWait(). */
394 p_condvar->SignalObjectAndWait( p_mutex->mutex,
400 LeaveCriticalSection( &p_mutex->csection );
401 WaitForSingleObject( p_condvar->event, INFINITE );
404 p_condvar->i_waiting_threads--;
406 else if( p_condvar->i_win9x_cv == 1 )
408 int i_waiting_threads;
410 /* Wait for the gate to be open */
411 WaitForSingleObject( p_condvar->event, INFINITE );
413 /* Increase our wait count */
414 p_condvar->i_waiting_threads++;
416 LeaveCriticalSection( &p_mutex->csection );
417 WaitForSingleObject( p_condvar->semaphore, INFINITE );
419 /* Decrement and test must be atomic */
420 EnterCriticalSection( &p_condvar->csection );
422 /* Decrease our wait count */
423 i_waiting_threads = --p_condvar->i_waiting_threads;
425 LeaveCriticalSection( &p_condvar->csection );
427 /* Reopen the gate if we were the last waiting thread */
428 if( !i_waiting_threads )
429 SetEvent( p_condvar->event );
433 int i_waiting_threads;
435 /* Increase our wait count */
436 p_condvar->i_waiting_threads++;
438 LeaveCriticalSection( &p_mutex->csection );
439 WaitForSingleObject( p_condvar->semaphore, INFINITE );
441 /* Decrement and test must be atomic */
442 EnterCriticalSection( &p_condvar->csection );
444 /* Decrease our wait count */
445 i_waiting_threads = --p_condvar->i_waiting_threads;
447 LeaveCriticalSection( &p_condvar->csection );
449 /* Signal that the last waiting thread just went through */
450 if( !i_waiting_threads )
451 SetEvent( p_condvar->event );
454 /* Reacquire the mutex before returning. */
455 vlc_mutex_lock( p_mutex );
459 #elif defined( HAVE_KERNEL_SCHEDULER_H )
460 if( p_condvar == NULL )
462 i_result = B_BAD_VALUE;
464 else if( p_mutex == NULL )
466 i_result = B_BAD_VALUE;
468 else if( p_condvar->init < 2000 )
470 i_result = B_NO_INIT;
473 /* The p_condvar->thread var is initialized before the unlock because
474 * it enables to identify when the thread is interrupted beetwen the
475 * unlock line and the suspend_thread line */
476 p_condvar->thread = find_thread( NULL );
477 vlc_mutex_unlock( p_mutex );
478 suspend_thread( p_condvar->thread );
479 p_condvar->thread = -1;
481 vlc_mutex_lock( p_mutex );
484 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
487 /* In debug mode, timeout */
489 struct timespec timeout;
491 gettimeofday( &now, NULL );
492 timeout.tv_sec = now.tv_sec + THREAD_COND_TIMEOUT;
493 timeout.tv_nsec = now.tv_usec * 1000;
495 i_result = pthread_cond_timedwait( &p_condvar->cond, &p_mutex->mutex,
498 if( i_result == ETIMEDOUT )
500 /* People keep pissing me off with this. --Meuuh */
501 msg_Dbg( p_condvar->p_this,
502 "thread %u: secret message triggered "
503 "at %s:%d (%s)", (int)pthread_self(),
504 psz_file, i_line, strerror(i_result) );
506 i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
510 i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
515 i_thread = (int)pthread_self();
516 psz_error = strerror(i_result);
519 #elif defined( HAVE_CTHREADS_H )
520 condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
527 msg_Err( p_condvar->p_this,
528 "thread %u: cond_wait failed at %s:%d (%d:%s)",
529 i_thread, psz_file, i_line, i_result, psz_error );
535 /*****************************************************************************
536 * vlc_cond_destroy: destroy a condition
537 *****************************************************************************/
538 #define vlc_cond_destroy( P_COND ) \
539 __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
541 /*****************************************************************************
542 * vlc_thread_create: create a thread
543 *****************************************************************************/
544 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT ) \
545 __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, PRIORITY, WAIT )
547 /*****************************************************************************
548 * vlc_thread_set_priority: set the priority of the calling thread
549 *****************************************************************************/
550 #define vlc_thread_set_priority( P_THIS, PRIORITY ) \
551 __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
553 /*****************************************************************************
554 * vlc_thread_ready: tell the parent thread we were successfully spawned
555 *****************************************************************************/
556 #define vlc_thread_ready( P_THIS ) \
557 __vlc_thread_ready( VLC_OBJECT(P_THIS) )
559 /*****************************************************************************
560 * vlc_thread_join: wait until a thread exits
561 *****************************************************************************/
562 #define vlc_thread_join( P_THIS ) \
563 __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )