1 /*****************************************************************************
2 * vlc_threads_funcs.h : threads implementation for the VideoLAN client
3 * This header provides a portable threads implementation.
4 *****************************************************************************
5 * Copyright (C) 1999, 2002 VideoLAN
6 * $Id: vlc_threads_funcs.h,v 1.16 2003/11/22 00:41:07 titer Exp $
8 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
9 * Samuel Hocevar <sam@via.ecp.fr>
10 * Gildas Bazin <gbazin@netcourrier.com>
11 * Christophe Massiot <massiot@via.ecp.fr>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
26 *****************************************************************************/
28 /*****************************************************************************
29 * Function definitions
30 *****************************************************************************/
31 VLC_EXPORT( int, __vlc_threads_init, ( vlc_object_t * ) );
32 VLC_EXPORT( int, __vlc_threads_end, ( vlc_object_t * ) );
33 VLC_EXPORT( int, __vlc_mutex_init, ( vlc_object_t *, vlc_mutex_t * ) );
34 VLC_EXPORT( int, __vlc_mutex_destroy, ( char *, int, vlc_mutex_t * ) );
35 VLC_EXPORT( int, __vlc_cond_init, ( vlc_object_t *, vlc_cond_t * ) );
36 VLC_EXPORT( int, __vlc_cond_destroy, ( char *, int, vlc_cond_t * ) );
37 VLC_EXPORT( int, __vlc_thread_create, ( vlc_object_t *, char *, int, char *, void * ( * ) ( void * ), int, vlc_bool_t ) );
38 VLC_EXPORT( int, __vlc_thread_set_priority, ( vlc_object_t *, char *, int, int ) );
39 VLC_EXPORT( void, __vlc_thread_ready, ( vlc_object_t * ) );
40 VLC_EXPORT( void, __vlc_thread_join, ( vlc_object_t *, char *, int ) );
43 /*****************************************************************************
44 * vlc_threads_init: initialize threads system
45 *****************************************************************************/
46 #define vlc_threads_init( P_THIS ) \
47 __vlc_threads_init( VLC_OBJECT(P_THIS) )
49 /*****************************************************************************
50 * vlc_threads_end: deinitialize threads system
51 *****************************************************************************/
52 #define vlc_threads_end( P_THIS ) \
53 __vlc_threads_end( VLC_OBJECT(P_THIS) )
55 /*****************************************************************************
56 * vlc_mutex_init: initialize a mutex
57 *****************************************************************************/
58 #define vlc_mutex_init( P_THIS, P_MUTEX ) \
59 __vlc_mutex_init( VLC_OBJECT(P_THIS), P_MUTEX )
61 /*****************************************************************************
62 * vlc_mutex_lock: lock a mutex
63 *****************************************************************************/
64 #define vlc_mutex_lock( P_MUTEX ) \
65 __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
67 static inline int __vlc_mutex_lock( char * psz_file, int i_line,
68 vlc_mutex_t * p_mutex )
71 /* In case of error : */
73 const char * psz_error = "";
75 #if defined( PTH_INIT_IN_PTH_H )
76 i_result = ( pth_mutex_acquire( &p_mutex->mutex, FALSE, NULL ) == FALSE );
78 #elif defined( ST_INIT_IN_ST_H )
79 i_result = st_mutex_lock( p_mutex->mutex );
81 #elif defined( UNDER_CE )
82 EnterCriticalSection( &p_mutex->csection );
85 #elif defined( WIN32 )
88 WaitForSingleObject( p_mutex->mutex, INFINITE );
92 EnterCriticalSection( &p_mutex->csection );
96 #elif defined( HAVE_KERNEL_SCHEDULER_H )
99 i_result = B_BAD_VALUE;
101 else if( p_mutex->init < 2000 )
103 i_result = B_NO_INIT;
107 i_result = acquire_sem( p_mutex->lock );
110 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
111 i_result = pthread_mutex_lock( &p_mutex->mutex );
114 i_thread = (int)pthread_self();
115 psz_error = strerror(i_result);
118 #elif defined( HAVE_CTHREADS_H )
119 mutex_lock( p_mutex->mutex );
126 msg_Err( p_mutex->p_this,
127 "thread %d: mutex_lock failed at %s:%d (%d:%s)",
128 i_thread, psz_file, i_line, i_result, psz_error );
133 /*****************************************************************************
134 * vlc_mutex_unlock: unlock a mutex
135 *****************************************************************************/
136 #define vlc_mutex_unlock( P_MUTEX ) \
137 __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
139 static inline int __vlc_mutex_unlock( char * psz_file, int i_line,
140 vlc_mutex_t *p_mutex )
143 /* In case of error : */
145 const char * psz_error = "";
147 #if defined( PTH_INIT_IN_PTH_H )
148 i_result = ( pth_mutex_release( &p_mutex->mutex ) == FALSE );
150 #elif defined( ST_INIT_IN_ST_H )
151 i_result = st_mutex_unlock( p_mutex->mutex );
153 #elif defined( UNDER_CE )
154 LeaveCriticalSection( &p_mutex->csection );
157 #elif defined( WIN32 )
160 ReleaseMutex( p_mutex->mutex );
164 LeaveCriticalSection( &p_mutex->csection );
168 #elif defined( HAVE_KERNEL_SCHEDULER_H )
169 if( p_mutex == NULL )
171 i_result = B_BAD_VALUE;
173 else if( p_mutex->init < 2000 )
175 i_result = B_NO_INIT;
179 release_sem( p_mutex->lock );
183 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
184 i_result = pthread_mutex_unlock( &p_mutex->mutex );
187 i_thread = (int)pthread_self();
188 psz_error = strerror(i_result);
191 #elif defined( HAVE_CTHREADS_H )
192 mutex_unlock( p_mutex );
199 msg_Err( p_mutex->p_this,
200 "thread %d: mutex_unlock failed at %s:%d (%d:%s)",
201 i_thread, psz_file, i_line, i_result, psz_error );
207 /*****************************************************************************
208 * vlc_mutex_destroy: destroy a mutex
209 *****************************************************************************/
210 #define vlc_mutex_destroy( P_MUTEX ) \
211 __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
213 /*****************************************************************************
214 * vlc_cond_init: initialize a condition
215 *****************************************************************************/
216 #define vlc_cond_init( P_THIS, P_COND ) \
217 __vlc_cond_init( VLC_OBJECT(P_THIS), P_COND )
219 /*****************************************************************************
220 * vlc_cond_signal: start a thread on condition completion
221 *****************************************************************************/
222 #define vlc_cond_signal( P_COND ) \
223 __vlc_cond_signal( __FILE__, __LINE__, P_COND )
225 static inline int __vlc_cond_signal( char * psz_file, int i_line,
226 vlc_cond_t *p_condvar )
229 /* In case of error : */
231 const char * psz_error = "";
233 #if defined( PTH_INIT_IN_PTH_H )
234 i_result = ( pth_cond_notify( &p_condvar->cond, FALSE ) == FALSE );
236 #elif defined( ST_INIT_IN_ST_H )
237 i_result = st_cond_signal( p_condvar->cond );
239 #elif defined( UNDER_CE )
240 PulseEvent( p_condvar->event );
243 #elif defined( WIN32 )
244 /* Release one waiting thread if one is available. */
245 /* For this trick to work properly, the vlc_cond_signal must be surrounded
246 * by a mutex. This will prevent another thread from stealing the signal */
247 if( !p_condvar->semaphore )
249 PulseEvent( p_condvar->event );
251 else if( p_condvar->i_win9x_cv == 1 )
253 /* Wait for the gate to be open */
254 WaitForSingleObject( p_condvar->event, INFINITE );
256 if( p_condvar->i_waiting_threads )
258 /* Using a semaphore exposes us to a race condition. It is
259 * possible for another thread to start waiting on the semaphore
260 * just after we signaled it and thus steal the signal.
261 * We have to prevent new threads from entering the cond_wait(). */
262 ResetEvent( p_condvar->event );
264 /* A semaphore is used here because Win9x doesn't have
265 * SignalObjectAndWait() and thus a race condition exists
266 * during the time we release the mutex and the time we start
267 * waiting on the event (more precisely, the signal can sometimes
268 * be missed by the waiting thread if we use PulseEvent()). */
269 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
274 if( p_condvar->i_waiting_threads )
276 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
278 /* Wait for the last thread to be awakened */
279 WaitForSingleObject( p_condvar->event, INFINITE );
284 #elif defined( HAVE_KERNEL_SCHEDULER_H )
285 if( p_condvar == NULL )
287 i_result = B_BAD_VALUE;
289 else if( p_condvar->init < 2000 )
291 i_result = B_NO_INIT;
295 while( p_condvar->thread != -1 )
298 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
303 if( info.state != B_THREAD_SUSPENDED )
305 /* The waiting thread is not suspended so it could
306 * have been interrupted beetwen the unlock and the
307 * suspend_thread line. That is why we sleep a little
308 * before retesting p_condver->thread. */
313 /* Ok, we have to wake up that thread */
314 resume_thread( p_condvar->thread );
321 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
322 i_result = pthread_cond_signal( &p_condvar->cond );
325 i_thread = (int)pthread_self();
326 psz_error = strerror(i_result);
329 #elif defined( HAVE_CTHREADS_H )
330 /* condition_signal() */
331 if ( p_condvar->queue.head || p_condvar->implications )
333 cond_signal( (condition_t)p_condvar );
341 msg_Err( p_condvar->p_this,
342 "thread %d: cond_signal failed at %s:%d (%d:%s)",
343 i_thread, psz_file, i_line, i_result, psz_error );
349 /*****************************************************************************
350 * vlc_cond_broadcast: start all threads waiting on condition completion
351 *****************************************************************************/
353 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
354 * Only works with pthreads, st, win32
355 * You need to adapt it for others
356 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
358 #define vlc_cond_broadcast( P_COND ) \
359 __vlc_cond_broadcast( __FILE__, __LINE__, P_COND )
361 static inline int __vlc_cond_broadcast( char * psz_file, int i_line,
362 vlc_cond_t *p_condvar )
365 /* In case of error : */
367 const char * psz_error = "";
369 #if defined( PTH_INIT_IN_PTH_H )
370 i_result = ( pth_cond_notify( &p_condvar->cond, TRUE ) == FALSE );
372 #elif defined( ST_INIT_IN_ST_H )
373 i_result = st_cond_broadcast( p_condvar->cond );
375 #elif defined( UNDER_CE )
378 /* Release all waiting threads. */
379 for( i = p_condvar->i_waiting_threads; i > 0; i-- )
381 PulseEvent( p_condvar->event );
385 #elif defined( WIN32 )
388 /* Release all waiting threads. */
389 if( !p_condvar->semaphore )
391 for( i = p_condvar->i_waiting_threads; i > 0; i-- )
393 PulseEvent( p_condvar->event );
396 else if( p_condvar->i_win9x_cv == 1 )
398 /* Wait for the gate to be open */
399 WaitForSingleObject( p_condvar->event, INFINITE );
401 if( p_condvar->i_waiting_threads )
403 /* Using a semaphore exposes us to a race condition. It is
404 * possible for another thread to start waiting on the semaphore
405 * just after we signaled it and thus steal the signal.
406 * We have to prevent new threads from entering the cond_wait(). */
407 ResetEvent( p_condvar->event );
409 /* A semaphore is used here because Win9x doesn't have
410 * SignalObjectAndWait() and thus a race condition exists
411 * during the time we release the mutex and the time we start
412 * waiting on the event (more precisely, the signal can sometimes
413 * be missed by the waiting thread if we use PulseEvent()). */
414 ReleaseSemaphore( p_condvar->semaphore,
415 p_condvar->i_waiting_threads, 0 );
420 if( p_condvar->i_waiting_threads )
422 ReleaseSemaphore( p_condvar->semaphore,
423 p_condvar->i_waiting_threads, 0 );
425 /* Wait for the last thread to be awakened */
426 WaitForSingleObject( p_condvar->event, INFINITE );
431 #elif defined( HAVE_KERNEL_SCHEDULER_H )
432 if( p_condvar == NULL )
434 i_result = B_BAD_VALUE;
436 else if( p_condvar->init < 2000 )
438 i_result = B_NO_INIT;
442 while( p_condvar->thread != -1 )
445 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
450 if( info.state != B_THREAD_SUSPENDED )
452 /* The waiting thread is not suspended so it could
453 * have been interrupted beetwen the unlock and the
454 * suspend_thread line. That is why we sleep a little
455 * before retesting p_condver->thread. */
460 /* Ok, we have to wake up that thread */
461 resume_thread( p_condvar->thread );
468 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
469 i_result = pthread_cond_broadcast( &p_condvar->cond );
472 i_thread = (int)pthread_self();
473 psz_error = strerror(i_result);
476 #elif defined( HAVE_CTHREADS_H )
477 /* condition_signal() */
478 if ( p_condvar->queue.head || p_condvar->implications )
480 cond_signal( (condition_t)p_condvar );
488 msg_Err( p_condvar->p_this,
489 "thread %d: cond_broadcast failed at %s:%d (%d:%s)",
490 i_thread, psz_file, i_line, i_result, psz_error );
496 /*****************************************************************************
497 * vlc_cond_wait: wait until condition completion
498 *****************************************************************************/
499 #define vlc_cond_wait( P_COND, P_MUTEX ) \
500 __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX )
502 static inline int __vlc_cond_wait( char * psz_file, int i_line,
503 vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
506 /* In case of error : */
508 const char * psz_error = "";
510 #if defined( PTH_INIT_IN_PTH_H )
511 i_result = ( pth_cond_await( &p_condvar->cond, &p_mutex->mutex, NULL )
514 #elif defined( ST_INIT_IN_ST_H )
515 st_mutex_unlock( p_mutex->mutex );
516 i_result = st_cond_wait( p_condvar->cond );
517 st_mutex_lock( p_mutex->mutex );
519 #elif defined( UNDER_CE )
520 p_condvar->i_waiting_threads++;
521 LeaveCriticalSection( &p_mutex->csection );
522 WaitForSingleObject( p_condvar->event, INFINITE );
523 p_condvar->i_waiting_threads--;
525 /* Reacquire the mutex before returning. */
526 vlc_mutex_lock( p_mutex );
530 #elif defined( WIN32 )
531 if( !p_condvar->semaphore )
533 /* Increase our wait count */
534 p_condvar->i_waiting_threads++;
538 /* It is only possible to atomically release the mutex and
539 * initiate the waiting on WinNT/2K/XP. Win9x doesn't have
540 * SignalObjectAndWait(). */
541 p_condvar->SignalObjectAndWait( p_mutex->mutex,
547 LeaveCriticalSection( &p_mutex->csection );
548 WaitForSingleObject( p_condvar->event, INFINITE );
551 p_condvar->i_waiting_threads--;
553 else if( p_condvar->i_win9x_cv == 1 )
555 int i_waiting_threads;
557 /* Wait for the gate to be open */
558 WaitForSingleObject( p_condvar->event, INFINITE );
560 /* Increase our wait count */
561 p_condvar->i_waiting_threads++;
563 LeaveCriticalSection( &p_mutex->csection );
564 WaitForSingleObject( p_condvar->semaphore, INFINITE );
566 /* Decrement and test must be atomic */
567 EnterCriticalSection( &p_condvar->csection );
569 /* Decrease our wait count */
570 i_waiting_threads = --p_condvar->i_waiting_threads;
572 LeaveCriticalSection( &p_condvar->csection );
574 /* Reopen the gate if we were the last waiting thread */
575 if( !i_waiting_threads )
576 SetEvent( p_condvar->event );
580 int i_waiting_threads;
582 /* Increase our wait count */
583 p_condvar->i_waiting_threads++;
585 LeaveCriticalSection( &p_mutex->csection );
586 WaitForSingleObject( p_condvar->semaphore, INFINITE );
588 /* Decrement and test must be atomic */
589 EnterCriticalSection( &p_condvar->csection );
591 /* Decrease our wait count */
592 i_waiting_threads = --p_condvar->i_waiting_threads;
594 LeaveCriticalSection( &p_condvar->csection );
596 /* Signal that the last waiting thread just went through */
597 if( !i_waiting_threads )
598 SetEvent( p_condvar->event );
601 /* Reacquire the mutex before returning. */
602 vlc_mutex_lock( p_mutex );
606 #elif defined( HAVE_KERNEL_SCHEDULER_H )
607 if( p_condvar == NULL )
609 i_result = B_BAD_VALUE;
611 else if( p_mutex == NULL )
613 i_result = B_BAD_VALUE;
615 else if( p_condvar->init < 2000 )
617 i_result = B_NO_INIT;
620 /* The p_condvar->thread var is initialized before the unlock because
621 * it enables to identify when the thread is interrupted beetwen the
622 * unlock line and the suspend_thread line */
623 p_condvar->thread = find_thread( NULL );
624 vlc_mutex_unlock( p_mutex );
625 suspend_thread( p_condvar->thread );
626 p_condvar->thread = -1;
628 vlc_mutex_lock( p_mutex );
631 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
634 /* In debug mode, timeout */
636 struct timespec timeout;
638 gettimeofday( &now, NULL );
639 timeout.tv_sec = now.tv_sec + THREAD_COND_TIMEOUT;
640 timeout.tv_nsec = now.tv_usec * 1000;
642 i_result = pthread_cond_timedwait( &p_condvar->cond, &p_mutex->mutex,
645 if( i_result == ETIMEDOUT )
647 /* People keep pissing me off with this. --Meuuh */
648 msg_Dbg( p_condvar->p_this,
649 "thread %d: secret message triggered "
650 "at %s:%d (%s)", (int)pthread_self(),
651 psz_file, i_line, strerror(i_result) );
653 i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
657 i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
662 i_thread = (int)pthread_self();
663 psz_error = strerror(i_result);
666 #elif defined( HAVE_CTHREADS_H )
667 condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
674 msg_Err( p_condvar->p_this,
675 "thread %d: cond_wait failed at %s:%d (%d:%s)",
676 i_thread, psz_file, i_line, i_result, psz_error );
682 /*****************************************************************************
683 * vlc_cond_destroy: destroy a condition
684 *****************************************************************************/
685 #define vlc_cond_destroy( P_COND ) \
686 __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
688 /*****************************************************************************
689 * vlc_thread_create: create a thread
690 *****************************************************************************/
691 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT ) \
692 __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, PRIORITY, WAIT )
694 /*****************************************************************************
695 * vlc_thread_set_priority: set the priority of the calling thread
696 *****************************************************************************/
697 #define vlc_thread_set_priority( P_THIS, PRIORITY ) \
698 __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
700 /*****************************************************************************
701 * vlc_thread_ready: tell the parent thread we were successfully spawned
702 *****************************************************************************/
703 #define vlc_thread_ready( P_THIS ) \
704 __vlc_thread_ready( VLC_OBJECT(P_THIS) )
706 /*****************************************************************************
707 * vlc_thread_join: wait until a thread exits
708 *****************************************************************************/
709 #define vlc_thread_join( P_THIS ) \
710 __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )