1 /*****************************************************************************
2 * threads.c : threads implementation for the VideoLAN client
3 *****************************************************************************
4 * Copyright (C) 1999, 2000, 2001, 2002, 2003 VideoLAN
5 * $Id: threads.c,v 1.44 2003/11/22 00:41:07 titer 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( UNDER_CE )
43 #elif defined( WIN32 )
44 #elif defined( HAVE_KERNEL_SCHEDULER_H )
45 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
46 static pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER;
47 #elif defined( HAVE_CTHREADS_H )
50 /*****************************************************************************
51 * Global variable for named mutexes
52 *****************************************************************************/
53 typedef struct vlc_namedmutex_t vlc_namedmutex_t;
54 struct vlc_namedmutex_t
60 vlc_namedmutex_t *p_next;
63 /*****************************************************************************
64 * vlc_threads_init: initialize threads system
65 *****************************************************************************
66 * This function requires lazy initialization of a global lock in order to
67 * keep the library really thread-safe. Some architectures don't support this
68 * and thus do not guarantee the complete reentrancy.
69 *****************************************************************************/
70 int __vlc_threads_init( vlc_object_t *p_this )
72 static volatile int i_status = VLC_THREADS_UNINITIALIZED;
74 libvlc_t *p_libvlc = (libvlc_t *)p_this;
75 int i_ret = VLC_SUCCESS;
77 /* If we have lazy mutex initialization, use it. Otherwise, we just
78 * hope nothing wrong happens. */
79 #if defined( PTH_INIT_IN_PTH_H )
80 #elif defined( ST_INIT_IN_ST_H )
81 #elif defined( UNDER_CE )
82 #elif defined( WIN32 )
84 #elif defined( HAVE_KERNEL_SCHEDULER_H )
85 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
86 pthread_mutex_lock( &once_mutex );
87 #elif defined( HAVE_CTHREADS_H )
90 if( i_status == VLC_THREADS_UNINITIALIZED )
92 i_status = VLC_THREADS_PENDING;
94 /* We should be safe now. Do all the initialization stuff we want. */
95 p_libvlc->b_ready = VLC_FALSE;
97 #if defined( PTH_INIT_IN_PTH_H )
98 i_ret = ( pth_init() == FALSE );
100 #elif defined( ST_INIT_IN_ST_H )
103 #elif defined( UNDER_CE )
104 /* Nothing to initialize */
106 #elif defined( WIN32 )
107 /* Dynamically get the address of SignalObjectAndWait */
108 if( GetVersion() < 0x80000000 )
110 /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
111 hInstLib = LoadLibrary( "kernel32" );
114 p_libvlc->SignalObjectAndWait =
115 (SIGNALOBJECTANDWAIT)GetProcAddress( hInstLib,
116 "SignalObjectAndWait" );
121 p_libvlc->SignalObjectAndWait = NULL;
124 p_libvlc->b_fast_mutex = 0;
125 p_libvlc->i_win9x_cv = 0;
127 #elif defined( HAVE_KERNEL_SCHEDULER_H )
128 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
129 #elif defined( HAVE_CTHREADS_H )
132 vlc_object_create( p_libvlc, VLC_OBJECT_ROOT );
136 i_status = VLC_THREADS_ERROR;
141 i_status = VLC_THREADS_READY;
146 /* Just increment the initialization count */
150 /* If we have lazy mutex initialization support, unlock the mutex;
151 * otherwize, do a naive wait loop. */
152 #if defined( PTH_INIT_IN_PTH_H )
153 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
154 #elif defined( ST_INIT_IN_ST_H )
155 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
156 #elif defined( UNDER_CE )
157 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
158 #elif defined( WIN32 )
159 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
160 #elif defined( HAVE_KERNEL_SCHEDULER_H )
161 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
162 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
163 pthread_mutex_unlock( &once_mutex );
164 #elif defined( HAVE_CTHREADS_H )
165 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
168 if( i_status != VLC_THREADS_READY )
176 /*****************************************************************************
177 * vlc_threads_end: stop threads system
178 *****************************************************************************
179 * FIXME: This function is far from being threadsafe. We should undo exactly
180 * what we did above in vlc_threads_init.
181 *****************************************************************************/
182 int __vlc_threads_end( vlc_object_t *p_this )
184 #if defined( PTH_INIT_IN_PTH_H )
186 if( i_initializations == 0 )
188 return ( pth_kill() == FALSE );
191 #elif defined( ST_INIT_IN_ST_H )
194 #elif defined( UNDER_CE )
197 #elif defined( WIN32 )
200 #elif defined( HAVE_KERNEL_SCHEDULER_H )
203 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
204 pthread_mutex_lock( &once_mutex );
206 pthread_mutex_unlock( &once_mutex );
208 #elif defined( HAVE_CTHREADS_H )
215 /*****************************************************************************
216 * vlc_mutex_init: initialize a mutex
217 *****************************************************************************/
218 int __vlc_mutex_init( vlc_object_t *p_this, vlc_mutex_t *p_mutex )
220 p_mutex->p_this = p_this;
222 #if defined( PTH_INIT_IN_PTH_H )
223 return ( pth_mutex_init( &p_mutex->mutex ) == FALSE );
225 #elif defined( ST_INIT_IN_ST_H )
226 p_mutex->mutex = st_mutex_new();
227 return ( p_mutex->mutex == NULL ) ? errno : 0;
229 #elif defined( UNDER_CE )
230 InitializeCriticalSection( &p_mutex->csection );
233 #elif defined( WIN32 )
234 /* We use mutexes on WinNT/2K/XP because we can use the SignalObjectAndWait
235 * function and have a 100% correct vlc_cond_wait() implementation.
236 * As this function is not available on Win9x, we can use the faster
237 * CriticalSections */
238 if( p_this->p_libvlc->SignalObjectAndWait &&
239 !p_this->p_libvlc->b_fast_mutex )
241 /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
242 p_mutex->mutex = CreateMutex( 0, FALSE, 0 );
243 return ( p_mutex->mutex != NULL ? 0 : 1 );
247 p_mutex->mutex = NULL;
248 InitializeCriticalSection( &p_mutex->csection );
252 #elif defined( HAVE_KERNEL_SCHEDULER_H )
253 /* check the arguments and whether it's already been initialized */
254 if( p_mutex == NULL )
259 if( p_mutex->init == 9999 )
264 p_mutex->lock = create_sem( 1, "BeMutex" );
265 if( p_mutex->lock < B_NO_ERROR )
270 p_mutex->init = 9999;
273 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
274 # if defined(DEBUG) && defined(SYS_LINUX)
276 /* Create error-checking mutex to detect problems more easily. */
277 pthread_mutexattr_t attr;
280 pthread_mutexattr_init( &attr );
281 pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
282 i_result = pthread_mutex_init( &p_mutex->mutex, &attr );
283 pthread_mutexattr_destroy( &attr );
287 return pthread_mutex_init( &p_mutex->mutex, NULL );
289 #elif defined( HAVE_CTHREADS_H )
290 mutex_init( p_mutex );
296 /*****************************************************************************
297 * vlc_mutex_destroy: destroy a mutex, inner version
298 *****************************************************************************/
299 int __vlc_mutex_destroy( char * psz_file, int i_line, vlc_mutex_t *p_mutex )
302 /* In case of error : */
304 const char * psz_error = "";
306 #if defined( PTH_INIT_IN_PTH_H )
309 #elif defined( ST_INIT_IN_ST_H )
310 i_result = st_mutex_destroy( p_mutex->mutex );
312 #elif defined( UNDER_CE )
313 DeleteCriticalSection( &p_mutex->csection );
316 #elif defined( WIN32 )
319 CloseHandle( p_mutex->mutex );
323 DeleteCriticalSection( &p_mutex->csection );
327 #elif defined( HAVE_KERNEL_SCHEDULER_H )
328 if( p_mutex->init == 9999 )
330 delete_sem( p_mutex->lock );
336 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
337 i_result = pthread_mutex_destroy( &p_mutex->mutex );
340 i_thread = (int)pthread_self();
341 psz_error = strerror(i_result);
344 #elif defined( HAVE_CTHREADS_H )
351 msg_Err( p_mutex->p_this,
352 "thread %d: mutex_destroy failed at %s:%d (%d:%s)",
353 i_thread, psz_file, i_line, i_result, psz_error );
358 /*****************************************************************************
359 * vlc_cond_init: initialize a condition
360 *****************************************************************************/
361 int __vlc_cond_init( vlc_object_t *p_this, vlc_cond_t *p_condvar )
363 p_condvar->p_this = p_this;
365 #if defined( PTH_INIT_IN_PTH_H )
366 return ( pth_cond_init( &p_condvar->cond ) == FALSE );
368 #elif defined( ST_INIT_IN_ST_H )
369 p_condvar->cond = st_cond_new();
370 return ( p_condvar->cond == NULL ) ? errno : 0;
372 #elif defined( UNDER_CE )
373 /* Initialize counter */
374 p_condvar->i_waiting_threads = 0;
376 /* Create an auto-reset event. */
377 p_condvar->event = CreateEvent( NULL, /* no security */
378 FALSE, /* auto-reset event */
379 FALSE, /* start non-signaled */
380 NULL ); /* unnamed */
381 return !p_condvar->event;
383 #elif defined( WIN32 )
384 /* Initialize counter */
385 p_condvar->i_waiting_threads = 0;
388 p_condvar->i_win9x_cv = p_this->p_libvlc->i_win9x_cv;
389 p_condvar->SignalObjectAndWait = p_this->p_libvlc->SignalObjectAndWait;
391 if( (p_condvar->SignalObjectAndWait && !p_this->p_libvlc->b_fast_mutex)
392 || p_condvar->i_win9x_cv == 0 )
394 /* Create an auto-reset event. */
395 p_condvar->event = CreateEvent( NULL, /* no security */
396 FALSE, /* auto-reset event */
397 FALSE, /* start non-signaled */
398 NULL ); /* unnamed */
400 p_condvar->semaphore = NULL;
401 return !p_condvar->event;
405 p_condvar->semaphore = CreateSemaphore( NULL, /* no security */
406 0, /* initial count */
407 0x7fffffff, /* max count */
408 NULL ); /* unnamed */
410 if( p_condvar->i_win9x_cv == 1 )
411 /* Create a manual-reset event initially signaled. */
412 p_condvar->event = CreateEvent( NULL, TRUE, TRUE, NULL );
414 /* Create a auto-reset event. */
415 p_condvar->event = CreateEvent( NULL, FALSE, FALSE, NULL );
417 InitializeCriticalSection( &p_condvar->csection );
419 return !p_condvar->semaphore || !p_condvar->event;
422 #elif defined( HAVE_KERNEL_SCHEDULER_H )
428 if( p_condvar->init == 9999 )
433 p_condvar->thread = -1;
434 p_condvar->init = 9999;
437 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
438 return pthread_cond_init( &p_condvar->cond, NULL );
440 #elif defined( HAVE_CTHREADS_H )
441 /* condition_init() */
442 spin_lock_init( &p_condvar->lock );
443 cthread_queue_init( &p_condvar->queue );
445 p_condvar->implications = 0;
452 /*****************************************************************************
453 * vlc_cond_destroy: destroy a condition, inner version
454 *****************************************************************************/
455 int __vlc_cond_destroy( char * psz_file, int i_line, vlc_cond_t *p_condvar )
458 /* In case of error : */
460 const char * psz_error = "";
462 #if defined( PTH_INIT_IN_PTH_H )
465 #elif defined( ST_INIT_IN_ST_H )
466 i_result = st_cond_destroy( p_condvar->cond );
468 #elif defined( UNDER_CE )
469 i_result = !CloseHandle( p_condvar->event );
471 #elif defined( WIN32 )
472 if( !p_condvar->semaphore )
473 i_result = !CloseHandle( p_condvar->event );
475 i_result = !CloseHandle( p_condvar->event )
476 || !CloseHandle( p_condvar->semaphore );
478 #elif defined( HAVE_KERNEL_SCHEDULER_H )
482 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
483 i_result = pthread_cond_destroy( &p_condvar->cond );
486 i_thread = (int)pthread_self();
487 psz_error = strerror(i_result);
490 #elif defined( HAVE_CTHREADS_H )
497 msg_Err( p_condvar->p_this,
498 "thread %d: cond_destroy failed at %s:%d (%d:%s)",
499 i_thread, psz_file, i_line, i_result, psz_error );
504 /*****************************************************************************
505 * vlc_thread_create: create a thread, inner version
506 *****************************************************************************
507 * Note that i_priority is only taken into account on platforms supporting
508 * userland real-time priority threads.
509 *****************************************************************************/
510 int __vlc_thread_create( vlc_object_t *p_this, char * psz_file, int i_line,
511 char *psz_name, void * ( *func ) ( void * ),
512 int i_priority, vlc_bool_t b_wait )
515 void *p_data = (void *)p_this;
517 vlc_mutex_lock( &p_this->object_lock );
519 #if defined( PTH_INIT_IN_PTH_H )
520 p_this->thread_id = pth_spawn( PTH_ATTR_DEFAULT, func, p_data );
521 i_ret = p_this->thread_id == NULL;
523 #elif defined( ST_INIT_IN_ST_H )
524 p_this->thread_id = st_thread_create( func, p_data, 1, 0 );
527 #elif defined( WIN32 ) || defined( UNDER_CE )
530 /* When using the MSVCRT C library you have to use the _beginthreadex
531 * function instead of CreateThread, otherwise you'll end up with
532 * memory leaks and the signal functions not working (see Microsoft
533 * Knowledge Base, article 104641) */
535 #if defined( UNDER_CE )
536 (HANDLE)CreateThread( NULL, 0, (PTHREAD_START) func,
537 p_data, 0, &threadID );
539 (HANDLE)_beginthreadex( NULL, 0, (PTHREAD_START) func,
540 p_data, 0, &threadID );
544 if ( p_this->thread_id && i_priority )
546 if ( !SetThreadPriority(p_this->thread_id, i_priority) )
548 msg_Warn( p_this, "couldn't set a faster priority" );
553 i_ret = ( p_this->thread_id ? 0 : 1 );
555 #elif defined( HAVE_KERNEL_SCHEDULER_H )
556 p_this->thread_id = spawn_thread( (thread_func)func, psz_name,
557 i_priority, p_data );
558 i_ret = resume_thread( p_this->thread_id );
560 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
561 i_ret = pthread_create( &p_this->thread_id, NULL, func, p_data );
565 && config_GetInt( p_this, "rt-priority" )
569 int i_error, i_policy;
570 struct sched_param param;
571 memset( ¶m, 0, sizeof(struct sched_param) );
572 if ( i_priority < 0 )
574 param.sched_priority = (-1) * i_priority;
575 i_policy = SCHED_OTHER;
579 param.sched_priority = i_priority;
582 if ( (i_error = pthread_setschedparam( p_this->thread_id,
583 i_policy, ¶m )) )
585 msg_Warn( p_this, "couldn't set thread priority (%s:%d): %s",
586 psz_file, i_line, strerror(i_error) );
595 #elif defined( HAVE_CTHREADS_H )
596 p_this->thread_id = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
605 msg_Dbg( p_this, "waiting for thread completion" );
606 vlc_cond_wait( &p_this->object_wait, &p_this->object_lock );
609 p_this->b_thread = 1;
611 msg_Dbg( p_this, "thread %d (%s) created at priority %d (%s:%d)",
612 (int)p_this->thread_id, psz_name, i_priority,
615 vlc_mutex_unlock( &p_this->object_lock );
620 msg_Err( p_this, "%s thread could not be created at %s:%d (%s)",
621 psz_name, psz_file, i_line, strerror(i_ret) );
623 msg_Err( p_this, "%s thread could not be created at %s:%d",
624 psz_name, psz_file, i_line );
626 vlc_mutex_unlock( &p_this->object_lock );
632 /*****************************************************************************
633 * vlc_thread_set_priority: set the priority of the current thread when we
634 * couldn't set it in vlc_thread_create (for instance for the main thread)
635 *****************************************************************************/
636 int __vlc_thread_set_priority( vlc_object_t *p_this, char * psz_file,
637 int i_line, int i_priority )
639 #if defined( PTH_INIT_IN_PTH_H ) || defined( ST_INIT_IN_ST_H )
640 #elif defined( WIN32 ) || defined( UNDER_CE )
641 if ( !SetThreadPriority(GetCurrentThread(), i_priority) )
643 msg_Warn( p_this, "couldn't set a faster priority" );
647 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
652 int i_error, i_policy;
653 struct sched_param param;
654 memset( ¶m, 0, sizeof(struct sched_param) );
655 if ( i_priority < 0 )
657 param.sched_priority = (-1) * i_priority;
658 i_policy = SCHED_OTHER;
662 param.sched_priority = i_priority;
665 if ( (i_error = pthread_setschedparam( pthread_self(),
666 i_policy, ¶m )) )
668 msg_Warn( p_this, "couldn't set thread priority (%s:%d): %s",
669 psz_file, i_line, strerror(i_error) );
680 /*****************************************************************************
681 * vlc_thread_ready: tell the parent thread we were successfully spawned
682 *****************************************************************************/
683 void __vlc_thread_ready( vlc_object_t *p_this )
685 vlc_mutex_lock( &p_this->object_lock );
686 vlc_cond_signal( &p_this->object_wait );
687 vlc_mutex_unlock( &p_this->object_lock );
690 /*****************************************************************************
691 * vlc_thread_join: wait until a thread exits, inner version
692 *****************************************************************************/
693 void __vlc_thread_join( vlc_object_t *p_this, char * psz_file, int i_line )
697 #if defined( PTH_INIT_IN_PTH_H )
698 i_ret = ( pth_join( p_this->thread_id, NULL ) == FALSE );
700 #elif defined( ST_INIT_IN_ST_H )
701 i_ret = st_thread_join( p_this->thread_id, NULL );
703 #elif defined( UNDER_CE )
704 WaitForSingleObject( p_this->thread_id, INFINITE );
706 #elif defined( WIN32 )
707 WaitForSingleObject( p_this->thread_id, INFINITE );
709 #elif defined( HAVE_KERNEL_SCHEDULER_H )
711 wait_for_thread( p_this->thread_id, &exit_value );
713 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
714 i_ret = pthread_join( p_this->thread_id, NULL );
716 #elif defined( HAVE_CTHREADS_H )
717 cthread_join( p_this->thread_id );
725 msg_Err( p_this, "thread_join(%d) failed at %s:%d (%s)",
726 (int)p_this->thread_id, psz_file, i_line,
729 msg_Err( p_this, "thread_join(%d) failed at %s:%d",
730 (int)p_this->thread_id, psz_file, i_line );
735 msg_Dbg( p_this, "thread %d joined (%s:%d)",
736 (int)p_this->thread_id, psz_file, i_line );
739 p_this->b_thread = 0;