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.9 2002/11/11 14:39:11 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( UNDER_CE )
80 EnterCriticalSection( &p_mutex->csection );
83 #elif defined( WIN32 )
86 WaitForSingleObject( p_mutex->mutex, INFINITE );
90 EnterCriticalSection( &p_mutex->csection );
94 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
95 i_result = pthread_mutex_lock( &p_mutex->mutex );
98 i_thread = (int)pthread_self();
99 psz_error = strerror(i_result);
102 #elif defined( HAVE_CTHREADS_H )
103 mutex_lock( p_mutex->mutex );
106 #elif defined( HAVE_KERNEL_SCHEDULER_H )
107 if( p_mutex == NULL )
109 i_result = B_BAD_VALUE;
111 else if( p_mutex->init < 2000 )
113 i_result = B_NO_INIT;
117 i_result = acquire_sem( p_mutex->lock );
123 msg_Err( p_mutex->p_this,
124 "thread %d: mutex_lock failed at %s:%d (%d:%s)",
125 i_thread, psz_file, i_line, i_result, psz_error );
130 /*****************************************************************************
131 * vlc_mutex_unlock: unlock a mutex
132 *****************************************************************************/
133 #define vlc_mutex_unlock( P_MUTEX ) \
134 __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
136 static inline int __vlc_mutex_unlock( char * psz_file, int i_line,
137 vlc_mutex_t *p_mutex )
140 /* In case of error : */
142 const char * psz_error = "";
144 #if defined( PTH_INIT_IN_PTH_H )
145 i_result = pth_mutex_release( &p_mutex->mutex );
147 #elif defined( ST_INIT_IN_ST_H )
148 i_result = st_mutex_unlock( p_mutex->mutex );
150 #elif defined( UNDER_CE )
151 LeaveCriticalSection( &p_mutex->csection );
154 #elif defined( WIN32 )
157 ReleaseMutex( p_mutex->mutex );
161 LeaveCriticalSection( &p_mutex->csection );
165 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
166 i_result = pthread_mutex_unlock( &p_mutex->mutex );
169 i_thread = (int)pthread_self();
170 psz_error = strerror(i_result);
173 #elif defined( HAVE_CTHREADS_H )
174 mutex_unlock( p_mutex );
177 #elif defined( HAVE_KERNEL_SCHEDULER_H )
178 if( p_mutex == NULL )
180 i_result = B_BAD_VALUE;
182 else if( p_mutex->init < 2000 )
184 i_result = B_NO_INIT;
188 release_sem( p_mutex->lock );
195 msg_Err( p_mutex->p_this,
196 "thread %d: mutex_unlock failed at %s:%d (%d:%s)",
197 i_thread, psz_file, i_line, i_result, psz_error );
203 /*****************************************************************************
204 * vlc_mutex_destroy: destroy a mutex
205 *****************************************************************************/
206 #define vlc_mutex_destroy( P_MUTEX ) \
207 __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
209 /*****************************************************************************
210 * vlc_cond_init: initialize a condition
211 *****************************************************************************/
212 #define vlc_cond_init( P_THIS, P_COND ) \
213 __vlc_cond_init( VLC_OBJECT(P_THIS), P_COND )
215 /*****************************************************************************
216 * vlc_cond_signal: start a thread on condition completion
217 *****************************************************************************/
218 #define vlc_cond_signal( P_COND ) \
219 __vlc_cond_signal( __FILE__, __LINE__, P_COND )
221 static inline int __vlc_cond_signal( char * psz_file, int i_line,
222 vlc_cond_t *p_condvar )
225 /* In case of error : */
227 const char * psz_error = "";
229 #if defined( PTH_INIT_IN_PTH_H )
230 i_result = pth_cond_notify( &p_condvar->cond, FALSE );
232 #elif defined( ST_INIT_IN_ST_H )
233 i_result = st_cond_signal( p_condvar->cond );
235 #elif defined( UNDER_CE )
236 PulseEvent( p_condvar->event );
239 #elif defined( WIN32 )
240 /* Release one waiting thread if one is available. */
241 /* For this trick to work properly, the vlc_cond_signal must be surrounded
242 * by a mutex. This will prevent another thread from stealing the signal */
243 if( !p_condvar->semaphore )
245 PulseEvent( p_condvar->event );
247 else if( p_condvar->i_win9x_cv == 1 )
249 /* Wait for the gate to be open */
250 WaitForSingleObject( p_condvar->event, INFINITE );
252 if( p_condvar->i_waiting_threads )
254 /* Using a semaphore exposes us to a race condition. It is
255 * possible for another thread to start waiting on the semaphore
256 * just after we signaled it and thus steal the signal.
257 * We have to prevent new threads from entering the cond_wait(). */
258 ResetEvent( p_condvar->event );
260 /* A semaphore is used here because Win9x doesn't have
261 * SignalObjectAndWait() and thus a race condition exists
262 * during the time we release the mutex and the time we start
263 * waiting on the event (more precisely, the signal can sometimes
264 * be missed by the waiting thread if we use PulseEvent()). */
265 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
270 if( p_condvar->i_waiting_threads )
272 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
274 /* Wait for the last thread to be awakened */
275 WaitForSingleObject( p_condvar->event, INFINITE );
280 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
281 i_result = pthread_cond_signal( &p_condvar->cond );
284 i_thread = (int)pthread_self();
285 psz_error = strerror(i_result);
288 #elif defined( HAVE_CTHREADS_H )
289 /* condition_signal() */
290 if ( p_condvar->queue.head || p_condvar->implications )
292 cond_signal( (condition_t)p_condvar );
296 #elif defined( HAVE_KERNEL_SCHEDULER_H )
297 if( p_condvar == NULL )
299 i_result = B_BAD_VALUE;
301 else if( p_condvar->init < 2000 )
303 i_result = B_NO_INIT;
307 while( p_condvar->thread != -1 )
310 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
315 if( info.state != B_THREAD_SUSPENDED )
317 /* The waiting thread is not suspended so it could
318 * have been interrupted beetwen the unlock and the
319 * suspend_thread line. That is why we sleep a little
320 * before retesting p_condver->thread. */
325 /* Ok, we have to wake up that thread */
326 resume_thread( p_condvar->thread );
336 msg_Err( p_condvar->p_this,
337 "thread %d: cond_signal failed at %s:%d (%d:%s)",
338 i_thread, psz_file, i_line, i_result, psz_error );
344 /*****************************************************************************
345 * vlc_cond_broadcast: start all threads waiting on condition completion
346 *****************************************************************************/
348 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
349 * Only works with pthreads, st, win32
350 * You need to adapt it for others
351 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
353 #define vlc_cond_broadcast( P_COND ) \
354 __vlc_cond_broadcast( __FILE__, __LINE__, P_COND )
356 static inline int __vlc_cond_broadcast( char * psz_file, int i_line,
357 vlc_cond_t *p_condvar )
360 /* In case of error : */
362 const char * psz_error = "";
364 #if defined( PTH_INIT_IN_PTH_H )
365 i_result = pth_cond_notify( &p_condvar->cond, FALSE );
367 #elif defined( ST_INIT_IN_ST_H )
368 i_result = st_cond_broadcast( p_condvar->cond );
370 #elif defined( UNDER_CE )
373 /* Release all waiting threads. */
374 for( i = p_condvar->i_waiting_threads; i > 0; i-- )
376 PulseEvent( p_condvar->event );
380 #elif defined( WIN32 )
383 /* Release all waiting threads. */
384 if( !p_condvar->semaphore )
386 for( i = p_condvar->i_waiting_threads; i > 0; i-- )
388 PulseEvent( p_condvar->event );
391 else if( p_condvar->i_win9x_cv == 1 )
393 /* Wait for the gate to be open */
394 WaitForSingleObject( p_condvar->event, INFINITE );
396 if( p_condvar->i_waiting_threads )
398 /* Using a semaphore exposes us to a race condition. It is
399 * possible for another thread to start waiting on the semaphore
400 * just after we signaled it and thus steal the signal.
401 * We have to prevent new threads from entering the cond_wait(). */
402 ResetEvent( p_condvar->event );
404 /* A semaphore is used here because Win9x doesn't have
405 * SignalObjectAndWait() and thus a race condition exists
406 * during the time we release the mutex and the time we start
407 * waiting on the event (more precisely, the signal can sometimes
408 * be missed by the waiting thread if we use PulseEvent()). */
409 ReleaseSemaphore( p_condvar->semaphore,
410 p_condvar->i_waiting_threads, 0 );
415 if( p_condvar->i_waiting_threads )
417 ReleaseSemaphore( p_condvar->semaphore,
418 p_condvar->i_waiting_threads, 0 );
420 /* Wait for the last thread to be awakened */
421 WaitForSingleObject( p_condvar->event, INFINITE );
426 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
427 i_result = pthread_cond_broadcast( &p_condvar->cond );
430 i_thread = (int)pthread_self();
431 psz_error = strerror(i_result);
434 #elif defined( HAVE_CTHREADS_H )
435 /* condition_signal() */
436 if ( p_condvar->queue.head || p_condvar->implications )
438 cond_signal( (condition_t)p_condvar );
442 #elif defined( HAVE_KERNEL_SCHEDULER_H )
443 if( p_condvar == NULL )
445 i_result = B_BAD_VALUE;
447 else if( p_condvar->init < 2000 )
449 i_result = B_NO_INIT;
453 while( p_condvar->thread != -1 )
456 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
461 if( info.state != B_THREAD_SUSPENDED )
463 /* The waiting thread is not suspended so it could
464 * have been interrupted beetwen the unlock and the
465 * suspend_thread line. That is why we sleep a little
466 * before retesting p_condver->thread. */
471 /* Ok, we have to wake up that thread */
472 resume_thread( p_condvar->thread );
482 msg_Err( p_condvar->p_this,
483 "thread %d: cond_broadcast failed at %s:%d (%d:%s)",
484 i_thread, psz_file, i_line, i_result, psz_error );
490 /*****************************************************************************
491 * vlc_cond_wait: wait until condition completion
492 *****************************************************************************/
493 #define vlc_cond_wait( P_COND, P_MUTEX ) \
494 __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX )
496 static inline int __vlc_cond_wait( char * psz_file, int i_line,
497 vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
500 /* In case of error : */
502 const char * psz_error = "";
504 #if defined( PTH_INIT_IN_PTH_H )
505 i_result = pth_cond_await( &p_condvar->cond, &p_mutex->mutex, NULL );
507 #elif defined( ST_INIT_IN_ST_H )
508 st_mutex_unlock( p_mutex->mutex );
509 i_result = st_cond_wait( p_condvar->cond );
510 st_mutex_lock( p_mutex->mutex );
512 #elif defined( UNDER_CE )
513 p_condvar->i_waiting_threads++;
514 LeaveCriticalSection( &p_mutex->csection );
515 WaitForSingleObject( p_condvar->event, INFINITE );
516 p_condvar->i_waiting_threads--;
518 /* Reacquire the mutex before returning. */
519 vlc_mutex_lock( p_mutex );
523 #elif defined( WIN32 )
524 if( !p_condvar->semaphore )
526 /* Increase our wait count */
527 p_condvar->i_waiting_threads++;
531 /* It is only possible to atomically release the mutex and
532 * initiate the waiting on WinNT/2K/XP. Win9x doesn't have
533 * SignalObjectAndWait(). */
534 p_condvar->SignalObjectAndWait( p_mutex->mutex,
540 LeaveCriticalSection( &p_mutex->csection );
541 WaitForSingleObject( p_condvar->event, INFINITE );
544 p_condvar->i_waiting_threads--;
546 else if( p_condvar->i_win9x_cv == 1 )
548 int i_waiting_threads;
550 /* Wait for the gate to be open */
551 WaitForSingleObject( p_condvar->event, INFINITE );
553 /* Increase our wait count */
554 p_condvar->i_waiting_threads++;
556 LeaveCriticalSection( &p_mutex->csection );
557 WaitForSingleObject( p_condvar->semaphore, INFINITE );
559 /* Decrement and test must be atomic */
560 EnterCriticalSection( &p_condvar->csection );
562 /* Decrease our wait count */
563 i_waiting_threads = --p_condvar->i_waiting_threads;
565 LeaveCriticalSection( &p_condvar->csection );
567 /* Reopen the gate if we were the last waiting thread */
568 if( !i_waiting_threads )
569 SetEvent( p_condvar->event );
573 int i_waiting_threads;
575 /* Increase our wait count */
576 p_condvar->i_waiting_threads++;
578 LeaveCriticalSection( &p_mutex->csection );
579 WaitForSingleObject( p_condvar->semaphore, INFINITE );
581 /* Decrement and test must be atomic */
582 EnterCriticalSection( &p_condvar->csection );
584 /* Decrease our wait count */
585 i_waiting_threads = --p_condvar->i_waiting_threads;
587 LeaveCriticalSection( &p_condvar->csection );
589 /* Signal that the last waiting thread just went through */
590 if( !i_waiting_threads )
591 SetEvent( p_condvar->event );
594 /* Reacquire the mutex before returning. */
595 vlc_mutex_lock( p_mutex );
599 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
602 /* In debug mode, timeout */
604 struct timespec timeout;
608 gettimeofday( &now, NULL );
609 timeout.tv_sec = now.tv_sec + THREAD_COND_TIMEOUT;
610 timeout.tv_nsec = now.tv_usec * 1000;
612 i_result = pthread_cond_timedwait( &p_condvar->cond, &p_mutex->mutex,
615 if( i_result == ETIMEDOUT )
617 msg_Warn( p_condvar->p_this,
618 "thread %d: possible deadlock detected "
619 "in cond_wait at %s:%d (%s)", pthread_self(),
620 psz_file, i_line, strerror(i_result) );
625 i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
630 i_thread = (int)pthread_self();
631 psz_error = strerror(i_result);
634 #elif defined( HAVE_CTHREADS_H )
635 condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
638 #elif defined( HAVE_KERNEL_SCHEDULER_H )
639 if( p_condvar == NULL )
641 i_result = B_BAD_VALUE;
643 else if( p_mutex == NULL )
645 i_result = B_BAD_VALUE;
647 else if( p_condvar->init < 2000 )
649 i_result = B_NO_INIT;
652 /* The p_condvar->thread var is initialized before the unlock because
653 * it enables to identify when the thread is interrupted beetwen the
654 * unlock line and the suspend_thread line */
655 p_condvar->thread = find_thread( NULL );
656 vlc_mutex_unlock( p_mutex );
657 suspend_thread( p_condvar->thread );
658 p_condvar->thread = -1;
660 vlc_mutex_lock( p_mutex );
667 msg_Err( p_condvar->p_this,
668 "thread %d: cond_wait failed at %s:%d (%d:%s)",
669 i_thread, psz_file, i_line, i_result, psz_error );
675 /*****************************************************************************
676 * vlc_cond_destroy: destroy a condition
677 *****************************************************************************/
678 #define vlc_cond_destroy( P_COND ) \
679 __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
681 /*****************************************************************************
682 * vlc_thread_create: create a thread
683 *****************************************************************************/
684 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT ) \
685 __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, PRIORITY, WAIT )
687 /*****************************************************************************
688 * vlc_thread_ready: tell the parent thread we were successfully spawned
689 *****************************************************************************/
690 #define vlc_thread_ready( P_THIS ) \
691 __vlc_thread_ready( VLC_OBJECT(P_THIS) )
693 /*****************************************************************************
694 * vlc_thread_join: wait until a thread exits
695 *****************************************************************************/
696 #define vlc_thread_join( P_THIS ) \
697 __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )