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.42 2003/10/25 00:49:14 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( UNDER_CE )
43 #elif defined( WIN32 )
44 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
45 static pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER;
46 #elif defined( HAVE_CTHREADS_H )
47 #elif defined( HAVE_KERNEL_SCHEDULER_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( PTHREAD_COND_T_IN_PTHREAD_H )
85 pthread_mutex_lock( &once_mutex );
86 #elif defined( HAVE_CTHREADS_H )
87 #elif defined( HAVE_KERNEL_SCHEDULER_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( PTHREAD_COND_T_IN_PTHREAD_H )
128 #elif defined( HAVE_CTHREADS_H )
129 #elif defined( HAVE_KERNEL_SCHEDULER_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( PTHREAD_COND_T_IN_PTHREAD_H )
161 pthread_mutex_unlock( &once_mutex );
162 #elif defined( HAVE_CTHREADS_H )
163 while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
164 #elif defined( HAVE_KERNEL_SCHEDULER_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( PTHREAD_COND_T_IN_PTHREAD_H )
201 pthread_mutex_lock( &once_mutex );
203 pthread_mutex_unlock( &once_mutex );
205 #elif defined( HAVE_CTHREADS_H )
208 #elif defined( HAVE_KERNEL_SCHEDULER_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( PTHREAD_COND_T_IN_PTHREAD_H )
253 # if defined(DEBUG) && defined(SYS_LINUX)
255 /* Create error-checking mutex to detect problems more easily. */
256 pthread_mutexattr_t attr;
259 pthread_mutexattr_init( &attr );
260 pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
261 i_result = pthread_mutex_init( &p_mutex->mutex, &attr );
262 pthread_mutexattr_destroy( &attr );
266 return pthread_mutex_init( &p_mutex->mutex, NULL );
268 #elif defined( HAVE_CTHREADS_H )
269 mutex_init( p_mutex );
272 #elif defined( HAVE_KERNEL_SCHEDULER_H )
273 /* check the arguments and whether it's already been initialized */
274 if( p_mutex == NULL )
279 if( p_mutex->init == 9999 )
284 p_mutex->lock = create_sem( 1, "BeMutex" );
285 if( p_mutex->lock < B_NO_ERROR )
290 p_mutex->init = 9999;
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( PTHREAD_COND_T_IN_PTHREAD_H )
328 i_result = pthread_mutex_destroy( &p_mutex->mutex );
331 i_thread = (int)pthread_self();
332 psz_error = strerror(i_result);
335 #elif defined( HAVE_CTHREADS_H )
338 #elif defined( HAVE_KERNEL_SCHEDULER_H )
339 if( p_mutex->init == 9999 )
341 delete_sem( p_mutex->lock );
350 msg_Err( p_mutex->p_this,
351 "thread %d: mutex_destroy failed at %s:%d (%d:%s)",
352 i_thread, psz_file, i_line, i_result, psz_error );
357 /*****************************************************************************
358 * vlc_cond_init: initialize a condition
359 *****************************************************************************/
360 int __vlc_cond_init( vlc_object_t *p_this, vlc_cond_t *p_condvar )
362 p_condvar->p_this = p_this;
364 #if defined( PTH_INIT_IN_PTH_H )
365 return ( pth_cond_init( &p_condvar->cond ) == FALSE );
367 #elif defined( ST_INIT_IN_ST_H )
368 p_condvar->cond = st_cond_new();
369 return ( p_condvar->cond == NULL ) ? errno : 0;
371 #elif defined( UNDER_CE )
372 /* Initialize counter */
373 p_condvar->i_waiting_threads = 0;
375 /* Create an auto-reset event. */
376 p_condvar->event = CreateEvent( NULL, /* no security */
377 FALSE, /* auto-reset event */
378 FALSE, /* start non-signaled */
379 NULL ); /* unnamed */
380 return !p_condvar->event;
382 #elif defined( WIN32 )
383 /* Initialize counter */
384 p_condvar->i_waiting_threads = 0;
387 p_condvar->i_win9x_cv = p_this->p_libvlc->i_win9x_cv;
388 p_condvar->SignalObjectAndWait = p_this->p_libvlc->SignalObjectAndWait;
390 if( (p_condvar->SignalObjectAndWait && !p_this->p_libvlc->b_fast_mutex)
391 || p_condvar->i_win9x_cv == 0 )
393 /* Create an auto-reset event. */
394 p_condvar->event = CreateEvent( NULL, /* no security */
395 FALSE, /* auto-reset event */
396 FALSE, /* start non-signaled */
397 NULL ); /* unnamed */
399 p_condvar->semaphore = NULL;
400 return !p_condvar->event;
404 p_condvar->semaphore = CreateSemaphore( NULL, /* no security */
405 0, /* initial count */
406 0x7fffffff, /* max count */
407 NULL ); /* unnamed */
409 if( p_condvar->i_win9x_cv == 1 )
410 /* Create a manual-reset event initially signaled. */
411 p_condvar->event = CreateEvent( NULL, TRUE, TRUE, NULL );
413 /* Create a auto-reset event. */
414 p_condvar->event = CreateEvent( NULL, FALSE, FALSE, NULL );
416 InitializeCriticalSection( &p_condvar->csection );
418 return !p_condvar->semaphore || !p_condvar->event;
421 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
422 return pthread_cond_init( &p_condvar->cond, NULL );
424 #elif defined( HAVE_CTHREADS_H )
425 /* condition_init() */
426 spin_lock_init( &p_condvar->lock );
427 cthread_queue_init( &p_condvar->queue );
429 p_condvar->implications = 0;
433 #elif defined( HAVE_KERNEL_SCHEDULER_H )
439 if( p_condvar->init == 9999 )
444 p_condvar->thread = -1;
445 p_condvar->init = 9999;
450 /*****************************************************************************
451 * vlc_cond_destroy: destroy a condition, inner version
452 *****************************************************************************/
453 int __vlc_cond_destroy( char * psz_file, int i_line, vlc_cond_t *p_condvar )
456 /* In case of error : */
458 const char * psz_error = "";
460 #if defined( PTH_INIT_IN_PTH_H )
463 #elif defined( ST_INIT_IN_ST_H )
464 i_result = st_cond_destroy( p_condvar->cond );
466 #elif defined( UNDER_CE )
467 i_result = !CloseHandle( p_condvar->event );
469 #elif defined( WIN32 )
470 if( !p_condvar->semaphore )
471 i_result = !CloseHandle( p_condvar->event );
473 i_result = !CloseHandle( p_condvar->event )
474 || !CloseHandle( p_condvar->semaphore );
476 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
477 i_result = pthread_cond_destroy( &p_condvar->cond );
480 i_thread = (int)pthread_self();
481 psz_error = strerror(i_result);
484 #elif defined( HAVE_CTHREADS_H )
487 #elif defined( HAVE_KERNEL_SCHEDULER_H )
494 msg_Err( p_condvar->p_this,
495 "thread %d: cond_destroy failed at %s:%d (%d:%s)",
496 i_thread, psz_file, i_line, i_result, psz_error );
501 /*****************************************************************************
502 * vlc_thread_create: create a thread, inner version
503 *****************************************************************************
504 * Note that i_priority is only taken into account on platforms supporting
505 * userland real-time priority threads.
506 *****************************************************************************/
507 int __vlc_thread_create( vlc_object_t *p_this, char * psz_file, int i_line,
508 char *psz_name, void * ( *func ) ( void * ),
509 int i_priority, vlc_bool_t b_wait )
512 void *p_data = (void *)p_this;
514 vlc_mutex_lock( &p_this->object_lock );
516 #if defined( PTH_INIT_IN_PTH_H )
517 p_this->thread_id = pth_spawn( PTH_ATTR_DEFAULT, func, p_data );
518 i_ret = p_this->thread_id == NULL;
520 #elif defined( ST_INIT_IN_ST_H )
521 p_this->thread_id = st_thread_create( func, p_data, 1, 0 );
524 #elif defined( WIN32 ) || defined( UNDER_CE )
527 /* When using the MSVCRT C library you have to use the _beginthreadex
528 * function instead of CreateThread, otherwise you'll end up with
529 * memory leaks and the signal functions not working (see Microsoft
530 * Knowledge Base, article 104641) */
532 #if defined( UNDER_CE )
533 (HANDLE)CreateThread( NULL, 0, (PTHREAD_START) func,
534 p_data, 0, &threadID );
536 (HANDLE)_beginthreadex( NULL, 0, (PTHREAD_START) func,
537 p_data, 0, &threadID );
541 if ( p_this->thread_id && i_priority )
543 if ( !SetThreadPriority(p_this->thread_id, i_priority) )
545 msg_Warn( p_this, "couldn't set a faster priority" );
550 i_ret = ( p_this->thread_id ? 0 : 1 );
552 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
553 i_ret = pthread_create( &p_this->thread_id, NULL, func, p_data );
558 int i_error, i_policy;
559 struct sched_param param;
560 memset( ¶m, 0, sizeof(struct sched_param) );
561 if ( i_priority < 0 )
563 param.sched_priority = (-1) * i_priority;
564 i_policy = SCHED_OTHER;
568 param.sched_priority = i_priority;
571 if ( (i_error = pthread_setschedparam( p_this->thread_id,
572 i_policy, ¶m )) )
574 msg_Warn( p_this, "couldn't set thread priority (%s:%d): %s",
575 psz_file, i_line, strerror(i_error) );
581 #elif defined( HAVE_CTHREADS_H )
582 p_this->thread_id = cthread_fork( (cthread_fn_t)func, (any_t)p_data );
585 #elif defined( HAVE_KERNEL_SCHEDULER_H )
586 p_this->thread_id = spawn_thread( (thread_func)func, psz_name,
587 i_priority, p_data );
588 i_ret = resume_thread( p_this->thread_id );
596 msg_Dbg( p_this, "waiting for thread completion" );
597 vlc_cond_wait( &p_this->object_wait, &p_this->object_lock );
600 p_this->b_thread = 1;
602 msg_Dbg( p_this, "thread %d (%s) created at priority %d (%s:%d)",
603 (int)p_this->thread_id, psz_name, i_priority,
606 vlc_mutex_unlock( &p_this->object_lock );
611 msg_Err( p_this, "%s thread could not be created at %s:%d (%s)",
612 psz_name, psz_file, i_line, strerror(i_ret) );
614 msg_Err( p_this, "%s thread could not be created at %s:%d",
615 psz_name, psz_file, i_line );
617 vlc_mutex_unlock( &p_this->object_lock );
623 /*****************************************************************************
624 * vlc_thread_set_priority: set the priority of the current thread when we
625 * couldn't set it in vlc_thread_create (for instance for the main thread)
626 *****************************************************************************/
627 int __vlc_thread_set_priority( vlc_object_t *p_this, char * psz_file,
628 int i_line, int i_priority )
630 #if defined( PTH_INIT_IN_PTH_H ) || defined( ST_INIT_IN_ST_H )
631 #elif defined( WIN32 ) || defined( UNDER_CE )
632 if ( !SetThreadPriority(GetCurrentThread(), i_priority) )
634 msg_Warn( p_this, "couldn't set a faster priority" );
638 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
643 int i_error, i_policy;
644 struct sched_param param;
645 memset( ¶m, 0, sizeof(struct sched_param) );
646 if ( i_priority < 0 )
648 param.sched_priority = (-1) * i_priority;
649 i_policy = SCHED_OTHER;
653 param.sched_priority = i_priority;
656 if ( (i_error = pthread_setschedparam( pthread_self(),
657 i_policy, ¶m )) )
659 msg_Warn( p_this, "couldn't set thread priority (%s:%d): %s",
660 psz_file, i_line, strerror(i_error) );
671 /*****************************************************************************
672 * vlc_thread_ready: tell the parent thread we were successfully spawned
673 *****************************************************************************/
674 void __vlc_thread_ready( vlc_object_t *p_this )
676 vlc_mutex_lock( &p_this->object_lock );
677 vlc_cond_signal( &p_this->object_wait );
678 vlc_mutex_unlock( &p_this->object_lock );
681 /*****************************************************************************
682 * vlc_thread_join: wait until a thread exits, inner version
683 *****************************************************************************/
684 void __vlc_thread_join( vlc_object_t *p_this, char * psz_file, int i_line )
688 #if defined( PTH_INIT_IN_PTH_H )
689 i_ret = ( pth_join( p_this->thread_id, NULL ) == FALSE );
691 #elif defined( ST_INIT_IN_ST_H )
692 i_ret = st_thread_join( p_this->thread_id, NULL );
694 #elif defined( UNDER_CE )
695 WaitForSingleObject( p_this->thread_id, INFINITE );
697 #elif defined( WIN32 )
698 WaitForSingleObject( p_this->thread_id, INFINITE );
700 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
701 i_ret = pthread_join( p_this->thread_id, NULL );
703 #elif defined( HAVE_CTHREADS_H )
704 cthread_join( p_this->thread_id );
707 #elif defined( HAVE_KERNEL_SCHEDULER_H )
709 wait_for_thread( p_this->thread_id, &exit_value );
716 msg_Err( p_this, "thread_join(%d) failed at %s:%d (%s)",
717 (int)p_this->thread_id, psz_file, i_line,
720 msg_Err( p_this, "thread_join(%d) failed at %s:%d",
721 (int)p_this->thread_id, psz_file, i_line );
726 msg_Dbg( p_this, "thread %d joined (%s:%d)",
727 (int)p_this->thread_id, psz_file, i_line );
730 p_this->b_thread = 0;