1 /*****************************************************************************
2 * threads.c : threads implementation for the VideoLAN client
3 *****************************************************************************
4 * Copyright (C) 1999, 2000, 2001, 2002 VideoLAN
5 * $Id: threads.c,v 1.19 2002/10/03 17:01:58 gbazin Exp $
7 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * Gildas Bazin <gbazin@netcourrier.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
28 #define VLC_THREADS_UNINITIALIZED 0
29 #define VLC_THREADS_PENDING 1
30 #define VLC_THREADS_ERROR 2
31 #define VLC_THREADS_READY 3
33 /*****************************************************************************
34 * Global mutex for lazy initialization of the threads system
35 *****************************************************************************/
36 static volatile int i_initializations = 0;
38 #if defined( PTH_INIT_IN_PTH_H )
39 #elif defined( ST_INIT_IN_ST_H )
40 #elif defined( WIN32 )
41 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
42 static pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER;
43 #elif defined( HAVE_CTHREADS_H )
44 #elif defined( HAVE_KERNEL_SCHEDULER_H )
47 /*****************************************************************************
48 * vlc_threads_init: initialize threads system
49 *****************************************************************************
50 * This function requires lazy initialization of a global lock in order to
51 * keep the library really thread-safe. Some architectures don't support this
52 * and thus do not guarantee the complete reentrancy.
53 *****************************************************************************/
54 int __vlc_threads_init( vlc_object_t *p_this )
56 static volatile int i_status = VLC_THREADS_UNINITIALIZED;
58 libvlc_t *p_libvlc = (libvlc_t *)p_this;
59 int i_ret = VLC_SUCCESS;
61 /* If we have lazy mutex initialization, use it. Otherwise, we just
62 * hope nothing wrong happens. */
63 #if defined( PTH_INIT_IN_PTH_H )
64 #elif defined( ST_INIT_IN_ST_H )
65 #elif defined( WIN32 )
67 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
68 pthread_mutex_lock( &once_mutex );
69 #elif defined( HAVE_CTHREADS_H )
70 #elif defined( HAVE_KERNEL_SCHEDULER_H )
73 if( i_status == VLC_THREADS_UNINITIALIZED )
75 i_status = VLC_THREADS_PENDING;
77 /* We should be safe now. Do all the initialization stuff we want. */
78 vlc_object_create( p_libvlc, VLC_OBJECT_ROOT );
79 p_libvlc->b_ready = VLC_FALSE;
81 #if defined( PTH_INIT_IN_PTH_H )
84 #elif defined( ST_INIT_IN_ST_H )
87 #elif defined( WIN32 )
88 /* Dynamically get the address of SignalObjectAndWait */
89 if( GetVersion() < 0x80000000 )
91 /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
92 hInstLib = LoadLibrary( "kernel32" );
95 p_libvlc->SignalObjectAndWait =
96 (SIGNALOBJECTANDWAIT)GetProcAddress( hInstLib,
97 "SignalObjectAndWait" );
102 p_libvlc->SignalObjectAndWait = NULL;
105 p_libvlc->b_fast_mutex = 0;
106 p_libvlc->i_win9x_cv = 0;
108 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
109 #elif defined( HAVE_CTHREADS_H )
110 #elif defined( HAVE_KERNEL_SCHEDULER_H )
113 vlc_mutex_init( p_libvlc, &p_libvlc->global_lock );
117 i_status = VLC_THREADS_ERROR;
122 i_status = VLC_THREADS_READY;
127 /* Just increment the initialization count */
131 /* If we have lazy mutex initialization support, unlock the mutex;
132 * otherwize, do a naive wait loop. */
133 #if defined( PTH_INIT_IN_PTH_H )
134 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
135 #elif defined( ST_INIT_IN_ST_H )
136 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
137 #elif defined( WIN32 )
138 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
139 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
140 pthread_mutex_unlock( &once_mutex );
141 #elif defined( HAVE_CTHREADS_H )
142 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
143 #elif defined( HAVE_KERNEL_SCHEDULER_H )
144 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
147 if( i_status != VLC_THREADS_READY )
155 /*****************************************************************************
156 * vlc_threads_end: stop threads system
157 *****************************************************************************
158 * FIXME: This function is far from being threadsafe. We should undo exactly
159 * what we did above in vlc_threads_init.
160 *****************************************************************************/
161 int __vlc_threads_end( vlc_object_t *p_this )
163 #if defined( PTH_INIT_IN_PTH_H )
165 if( i_initializations == 0 )
170 #elif defined( ST_INIT_IN_ST_H )
173 #elif defined( WIN32 )
176 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
177 pthread_mutex_lock( &once_mutex );
179 pthread_mutex_unlock( &once_mutex );
181 #elif defined( HAVE_CTHREADS_H )
184 #elif defined( HAVE_KERNEL_SCHEDULER_H )
191 /*****************************************************************************
192 * Prototype for GPROF wrapper
193 *****************************************************************************/
195 /* Wrapper function for profiling */
196 static void * vlc_thread_wrapper ( void *p_wrapper );
200 # define ITIMER_REAL 1
201 # define ITIMER_PROF 2
205 struct timeval it_value;
206 struct timeval it_interval;
209 int setitimer(int kind, const struct itimerval* itnew, struct itimerval* itold);
212 typedef struct wrapper_t
214 /* Data lock access */
218 /* Data used to spawn the real thread */
219 vlc_thread_func_t func;
222 /* Profiling timer passed to the thread */
223 struct itimerval itimer;
229 /*****************************************************************************
230 * vlc_mutex_init: initialize a mutex
231 *****************************************************************************/
232 int __vlc_mutex_init( vlc_object_t *p_this, vlc_mutex_t *p_mutex )
234 p_mutex->p_this = p_this;
236 #if defined( PTH_INIT_IN_PTH_H )
237 return pth_mutex_init( &p_mutex->mutex );
239 #elif defined( ST_INIT_IN_ST_H )
240 p_mutex->mutex = st_mutex_new();
241 return ( p_mutex->mutex == NULL ) ? errno : 0;
243 #elif defined( WIN32 )
244 /* We use mutexes on WinNT/2K/XP because we can use the SignalObjectAndWait
245 * function and have a 100% correct vlc_cond_wait() implementation.
246 * As this function is not available on Win9x, we can use the faster
247 * CriticalSections */
248 if( p_this->p_vlc->SignalObjectAndWait && !p_this->p_vlc->b_fast_mutex )
250 /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
251 p_mutex->mutex = CreateMutex( 0, FALSE, 0 );
252 return ( p_mutex->mutex != NULL ? 0 : 1 );
256 p_mutex->mutex = NULL;
257 InitializeCriticalSection( &p_mutex->csection );
261 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
262 # if defined(DEBUG) && defined(SYS_LINUX)
264 /* Create error-checking mutex to detect problems more easily. */
265 pthread_mutexattr_t attr;
268 pthread_mutexattr_init( &attr );
269 pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
270 i_result = pthread_mutex_init( &p_mutex->mutex, &attr );
271 pthread_mutexattr_destroy( &attr );
275 return pthread_mutex_init( &p_mutex->mutex, NULL );
277 #elif defined( HAVE_CTHREADS_H )
278 mutex_init( p_mutex );
281 #elif defined( HAVE_KERNEL_SCHEDULER_H )
282 /* check the arguments and whether it's already been initialized */
283 if( p_mutex == NULL )
288 if( p_mutex->init == 9999 )
293 p_mutex->lock = create_sem( 1, "BeMutex" );
294 if( p_mutex->lock < B_NO_ERROR )
299 p_mutex->init = 9999;
305 /*****************************************************************************
306 * vlc_mutex_destroy: destroy a mutex, inner version
307 *****************************************************************************/
308 int __vlc_mutex_destroy( char * psz_file, int i_line, vlc_mutex_t *p_mutex )
311 /* In case of error : */
313 const char * psz_error = "";
315 #if defined( PTH_INIT_IN_PTH_H )
318 #elif defined( ST_INIT_IN_ST_H )
319 i_result = st_mutex_destroy( p_mutex->mutex );
321 #elif defined( WIN32 )
324 CloseHandle( p_mutex->mutex );
328 DeleteCriticalSection( &p_mutex->csection );
332 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
333 i_result = pthread_mutex_destroy( &p_mutex->mutex );
336 i_thread = (int)pthread_self();
337 psz_error = strerror(i_result);
340 #elif defined( HAVE_CTHREADS_H )
343 #elif defined( HAVE_KERNEL_SCHEDULER_H )
344 if( p_mutex->init == 9999 )
346 delete_sem( p_mutex->lock );
355 msg_Err( p_mutex->p_this,
356 "thread %d: mutex_destroy failed at %s:%d (%d:%s)",
357 i_thread, psz_file, i_line, i_result, psz_error );
362 /*****************************************************************************
363 * vlc_cond_init: initialize a condition
364 *****************************************************************************/
365 int __vlc_cond_init( vlc_object_t *p_this, vlc_cond_t *p_condvar )
367 p_condvar->p_this = p_this;
369 #if defined( PTH_INIT_IN_PTH_H )
370 return pth_cond_init( &p_condvar->cond );
372 #elif defined( ST_INIT_IN_ST_H )
373 p_condvar->cond = st_cond_new();
374 return ( p_condvar->cond == NULL ) ? errno : 0;
376 #elif defined( WIN32 )
377 /* Initialize counter */
378 p_condvar->i_waiting_threads = 0;
381 p_condvar->i_win9x_cv = p_this->p_vlc->i_win9x_cv;
382 p_condvar->SignalObjectAndWait = p_this->p_vlc->SignalObjectAndWait;
384 if( (p_condvar->SignalObjectAndWait && !p_this->p_vlc->b_fast_mutex)
385 || p_condvar->i_win9x_cv == 0 )
387 /* Create an auto-reset event. */
388 p_condvar->event = CreateEvent( NULL, /* no security */
389 FALSE, /* auto-reset event */
390 FALSE, /* start non-signaled */
391 NULL ); /* unnamed */
393 p_condvar->semaphore = NULL;
394 return !p_condvar->event;
398 p_condvar->semaphore = CreateSemaphore( NULL, /* no security */
399 0, /* initial count */
400 0x7fffffff, /* max count */
401 NULL ); /* unnamed */
403 if( p_this->p_vlc->i_win9x_cv == 1 )
404 /* Create a manual-reset event initially signaled. */
405 p_condvar->event = CreateEvent( NULL, TRUE, TRUE, NULL );
407 /* Create a auto-reset event. */
408 p_condvar->event = CreateEvent( NULL, FALSE, FALSE, NULL );
410 InitializeCriticalSection( &p_condvar->csection );
412 return !p_condvar->semaphore || !p_condvar->event;
415 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
416 return pthread_cond_init( &p_condvar->cond, NULL );
418 #elif defined( HAVE_CTHREADS_H )
419 /* condition_init() */
420 spin_lock_init( &p_condvar->lock );
421 cthread_queue_init( &p_condvar->queue );
423 p_condvar->implications = 0;
427 #elif defined( HAVE_KERNEL_SCHEDULER_H )
433 if( p_condvar->init == 9999 )
438 p_condvar->thread = -1;
439 p_condvar->init = 9999;
444 /*****************************************************************************
445 * vlc_cond_destroy: destroy a condition, inner version
446 *****************************************************************************/
447 int __vlc_cond_destroy( char * psz_file, int i_line, vlc_cond_t *p_condvar )
450 /* In case of error : */
452 const char * psz_error = "";
454 #if defined( PTH_INIT_IN_PTH_H )
457 #elif defined( ST_INIT_IN_ST_H )
458 i_result = st_cond_destroy( p_condvar->cond );
460 #elif defined( WIN32 )
461 if( !p_condvar->semaphore )
462 i_result = !CloseHandle( p_condvar->event );
464 i_result = !CloseHandle( p_condvar->event )
465 || !CloseHandle( p_condvar->semaphore );
467 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
468 i_result = pthread_cond_destroy( &p_condvar->cond );
471 i_thread = (int)pthread_self();
472 psz_error = strerror(i_result);
475 #elif defined( HAVE_CTHREADS_H )
478 #elif defined( HAVE_KERNEL_SCHEDULER_H )
485 msg_Err( p_condvar->p_this,
486 "thread %d: cond_destroy failed at %s:%d (%d:%s)",
487 i_thread, psz_file, i_line, i_result, psz_error );
492 /*****************************************************************************
493 * vlc_thread_create: create a thread, inner version
494 *****************************************************************************
495 * Note that i_priority is only taken into account on platforms supporting
496 * userland real-time priority threads.
497 *****************************************************************************/
498 int __vlc_thread_create( vlc_object_t *p_this, char * psz_file, int i_line,
499 char *psz_name, void * ( *func ) ( void * ),
500 int i_priority, vlc_bool_t b_wait )
504 vlc_mutex_lock( &p_this->object_lock );
509 /* Initialize the wrapper structure */
511 wrapper.p_data = (void *)p_this;
512 getitimer( ITIMER_PROF, &wrapper.itimer );
513 vlc_mutex_init( p_this, &wrapper.lock );
514 vlc_cond_init( p_this, &wrapper.wait );
515 vlc_mutex_lock( &wrapper.lock );
517 /* Alter user-passed data so that we call the wrapper instead
518 * of the real function */
520 func = vlc_thread_wrapper;
523 #if defined( PTH_INIT_IN_PTH_H )
524 p_this->thread_id = pth_spawn( PTH_ATTR_DEFAULT, func, (void *)p_this );
527 #elif defined( ST_INIT_IN_ST_H )
528 p_this->thread_id = st_thread_create( func, (void *)p_this, 1, 0 );
531 #elif defined( WIN32 )
534 /* When using the MSVCRT C library you have to use the _beginthreadex
535 * function instead of CreateThread, otherwise you'll end up with
536 * memory leaks and the signal functions not working */
538 (HANDLE)_beginthreadex( NULL, 0, (PTHREAD_START) func,
539 (void *)p_this, 0, &threadID );
542 if ( p_this->thread_id && i_priority )
544 if ( !SetThreadPriority(p_this->thread_id, i_priority) )
546 msg_Warn( p_this, "couldn't set a faster priority" );
551 i_ret = ( p_this->thread_id ? 0 : 1 );
553 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
554 i_ret = pthread_create( &p_this->thread_id, NULL, func, (void *)p_this );
558 struct sched_param param;
559 memset( ¶m, 0, sizeof(struct sched_param) );
560 param.sched_priority = i_priority;
561 if ( pthread_setschedparam( p_this->thread_id, SCHED_RR, ¶m ) )
563 msg_Warn( p_this, "couldn't go to real-time priority" );
568 #elif defined( HAVE_CTHREADS_H )
569 p_this->thread_id = cthread_fork( (cthread_fn_t)func, (any_t)p_this );
572 #elif defined( HAVE_KERNEL_SCHEDULER_H )
573 p_this->thread_id = spawn_thread( (thread_func)func, psz_name,
574 B_NORMAL_PRIORITY, (void *)p_this );
575 i_ret = resume_thread( p_this->thread_id );
582 vlc_cond_wait( &wrapper.wait, &wrapper.lock );
585 vlc_mutex_unlock( &wrapper.lock );
586 vlc_mutex_destroy( &wrapper.lock );
587 vlc_cond_destroy( &wrapper.wait );
594 msg_Dbg( p_this, "waiting for thread completion" );
595 vlc_cond_wait( &p_this->object_wait, &p_this->object_lock );
598 p_this->b_thread = 1;
600 msg_Dbg( p_this, "thread %d (%s) created at priority %d (%s:%d)",
601 p_this->thread_id, psz_name, i_priority,
604 vlc_mutex_unlock( &p_this->object_lock );
608 msg_Err( p_this, "%s thread could not be created at %s:%d (%s)",
609 psz_name, psz_file, i_line, strerror(i_ret) );
610 vlc_mutex_unlock( &p_this->object_lock );
616 /*****************************************************************************
617 * vlc_thread_ready: tell the parent thread we were successfully spawned
618 *****************************************************************************/
619 void __vlc_thread_ready( vlc_object_t *p_this )
621 vlc_mutex_lock( &p_this->object_lock );
622 vlc_cond_signal( &p_this->object_wait );
623 vlc_mutex_unlock( &p_this->object_lock );
626 /*****************************************************************************
627 * vlc_thread_join: wait until a thread exits, inner version
628 *****************************************************************************/
629 void __vlc_thread_join( vlc_object_t *p_this, char * psz_file, int i_line )
633 #if defined( PTH_INIT_IN_PTH_H )
634 i_ret = pth_join( p_this->thread_id, NULL );
636 #elif defined( ST_INIT_IN_ST_H )
637 i_ret = st_thread_join( p_this->thread_id, NULL );
639 #elif defined( WIN32 )
640 WaitForSingleObject( p_this->thread_id, INFINITE );
642 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
643 i_ret = pthread_join( p_this->thread_id, NULL );
645 #elif defined( HAVE_CTHREADS_H )
646 cthread_join( p_this->thread_id );
649 #elif defined( HAVE_KERNEL_SCHEDULER_H )
651 wait_for_thread( p_this->thread_id, &exit_value );
657 msg_Err( p_this, "thread_join(%d) failed at %s:%d (%s)",
658 p_this->thread_id, psz_file, i_line, strerror(i_ret) );
662 msg_Dbg( p_this, "thread %d joined (%s:%d)",
663 p_this->thread_id, psz_file, i_line );
666 p_this->b_thread = 0;
669 /*****************************************************************************
670 * vlc_thread_wrapper: wrapper around thread functions used when profiling.
671 *****************************************************************************/
673 static void *vlc_thread_wrapper( void *p_wrapper )
675 /* Put user data in thread-local variables */
676 void * p_data = ((wrapper_t*)p_wrapper)->p_data;
677 vlc_thread_func_t func = ((wrapper_t*)p_wrapper)->func;
679 /* Set the profile timer value */
680 setitimer( ITIMER_PROF, &((wrapper_t*)p_wrapper)->itimer, NULL );
682 /* Tell the calling thread that we don't need its data anymore */
683 vlc_mutex_lock( &((wrapper_t*)p_wrapper)->lock );
684 vlc_cond_signal( &((wrapper_t*)p_wrapper)->wait );
685 vlc_mutex_unlock( &((wrapper_t*)p_wrapper)->lock );
687 /* Call the real function */
688 return func( p_data );