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.23 2002/10/16 10:31:58 sam 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 *****************************************************************************/
30 #define VLC_THREADS_UNINITIALIZED 0
31 #define VLC_THREADS_PENDING 1
32 #define VLC_THREADS_ERROR 2
33 #define VLC_THREADS_READY 3
35 /*****************************************************************************
36 * Global mutex for lazy initialization of the threads system
37 *****************************************************************************/
38 static volatile int i_initializations = 0;
40 #if defined( PTH_INIT_IN_PTH_H )
41 #elif defined( ST_INIT_IN_ST_H )
42 #elif defined( WIN32 )
43 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
44 static pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER;
45 #elif defined( HAVE_CTHREADS_H )
46 #elif defined( HAVE_KERNEL_SCHEDULER_H )
49 /*****************************************************************************
50 * Global variable for named mutexes
51 *****************************************************************************/
52 typedef struct vlc_namedmutex_t vlc_namedmutex_t;
53 struct vlc_namedmutex_t
59 vlc_namedmutex_t *p_next;
62 /*****************************************************************************
63 * vlc_threads_init: initialize threads system
64 *****************************************************************************
65 * This function requires lazy initialization of a global lock in order to
66 * keep the library really thread-safe. Some architectures don't support this
67 * and thus do not guarantee the complete reentrancy.
68 *****************************************************************************/
69 int __vlc_threads_init( vlc_object_t *p_this )
71 static volatile int i_status = VLC_THREADS_UNINITIALIZED;
73 libvlc_t *p_libvlc = (libvlc_t *)p_this;
74 int i_ret = VLC_SUCCESS;
76 /* If we have lazy mutex initialization, use it. Otherwise, we just
77 * hope nothing wrong happens. */
78 #if defined( PTH_INIT_IN_PTH_H )
79 #elif defined( ST_INIT_IN_ST_H )
80 #elif defined( WIN32 )
82 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
83 pthread_mutex_lock( &once_mutex );
84 #elif defined( HAVE_CTHREADS_H )
85 #elif defined( HAVE_KERNEL_SCHEDULER_H )
88 if( i_status == VLC_THREADS_UNINITIALIZED )
90 i_status = VLC_THREADS_PENDING;
92 /* We should be safe now. Do all the initialization stuff we want. */
93 vlc_object_create( p_libvlc, VLC_OBJECT_ROOT );
94 p_libvlc->b_ready = VLC_FALSE;
96 #if defined( PTH_INIT_IN_PTH_H )
99 #elif defined( ST_INIT_IN_ST_H )
102 #elif defined( WIN32 )
103 /* Dynamically get the address of SignalObjectAndWait */
104 if( GetVersion() < 0x80000000 )
106 /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
107 hInstLib = LoadLibrary( "kernel32" );
110 p_libvlc->SignalObjectAndWait =
111 (SIGNALOBJECTANDWAIT)GetProcAddress( hInstLib,
112 "SignalObjectAndWait" );
117 p_libvlc->SignalObjectAndWait = NULL;
120 p_libvlc->b_fast_mutex = 0;
121 p_libvlc->i_win9x_cv = 0;
123 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
124 #elif defined( HAVE_CTHREADS_H )
125 #elif defined( HAVE_KERNEL_SCHEDULER_H )
130 i_status = VLC_THREADS_ERROR;
135 i_status = VLC_THREADS_READY;
140 /* Just increment the initialization count */
144 /* If we have lazy mutex initialization support, unlock the mutex;
145 * otherwize, do a naive wait loop. */
146 #if defined( PTH_INIT_IN_PTH_H )
147 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
148 #elif defined( ST_INIT_IN_ST_H )
149 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
150 #elif defined( WIN32 )
151 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
152 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
153 pthread_mutex_unlock( &once_mutex );
154 #elif defined( HAVE_CTHREADS_H )
155 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
156 #elif defined( HAVE_KERNEL_SCHEDULER_H )
157 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
160 if( i_status != VLC_THREADS_READY )
168 /*****************************************************************************
169 * vlc_threads_end: stop threads system
170 *****************************************************************************
171 * FIXME: This function is far from being threadsafe. We should undo exactly
172 * what we did above in vlc_threads_init.
173 *****************************************************************************/
174 int __vlc_threads_end( vlc_object_t *p_this )
176 #if defined( PTH_INIT_IN_PTH_H )
178 if( i_initializations == 0 )
183 #elif defined( ST_INIT_IN_ST_H )
186 #elif defined( WIN32 )
189 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
190 pthread_mutex_lock( &once_mutex );
192 pthread_mutex_unlock( &once_mutex );
194 #elif defined( HAVE_CTHREADS_H )
197 #elif defined( HAVE_KERNEL_SCHEDULER_H )
204 /*****************************************************************************
205 * Prototype for GPROF wrapper
206 *****************************************************************************/
208 /* Wrapper function for profiling */
209 static void * vlc_thread_wrapper ( void *p_wrapper );
213 # define ITIMER_REAL 1
214 # define ITIMER_PROF 2
218 struct timeval it_value;
219 struct timeval it_interval;
222 int setitimer(int kind, const struct itimerval* itnew, struct itimerval* itold);
225 typedef struct wrapper_t
227 /* Data lock access */
231 /* Data used to spawn the real thread */
232 vlc_thread_func_t func;
235 /* Profiling timer passed to the thread */
236 struct itimerval itimer;
242 /*****************************************************************************
243 * vlc_mutex_init: initialize a mutex
244 *****************************************************************************/
245 int __vlc_mutex_init( vlc_object_t *p_this, vlc_mutex_t *p_mutex )
247 p_mutex->p_this = p_this;
249 #if defined( PTH_INIT_IN_PTH_H )
250 return pth_mutex_init( &p_mutex->mutex );
252 #elif defined( ST_INIT_IN_ST_H )
253 p_mutex->mutex = st_mutex_new();
254 return ( p_mutex->mutex == NULL ) ? errno : 0;
256 #elif defined( WIN32 )
257 /* We use mutexes on WinNT/2K/XP because we can use the SignalObjectAndWait
258 * function and have a 100% correct vlc_cond_wait() implementation.
259 * As this function is not available on Win9x, we can use the faster
260 * CriticalSections */
261 if( p_this->p_libvlc->SignalObjectAndWait &&
262 !p_this->p_libvlc->b_fast_mutex )
264 /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
265 p_mutex->mutex = CreateMutex( 0, FALSE, 0 );
266 return ( p_mutex->mutex != NULL ? 0 : 1 );
270 p_mutex->mutex = NULL;
271 InitializeCriticalSection( &p_mutex->csection );
275 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
276 # if defined(DEBUG) && defined(SYS_LINUX)
278 /* Create error-checking mutex to detect problems more easily. */
279 pthread_mutexattr_t attr;
282 pthread_mutexattr_init( &attr );
283 pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
284 i_result = pthread_mutex_init( &p_mutex->mutex, &attr );
285 pthread_mutexattr_destroy( &attr );
289 return pthread_mutex_init( &p_mutex->mutex, NULL );
291 #elif defined( HAVE_CTHREADS_H )
292 mutex_init( p_mutex );
295 #elif defined( HAVE_KERNEL_SCHEDULER_H )
296 /* check the arguments and whether it's already been initialized */
297 if( p_mutex == NULL )
302 if( p_mutex->init == 9999 )
307 p_mutex->lock = create_sem( 1, "BeMutex" );
308 if( p_mutex->lock < B_NO_ERROR )
313 p_mutex->init = 9999;
319 /*****************************************************************************
320 * vlc_mutex_destroy: destroy a mutex, inner version
321 *****************************************************************************/
322 int __vlc_mutex_destroy( char * psz_file, int i_line, vlc_mutex_t *p_mutex )
325 /* In case of error : */
327 const char * psz_error = "";
329 #if defined( PTH_INIT_IN_PTH_H )
332 #elif defined( ST_INIT_IN_ST_H )
333 i_result = st_mutex_destroy( p_mutex->mutex );
335 #elif defined( WIN32 )
338 CloseHandle( p_mutex->mutex );
342 DeleteCriticalSection( &p_mutex->csection );
346 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
347 i_result = pthread_mutex_destroy( &p_mutex->mutex );
350 i_thread = (int)pthread_self();
351 psz_error = strerror(i_result);
354 #elif defined( HAVE_CTHREADS_H )
357 #elif defined( HAVE_KERNEL_SCHEDULER_H )
358 if( p_mutex->init == 9999 )
360 delete_sem( p_mutex->lock );
369 msg_Err( p_mutex->p_this,
370 "thread %d: mutex_destroy failed at %s:%d (%d:%s)",
371 i_thread, psz_file, i_line, i_result, psz_error );
376 /*****************************************************************************
377 * vlc_cond_init: initialize a condition
378 *****************************************************************************/
379 int __vlc_cond_init( vlc_object_t *p_this, vlc_cond_t *p_condvar )
381 p_condvar->p_this = p_this;
383 #if defined( PTH_INIT_IN_PTH_H )
384 return pth_cond_init( &p_condvar->cond );
386 #elif defined( ST_INIT_IN_ST_H )
387 p_condvar->cond = st_cond_new();
388 return ( p_condvar->cond == NULL ) ? errno : 0;
390 #elif defined( WIN32 )
391 /* Initialize counter */
392 p_condvar->i_waiting_threads = 0;
395 p_condvar->i_win9x_cv = p_this->p_libvlc->i_win9x_cv;
396 p_condvar->SignalObjectAndWait = p_this->p_libvlc->SignalObjectAndWait;
398 if( (p_condvar->SignalObjectAndWait && !p_this->p_libvlc->b_fast_mutex)
399 || p_condvar->i_win9x_cv == 0 )
401 /* Create an auto-reset event. */
402 p_condvar->event = CreateEvent( NULL, /* no security */
403 FALSE, /* auto-reset event */
404 FALSE, /* start non-signaled */
405 NULL ); /* unnamed */
407 p_condvar->semaphore = NULL;
408 return !p_condvar->event;
412 p_condvar->semaphore = CreateSemaphore( NULL, /* no security */
413 0, /* initial count */
414 0x7fffffff, /* max count */
415 NULL ); /* unnamed */
417 if( p_condvar->i_win9x_cv == 1 )
418 /* Create a manual-reset event initially signaled. */
419 p_condvar->event = CreateEvent( NULL, TRUE, TRUE, NULL );
421 /* Create a auto-reset event. */
422 p_condvar->event = CreateEvent( NULL, FALSE, FALSE, NULL );
424 InitializeCriticalSection( &p_condvar->csection );
426 return !p_condvar->semaphore || !p_condvar->event;
429 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
430 return pthread_cond_init( &p_condvar->cond, NULL );
432 #elif defined( HAVE_CTHREADS_H )
433 /* condition_init() */
434 spin_lock_init( &p_condvar->lock );
435 cthread_queue_init( &p_condvar->queue );
437 p_condvar->implications = 0;
441 #elif defined( HAVE_KERNEL_SCHEDULER_H )
447 if( p_condvar->init == 9999 )
452 p_condvar->thread = -1;
453 p_condvar->init = 9999;
458 /*****************************************************************************
459 * vlc_cond_destroy: destroy a condition, inner version
460 *****************************************************************************/
461 int __vlc_cond_destroy( char * psz_file, int i_line, vlc_cond_t *p_condvar )
464 /* In case of error : */
466 const char * psz_error = "";
468 #if defined( PTH_INIT_IN_PTH_H )
471 #elif defined( ST_INIT_IN_ST_H )
472 i_result = st_cond_destroy( p_condvar->cond );
474 #elif defined( WIN32 )
475 if( !p_condvar->semaphore )
476 i_result = !CloseHandle( p_condvar->event );
478 i_result = !CloseHandle( p_condvar->event )
479 || !CloseHandle( p_condvar->semaphore );
481 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
482 i_result = pthread_cond_destroy( &p_condvar->cond );
485 i_thread = (int)pthread_self();
486 psz_error = strerror(i_result);
489 #elif defined( HAVE_CTHREADS_H )
492 #elif defined( HAVE_KERNEL_SCHEDULER_H )
499 msg_Err( p_condvar->p_this,
500 "thread %d: cond_destroy failed at %s:%d (%d:%s)",
501 i_thread, psz_file, i_line, i_result, psz_error );
506 /*****************************************************************************
507 * vlc_thread_create: create a thread, inner version
508 *****************************************************************************
509 * Note that i_priority is only taken into account on platforms supporting
510 * userland real-time priority threads.
511 *****************************************************************************/
512 int __vlc_thread_create( vlc_object_t *p_this, char * psz_file, int i_line,
513 char *psz_name, void * ( *func ) ( void * ),
514 int i_priority, vlc_bool_t b_wait )
518 vlc_mutex_lock( &p_this->object_lock );
523 /* Initialize the wrapper structure */
525 wrapper.p_data = (void *)p_this;
526 getitimer( ITIMER_PROF, &wrapper.itimer );
527 vlc_mutex_init( p_this, &wrapper.lock );
528 vlc_cond_init( p_this, &wrapper.wait );
529 vlc_mutex_lock( &wrapper.lock );
531 /* Alter user-passed data so that we call the wrapper instead
532 * of the real function */
534 func = vlc_thread_wrapper;
537 #if defined( PTH_INIT_IN_PTH_H )
538 p_this->thread_id = pth_spawn( PTH_ATTR_DEFAULT, func, (void *)p_this );
541 #elif defined( ST_INIT_IN_ST_H )
542 p_this->thread_id = st_thread_create( func, (void *)p_this, 1, 0 );
545 #elif defined( WIN32 )
548 /* When using the MSVCRT C library you have to use the _beginthreadex
549 * function instead of CreateThread, otherwise you'll end up with
550 * memory leaks and the signal functions not working */
552 (HANDLE)_beginthreadex( NULL, 0, (PTHREAD_START) func,
553 (void *)p_this, 0, &threadID );
556 if ( p_this->thread_id && i_priority )
558 if ( !SetThreadPriority(p_this->thread_id, i_priority) )
560 msg_Warn( p_this, "couldn't set a faster priority" );
565 i_ret = ( p_this->thread_id ? 0 : 1 );
567 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
568 i_ret = pthread_create( &p_this->thread_id, NULL, func, (void *)p_this );
572 struct sched_param param;
573 memset( ¶m, 0, sizeof(struct sched_param) );
574 param.sched_priority = i_priority;
575 if ( pthread_setschedparam( p_this->thread_id, SCHED_RR, ¶m ) )
577 msg_Warn( p_this, "couldn't go to real-time priority" );
582 #elif defined( HAVE_CTHREADS_H )
583 p_this->thread_id = cthread_fork( (cthread_fn_t)func, (any_t)p_this );
586 #elif defined( HAVE_KERNEL_SCHEDULER_H )
587 p_this->thread_id = spawn_thread( (thread_func)func, psz_name,
588 B_NORMAL_PRIORITY, (void *)p_this );
589 i_ret = resume_thread( p_this->thread_id );
596 vlc_cond_wait( &wrapper.wait, &wrapper.lock );
599 vlc_mutex_unlock( &wrapper.lock );
600 vlc_mutex_destroy( &wrapper.lock );
601 vlc_cond_destroy( &wrapper.wait );
608 msg_Dbg( p_this, "waiting for thread completion" );
609 vlc_cond_wait( &p_this->object_wait, &p_this->object_lock );
612 p_this->b_thread = 1;
614 msg_Dbg( p_this, "thread %d (%s) created at priority %d (%s:%d)",
615 p_this->thread_id, psz_name, i_priority,
618 vlc_mutex_unlock( &p_this->object_lock );
622 msg_Err( p_this, "%s thread could not be created at %s:%d (%s)",
623 psz_name, psz_file, i_line, strerror(i_ret) );
624 vlc_mutex_unlock( &p_this->object_lock );
630 /*****************************************************************************
631 * vlc_thread_ready: tell the parent thread we were successfully spawned
632 *****************************************************************************/
633 void __vlc_thread_ready( vlc_object_t *p_this )
635 vlc_mutex_lock( &p_this->object_lock );
636 vlc_cond_signal( &p_this->object_wait );
637 vlc_mutex_unlock( &p_this->object_lock );
640 /*****************************************************************************
641 * vlc_thread_join: wait until a thread exits, inner version
642 *****************************************************************************/
643 void __vlc_thread_join( vlc_object_t *p_this, char * psz_file, int i_line )
647 #if defined( PTH_INIT_IN_PTH_H )
648 i_ret = pth_join( p_this->thread_id, NULL );
650 #elif defined( ST_INIT_IN_ST_H )
651 i_ret = st_thread_join( p_this->thread_id, NULL );
653 #elif defined( WIN32 )
654 WaitForSingleObject( p_this->thread_id, INFINITE );
656 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
657 i_ret = pthread_join( p_this->thread_id, NULL );
659 #elif defined( HAVE_CTHREADS_H )
660 cthread_join( p_this->thread_id );
663 #elif defined( HAVE_KERNEL_SCHEDULER_H )
665 wait_for_thread( p_this->thread_id, &exit_value );
671 msg_Err( p_this, "thread_join(%d) failed at %s:%d (%s)",
672 p_this->thread_id, psz_file, i_line, strerror(i_ret) );
676 msg_Dbg( p_this, "thread %d joined (%s:%d)",
677 p_this->thread_id, psz_file, i_line );
680 p_this->b_thread = 0;
683 /*****************************************************************************
684 * vlc_thread_wrapper: wrapper around thread functions used when profiling.
685 *****************************************************************************/
687 static void *vlc_thread_wrapper( void *p_wrapper )
689 /* Put user data in thread-local variables */
690 void * p_data = ((wrapper_t*)p_wrapper)->p_data;
691 vlc_thread_func_t func = ((wrapper_t*)p_wrapper)->func;
693 /* Set the profile timer value */
694 setitimer( ITIMER_PROF, &((wrapper_t*)p_wrapper)->itimer, NULL );
696 /* Tell the calling thread that we don't need its data anymore */
697 vlc_mutex_lock( &((wrapper_t*)p_wrapper)->lock );
698 vlc_cond_signal( &((wrapper_t*)p_wrapper)->wait );
699 vlc_mutex_unlock( &((wrapper_t*)p_wrapper)->lock );
701 /* Call the real function */
702 return func( p_data );