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.11 2002/07/29 19:05:47 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 * Prototype for GPROF wrapper
35 *****************************************************************************/
37 /* Wrapper function for profiling */
38 static void * vlc_thread_wrapper ( void *p_wrapper );
42 # define ITIMER_REAL 1
43 # define ITIMER_PROF 2
47 struct timeval it_value;
48 struct timeval it_interval;
51 int setitimer(int kind, const struct itimerval* itnew, struct itimerval* itold);
54 typedef struct wrapper_t
56 /* Data lock access */
60 /* Data used to spawn the real thread */
61 vlc_thread_func_t func;
64 /* Profiling timer passed to the thread */
65 struct itimerval itimer;
71 /*****************************************************************************
72 * Global mutexes for lazy initialization of the threads system
73 *****************************************************************************/
74 static volatile int i_initializations = 0;
76 #if defined( PTH_INIT_IN_PTH_H )
78 #elif defined( ST_INIT_IN_ST_H )
80 #elif defined( WIN32 )
82 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
83 static pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER;
84 #elif defined( HAVE_CTHREADS_H )
86 #elif defined( HAVE_KERNEL_SCHEDULER_H )
90 /*****************************************************************************
91 * vlc_threads_init: initialize threads system
92 *****************************************************************************
93 * This function requires lazy initialization of a global lock in order to
94 * keep the library really thread-safe. Some architectures don't support this
95 * and thus do not guarantee the complete reentrancy.
96 *****************************************************************************/
97 int __vlc_threads_init( vlc_object_t *p_this )
99 static volatile int i_status = VLC_THREADS_UNINITIALIZED;
102 #if defined( PTH_INIT_IN_PTH_H )
104 #elif defined( ST_INIT_IN_ST_H )
106 #elif defined( WIN32 )
108 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
109 pthread_mutex_lock( &once_mutex );
110 #elif defined( HAVE_CTHREADS_H )
112 #elif defined( HAVE_KERNEL_SCHEDULER_H )
116 if( i_status == VLC_THREADS_UNINITIALIZED )
118 i_status = VLC_THREADS_PENDING;
120 #if defined( PTH_INIT_IN_PTH_H )
122 #elif defined( ST_INIT_IN_ST_H )
124 #elif defined( WIN32 )
126 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
128 #elif defined( HAVE_CTHREADS_H )
130 #elif defined( HAVE_KERNEL_SCHEDULER_H )
136 i_status = VLC_THREADS_ERROR;
140 vlc_mutex_init( p_this, p_this->p_vlc->p_global_lock );
141 i_status = VLC_THREADS_READY;
146 i_ret = ( i_status == VLC_THREADS_READY );
151 #if defined( PTH_INIT_IN_PTH_H )
153 #elif defined( ST_INIT_IN_ST_H )
155 #elif defined( WIN32 )
157 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
158 pthread_mutex_unlock( &once_mutex );
160 #elif defined( HAVE_CTHREADS_H )
162 #elif defined( HAVE_KERNEL_SCHEDULER_H )
166 /* Wait until the other thread has initialized the thread library */
167 while( i_status == VLC_THREADS_PENDING )
169 msleep( THREAD_SLEEP );
172 return i_status == VLC_THREADS_READY;
175 /*****************************************************************************
176 * vlc_threads_end: stop threads system
177 *****************************************************************************
178 * FIXME: This function is far from being threadsafe. We should undo exactly
179 * what we did above in vlc_threads_init.
180 *****************************************************************************/
181 int __vlc_threads_end( vlc_object_t *p_this )
183 #if defined( PTH_INIT_IN_PTH_H )
185 if( i_initializations == 0 )
191 #elif defined( ST_INIT_IN_ST_H )
195 #elif defined( WIN32 )
199 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
200 pthread_mutex_lock( &once_mutex );
202 pthread_mutex_unlock( &once_mutex );
205 #elif defined( HAVE_CTHREADS_H )
209 #elif defined( HAVE_KERNEL_SCHEDULER_H )
216 /*****************************************************************************
217 * vlc_mutex_init: initialize a mutex
218 *****************************************************************************/
219 int __vlc_mutex_init( vlc_object_t *p_this, vlc_mutex_t *p_mutex )
221 #if defined( PTH_INIT_IN_PTH_H )
222 return pth_mutex_init( p_mutex );
224 #elif defined( ST_INIT_IN_ST_H )
225 *p_mutex = st_mutex_new();
226 return ( *p_mutex == NULL ) ? errno : 0;
228 #elif defined( WIN32 )
229 /* We use mutexes on WinNT/2K/XP because we can use the SignalObjectAndWait
230 * function and have a 100% correct vlc_cond_wait() implementation.
231 * As this function is not available on Win9x, we can use the faster
232 * CriticalSections */
233 if( p_this->p_vlc->SignalObjectAndWait && !p_this->p_vlc->b_fast_mutex )
235 /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
236 p_mutex->mutex = CreateMutex( 0, FALSE, 0 );
237 return ( p_mutex->mutex ? 0 : 1 );
241 p_mutex->mutex = NULL;
242 InitializeCriticalSection( &p_mutex->csection );
246 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
247 # if defined(DEBUG) && defined(SYS_LINUX)
248 /* Create error-checking mutex to detect threads problems more easily. */
249 pthread_mutexattr_t attr;
252 pthread_mutexattr_init( &attr );
253 pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
254 i_result = pthread_mutex_init( p_mutex, &attr );
255 pthread_mutexattr_destroy( &attr );
258 return pthread_mutex_init( p_mutex, NULL );
260 #elif defined( HAVE_CTHREADS_H )
261 mutex_init( p_mutex );
264 #elif defined( HAVE_KERNEL_SCHEDULER_H )
266 /* check the arguments and whether it's already been initialized */
267 if( p_mutex == NULL )
272 if( p_mutex->init == 9999 )
277 p_mutex->lock = create_sem( 1, "BeMutex" );
278 if( p_mutex->lock < B_NO_ERROR )
283 p_mutex->init = 9999;
289 /*****************************************************************************
290 * vlc_mutex_destroy: destroy a mutex, inner version
291 *****************************************************************************/
292 int __vlc_mutex_destroy( char * psz_file, int i_line, vlc_mutex_t *p_mutex )
294 #if defined( PTH_INIT_IN_PTH_H )
297 #elif defined( ST_INIT_IN_ST_H )
298 return st_mutex_destroy( *p_mutex );
300 #elif defined( WIN32 )
303 CloseHandle( p_mutex->mutex );
307 DeleteCriticalSection( &p_mutex->csection );
311 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
312 int i_return = pthread_mutex_destroy( p_mutex );
315 //X intf_ErrMsg( "thread %d error: mutex_destroy failed at %s:%d (%s)",
316 //X pthread_self(), psz_file, i_line, strerror(i_return) );
320 #elif defined( HAVE_CTHREADS_H )
323 #elif defined( HAVE_KERNEL_SCHEDULER_H )
324 if( p_mutex->init == 9999 )
326 delete_sem( p_mutex->lock );
335 /*****************************************************************************
336 * vlc_cond_init: initialize a condition
337 *****************************************************************************/
338 int __vlc_cond_init( vlc_object_t *p_this, vlc_cond_t *p_condvar )
340 #if defined( PTH_INIT_IN_PTH_H )
341 return pth_cond_init( p_condvar );
343 #elif defined( ST_INIT_IN_ST_H )
344 *p_condvar = st_cond_new();
345 return ( *p_condvar == NULL ) ? errno : 0;
347 #elif defined( WIN32 )
348 /* Initialize counter */
349 p_condvar->i_waiting_threads = 0;
352 p_condvar->i_win9x_cv = p_this->p_vlc->i_win9x_cv;
353 p_condvar->SignalObjectAndWait = p_this->p_vlc->SignalObjectAndWait;
355 if( p_this->p_vlc->i_win9x_cv == 0 )
357 /* Create an auto-reset event. */
358 p_condvar->event = CreateEvent( NULL, /* no security */
359 FALSE, /* auto-reset event */
360 FALSE, /* start non-signaled */
361 NULL ); /* unnamed */
363 p_condvar->semaphore = NULL;
364 return !p_condvar->event;
368 p_condvar->semaphore = CreateSemaphore( NULL, /* no security */
369 0, /* initial count */
370 0x7fffffff, /* max count */
371 NULL ); /* unnamed */
373 if( p_this->p_vlc->i_win9x_cv == 1 )
374 /* Create a manual-reset event initially signaled. */
375 p_condvar->event = CreateEvent( NULL, TRUE, TRUE, NULL );
377 /* Create a auto-reset event. */
378 p_condvar->event = CreateEvent( NULL, FALSE, FALSE, NULL );
380 InitializeCriticalSection( &p_condvar->csection );
382 return !p_condvar->semaphore || !p_condvar->event;
385 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
386 return pthread_cond_init( p_condvar, NULL );
388 #elif defined( HAVE_CTHREADS_H )
389 /* condition_init() */
390 spin_lock_init( &p_condvar->lock );
391 cthread_queue_init( &p_condvar->queue );
393 p_condvar->implications = 0;
397 #elif defined( HAVE_KERNEL_SCHEDULER_H )
403 if( p_condvar->init == 9999 )
408 p_condvar->thread = -1;
409 p_condvar->init = 9999;
415 /*****************************************************************************
416 * vlc_cond_destroy: destroy a condition, inner version
417 *****************************************************************************/
418 int __vlc_cond_destroy( char * psz_file, int i_line, vlc_cond_t *p_condvar )
420 #if defined( PTH_INIT_IN_PTH_H )
423 #elif defined( ST_INIT_IN_ST_H )
424 return st_cond_destroy( *p_condvar );
426 #elif defined( WIN32 )
427 if( !p_condvar->semaphore )
428 return !CloseHandle( p_condvar->event );
430 return !CloseHandle( p_condvar->event )
431 || !CloseHandle( p_condvar->semaphore );
433 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
434 int i_result = pthread_cond_destroy( p_condvar );
437 //X intf_ErrMsg( "thread %d error: cond_destroy failed at %s:%d (%s)",
438 //X pthread_self(), psz_file, i_line, strerror(i_result) );
442 #elif defined( HAVE_CTHREADS_H )
445 #elif defined( HAVE_KERNEL_SCHEDULER_H )
452 /*****************************************************************************
453 * vlc_thread_create: create a thread, inner version
454 *****************************************************************************/
455 int __vlc_thread_create( vlc_object_t *p_this, char * psz_file, int i_line,
456 char *psz_name, void * ( *func ) ( void * ),
461 vlc_mutex_lock( &p_this->object_lock );
466 /* Initialize the wrapper structure */
468 wrapper.p_data = (void *)p_this;
469 getitimer( ITIMER_PROF, &wrapper.itimer );
470 vlc_mutex_init( p_this, &wrapper.lock );
471 vlc_cond_init( p_this, &wrapper.wait );
472 vlc_mutex_lock( &wrapper.lock );
474 /* Alter user-passed data so that we call the wrapper instead
475 * of the real function */
477 func = vlc_thread_wrapper;
480 #if defined( PTH_INIT_IN_PTH_H )
481 p_this->thread_id = pth_spawn( PTH_ATTR_DEFAULT, func, (void *)p_this );
484 #elif defined( ST_INIT_IN_ST_H )
485 p_this->thread_id = st_thread_create( func, (void *)p_this, 1, 0 );
488 #elif defined( WIN32 )
491 /* When using the MSVCRT C library you have to use the _beginthreadex
492 * function instead of CreateThread, otherwise you'll end up with
493 * memory leaks and the signal functions not working */
495 (HANDLE)_beginthreadex( NULL, 0, (PTHREAD_START) func,
496 (void *)p_this, 0, &threadID );
499 i_ret = ( p_this->thread_id ? 0 : 1 );
501 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
502 i_ret = pthread_create( &p_this->thread_id, NULL, func, (void *)p_this );
504 #elif defined( HAVE_CTHREADS_H )
505 p_this->thread_id = cthread_fork( (cthread_fn_t)func, (any_t)p_this );
508 #elif defined( HAVE_KERNEL_SCHEDULER_H )
509 p_this->thread_id = spawn_thread( (thread_func)func, psz_name,
510 B_NORMAL_PRIORITY, (void *)p_this );
511 i_ret = resume_thread( p_this->thread_id );
518 vlc_cond_wait( &wrapper.wait, &wrapper.lock );
521 vlc_mutex_unlock( &wrapper.lock );
522 vlc_mutex_destroy( &wrapper.lock );
523 vlc_cond_destroy( &wrapper.wait );
530 msg_Dbg( p_this, "waiting for thread completion" );
531 vlc_cond_wait( &p_this->object_wait, &p_this->object_lock );
534 p_this->b_thread = 1;
536 msg_Dbg( p_this, "thread %d (%s) created (%s:%d)",
537 p_this->thread_id, psz_name, psz_file, i_line );
539 vlc_mutex_unlock( &p_this->object_lock );
543 msg_Err( p_this, "%s thread could not be created at %s:%d (%s)",
544 psz_name, psz_file, i_line, strerror(i_ret) );
545 vlc_mutex_unlock( &p_this->object_lock );
551 /*****************************************************************************
552 * vlc_thread_ready: tell the parent thread we were successfully spawned
553 *****************************************************************************/
554 void __vlc_thread_ready( vlc_object_t *p_this )
556 vlc_mutex_lock( &p_this->object_lock );
557 vlc_cond_signal( &p_this->object_wait );
558 vlc_mutex_unlock( &p_this->object_lock );
561 /*****************************************************************************
562 * vlc_thread_join: wait until a thread exits, inner version
563 *****************************************************************************/
564 void __vlc_thread_join( vlc_object_t *p_this, char * psz_file, int i_line )
568 #if defined( PTH_INIT_IN_PTH_H )
569 i_ret = pth_join( p_this->thread_id, NULL );
571 #elif defined( ST_INIT_IN_ST_H )
572 i_ret = st_thread_join( p_this->thread_id, NULL );
574 #elif defined( WIN32 )
575 WaitForSingleObject( p_this->thread_id, INFINITE );
577 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
578 i_ret = pthread_join( p_this->thread_id, NULL );
580 #elif defined( HAVE_CTHREADS_H )
581 cthread_join( p_this->thread_id );
584 #elif defined( HAVE_KERNEL_SCHEDULER_H )
586 wait_for_thread( p_this->thread_id, &exit_value );
592 msg_Err( p_this, "thread_join(%d) failed at %s:%d (%s)",
593 p_this->thread_id, psz_file, i_line, strerror(i_ret) );
597 msg_Dbg( p_this, "thread %d joined (%s:%d)",
598 p_this->thread_id, psz_file, i_line );
601 p_this->b_thread = 0;
604 /*****************************************************************************
605 * vlc_thread_wrapper: wrapper around thread functions used when profiling.
606 *****************************************************************************/
608 static void *vlc_thread_wrapper( void *p_wrapper )
610 /* Put user data in thread-local variables */
611 void * p_data = ((wrapper_t*)p_wrapper)->p_data;
612 vlc_thread_func_t func = ((wrapper_t*)p_wrapper)->func;
614 /* Set the profile timer value */
615 setitimer( ITIMER_PROF, &((wrapper_t*)p_wrapper)->itimer, NULL );
617 /* Tell the calling thread that we don't need its data anymore */
618 vlc_mutex_lock( &((wrapper_t*)p_wrapper)->lock );
619 vlc_cond_signal( &((wrapper_t*)p_wrapper)->wait );
620 vlc_mutex_unlock( &((wrapper_t*)p_wrapper)->lock );
622 /* Call the real function */
623 return func( p_data );