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.1 2002/08/29 23:53:22 massiot 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( void, __vlc_thread_ready, ( vlc_object_t * ) );
39 VLC_EXPORT( void, __vlc_thread_join, ( vlc_object_t *, char *, int ) );
41 /*****************************************************************************
42 * vlc_threads_init: initialize threads system
43 *****************************************************************************/
44 #define vlc_threads_init( P_THIS ) \
45 __vlc_threads_init( VLC_OBJECT(P_THIS) )
47 /*****************************************************************************
48 * vlc_threads_end: deinitialize threads system
49 *****************************************************************************/
50 #define vlc_threads_end( P_THIS ) \
51 __vlc_threads_end( VLC_OBJECT(P_THIS) )
53 /*****************************************************************************
54 * vlc_mutex_init: initialize a mutex
55 *****************************************************************************/
56 #define vlc_mutex_init( P_THIS, P_MUTEX ) \
57 __vlc_mutex_init( VLC_OBJECT(P_THIS), P_MUTEX )
59 /*****************************************************************************
60 * vlc_mutex_lock: lock a mutex
61 *****************************************************************************/
62 #define vlc_mutex_lock( P_MUTEX ) \
63 __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
65 static inline int __vlc_mutex_lock( char * psz_file, int i_line,
66 vlc_mutex_t * p_mutex )
69 /* In case of error : */
71 const char * psz_error = "";
73 #if defined( PTH_INIT_IN_PTH_H )
74 i_result = pth_mutex_acquire( &p_mutex->mutex, TRUE, NULL );
76 #elif defined( ST_INIT_IN_ST_H )
77 i_result = st_mutex_lock( *p_mutex->mutex );
79 #elif defined( WIN32 )
82 WaitForSingleObject( p_mutex->mutex, INFINITE );
86 EnterCriticalSection( &p_mutex->csection );
90 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
91 i_result = pthread_mutex_lock( &p_mutex->mutex );
94 i_thread = (int)pthread_self();
95 psz_error = strerror(i_result);
98 #elif defined( HAVE_CTHREADS_H )
99 mutex_lock( p_mutex->mutex );
102 #elif defined( HAVE_KERNEL_SCHEDULER_H )
103 if( p_mutex == NULL )
105 i_result = B_BAD_VALUE;
107 else if( p_mutex->init < 2000 )
109 i_result = B_NO_INIT;
113 i_result = acquire_sem( p_mutex->lock );
119 msg_Err( p_mutex->p_this,
120 "thread %d: mutex_lock failed at %s:%d (%d:%s)",
121 i_thread, psz_file, i_line, i_result, psz_error );
126 /*****************************************************************************
127 * vlc_mutex_unlock: unlock a mutex
128 *****************************************************************************/
129 #define vlc_mutex_unlock( P_MUTEX ) \
130 __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
132 static inline int __vlc_mutex_unlock( char * psz_file, int i_line,
133 vlc_mutex_t *p_mutex )
136 /* In case of error : */
138 const char * psz_error = "";
140 #if defined( PTH_INIT_IN_PTH_H )
141 i_result = pth_mutex_release( &p_mutex->mutex );
143 #elif defined( ST_INIT_IN_ST_H )
144 i_result = st_mutex_unlock( *p_mutex->mutex );
146 #elif defined( WIN32 )
149 ReleaseMutex( p_mutex->mutex );
153 LeaveCriticalSection( &p_mutex->csection );
157 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
158 i_result = pthread_mutex_unlock( &p_mutex->mutex );
161 i_thread = (int)pthread_self();
162 psz_error = strerror(i_result);
165 #elif defined( HAVE_CTHREADS_H )
166 mutex_unlock( p_mutex );
169 #elif defined( HAVE_KERNEL_SCHEDULER_H )
170 if( p_mutex == NULL )
172 i_result = B_BAD_VALUE;
174 else if( p_mutex->init < 2000 )
176 i_result = B_NO_INIT;
180 release_sem( p_mutex->lock );
187 msg_Err( p_mutex->p_this,
188 "thread %d: mutex_unlock failed at %s:%d (%d:%s)",
189 i_thread, psz_file, i_line, i_result, psz_error );
195 /*****************************************************************************
196 * vlc_mutex_destroy: destroy a mutex
197 *****************************************************************************/
198 #define vlc_mutex_destroy( P_MUTEX ) \
199 __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
201 /*****************************************************************************
202 * vlc_cond_init: initialize a condition
203 *****************************************************************************/
204 #define vlc_cond_init( P_THIS, P_COND ) \
205 __vlc_cond_init( VLC_OBJECT(P_THIS), P_COND )
207 /*****************************************************************************
208 * vlc_cond_signal: start a thread on condition completion
209 *****************************************************************************/
210 #define vlc_cond_signal( P_COND ) \
211 __vlc_cond_signal( __FILE__, __LINE__, P_COND )
213 static inline int __vlc_cond_signal( char * psz_file, int i_line,
214 vlc_cond_t *p_condvar )
217 /* In case of error : */
219 const char * psz_error = "";
221 #if defined( PTH_INIT_IN_PTH_H )
222 i_result = pth_cond_notify( &p_condvar->cond, FALSE );
224 #elif defined( ST_INIT_IN_ST_H )
225 i_result = st_cond_signal( *p_condvar->cond );
227 #elif defined( WIN32 )
228 /* Release one waiting thread if one is available. */
229 /* For this trick to work properly, the vlc_cond_signal must be surrounded
230 * by a mutex. This will prevent another thread from stealing the signal */
231 if( !p_condvar->semaphore )
233 PulseEvent( p_condvar->event );
235 else if( p_condvar->i_win9x_cv == 1 )
237 /* Wait for the gate to be open */
238 WaitForSingleObject( p_condvar->event, INFINITE );
240 if( p_condvar->i_waiting_threads )
242 /* Using a semaphore exposes us to a race condition. It is
243 * possible for another thread to start waiting on the semaphore
244 * just after we signaled it and thus steal the signal.
245 * We have to prevent new threads from entering the cond_wait(). */
246 ResetEvent( p_condvar->event );
248 /* A semaphore is used here because Win9x doesn't have
249 * SignalObjectAndWait() and thus a race condition exists
250 * during the time we release the mutex and the time we start
251 * waiting on the event (more precisely, the signal can sometimes
252 * be missed by the waiting thread if we use PulseEvent()). */
253 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
258 if( p_condvar->i_waiting_threads )
260 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
262 /* Wait for the last thread to be awakened */
263 WaitForSingleObject( p_condvar->event, INFINITE );
268 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
269 i_result = pthread_cond_signal( &p_condvar->cond );
272 i_thread = (int)pthread_self();
273 psz_error = strerror(i_result);
276 #elif defined( HAVE_CTHREADS_H )
277 /* condition_signal() */
278 if ( p_condvar->queue.head || p_condvar->implications )
280 cond_signal( (condition_t)p_condvar );
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 );
323 msg_Err( p_condvar->p_this,
324 "thread %d: cond_signal failed at %s:%d (%d:%s)",
325 i_thread, psz_file, i_line, i_result, psz_error );
331 /*****************************************************************************
332 * vlc_cond_broadcast: start all threads waiting on condition completion
333 *****************************************************************************/
335 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
336 * Only works with pthreads, st, win32
337 * You need to adapt it for others
338 * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
340 #define vlc_cond_broadcast( P_COND ) \
341 __vlc_cond_broadcast( __FILE__, __LINE__, P_COND )
343 static inline int __vlc_cond_broadcast( char * psz_file, int i_line,
344 vlc_cond_t *p_condvar )
347 /* In case of error : */
349 const char * psz_error = "";
351 #if defined( PTH_INIT_IN_PTH_H )
352 i_result = pth_cond_notify( &p_condvar->cond, FALSE );
354 #elif defined( ST_INIT_IN_ST_H )
355 i_result = st_cond_broadcast( *p_condvar->cond );
357 #elif defined( WIN32 )
358 /* Release all waiting threads. */
359 if( !p_condvar->semaphore )
361 for( i = p_condvar->i_waiting_threads; i > 0; i-- )
363 PulseEvent( p_condvar->event );
366 else if( p_condvar->i_win9x_cv == 1 )
368 /* Wait for the gate to be open */
369 WaitForSingleObject( p_condvar->event, INFINITE );
371 if( p_condvar->i_waiting_threads )
373 /* Using a semaphore exposes us to a race condition. It is
374 * possible for another thread to start waiting on the semaphore
375 * just after we signaled it and thus steal the signal.
376 * We have to prevent new threads from entering the cond_wait(). */
377 ResetEvent( p_condvar->event );
379 /* A semaphore is used here because Win9x doesn't have
380 * SignalObjectAndWait() and thus a race condition exists
381 * during the time we release the mutex and the time we start
382 * waiting on the event (more precisely, the signal can sometimes
383 * be missed by the waiting thread if we use PulseEvent()). */
384 ReleaseSemaphore( p_condvar->semaphore,
385 p_condvar->i_waiting_threads, 0 );
390 if( p_condvar->i_waiting_threads )
392 ReleaseSemaphore( p_condvar->semaphore,
393 p_condvar->i_waiting_threads, 0 );
395 /* Wait for the last thread to be awakened */
396 WaitForSingleObject( p_condvar->event, INFINITE );
401 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
402 i_result = pthread_cond_broadcast( &p_condvar->cond );
405 i_thread = (int)pthread_self();
406 psz_error = strerror(i_result);
409 #elif defined( HAVE_CTHREADS_H )
410 /* condition_signal() */
411 if ( p_condvar->queue.head || p_condvar->implications )
413 cond_signal( (condition_t)p_condvar );
417 #elif defined( HAVE_KERNEL_SCHEDULER_H )
418 if( p_condvar == NULL )
420 i_result = B_BAD_VALUE;
422 else if( p_condvar->init < 2000 )
424 i_result = B_NO_INIT;
428 while( p_condvar->thread != -1 )
431 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
436 if( info.state != B_THREAD_SUSPENDED )
438 /* The waiting thread is not suspended so it could
439 * have been interrupted beetwen the unlock and the
440 * suspend_thread line. That is why we sleep a little
441 * before retesting p_condver->thread. */
446 /* Ok, we have to wake up that thread */
447 resume_thread( p_condvar->thread );
456 msg_Err( p_condvar->p_this,
457 "thread %d: cond_broadcast failed at %s:%d (%d:%s)",
458 i_thread, psz_file, i_line, i_result, psz_error );
464 /*****************************************************************************
465 * vlc_cond_wait: wait until condition completion
466 *****************************************************************************/
467 #define vlc_cond_wait( P_COND, P_MUTEX ) \
468 __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX )
470 static inline int __vlc_cond_wait( char * psz_file, int i_line,
471 vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
474 /* In case of error : */
476 const char * psz_error = "";
478 #if defined( PTH_INIT_IN_PTH_H )
479 i_result = pth_cond_await( &p_condvar->cond, &p_mutex->mutex, NULL );
481 #elif defined( ST_INIT_IN_ST_H )
482 st_mutex_unlock( *p_mutex->mutex );
483 i_result = st_cond_wait( *p_condvar->cond );
484 st_mutex_lock( *p_mutex->mutex );
486 #elif defined( WIN32 )
487 if( !p_condvar->semaphore )
489 /* Increase our wait count */
490 p_condvar->i_waiting_threads++;
492 if( p_condvar->SignalObjectAndWait && p_mutex->mutex )
494 /* It is only possible to atomically release the mutex and
495 * initiate the waiting on WinNT/2K/XP. Win9x doesn't have
496 * SignalObjectAndWait(). */
497 p_condvar->SignalObjectAndWait( p_mutex->mutex,
503 LeaveCriticalSection( &p_mutex->csection );
504 WaitForSingleObject( p_condvar->event, INFINITE );
507 p_condvar->i_waiting_threads--;
509 else if( p_condvar->i_win9x_cv == 1 )
511 int i_waiting_threads;
513 /* Wait for the gate to be open */
514 WaitForSingleObject( p_condvar->event, INFINITE );
516 /* Increase our wait count */
517 p_condvar->i_waiting_threads++;
519 LeaveCriticalSection( &p_mutex->csection );
520 WaitForSingleObject( p_condvar->semaphore, INFINITE );
522 /* Decrement and test must be atomic */
523 EnterCriticalSection( &p_condvar->csection );
525 /* Decrease our wait count */
526 i_waiting_threads = --p_condvar->i_waiting_threads;
528 LeaveCriticalSection( &p_condvar->csection );
530 /* Reopen the gate if we were the last waiting thread */
531 if( !i_waiting_threads )
532 SetEvent( p_condvar->event );
536 int i_waiting_threads;
538 /* Increase our wait count */
539 p_condvar->i_waiting_threads++;
541 LeaveCriticalSection( &p_mutex->csection );
542 WaitForSingleObject( p_condvar->semaphore, INFINITE );
544 /* Decrement and test must be atomic */
545 EnterCriticalSection( &p_condvar->csection );
547 /* Decrease our wait count */
548 i_waiting_threads = --p_condvar->i_waiting_threads;
550 LeaveCriticalSection( &p_condvar->csection );
552 /* Signal that the last waiting thread just went through */
553 if( !i_waiting_threads )
554 SetEvent( p_condvar->event );
557 /* Reacquire the mutex before returning. */
558 vlc_mutex_lock( p_mutex );
562 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
565 /* In debug mode, timeout */
567 struct timespec timeout;
571 gettimeofday( &now, NULL );
572 timeout.tv_sec = now.tv_sec + THREAD_COND_TIMEOUT;
573 timeout.tv_nsec = now.tv_usec * 1000;
575 i_result = pthread_cond_timedwait( &p_condvar->cond, &p_mutex->mutex,
578 if( i_result == ETIMEDOUT )
580 msg_Warn( p_condvar->p_this,
581 "thread %d: possible deadlock detected "
582 "in cond_wait at %s:%d (%s)", pthread_self(),
583 psz_file, i_line, strerror(i_result) );
588 i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
593 i_thread = (int)pthread_self();
594 psz_error = strerror(i_result);
597 #elif defined( HAVE_CTHREADS_H )
598 condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
601 #elif defined( HAVE_KERNEL_SCHEDULER_H )
602 if( p_condvar == NULL )
604 i_result = B_BAD_VALUE;
606 else if( p_mutex == NULL )
608 i_result = B_BAD_VALUE;
610 else if( p_condvar->init < 2000 )
612 i_result = B_NO_INIT;
615 /* The p_condvar->thread var is initialized before the unlock because
616 * it enables to identify when the thread is interrupted beetwen the
617 * unlock line and the suspend_thread line */
618 p_condvar->thread = find_thread( NULL );
619 vlc_mutex_unlock( p_mutex );
620 suspend_thread( p_condvar->thread );
621 p_condvar->thread = -1;
623 vlc_mutex_lock( p_mutex );
630 msg_Err( p_condvar->p_this,
631 "thread %d: cond_wait failed at %s:%d (%d:%s)",
632 i_thread, psz_file, i_line, i_result, psz_error );
638 /*****************************************************************************
639 * vlc_cond_destroy: destroy a condition
640 *****************************************************************************/
641 #define vlc_cond_destroy( P_COND ) \
642 __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
644 /*****************************************************************************
645 * vlc_thread_create: create a thread
646 *****************************************************************************/
647 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT ) \
648 __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, PRIORITY, WAIT )
650 /*****************************************************************************
651 * vlc_thread_ready: tell the parent thread we were successfully spawned
652 *****************************************************************************/
653 #define vlc_thread_ready( P_THIS ) \
654 __vlc_thread_ready( VLC_OBJECT(P_THIS) )
656 /*****************************************************************************
657 * vlc_thread_join: wait until a thread exits
658 *****************************************************************************/
659 #define vlc_thread_join( P_THIS ) \
660 __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )