1 /*****************************************************************************
2 * threads_funcs.h : threads implementation for the VideoLAN client
3 * This header provides a portable threads implementation.
4 *****************************************************************************
5 * Copyright (C) 1999, 2000 VideoLAN
6 * $Id: threads_funcs.h,v 1.1 2002/04/27 22:11:22 gbazin Exp $
8 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
9 * Samuel Hocevar <sam@via.ecp.fr>
10 * Gildas Bazin <gbazin@netcourrier.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
28 * Function definitions
29 *****************************************************************************/
31 /*****************************************************************************
32 * vlc_threads_init: initialize threads system
33 *****************************************************************************/
34 static __inline__ int vlc_threads_init( void )
36 #if defined( PTH_INIT_IN_PTH_H )
39 #elif defined( ST_INIT_IN_ST_H )
42 #elif defined( WIN32 )
45 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
48 #elif defined( HAVE_CTHREADS_H )
51 #elif defined( HAVE_KERNEL_SCHEDULER_H )
57 /*****************************************************************************
58 * vlc_threads_end: stop threads system
59 *****************************************************************************/
60 static __inline__ int vlc_threads_end( void )
62 #if defined( PTH_INIT_IN_PTH_H )
65 #elif defined( ST_INIT_IN_ST_H )
68 #elif defined( WIN32 )
71 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
74 #elif defined( HAVE_CTHREADS_H )
77 #elif defined( HAVE_KERNEL_SCHEDULER_H )
83 /*****************************************************************************
84 * vlc_mutex_init: initialize a mutex
85 *****************************************************************************/
86 static __inline__ int vlc_mutex_init( vlc_mutex_t *p_mutex )
88 #if defined( PTH_INIT_IN_PTH_H )
89 return pth_mutex_init( p_mutex );
91 #elif defined( ST_INIT_IN_ST_H )
92 *p_mutex = st_mutex_new();
93 return ( *p_mutex == NULL ) ? errno : 0;
95 #elif defined( WIN32 )
96 /* We use mutexes on WinNT/2K/XP because we can use the SignalObjectAndWait
97 * function and have a 100% correct vlc_cond_wait() implementation.
98 * As this function is not available on Win9x, we can use the faster
100 if( (GetVersion() < 0x80000000) && !p_main->p_sys->b_fast_pthread )
102 /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
103 p_mutex->mutex = CreateMutex( 0, FALSE, 0 );
104 return ( p_mutex->mutex ? 0 : 1 );
108 InitializeCriticalSection( &p_mutex->csection );
109 p_mutex->mutex = NULL;
113 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
114 # if defined(DEBUG) && defined(SYS_LINUX)
115 /* Create error-checking mutex to detect threads problems more easily. */
116 pthread_mutexattr_t attr;
119 pthread_mutexattr_init( &attr );
120 pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
121 i_result = pthread_mutex_init( p_mutex, &attr );
122 pthread_mutexattr_destroy( &attr );
126 return pthread_mutex_init( p_mutex, NULL );
128 #elif defined( HAVE_CTHREADS_H )
129 mutex_init( p_mutex );
132 #elif defined( HAVE_KERNEL_SCHEDULER_H )
134 /* check the arguments and whether it's already been initialized */
135 if( p_mutex == NULL )
140 if( p_mutex->init == 9999 )
145 p_mutex->lock = create_sem( 1, "BeMutex" );
146 if( p_mutex->lock < B_NO_ERROR )
151 p_mutex->init = 9999;
157 /*****************************************************************************
158 * vlc_mutex_lock: lock a mutex
159 *****************************************************************************/
161 # define vlc_mutex_lock( P_MUTEX ) \
162 _vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
164 # define vlc_mutex_lock( P_MUTEX ) \
165 _vlc_mutex_lock( "(unknown)", 0, P_MUTEX )
168 static __inline__ int _vlc_mutex_lock( char * psz_file, int i_line,
169 vlc_mutex_t *p_mutex )
171 #if defined( PTH_INIT_IN_PTH_H )
172 return pth_mutex_acquire( p_mutex, TRUE, NULL );
174 #elif defined( ST_INIT_IN_ST_H )
175 return st_mutex_lock( *p_mutex );
177 #elif defined( WIN32 )
180 WaitForSingleObject( p_mutex->mutex, INFINITE );
184 EnterCriticalSection( &p_mutex->csection );
188 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
189 int i_return = pthread_mutex_lock( p_mutex );
192 intf_ErrMsg( "thread %d error: mutex_lock failed at %s:%d (%s)",
193 pthread_self(), psz_file, i_line, strerror(i_return) );
197 #elif defined( HAVE_CTHREADS_H )
198 mutex_lock( p_mutex );
201 #elif defined( HAVE_KERNEL_SCHEDULER_H )
209 if( p_mutex->init < 2000 )
214 err = acquire_sem( p_mutex->lock );
220 /*****************************************************************************
221 * vlc_mutex_unlock: unlock a mutex
222 *****************************************************************************/
224 # define vlc_mutex_unlock( P_MUTEX ) \
225 _vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
227 # define vlc_mutex_unlock( P_MUTEX ) \
228 _vlc_mutex_unlock( "(unknown)", 0, P_MUTEX )
231 static __inline__ int _vlc_mutex_unlock( char * psz_file, int i_line,
232 vlc_mutex_t *p_mutex )
234 #if defined( PTH_INIT_IN_PTH_H )
235 return pth_mutex_release( p_mutex );
237 #elif defined( ST_INIT_IN_ST_H )
238 return st_mutex_unlock( *p_mutex );
240 #elif defined( WIN32 )
243 ReleaseMutex( p_mutex->mutex );
247 LeaveCriticalSection( &p_mutex->csection );
251 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
252 int i_return = pthread_mutex_unlock( p_mutex );
255 intf_ErrMsg( "thread %d error: mutex_unlock failed at %s:%d (%s)",
256 pthread_self(), psz_file, i_line, strerror(i_return) );
260 #elif defined( HAVE_CTHREADS_H )
261 mutex_unlock( p_mutex );
264 #elif defined( HAVE_KERNEL_SCHEDULER_H )
270 if( p_mutex->init < 2000 )
275 release_sem( p_mutex->lock );
281 /*****************************************************************************
282 * vlc_mutex_destroy: destroy a mutex
283 *****************************************************************************/
285 # define vlc_mutex_destroy( P_MUTEX ) \
286 _vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
288 # define vlc_mutex_destroy( P_MUTEX ) \
289 _vlc_mutex_destroy( "(unknown)", 0, P_MUTEX )
292 static __inline__ int _vlc_mutex_destroy( char * psz_file, int i_line,
293 vlc_mutex_t *p_mutex )
295 #if defined( PTH_INIT_IN_PTH_H )
298 #elif defined( ST_INIT_IN_ST_H )
299 return st_mutex_destroy( *p_mutex );
301 #elif defined( WIN32 )
304 CloseHandle( p_mutex->mutex );
308 DeleteCriticalSection( &p_mutex->csection );
312 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
313 int i_return = pthread_mutex_destroy( p_mutex );
316 intf_ErrMsg( "thread %d error: mutex_destroy failed at %s:%d (%s)",
317 pthread_self(), psz_file, i_line, strerror(i_return) );
321 #elif defined( HAVE_CTHREADS_H )
324 #elif defined( HAVE_KERNEL_SCHEDULER_H )
325 if( p_mutex->init == 9999 )
327 delete_sem( p_mutex->lock );
336 /*****************************************************************************
337 * vlc_cond_init: initialize a condition
338 *****************************************************************************/
339 static __inline__ int vlc_cond_init( vlc_cond_t *p_condvar )
341 #if defined( PTH_INIT_IN_PTH_H )
342 return pth_cond_init( p_condvar );
344 #elif defined( ST_INIT_IN_ST_H )
345 *p_condvar = st_cond_new();
346 return ( *p_condvar == NULL ) ? errno : 0;
348 #elif defined( WIN32 )
349 /* initialise counter */
350 p_condvar->i_waiting_threads = 0;
352 /* Create an auto-reset event. */
353 p_condvar->signal = CreateEvent( NULL, /* no security */
354 FALSE, /* auto-reset event */
355 FALSE, /* non-signaled initially */
356 NULL ); /* unnamed */
358 return( !p_condvar->signal );
360 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
361 return pthread_cond_init( p_condvar, NULL );
363 #elif defined( HAVE_CTHREADS_H )
364 /* condition_init() */
365 spin_lock_init( &p_condvar->lock );
366 cthread_queue_init( &p_condvar->queue );
368 p_condvar->implications = 0;
372 #elif defined( HAVE_KERNEL_SCHEDULER_H )
378 if( p_condvar->init == 9999 )
383 p_condvar->thread = -1;
384 p_condvar->init = 9999;
390 /*****************************************************************************
391 * vlc_cond_signal: start a thread on condition completion
392 *****************************************************************************/
393 static __inline__ int vlc_cond_signal( vlc_cond_t *p_condvar )
395 #if defined( PTH_INIT_IN_PTH_H )
396 return pth_cond_notify( p_condvar, FALSE );
398 #elif defined( ST_INIT_IN_ST_H )
399 return st_cond_signal( *p_condvar );
401 #elif defined( WIN32 )
402 /* Release one waiting thread if one is available. */
403 /* For this trick to work properly, the vlc_cond_signal must be surrounded
404 * by a mutex. This will prevent another thread from stealing the signal */
405 PulseEvent( p_condvar->signal );
408 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
409 return pthread_cond_signal( p_condvar );
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 )
425 if( p_condvar->init < 2000 )
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 /*****************************************************************************
459 * vlc_cond_broadcast: start all threads waiting on condition completion
460 *****************************************************************************/
462 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
463 * Only works with pthreads, you need to adapt it for others
464 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
466 static __inline__ int vlc_cond_broadcast( vlc_cond_t *p_condvar )
468 #if defined( PTH_INIT_IN_PTH_H )
469 return pth_cond_notify( p_condvar, FALSE );
471 #elif defined( ST_INIT_IN_ST_H )
472 return st_cond_broadcast( p_condvar );
474 #elif defined( WIN32 )
475 /* Release all waiting threads. */
476 /* For this trick to work properly, the vlc_cond_signal must be surrounded
477 * by a mutex. This will prevent another thread from stealing the signal */
478 while( p_condvar->i_waiting_threads )
480 PulseEvent( p_condvar->signal );
481 Sleep( 1 ); /* deschedule the current thread */
485 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
486 return pthread_cond_broadcast( p_condvar );
488 #elif defined( HAVE_CTHREADS_H )
489 /* condition_signal() */
490 if ( p_condvar->queue.head || p_condvar->implications )
492 cond_signal( (condition_t)p_condvar );
496 #elif defined( HAVE_KERNEL_SCHEDULER_H )
502 if( p_condvar->init < 2000 )
507 while( p_condvar->thread != -1 )
510 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
515 if( info.state != B_THREAD_SUSPENDED )
517 /* The waiting thread is not suspended so it could
518 * have been interrupted beetwen the unlock and the
519 * suspend_thread line. That is why we sleep a little
520 * before retesting p_condver->thread. */
525 /* Ok, we have to wake up that thread */
526 resume_thread( p_condvar->thread );
535 /*****************************************************************************
536 * vlc_cond_wait: wait until condition completion
537 *****************************************************************************/
539 # define vlc_cond_wait( P_COND, P_MUTEX ) \
540 _vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX )
542 # define vlc_cond_wait( P_COND, P_MUTEX ) \
543 _vlc_cond_wait( "(unknown)", 0, P_COND, P_MUTEX )
546 static __inline__ int _vlc_cond_wait( char * psz_file, int i_line,
547 vlc_cond_t *p_condvar,
548 vlc_mutex_t *p_mutex )
550 #if defined( PTH_INIT_IN_PTH_H )
551 return pth_cond_await( p_condvar, p_mutex, NULL );
553 #elif defined( ST_INIT_IN_ST_H )
556 st_mutex_unlock( *p_mutex );
557 i_ret = st_cond_wait( *p_condvar );
558 st_mutex_lock( *p_mutex );
562 #elif defined( WIN32 )
563 /* The ideal would be to use a function which atomically releases the
564 * mutex and initiate the waiting.
565 * Unfortunately only the SignalObjectAndWait function does this and it's
566 * only supported on WinNT/2K, furthermore it cannot take multiple
567 * events as parameters.
569 * The solution we use should however fulfill all our needs (even though
570 * it is not a correct pthreads implementation)
574 p_condvar->i_waiting_threads ++;
578 p_main->p_sys->SignalObjectAndWait( p_mutex->mutex, p_condvar->signal,
583 /* Release the mutex */
584 vlc_mutex_unlock( p_mutex );
585 i_result = WaitForSingleObject( p_condvar->signal, INFINITE);
586 p_condvar->i_waiting_threads --;
589 /* Reacquire the mutex before returning. */
590 vlc_mutex_lock( p_mutex );
592 return( i_result == WAIT_FAILED );
594 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
597 return pthread_cond_wait( p_condvar, p_mutex );
599 /* In debug mode, timeout */
601 struct timespec timeout;
606 gettimeofday( &now, NULL );
607 timeout.tv_sec = now.tv_sec + THREAD_COND_TIMEOUT;
608 timeout.tv_nsec = now.tv_usec * 1000;
610 i_result = pthread_cond_timedwait( p_condvar, p_mutex, &timeout );
612 if( i_result == ETIMEDOUT )
614 intf_WarnMsg( 1, "thread %d warning: Possible deadlock detected in cond_wait at %s:%d (%s)",
615 pthread_self(), psz_file, i_line, strerror(i_result) );
621 intf_ErrMsg( "thread %d error: cond_wait failed at %s:%d (%s)",
622 pthread_self(), psz_file, i_line, strerror(i_result) );
628 #elif defined( HAVE_CTHREADS_H )
629 condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
632 #elif defined( HAVE_KERNEL_SCHEDULER_H )
643 if( p_condvar->init < 2000 )
648 /* The p_condvar->thread var is initialized before the unlock because
649 * it enables to identify when the thread is interrupted beetwen the
650 * unlock line and the suspend_thread line */
651 p_condvar->thread = find_thread( NULL );
652 vlc_mutex_unlock( p_mutex );
653 suspend_thread( p_condvar->thread );
654 p_condvar->thread = -1;
656 vlc_mutex_lock( p_mutex );
662 /*****************************************************************************
663 * vlc_cond_destroy: destroy a condition
664 *****************************************************************************/
666 # define vlc_cond_destroy( P_COND ) \
667 _vlc_cond_destroy( __FILE__, __LINE__, P_COND )
669 # define vlc_cond_destroy( P_COND ) \
670 _vlc_cond_destroy( "(unknown)", 0, P_COND )
673 static __inline__ int _vlc_cond_destroy( char * psz_file, int i_line,
674 vlc_cond_t *p_condvar )
676 #if defined( PTH_INIT_IN_PTH_H )
679 #elif defined( ST_INIT_IN_ST_H )
680 return st_cond_destroy( *p_condvar );
682 #elif defined( WIN32 )
683 return( !CloseHandle( p_condvar->signal ) );
685 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
686 int i_result = pthread_cond_destroy( p_condvar );
689 intf_ErrMsg( "thread %d error: cond_destroy failed at %s:%d (%s)",
690 pthread_self(), psz_file, i_line, strerror(i_result) );
694 #elif defined( HAVE_CTHREADS_H )
697 #elif defined( HAVE_KERNEL_SCHEDULER_H )
704 /*****************************************************************************
705 * vlc_thread_create: create a thread
706 *****************************************************************************/
708 # define vlc_thread_create( P_THREAD, PSZ_NAME, FUNC, P_DATA ) \
709 _vlc_thread_create( __FILE__, __LINE__, P_THREAD, PSZ_NAME, FUNC, P_DATA )
711 # define vlc_thread_create( P_THREAD, PSZ_NAME, FUNC, P_DATA ) \
712 _vlc_thread_create( "(unknown)", 0, P_THREAD, PSZ_NAME, FUNC, P_DATA )
715 static __inline__ int _vlc_thread_create( char * psz_file, int i_line,
716 vlc_thread_t *p_thread,
718 vlc_thread_func_t func,
726 /* Initialize the wrapper structure */
728 wrapper.p_data = p_data;
729 getitimer( ITIMER_PROF, &wrapper.itimer );
730 vlc_mutex_init( &wrapper.lock );
731 vlc_cond_init( &wrapper.wait );
732 vlc_mutex_lock( &wrapper.lock );
734 /* Alter user-passed data so that we call the wrapper instead
735 * of the real function */
737 func = vlc_thread_wrapper;
740 #if defined( PTH_INIT_IN_PTH_H )
741 *p_thread = pth_spawn( PTH_ATTR_DEFAULT, func, p_data );
742 i_ret = ( p_thread == NULL );
744 #elif defined( ST_INIT_IN_ST_H )
745 *p_thread = st_thread_create( func, p_data, 1, 0 );
746 i_ret = ( p_thread == NULL );
748 #elif defined( WIN32 )
750 /* When using the MSVCRT C library you have to use the _beginthreadex
751 * function instead of CreateThread, otherwise you'll end up with memory
752 * leaks and the signal functions not working */
753 *p_thread = (HANDLE)_beginthreadex( NULL, 0, (PTHREAD_START) func,
754 p_data, 0, &threadID );
756 i_ret = ( *p_thread ? 0 : 1 );
758 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
759 i_ret = pthread_create( p_thread, NULL, func, p_data );
761 #elif defined( HAVE_CTHREADS_H )
762 *p_thread = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
765 #elif defined( HAVE_KERNEL_SCHEDULER_H )
766 *p_thread = spawn_thread( (thread_func)func, psz_name,
767 B_NORMAL_PRIORITY, p_data );
768 i_ret = resume_thread( *p_thread );
775 vlc_cond_wait( &wrapper.wait, &wrapper.lock );
778 vlc_mutex_unlock( &wrapper.lock );
779 vlc_mutex_destroy( &wrapper.lock );
780 vlc_cond_destroy( &wrapper.wait );
785 intf_WarnMsg( 2, "thread info: %d (%s) has been created (%s:%d)",
786 *p_thread, psz_name, psz_file, i_line );
790 intf_ErrMsg( "thread error: %s couldn't be created at %s:%d (%s)",
791 psz_name, psz_file, i_line, strerror(i_ret) );
797 /*****************************************************************************
798 * vlc_thread_exit: terminate a thread
799 *****************************************************************************/
800 static __inline__ void vlc_thread_exit( void )
802 #if defined( PTH_INIT_IN_PTH_H )
805 #elif defined( ST_INIT_IN_ST_H )
807 st_thread_exit( &result );
809 #elif defined( WIN32 )
810 /* For now we don't close the thread handles (because of race conditions).
811 * Need to be looked at. */
814 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
817 #elif defined( HAVE_CTHREADS_H )
819 cthread_exit( &result );
821 #elif defined( HAVE_KERNEL_SCHEDULER_H )
827 /*****************************************************************************
828 * vlc_thread_join: wait until a thread exits
829 *****************************************************************************/
831 # define vlc_thread_join( THREAD ) \
832 _vlc_thread_join( __FILE__, __LINE__, THREAD )
834 # define vlc_thread_join( THREAD ) \
835 _vlc_thread_join( "(unknown)", 0, THREAD )
838 static __inline__ void _vlc_thread_join( char * psz_file, int i_line,
839 vlc_thread_t thread )
843 #if defined( PTH_INIT_IN_PTH_H )
844 i_ret = pth_join( thread, NULL );
846 #elif defined( ST_INIT_IN_ST_H )
847 i_ret = st_thread_join( thread, NULL );
849 #elif defined( WIN32 )
850 WaitForSingleObject( thread, INFINITE );
852 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
853 i_ret = pthread_join( thread, NULL );
855 #elif defined( HAVE_CTHREADS_H )
856 cthread_join( thread );
859 #elif defined( HAVE_KERNEL_SCHEDULER_H )
861 wait_for_thread( thread, &exit_value );
867 intf_ErrMsg( "thread error: thread_join(%d) failed at %s:%d (%s)",
868 thread, psz_file, i_line, strerror(i_ret) );
872 intf_WarnMsg( 2, "thread info: %d has been joined (%s:%d)",
873 thread, psz_file, i_line );
877 /*****************************************************************************
878 * vlc_thread_wrapper: wrapper around thread functions used when profiling.
879 *****************************************************************************/
881 static void *vlc_thread_wrapper( void *p_wrapper )
883 /* Put user data in thread-local variables */
884 void * p_data = ((wrapper_t*)p_wrapper)->p_data;
885 vlc_thread_func_t func = ((wrapper_t*)p_wrapper)->func;
887 /* Set the profile timer value */
888 setitimer( ITIMER_PROF, &((wrapper_t*)p_wrapper)->itimer, NULL );
890 /* Tell the calling thread that we don't need its data anymore */
891 vlc_mutex_lock( &((wrapper_t*)p_wrapper)->lock );
892 vlc_cond_signal( &((wrapper_t*)p_wrapper)->wait );
893 vlc_mutex_unlock( &((wrapper_t*)p_wrapper)->lock );
895 /* Call the real function */
896 return func( p_data );