]> git.sesse.net Git - vlc/blob - include/vlc_threads.h
* ./modules/audio_output/oss.c: compilation fixes.
[vlc] / include / vlc_threads.h
1 /*****************************************************************************
2  * threads.h : threads implementation for the VideoLAN client
3  * This header provides a portable threads implementation.
4  *****************************************************************************
5  * Copyright (C) 1999, 2000 VideoLAN
6  * $Id: vlc_threads.h,v 1.9 2002/08/08 00:35:10 sam Exp $
7  *
8  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
9  *          Samuel Hocevar <sam@via.ecp.fr>
10  *          Gildas Bazin <gbazin@netcourrier.com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 #include <stdio.h>
28
29 #if defined(GPROF) || defined(DEBUG)
30 #   include <sys/time.h>
31 #endif
32
33 #if defined( PTH_INIT_IN_PTH_H )                                  /* GNU Pth */
34 #   include <pth.h>
35
36 #elif defined( ST_INIT_IN_ST_H )                            /* State threads */
37 #   include <st.h>
38
39 #elif defined( WIN32 )                                          /* Win32 API */
40 #   include <process.h>
41
42 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )  /* pthreads (like Linux & BSD) */
43 #   include <pthread.h>
44 #   ifdef DEBUG
45         /* Needed for pthread_cond_timedwait */
46 #       include <errno.h>
47 #   endif
48     /* This is not prototyped under Linux, though it exists. */
49     int pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, int kind );
50
51 #elif defined( HAVE_CTHREADS_H )                                  /* GNUMach */
52 #   include <cthreads.h>
53
54 #elif defined( HAVE_KERNEL_SCHEDULER_H )                             /* BeOS */
55 #   include <kernel/OS.h>
56 #   include <kernel/scheduler.h>
57 #   include <byteorder.h>
58
59 #else
60 #   error no threads available on your system !
61
62 #endif
63
64 /*****************************************************************************
65  * Constants
66  *****************************************************************************
67  * These constants are used by all threads in *_CreateThread() and
68  * *_DestroyThreads() functions. Since those calls are non-blocking, an integer
69  * value is used as a shared flag to represent the status of the thread.
70  *****************************************************************************/
71
72 /* Void status - this value can be used to make sure no operation is currently
73  * in progress on the concerned thread in an array of recorded threads */
74 #define THREAD_NOP          0                            /* nothing happened */
75
76 /* Creation status */
77 #define THREAD_CREATE       10                     /* thread is initializing */
78 #define THREAD_START        11                          /* thread has forked */
79 #define THREAD_READY        19                            /* thread is ready */
80
81 /* Destructions status */
82 #define THREAD_DESTROY      20            /* destruction order has been sent */
83 #define THREAD_END          21        /* destruction order has been received */
84 #define THREAD_OVER         29             /* thread does not exist any more */
85
86 /* Error status */
87 #define THREAD_ERROR        30                           /* an error occured */
88 #define THREAD_FATAL        31  /* an fatal error occured - program must end */
89
90 /*****************************************************************************
91  * Type definitions
92  *****************************************************************************/
93
94 #if defined( PTH_INIT_IN_PTH_H )
95 typedef pth_t            vlc_thread_t;
96 typedef pth_mutex_t      vlc_mutex_t;
97 typedef pth_cond_t       vlc_cond_t;
98
99 #elif defined( ST_INIT_IN_ST_H )
100 typedef st_thread_t *    vlc_thread_t;
101 typedef st_mutex_t *     vlc_mutex_t;
102 typedef st_cond_t *      vlc_cond_t;
103
104 #elif defined( WIN32 )
105 typedef HANDLE vlc_thread_t;
106 typedef BOOL (WINAPI *SIGNALOBJECTANDWAIT) ( HANDLE, HANDLE, DWORD, BOOL );
107 typedef unsigned (__stdcall *PTHREAD_START) (void *);
108
109 typedef struct
110 {
111     /* WinNT/2K/XP implementation */
112     HANDLE              mutex;
113     /* Win95/98/ME implementation */
114     CRITICAL_SECTION    csection;
115 } vlc_mutex_t;
116
117 typedef struct
118 {
119     volatile int        i_waiting_threads;
120     /* WinNT/2K/XP implementation */
121     HANDLE              event;
122     SIGNALOBJECTANDWAIT SignalObjectAndWait;
123     /* Win95/98/ME implementation */
124     HANDLE              semaphore;
125     CRITICAL_SECTION    csection;
126     int                 i_win9x_cv;
127 } vlc_cond_t;
128
129 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
130 typedef pthread_t        vlc_thread_t;
131 typedef pthread_mutex_t  vlc_mutex_t;
132 typedef pthread_cond_t   vlc_cond_t;
133
134 #elif defined( HAVE_CTHREADS_H )
135 typedef cthread_t        vlc_thread_t;
136
137 /* Those structs are the ones defined in /include/cthreads.h but we need
138  * to handle (&foo) where foo is a (mutex_t) while they handle (foo) where
139  * foo is a (mutex_t*) */
140 typedef struct
141 {
142     spin_lock_t held;
143     spin_lock_t lock;
144     char *name;
145     struct cthread_queue queue;
146 } vlc_mutex_t;
147
148 typedef struct
149 {
150     spin_lock_t lock;
151     struct cthread_queue queue;
152     char *name;
153     struct cond_imp *implications;
154 } vlc_cond_t;
155
156 #elif defined( HAVE_KERNEL_SCHEDULER_H )
157 /* This is the BeOS implementation of the vlc threads, note that the mutex is
158  * not a real mutex and the cond_var is not like a pthread cond_var but it is
159  * enough for what wee need */
160
161 typedef thread_id vlc_thread_t;
162
163 typedef struct
164 {
165     int32           init;
166     sem_id          lock;
167 } vlc_mutex_t;
168
169 typedef struct
170 {
171     int32           init;
172     thread_id       thread;
173 } vlc_cond_t;
174
175 #endif
176
177 /*****************************************************************************
178  * Function definitions
179  *****************************************************************************/
180 VLC_EXPORT( int,  __vlc_threads_init,  ( vlc_object_t * ) );
181 VLC_EXPORT( int,  __vlc_threads_end,   ( vlc_object_t * ) );
182 VLC_EXPORT( int,  __vlc_mutex_init,    ( vlc_object_t *, vlc_mutex_t * ) );
183 VLC_EXPORT( int,  __vlc_mutex_destroy, ( char *, int, vlc_mutex_t * ) );
184 VLC_EXPORT( int,  __vlc_cond_init,     ( vlc_object_t *, vlc_cond_t * ) );
185 VLC_EXPORT( int,  __vlc_cond_destroy,  ( char *, int, vlc_cond_t * ) );
186 VLC_EXPORT( int,  __vlc_thread_create, ( vlc_object_t *, char *, int, char *, void * ( * ) ( void * ), vlc_bool_t ) );
187 VLC_EXPORT( void, __vlc_thread_ready,  ( vlc_object_t * ) );
188 VLC_EXPORT( void, __vlc_thread_join,   ( vlc_object_t *, char *, int ) );
189
190 /*****************************************************************************
191  * vlc_threads_init: initialize threads system
192  *****************************************************************************/
193 #define vlc_threads_init( P_THIS )                                          \
194     __vlc_threads_init( VLC_OBJECT(P_THIS) )
195
196 /*****************************************************************************
197  * vlc_threads_end: deinitialize threads system
198  *****************************************************************************/
199 #define vlc_threads_end( P_THIS )                                          \
200     __vlc_threads_end( VLC_OBJECT(P_THIS) )
201
202 /*****************************************************************************
203  * vlc_mutex_init: initialize a mutex
204  *****************************************************************************/
205 #define vlc_mutex_init( P_THIS, P_MUTEX )                                   \
206     __vlc_mutex_init( VLC_OBJECT(P_THIS), P_MUTEX )
207
208 /*****************************************************************************
209  * vlc_mutex_lock: lock a mutex
210  *****************************************************************************/
211 #ifdef DEBUG
212 #   define vlc_mutex_lock( P_MUTEX )                                        \
213         __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
214 #else
215 #   define vlc_mutex_lock( P_MUTEX )                                        \
216         __vlc_mutex_lock( "(unknown)", 0, P_MUTEX )
217 #endif
218
219 static inline int __vlc_mutex_lock( char * psz_file, int i_line,
220                                     vlc_mutex_t *p_mutex )
221 {
222 #if defined( PTH_INIT_IN_PTH_H )
223     return pth_mutex_acquire( p_mutex, TRUE, NULL );
224
225 #elif defined( ST_INIT_IN_ST_H )
226     return st_mutex_lock( *p_mutex );
227
228 #elif defined( WIN32 )
229     if( p_mutex->mutex )
230     {
231         WaitForSingleObject( p_mutex->mutex, INFINITE );
232     }
233     else
234     {
235         EnterCriticalSection( &p_mutex->csection );
236     }
237     return 0;
238
239 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
240     int i_return = pthread_mutex_lock( p_mutex );
241     if( i_return )
242     {
243 #if 0
244         msg_Err( "thread %d: mutex_lock failed at %s:%d (%s)",
245                  pthread_self(), psz_file, i_line, strerror(i_return) );
246 #endif
247     }
248     return i_return;
249
250 #elif defined( HAVE_CTHREADS_H )
251     mutex_lock( p_mutex );
252     return 0;
253
254 #elif defined( HAVE_KERNEL_SCHEDULER_H )
255     status_t err;
256
257     if( !p_mutex )
258     {
259         return B_BAD_VALUE;
260     }
261
262     if( p_mutex->init < 2000 )
263     {
264         return B_NO_INIT;
265     }
266
267     err = acquire_sem( p_mutex->lock );
268     return err;
269
270 #endif
271 }
272
273 /*****************************************************************************
274  * vlc_mutex_unlock: unlock a mutex
275  *****************************************************************************/
276 #ifdef DEBUG
277 #   define vlc_mutex_unlock( P_MUTEX )                                      \
278         __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
279 #else
280 #   define vlc_mutex_unlock( P_MUTEX )                                      \
281         __vlc_mutex_unlock( "(unknown)", 0, P_MUTEX )
282 #endif
283
284 static inline int __vlc_mutex_unlock( char * psz_file, int i_line,
285                                       vlc_mutex_t *p_mutex )
286 {
287 #if defined( PTH_INIT_IN_PTH_H )
288     return pth_mutex_release( p_mutex );
289
290 #elif defined( ST_INIT_IN_ST_H )
291     return st_mutex_unlock( *p_mutex );
292
293 #elif defined( WIN32 )
294     if( p_mutex->mutex )
295     {
296         ReleaseMutex( p_mutex->mutex );
297     }
298     else
299     {
300         LeaveCriticalSection( &p_mutex->csection );
301     }
302     return 0;
303
304 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
305     int i_return = pthread_mutex_unlock( p_mutex );
306     if( i_return )
307     {
308 #if 0
309         msg_Err( "thread %d: mutex_unlock failed at %s:%d (%s)",
310                  pthread_self(), psz_file, i_line, strerror(i_return) );
311 #endif
312     }
313     return i_return;
314
315 #elif defined( HAVE_CTHREADS_H )
316     mutex_unlock( p_mutex );
317     return 0;
318
319 #elif defined( HAVE_KERNEL_SCHEDULER_H )
320     if( !p_mutex)
321     {
322         return B_BAD_VALUE;
323     }
324
325     if( p_mutex->init < 2000 )
326     {
327         return B_NO_INIT;
328     }
329
330     release_sem( p_mutex->lock );
331     return B_OK;
332
333 #endif
334 }
335
336 /*****************************************************************************
337  * vlc_mutex_destroy: destroy a mutex
338  *****************************************************************************/
339 #ifdef DEBUG
340 #   define vlc_mutex_destroy( P_MUTEX )                                     \
341         __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
342 #else
343 #   define vlc_mutex_destroy( P_MUTEX )                                     \
344         __vlc_mutex_destroy( "(unknown)", 0, P_MUTEX )
345 #endif
346
347 /*****************************************************************************
348  * vlc_cond_init: initialize a condition
349  *****************************************************************************/
350 #define vlc_cond_init( P_THIS, P_COND )                                   \
351     __vlc_cond_init( VLC_OBJECT(P_THIS), P_COND )
352
353 /*****************************************************************************
354  * vlc_cond_signal: start a thread on condition completion
355  *****************************************************************************/
356 static inline int vlc_cond_signal( vlc_cond_t *p_condvar )
357 {
358 #if defined( PTH_INIT_IN_PTH_H )
359     return pth_cond_notify( p_condvar, FALSE );
360
361 #elif defined( ST_INIT_IN_ST_H )
362     return st_cond_signal( *p_condvar );
363
364 #elif defined( WIN32 )
365     /* Release one waiting thread if one is available. */
366     /* For this trick to work properly, the vlc_cond_signal must be surrounded
367      * by a mutex. This will prevent another thread from stealing the signal */
368     if( !p_condvar->semaphore )
369     {
370         PulseEvent( p_condvar->event );
371     }
372     else if( p_condvar->i_win9x_cv == 1 )
373     {
374         /* Wait for the gate to be open */
375         WaitForSingleObject( p_condvar->event, INFINITE );
376
377         if( p_condvar->i_waiting_threads )
378         {
379             /* Using a semaphore exposes us to a race condition. It is
380              * possible for another thread to start waiting on the semaphore
381              * just after we signaled it and thus steal the signal.
382              * We have to prevent new threads from entering the cond_wait(). */
383             ResetEvent( p_condvar->event );
384
385             /* A semaphore is used here because Win9x doesn't have
386              * SignalObjectAndWait() and thus a race condition exists
387              * during the time we release the mutex and the time we start
388              * waiting on the event (more precisely, the signal can sometimes
389              * be missed by the waiting thread if we use PulseEvent()). */
390             ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
391         }
392     }
393     else
394     {
395         if( p_condvar->i_waiting_threads )
396         {
397             ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
398
399             /* Wait for the last thread to be awakened */
400             WaitForSingleObject( p_condvar->event, INFINITE );
401         }
402     }
403     return 0;
404
405 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
406     return pthread_cond_signal( p_condvar );
407
408 #elif defined( HAVE_CTHREADS_H )
409     /* condition_signal() */
410     if ( p_condvar->queue.head || p_condvar->implications )
411     {
412         cond_signal( (condition_t)p_condvar );
413     }
414     return 0;
415
416 #elif defined( HAVE_KERNEL_SCHEDULER_H )
417     if( !p_condvar )
418     {
419         return B_BAD_VALUE;
420     }
421
422     if( p_condvar->init < 2000 )
423     {
424         return B_NO_INIT;
425     }
426
427     while( p_condvar->thread != -1 )
428     {
429         thread_info info;
430         if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
431         {
432             return 0;
433         }
434
435         if( info.state != B_THREAD_SUSPENDED )
436         {
437             /* The  waiting thread is not suspended so it could
438              * have been interrupted beetwen the unlock and the
439              * suspend_thread line. That is why we sleep a little
440              * before retesting p_condver->thread. */
441             snooze( 10000 );
442         }
443         else
444         {
445             /* Ok, we have to wake up that thread */
446             resume_thread( p_condvar->thread );
447             return 0;
448         }
449     }
450     return 0;
451
452 #endif
453 }
454
455 /*****************************************************************************
456  * vlc_cond_broadcast: start all threads waiting on condition completion
457  *****************************************************************************/
458 /*
459  * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
460  * Only works with pthreads, you need to adapt it for others
461  * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
462  */
463 static inline int vlc_cond_broadcast( vlc_cond_t *p_condvar )
464 {
465 #if defined( PTH_INIT_IN_PTH_H )
466     return pth_cond_notify( p_condvar, FALSE );
467
468 #elif defined( ST_INIT_IN_ST_H )
469     return st_cond_broadcast( p_condvar );
470
471 #elif defined( WIN32 )
472     /* Release all waiting threads. */
473     int i;
474
475     if( !p_condvar->semaphore )
476         for( i = p_condvar->i_waiting_threads; i > 0; i-- )
477             PulseEvent( p_condvar->event );
478     else if( p_condvar->i_win9x_cv == 1 )
479     {
480         /* Wait for the gate to be open */
481         WaitForSingleObject( p_condvar->event, INFINITE );
482
483         if( p_condvar->i_waiting_threads )
484         {
485             /* close gate */
486             ResetEvent( p_condvar->event );
487
488             ReleaseSemaphore( p_condvar->semaphore,
489                               p_condvar->i_waiting_threads, 0 );
490         }
491     }
492     else
493     {
494         if( p_condvar->i_waiting_threads )
495         {
496             ReleaseSemaphore( p_condvar->semaphore,
497                               p_condvar->i_waiting_threads, 0 );
498             /* Wait for the last thread to be awakened */
499             WaitForSingleObject( p_condvar->event, INFINITE );
500         }
501     }
502
503     return 0;
504
505 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
506     return pthread_cond_broadcast( p_condvar );
507
508 #elif defined( HAVE_CTHREADS_H )
509     /* condition_signal() */
510     if ( p_condvar->queue.head || p_condvar->implications )
511     {
512         cond_signal( (condition_t)p_condvar );
513     }
514     return 0;
515
516 #elif defined( HAVE_KERNEL_SCHEDULER_H )
517     if( !p_condvar )
518     {
519         return B_BAD_VALUE;
520     }
521
522     if( p_condvar->init < 2000 )
523     {
524         return B_NO_INIT;
525     }
526
527     while( p_condvar->thread != -1 )
528     {
529         thread_info info;
530         if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
531         {
532             return 0;
533         }
534
535         if( info.state != B_THREAD_SUSPENDED )
536         {
537             /* The  waiting thread is not suspended so it could
538              * have been interrupted beetwen the unlock and the
539              * suspend_thread line. That is why we sleep a little
540              * before retesting p_condver->thread. */
541             snooze( 10000 );
542         }
543         else
544         {
545             /* Ok, we have to wake up that thread */
546             resume_thread( p_condvar->thread );
547             return 0;
548         }
549     }
550     return 0;
551
552 #endif
553 }
554
555 /*****************************************************************************
556  * vlc_cond_wait: wait until condition completion
557  *****************************************************************************/
558 #ifdef DEBUG
559 #   define vlc_cond_wait( P_COND, P_MUTEX )                                   \
560         __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX  )
561 #else
562 #   define vlc_cond_wait( P_COND, P_MUTEX )                                   \
563         __vlc_cond_wait( "(unknown)", 0, P_COND, P_MUTEX )
564 #endif
565
566 static inline int __vlc_cond_wait( char * psz_file, int i_line,
567                                    vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
568 {
569 #if defined( PTH_INIT_IN_PTH_H )
570     return pth_cond_await( p_condvar, p_mutex, NULL );
571
572 #elif defined( ST_INIT_IN_ST_H )
573     int i_ret;
574
575     st_mutex_unlock( *p_mutex );
576     i_ret = st_cond_wait( *p_condvar );
577     st_mutex_lock( *p_mutex );
578
579     return i_ret;
580
581 #elif defined( WIN32 )
582     if( !p_condvar->semaphore )
583     {
584         /* Increase our wait count */
585         p_condvar->i_waiting_threads++;
586
587         if( p_condvar->SignalObjectAndWait && p_mutex->mutex )
588         /* It is only possible to atomically release the mutex and initiate the
589          * waiting on WinNT/2K/XP. Win9x doesn't have SignalObjectAndWait(). */
590             p_condvar->SignalObjectAndWait( p_mutex->mutex,
591                                             p_condvar->event,
592                                             INFINITE, FALSE );
593         else
594         {
595             LeaveCriticalSection( &p_mutex->csection );
596             WaitForSingleObject( p_condvar->event, INFINITE );
597         }
598
599         p_condvar->i_waiting_threads--;
600     }
601     else if( p_condvar->i_win9x_cv == 1 )
602     {
603         int i_waiting_threads;
604
605         /* Wait for the gate to be open */
606         WaitForSingleObject( p_condvar->event, INFINITE );
607
608         /* Increase our wait count */
609         p_condvar->i_waiting_threads++;
610
611         LeaveCriticalSection( &p_mutex->csection );
612         WaitForSingleObject( p_condvar->semaphore, INFINITE );
613
614         /* Decrement and test must be atomic */
615         EnterCriticalSection( &p_condvar->csection );
616
617         /* Decrease our wait count */
618         i_waiting_threads = --p_condvar->i_waiting_threads;
619
620         LeaveCriticalSection( &p_condvar->csection );
621
622         /* Reopen the gate if we were the last waiting thread */
623         if( !i_waiting_threads )
624             SetEvent( p_condvar->event );
625     }
626     else
627     {
628         int i_waiting_threads;
629
630         /* Increase our wait count */
631         p_condvar->i_waiting_threads++;
632
633         LeaveCriticalSection( &p_mutex->csection );
634         WaitForSingleObject( p_condvar->semaphore, INFINITE );
635
636         /* Decrement and test must be atomic */
637         EnterCriticalSection( &p_condvar->csection );
638
639         /* Decrease our wait count */
640         i_waiting_threads = --p_condvar->i_waiting_threads;
641
642         LeaveCriticalSection( &p_condvar->csection );
643
644         /* Signal that the last waiting thread just went through */
645         if( !i_waiting_threads )
646             SetEvent( p_condvar->event );
647     }
648
649     /* Reacquire the mutex before returning. */
650     vlc_mutex_lock( p_mutex );
651
652     return 0;
653
654 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
655
656 #   ifdef DEBUG
657     /* In debug mode, timeout */
658     struct timeval now;
659     struct timespec timeout;
660     int    i_result;
661
662     for( ; ; )
663     {
664         gettimeofday( &now, NULL );
665         timeout.tv_sec = now.tv_sec + THREAD_COND_TIMEOUT;
666         timeout.tv_nsec = now.tv_usec * 1000;
667
668         i_result = pthread_cond_timedwait( p_condvar, p_mutex, &timeout );
669
670         if( i_result == ETIMEDOUT )
671         {
672 #if 0
673             msg_Warn( "thread %d: possible deadlock detected "
674                       "in cond_wait at %s:%d (%s)", pthread_self(),
675                       psz_file, i_line, strerror(i_result) );
676 #endif
677             continue;
678         }
679
680         if( i_result )
681         {
682 #if 0
683             msg_Err( "thread %d: cond_wait failed at %s:%d (%s)",
684                      pthread_self(), psz_file, i_line, strerror(i_result) );
685 #endif
686         }
687         return( i_result );
688     }
689 #   else
690     return pthread_cond_wait( p_condvar, p_mutex );
691 #   endif
692
693 #elif defined( HAVE_CTHREADS_H )
694     condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
695     return 0;
696
697 #elif defined( HAVE_KERNEL_SCHEDULER_H )
698     if( !p_condvar )
699     {
700         return B_BAD_VALUE;
701     }
702
703     if( !p_mutex )
704     {
705         return B_BAD_VALUE;
706     }
707
708     if( p_condvar->init < 2000 )
709     {
710         return B_NO_INIT;
711     }
712
713     /* The p_condvar->thread var is initialized before the unlock because
714      * it enables to identify when the thread is interrupted beetwen the
715      * unlock line and the suspend_thread line */
716     p_condvar->thread = find_thread( NULL );
717     vlc_mutex_unlock( p_mutex );
718     suspend_thread( p_condvar->thread );
719     p_condvar->thread = -1;
720
721     vlc_mutex_lock( p_mutex );
722     return 0;
723
724 #endif
725 }
726
727 /*****************************************************************************
728  * vlc_cond_destroy: destroy a condition
729  *****************************************************************************/
730 #ifdef DEBUG
731 #   define vlc_cond_destroy( P_COND )                                       \
732         __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
733 #else
734 #   define vlc_cond_destroy( P_COND )                                       \
735         __vlc_cond_destroy( "(unknown)", 0, P_COND )
736 #endif
737
738 /*****************************************************************************
739  * vlc_thread_create: create a thread
740  *****************************************************************************/
741 #   define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, WAIT )                \
742         __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, WAIT )
743
744 /*****************************************************************************
745  * vlc_thread_ready: tell the parent thread we were successfully spawned
746  *****************************************************************************/
747 #   define vlc_thread_ready( P_THIS )                                       \
748         __vlc_thread_ready( VLC_OBJECT(P_THIS) )
749
750 /*****************************************************************************
751  * vlc_thread_join: wait until a thread exits
752  *****************************************************************************/
753 #ifdef DEBUG
754 #   define vlc_thread_join( P_THIS )                                        \
755         __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ ) 
756 #else
757 #   define vlc_thread_join( P_THIS )                                        \
758         __vlc_thread_join( VLC_OBJECT(P_THIS), "(unknown)", 0 ) 
759 #endif