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 VideoLAN
6 * $Id: vlc_threads_funcs.h,v 1.3 2002/08/30 19:16:05 sam Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, 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( void, __vlc_thread_ready, ( vlc_object_t * ) );
39 VLC_EXPORT( void, __vlc_thread_join, ( vlc_object_t *, char *, int ) );
41 /*****************************************************************************
42 * vlc_threads_init: initialize threads system
43 *****************************************************************************/
44 #define vlc_threads_init( P_THIS ) \
45 __vlc_threads_init( VLC_OBJECT(P_THIS) )
47 /*****************************************************************************
48 * vlc_threads_end: deinitialize threads system
49 *****************************************************************************/
50 #define vlc_threads_end( P_THIS ) \
51 __vlc_threads_end( VLC_OBJECT(P_THIS) )
53 /*****************************************************************************
54 * vlc_mutex_init: initialize a mutex
55 *****************************************************************************/
56 #define vlc_mutex_init( P_THIS, P_MUTEX ) \
57 __vlc_mutex_init( VLC_OBJECT(P_THIS), P_MUTEX )
59 /*****************************************************************************
60 * vlc_mutex_lock: lock a mutex
61 *****************************************************************************/
62 #define vlc_mutex_lock( P_MUTEX ) \
63 __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
65 static inline int __vlc_mutex_lock( char * psz_file, int i_line,
66 vlc_mutex_t * p_mutex )
69 /* In case of error : */
71 const char * psz_error = "";
73 #if defined( PTH_INIT_IN_PTH_H )
74 i_result = pth_mutex_acquire( &p_mutex->mutex, TRUE, NULL );
76 #elif defined( ST_INIT_IN_ST_H )
77 i_result = st_mutex_lock( p_mutex->mutex );
79 #elif defined( WIN32 )
82 WaitForSingleObject( p_mutex->mutex, INFINITE );
86 EnterCriticalSection( &p_mutex->csection );
90 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
91 i_result = pthread_mutex_lock( &p_mutex->mutex );
94 i_thread = (int)pthread_self();
95 psz_error = strerror(i_result);
98 #elif defined( HAVE_CTHREADS_H )
99 mutex_lock( p_mutex->mutex );
102 #elif defined( HAVE_KERNEL_SCHEDULER_H )
103 if( p_mutex == NULL )
105 i_result = B_BAD_VALUE;
107 else if( p_mutex->init < 2000 )
109 i_result = B_NO_INIT;
113 i_result = acquire_sem( p_mutex->lock );
119 msg_Err( p_mutex->p_this,
120 "thread %d: mutex_lock failed at %s:%d (%d:%s)",
121 i_thread, psz_file, i_line, i_result, psz_error );
126 /*****************************************************************************
127 * vlc_mutex_unlock: unlock a mutex
128 *****************************************************************************/
129 #define vlc_mutex_unlock( P_MUTEX ) \
130 __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
132 static inline int __vlc_mutex_unlock( char * psz_file, int i_line,
133 vlc_mutex_t *p_mutex )
136 /* In case of error : */
138 const char * psz_error = "";
140 #if defined( PTH_INIT_IN_PTH_H )
141 i_result = pth_mutex_release( &p_mutex->mutex );
143 #elif defined( ST_INIT_IN_ST_H )
144 i_result = st_mutex_unlock( p_mutex->mutex );
146 #elif defined( WIN32 )
149 ReleaseMutex( p_mutex->mutex );
153 LeaveCriticalSection( &p_mutex->csection );
157 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
158 i_result = pthread_mutex_unlock( &p_mutex->mutex );
161 i_thread = (int)pthread_self();
162 psz_error = strerror(i_result);
165 #elif defined( HAVE_CTHREADS_H )
166 mutex_unlock( p_mutex );
169 #elif defined( HAVE_KERNEL_SCHEDULER_H )
170 if( p_mutex == NULL )
172 i_result = B_BAD_VALUE;
174 else if( p_mutex->init < 2000 )
176 i_result = B_NO_INIT;
180 release_sem( p_mutex->lock );
187 msg_Err( p_mutex->p_this,
188 "thread %d: mutex_unlock failed at %s:%d (%d:%s)",
189 i_thread, psz_file, i_line, i_result, psz_error );
195 /*****************************************************************************
196 * vlc_mutex_destroy: destroy a mutex
197 *****************************************************************************/
198 #define vlc_mutex_destroy( P_MUTEX ) \
199 __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
201 /*****************************************************************************
202 * vlc_cond_init: initialize a condition
203 *****************************************************************************/
204 #define vlc_cond_init( P_THIS, P_COND ) \
205 __vlc_cond_init( VLC_OBJECT(P_THIS), P_COND )
207 /*****************************************************************************
208 * vlc_cond_signal: start a thread on condition completion
209 *****************************************************************************/
210 #define vlc_cond_signal( P_COND ) \
211 __vlc_cond_signal( __FILE__, __LINE__, P_COND )
213 static inline int __vlc_cond_signal( char * psz_file, int i_line,
214 vlc_cond_t *p_condvar )
217 /* In case of error : */
219 const char * psz_error = "";
221 #if defined( PTH_INIT_IN_PTH_H )
222 i_result = pth_cond_notify( &p_condvar->cond, FALSE );
224 #elif defined( ST_INIT_IN_ST_H )
225 i_result = st_cond_signal( p_condvar->cond );
227 #elif defined( WIN32 )
228 /* Release one waiting thread if one is available. */
229 /* For this trick to work properly, the vlc_cond_signal must be surrounded
230 * by a mutex. This will prevent another thread from stealing the signal */
231 if( !p_condvar->semaphore )
233 PulseEvent( p_condvar->event );
235 else if( p_condvar->i_win9x_cv == 1 )
237 /* Wait for the gate to be open */
238 WaitForSingleObject( p_condvar->event, INFINITE );
240 if( p_condvar->i_waiting_threads )
242 /* Using a semaphore exposes us to a race condition. It is
243 * possible for another thread to start waiting on the semaphore
244 * just after we signaled it and thus steal the signal.
245 * We have to prevent new threads from entering the cond_wait(). */
246 ResetEvent( p_condvar->event );
248 /* A semaphore is used here because Win9x doesn't have
249 * SignalObjectAndWait() and thus a race condition exists
250 * during the time we release the mutex and the time we start
251 * waiting on the event (more precisely, the signal can sometimes
252 * be missed by the waiting thread if we use PulseEvent()). */
253 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
258 if( p_condvar->i_waiting_threads )
260 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
262 /* Wait for the last thread to be awakened */
263 WaitForSingleObject( p_condvar->event, INFINITE );
268 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
269 i_result = pthread_cond_signal( &p_condvar->cond );
272 i_thread = (int)pthread_self();
273 psz_error = strerror(i_result);
276 #elif defined( HAVE_CTHREADS_H )
277 /* condition_signal() */
278 if ( p_condvar->queue.head || p_condvar->implications )
280 cond_signal( (condition_t)p_condvar );
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 );
323 msg_Err( p_condvar->p_this,
324 "thread %d: cond_signal failed at %s:%d (%d:%s)",
325 i_thread, psz_file, i_line, i_result, psz_error );
331 /*****************************************************************************
332 * vlc_cond_broadcast: start all threads waiting on condition completion
333 *****************************************************************************/
335 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
336 * Only works with pthreads, st, win32
337 * You need to adapt it for others
338 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
340 #define vlc_cond_broadcast( P_COND ) \
341 __vlc_cond_broadcast( __FILE__, __LINE__, P_COND )
343 static inline int __vlc_cond_broadcast( char * psz_file, int i_line,
344 vlc_cond_t *p_condvar )
347 /* In case of error : */
349 const char * psz_error = "";
351 #if defined( PTH_INIT_IN_PTH_H )
352 i_result = pth_cond_notify( &p_condvar->cond, FALSE );
354 #elif defined( ST_INIT_IN_ST_H )
355 i_result = st_cond_broadcast( p_condvar->cond );
357 #elif defined( WIN32 )
360 /* Release all waiting threads. */
361 if( !p_condvar->semaphore )
363 for( i = p_condvar->i_waiting_threads; i > 0; i-- )
365 PulseEvent( p_condvar->event );
368 else if( p_condvar->i_win9x_cv == 1 )
370 /* Wait for the gate to be open */
371 WaitForSingleObject( p_condvar->event, INFINITE );
373 if( p_condvar->i_waiting_threads )
375 /* Using a semaphore exposes us to a race condition. It is
376 * possible for another thread to start waiting on the semaphore
377 * just after we signaled it and thus steal the signal.
378 * We have to prevent new threads from entering the cond_wait(). */
379 ResetEvent( p_condvar->event );
381 /* A semaphore is used here because Win9x doesn't have
382 * SignalObjectAndWait() and thus a race condition exists
383 * during the time we release the mutex and the time we start
384 * waiting on the event (more precisely, the signal can sometimes
385 * be missed by the waiting thread if we use PulseEvent()). */
386 ReleaseSemaphore( p_condvar->semaphore,
387 p_condvar->i_waiting_threads, 0 );
392 if( p_condvar->i_waiting_threads )
394 ReleaseSemaphore( p_condvar->semaphore,
395 p_condvar->i_waiting_threads, 0 );
397 /* Wait for the last thread to be awakened */
398 WaitForSingleObject( p_condvar->event, INFINITE );
403 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
404 i_result = pthread_cond_broadcast( &p_condvar->cond );
407 i_thread = (int)pthread_self();
408 psz_error = strerror(i_result);
411 #elif defined( HAVE_CTHREADS_H )
412 /* condition_signal() */
413 if ( p_condvar->queue.head || p_condvar->implications )
415 cond_signal( (condition_t)p_condvar );
419 #elif defined( HAVE_KERNEL_SCHEDULER_H )
420 if( p_condvar == NULL )
422 i_result = B_BAD_VALUE;
424 else if( p_condvar->init < 2000 )
426 i_result = B_NO_INIT;
430 while( p_condvar->thread != -1 )
433 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
438 if( info.state != B_THREAD_SUSPENDED )
440 /* The waiting thread is not suspended so it could
441 * have been interrupted beetwen the unlock and the
442 * suspend_thread line. That is why we sleep a little
443 * before retesting p_condver->thread. */
448 /* Ok, we have to wake up that thread */
449 resume_thread( p_condvar->thread );
458 msg_Err( p_condvar->p_this,
459 "thread %d: cond_broadcast failed at %s:%d (%d:%s)",
460 i_thread, psz_file, i_line, i_result, psz_error );
466 /*****************************************************************************
467 * vlc_cond_wait: wait until condition completion
468 *****************************************************************************/
469 #define vlc_cond_wait( P_COND, P_MUTEX ) \
470 __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX )
472 static inline int __vlc_cond_wait( char * psz_file, int i_line,
473 vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
476 /* In case of error : */
478 const char * psz_error = "";
480 #if defined( PTH_INIT_IN_PTH_H )
481 i_result = pth_cond_await( &p_condvar->cond, &p_mutex->mutex, NULL );
483 #elif defined( ST_INIT_IN_ST_H )
484 st_mutex_unlock( p_mutex->mutex );
485 i_result = st_cond_wait( p_condvar->cond );
486 st_mutex_lock( p_mutex->mutex );
488 #elif defined( WIN32 )
489 if( !p_condvar->semaphore )
491 /* Increase our wait count */
492 p_condvar->i_waiting_threads++;
494 if( p_condvar->SignalObjectAndWait && p_mutex->mutex )
496 /* It is only possible to atomically release the mutex and
497 * initiate the waiting on WinNT/2K/XP. Win9x doesn't have
498 * SignalObjectAndWait(). */
499 p_condvar->SignalObjectAndWait( p_mutex->mutex,
505 LeaveCriticalSection( &p_mutex->csection );
506 WaitForSingleObject( p_condvar->event, INFINITE );
509 p_condvar->i_waiting_threads--;
511 else if( p_condvar->i_win9x_cv == 1 )
513 int i_waiting_threads;
515 /* Wait for the gate to be open */
516 WaitForSingleObject( p_condvar->event, INFINITE );
518 /* Increase our wait count */
519 p_condvar->i_waiting_threads++;
521 LeaveCriticalSection( &p_mutex->csection );
522 WaitForSingleObject( p_condvar->semaphore, INFINITE );
524 /* Decrement and test must be atomic */
525 EnterCriticalSection( &p_condvar->csection );
527 /* Decrease our wait count */
528 i_waiting_threads = --p_condvar->i_waiting_threads;
530 LeaveCriticalSection( &p_condvar->csection );
532 /* Reopen the gate if we were the last waiting thread */
533 if( !i_waiting_threads )
534 SetEvent( p_condvar->event );
538 int i_waiting_threads;
540 /* Increase our wait count */
541 p_condvar->i_waiting_threads++;
543 LeaveCriticalSection( &p_mutex->csection );
544 WaitForSingleObject( p_condvar->semaphore, INFINITE );
546 /* Decrement and test must be atomic */
547 EnterCriticalSection( &p_condvar->csection );
549 /* Decrease our wait count */
550 i_waiting_threads = --p_condvar->i_waiting_threads;
552 LeaveCriticalSection( &p_condvar->csection );
554 /* Signal that the last waiting thread just went through */
555 if( !i_waiting_threads )
556 SetEvent( p_condvar->event );
559 /* Reacquire the mutex before returning. */
560 vlc_mutex_lock( p_mutex );
564 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
567 /* In debug mode, timeout */
569 struct timespec timeout;
573 gettimeofday( &now, NULL );
574 timeout.tv_sec = now.tv_sec + THREAD_COND_TIMEOUT;
575 timeout.tv_nsec = now.tv_usec * 1000;
577 i_result = pthread_cond_timedwait( &p_condvar->cond, &p_mutex->mutex,
580 if( i_result == ETIMEDOUT )
582 msg_Warn( p_condvar->p_this,
583 "thread %d: possible deadlock detected "
584 "in cond_wait at %s:%d (%s)", pthread_self(),
585 psz_file, i_line, strerror(i_result) );
590 i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
595 i_thread = (int)pthread_self();
596 psz_error = strerror(i_result);
599 #elif defined( HAVE_CTHREADS_H )
600 condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
603 #elif defined( HAVE_KERNEL_SCHEDULER_H )
604 if( p_condvar == NULL )
606 i_result = B_BAD_VALUE;
608 else if( p_mutex == NULL )
610 i_result = B_BAD_VALUE;
612 else if( p_condvar->init < 2000 )
614 i_result = B_NO_INIT;
617 /* The p_condvar->thread var is initialized before the unlock because
618 * it enables to identify when the thread is interrupted beetwen the
619 * unlock line and the suspend_thread line */
620 p_condvar->thread = find_thread( NULL );
621 vlc_mutex_unlock( p_mutex );
622 suspend_thread( p_condvar->thread );
623 p_condvar->thread = -1;
625 vlc_mutex_lock( p_mutex );
632 msg_Err( p_condvar->p_this,
633 "thread %d: cond_wait failed at %s:%d (%d:%s)",
634 i_thread, psz_file, i_line, i_result, psz_error );
640 /*****************************************************************************
641 * vlc_cond_destroy: destroy a condition
642 *****************************************************************************/
643 #define vlc_cond_destroy( P_COND ) \
644 __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
646 /*****************************************************************************
647 * vlc_thread_create: create a thread
648 *****************************************************************************/
649 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT ) \
650 __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, PRIORITY, WAIT )
652 /*****************************************************************************
653 * vlc_thread_ready: tell the parent thread we were successfully spawned
654 *****************************************************************************/
655 #define vlc_thread_ready( P_THIS ) \
656 __vlc_thread_ready( VLC_OBJECT(P_THIS) )
658 /*****************************************************************************
659 * vlc_thread_join: wait until a thread exits
660 *****************************************************************************/
661 #define vlc_thread_join( P_THIS ) \
662 __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )