413f91768ef8b73478907b9c3b82277d740f9217
[vlc.git] / include / vlc_threads_funcs.h
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-2007 the VideoLAN team
6  * $Id$
7  *
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>
12  *
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.
17  *
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.
22  *
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27
28 #if !defined( __LIBVLC__ )
29   #error You are not libvlc or one of its plugins. You cannot include this file
30 #endif
31
32 #ifndef _VLC_THREADFUNCS_H_
33 #define _VLC_THREADFUNCS_H_
34
35 /*****************************************************************************
36  * Function definitions
37  *****************************************************************************/
38 VLC_EXPORT( int,  __vlc_mutex_init,    ( vlc_object_t *, vlc_mutex_t * ) );
39 VLC_EXPORT( int,  __vlc_mutex_destroy, ( const char *, int, vlc_mutex_t * ) );
40 VLC_EXPORT( int,  __vlc_cond_init,     ( vlc_object_t *, vlc_cond_t * ) );
41 VLC_EXPORT( int,  __vlc_cond_destroy,  ( const char *, int, vlc_cond_t * ) );
42 VLC_EXPORT( int, __vlc_threadvar_create, (vlc_object_t *, vlc_threadvar_t * ) );
43 VLC_EXPORT( int,  __vlc_thread_create, ( vlc_object_t *, const char *, int, const char *, void * ( * ) ( void * ), int, vlc_bool_t ) );
44 VLC_EXPORT( int,  __vlc_thread_set_priority, ( vlc_object_t *, const char *, int, int ) );
45 VLC_EXPORT( void, __vlc_thread_ready,  ( vlc_object_t * ) );
46 VLC_EXPORT( void, __vlc_thread_join,   ( vlc_object_t *, const char *, int ) );
47
48 /*****************************************************************************
49  * vlc_threads_init: initialize threads system
50  *****************************************************************************/
51 #define vlc_threads_init( P_THIS )                                          \
52     __vlc_threads_init( VLC_OBJECT(P_THIS) )
53
54 /*****************************************************************************
55  * vlc_threads_end: deinitialize threads system
56  *****************************************************************************/
57 #define vlc_threads_end( P_THIS )                                           \
58     __vlc_threads_end( VLC_OBJECT(P_THIS) )
59
60 /*****************************************************************************
61  * vlc_mutex_init: initialize a mutex
62  *****************************************************************************/
63 #define vlc_mutex_init( P_THIS, P_MUTEX )                                   \
64     __vlc_mutex_init( VLC_OBJECT(P_THIS), P_MUTEX )
65
66 /*****************************************************************************
67  * vlc_mutex_lock: lock a mutex
68  *****************************************************************************/
69 #define vlc_mutex_lock( P_MUTEX )                                           \
70     __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
71
72 #ifdef __APPLE__
73 #   define CAST_PTHREAD_TO_INT(t) (unsigned long int)(uintptr_t)(void *)t
74 #else
75 #   define CAST_PTHREAD_TO_INT(t) (unsigned long int)t
76 #endif
77
78 static inline int __vlc_mutex_lock( const char * psz_file, int i_line,
79                                     vlc_mutex_t * p_mutex )
80 {
81     int i_result;
82     /* In case of error : */
83     unsigned long int i_thread = 0;
84
85 #if defined( PTH_INIT_IN_PTH_H )
86     i_result = ( pth_mutex_acquire( &p_mutex->mutex, FALSE, NULL ) == FALSE );
87
88 #elif defined( ST_INIT_IN_ST_H )
89     i_result = st_mutex_lock( p_mutex->mutex );
90
91 #elif defined( UNDER_CE )
92     EnterCriticalSection( &p_mutex->csection );
93     i_result = 0;
94
95 #elif defined( WIN32 )
96     if( p_mutex->mutex )
97     {
98         WaitForSingleObject( p_mutex->mutex, INFINITE );
99     }
100     else
101     {
102         EnterCriticalSection( &p_mutex->csection );
103     }
104     i_result = 0;
105
106 #elif defined( HAVE_KERNEL_SCHEDULER_H )
107     if( p_mutex == NULL )
108     {
109         i_result = B_BAD_VALUE;
110     }
111     else if( p_mutex->init < 2000 )
112     {
113         i_result = B_NO_INIT;
114     }
115     else
116     {
117         i_result = acquire_sem( p_mutex->lock );
118     }
119
120 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
121 #   define vlc_assert_locked( m ) \
122            assert (pthread_mutex_lock (&((m)->mutex)) == EDEADLK)
123
124     i_result = pthread_mutex_lock( &p_mutex->mutex );
125     if ( i_result )
126     {
127         i_thread = CAST_PTHREAD_TO_INT(pthread_self());
128         errno = i_result;
129     }
130
131 #elif defined( HAVE_CTHREADS_H )
132     mutex_lock( p_mutex->mutex );
133     i_result = 0;
134
135 #endif
136
137     if( i_result )
138     {
139         msg_Err( p_mutex->p_this,
140                  "thread %li: mutex_lock failed at %s:%d (%d:%m)",
141                  i_thread, psz_file, i_line, i_result );
142     }
143     return i_result;
144 }
145
146 #ifndef vlc_assert_locked
147 # define vlc_assert_locked( m ) (void)0
148 #endif
149
150 /*****************************************************************************
151  * vlc_mutex_unlock: unlock a mutex
152  *****************************************************************************/
153 #define vlc_mutex_unlock( P_MUTEX )                                         \
154     __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
155
156 static inline int __vlc_mutex_unlock( const char * psz_file, int i_line,
157                                       vlc_mutex_t *p_mutex )
158 {
159     int i_result;
160     /* In case of error : */
161     unsigned long int i_thread = 0;
162
163 #if defined( PTH_INIT_IN_PTH_H )
164     i_result = ( pth_mutex_release( &p_mutex->mutex ) == FALSE );
165
166 #elif defined( ST_INIT_IN_ST_H )
167     i_result = st_mutex_unlock( p_mutex->mutex );
168
169 #elif defined( UNDER_CE )
170     LeaveCriticalSection( &p_mutex->csection );
171     i_result = 0;
172
173 #elif defined( WIN32 )
174     if( p_mutex->mutex )
175     {
176         ReleaseMutex( p_mutex->mutex );
177     }
178     else
179     {
180         LeaveCriticalSection( &p_mutex->csection );
181     }
182     i_result = 0;
183
184 #elif defined( HAVE_KERNEL_SCHEDULER_H )
185     if( p_mutex == NULL )
186     {
187         i_result = B_BAD_VALUE;
188     }
189     else if( p_mutex->init < 2000 )
190     {
191         i_result = B_NO_INIT;
192     }
193     else
194     {
195         release_sem( p_mutex->lock );
196         return B_OK;
197     }
198
199 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
200     i_result = pthread_mutex_unlock( &p_mutex->mutex );
201     if ( i_result )
202     {
203         i_thread = CAST_PTHREAD_TO_INT(pthread_self());
204         errno = i_result;
205     }
206
207 #elif defined( HAVE_CTHREADS_H )
208     mutex_unlock( p_mutex );
209     i_result = 0;
210
211 #endif
212
213     if( i_result )
214     {
215         msg_Err( p_mutex->p_this,
216                  "thread %li: mutex_unlock failed at %s:%d (%d:%m)",
217                  i_thread, psz_file, i_line, i_result );
218     }
219
220     return i_result;
221 }
222
223 /*****************************************************************************
224  * vlc_mutex_destroy: destroy a mutex
225  *****************************************************************************/
226 #define vlc_mutex_destroy( P_MUTEX )                                        \
227     __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
228
229 /*****************************************************************************
230  * vlc_cond_init: initialize a condition
231  *****************************************************************************/
232 #define vlc_cond_init( P_THIS, P_COND )                                     \
233     __vlc_cond_init( VLC_OBJECT(P_THIS), P_COND )
234
235 /*****************************************************************************
236  * vlc_cond_signal: start a thread on condition completion
237  *****************************************************************************/
238 #define vlc_cond_signal( P_COND )                                           \
239     __vlc_cond_signal( __FILE__, __LINE__, P_COND )
240
241 static inline int __vlc_cond_signal( const char * psz_file, int i_line,
242                                      vlc_cond_t *p_condvar )
243 {
244     int i_result;
245     /* In case of error : */
246     unsigned long int i_thread = 0;
247
248 #if defined( PTH_INIT_IN_PTH_H )
249     i_result = ( pth_cond_notify( &p_condvar->cond, FALSE ) == FALSE );
250
251 #elif defined( ST_INIT_IN_ST_H )
252     i_result = st_cond_signal( p_condvar->cond );
253
254 #elif defined( UNDER_CE )
255     PulseEvent( p_condvar->event );
256     i_result = 0;
257
258 #elif defined( WIN32 )
259     /* Release one waiting thread if one is available. */
260     /* For this trick to work properly, the vlc_cond_signal must be surrounded
261      * by a mutex. This will prevent another thread from stealing the signal */
262     if( !p_condvar->semaphore )
263     {
264         PulseEvent( p_condvar->event );
265     }
266     else if( p_condvar->i_win9x_cv == 1 )
267     {
268         /* Wait for the gate to be open */
269         WaitForSingleObject( p_condvar->event, INFINITE );
270
271         if( p_condvar->i_waiting_threads )
272         {
273             /* Using a semaphore exposes us to a race condition. It is
274              * possible for another thread to start waiting on the semaphore
275              * just after we signaled it and thus steal the signal.
276              * We have to prevent new threads from entering the cond_wait(). */
277             ResetEvent( p_condvar->event );
278
279             /* A semaphore is used here because Win9x doesn't have
280              * SignalObjectAndWait() and thus a race condition exists
281              * during the time we release the mutex and the time we start
282              * waiting on the event (more precisely, the signal can sometimes
283              * be missed by the waiting thread if we use PulseEvent()). */
284             ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
285         }
286     }
287     else
288     {
289         if( p_condvar->i_waiting_threads )
290         {
291             ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
292
293             /* Wait for the last thread to be awakened */
294             WaitForSingleObject( p_condvar->event, INFINITE );
295         }
296     }
297     i_result = 0;
298
299 #elif defined( HAVE_KERNEL_SCHEDULER_H )
300     if( p_condvar == NULL )
301     {
302         i_result = B_BAD_VALUE;
303     }
304     else if( p_condvar->init < 2000 )
305     {
306         i_result = B_NO_INIT;
307     }
308     else
309     {
310         while( p_condvar->thread != -1 )
311         {
312             thread_info info;
313             if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
314             {
315                 return 0;
316             }
317
318             if( info.state != B_THREAD_SUSPENDED )
319             {
320                 /* The  waiting thread is not suspended so it could
321                  * have been interrupted beetwen the unlock and the
322                  * suspend_thread line. That is why we sleep a little
323                  * before retesting p_condver->thread. */
324                 snooze( 10000 );
325             }
326             else
327             {
328                 /* Ok, we have to wake up that thread */
329                 resume_thread( p_condvar->thread );
330                 return 0;
331             }
332         }
333         i_result = 0;
334     }
335
336 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
337     i_result = pthread_cond_signal( &p_condvar->cond );
338     if ( i_result )
339     {
340         i_thread = CAST_PTHREAD_TO_INT(pthread_self());
341         errno = i_result;
342     }
343
344 #elif defined( HAVE_CTHREADS_H )
345     /* condition_signal() */
346     if ( p_condvar->queue.head || p_condvar->implications )
347     {
348         cond_signal( (condition_t)p_condvar );
349     }
350     i_result = 0;
351
352 #endif
353
354     if( i_result )
355     {
356         msg_Err( p_condvar->p_this,
357                  "thread %li: cond_signal failed at %s:%d (%d:%m)",
358                  i_thread, psz_file, i_line, i_result );
359     }
360
361     return i_result;
362 }
363
364 /*****************************************************************************
365  * vlc_cond_wait: wait until condition completion
366  *****************************************************************************/
367 #define vlc_cond_wait( P_COND, P_MUTEX )                                     \
368     __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX  )
369
370 static inline int __vlc_cond_wait( const char * psz_file, int i_line,
371                                    vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
372 {
373     int i_result;
374     /* In case of error : */
375     unsigned long int i_thread = 0;
376
377 #if defined( PTH_INIT_IN_PTH_H )
378     i_result = ( pth_cond_await( &p_condvar->cond, &p_mutex->mutex, NULL )
379                  == FALSE );
380
381 #elif defined( ST_INIT_IN_ST_H )
382     st_mutex_unlock( p_mutex->mutex );
383     i_result = st_cond_wait( p_condvar->cond );
384     st_mutex_lock( p_mutex->mutex );
385
386 #elif defined( UNDER_CE )
387     p_condvar->i_waiting_threads++;
388     LeaveCriticalSection( &p_mutex->csection );
389     WaitForSingleObject( p_condvar->event, INFINITE );
390     p_condvar->i_waiting_threads--;
391
392     /* Reacquire the mutex before returning. */
393     vlc_mutex_lock( p_mutex );
394
395     i_result = 0;
396
397 #elif defined( WIN32 )
398     if( !p_condvar->semaphore )
399     {
400         /* Increase our wait count */
401         p_condvar->i_waiting_threads++;
402
403         if( p_mutex->mutex )
404         {
405             /* It is only possible to atomically release the mutex and
406              * initiate the waiting on WinNT/2K/XP. Win9x doesn't have
407              * SignalObjectAndWait(). */
408             p_condvar->SignalObjectAndWait( p_mutex->mutex,
409                                             p_condvar->event,
410                                             INFINITE, FALSE );
411         }
412         else
413         {
414             LeaveCriticalSection( &p_mutex->csection );
415             WaitForSingleObject( p_condvar->event, INFINITE );
416         }
417
418         p_condvar->i_waiting_threads--;
419     }
420     else if( p_condvar->i_win9x_cv == 1 )
421     {
422         int i_waiting_threads;
423
424         /* Wait for the gate to be open */
425         WaitForSingleObject( p_condvar->event, INFINITE );
426
427         /* Increase our wait count */
428         p_condvar->i_waiting_threads++;
429
430         LeaveCriticalSection( &p_mutex->csection );
431         WaitForSingleObject( p_condvar->semaphore, INFINITE );
432
433         /* Decrement and test must be atomic */
434         EnterCriticalSection( &p_condvar->csection );
435
436         /* Decrease our wait count */
437         i_waiting_threads = --p_condvar->i_waiting_threads;
438
439         LeaveCriticalSection( &p_condvar->csection );
440
441         /* Reopen the gate if we were the last waiting thread */
442         if( !i_waiting_threads )
443             SetEvent( p_condvar->event );
444     }
445     else
446     {
447         int i_waiting_threads;
448
449         /* Increase our wait count */
450         p_condvar->i_waiting_threads++;
451
452         LeaveCriticalSection( &p_mutex->csection );
453         WaitForSingleObject( p_condvar->semaphore, INFINITE );
454
455         /* Decrement and test must be atomic */
456         EnterCriticalSection( &p_condvar->csection );
457
458         /* Decrease our wait count */
459         i_waiting_threads = --p_condvar->i_waiting_threads;
460
461         LeaveCriticalSection( &p_condvar->csection );
462
463         /* Signal that the last waiting thread just went through */
464         if( !i_waiting_threads )
465             SetEvent( p_condvar->event );
466     }
467
468     /* Reacquire the mutex before returning. */
469     vlc_mutex_lock( p_mutex );
470
471     i_result = 0;
472
473 #elif defined( HAVE_KERNEL_SCHEDULER_H )
474     if( p_condvar == NULL )
475     {
476         i_result = B_BAD_VALUE;
477     }
478     else if( p_mutex == NULL )
479     {
480         i_result = B_BAD_VALUE;
481     }
482     else if( p_condvar->init < 2000 )
483     {
484         i_result = B_NO_INIT;
485     }
486
487     /* The p_condvar->thread var is initialized before the unlock because
488      * it enables to identify when the thread is interrupted beetwen the
489      * unlock line and the suspend_thread line */
490     p_condvar->thread = find_thread( NULL );
491     vlc_mutex_unlock( p_mutex );
492     suspend_thread( p_condvar->thread );
493     p_condvar->thread = -1;
494
495     vlc_mutex_lock( p_mutex );
496     i_result = 0;
497
498 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
499
500 #   ifdef DEBUG
501     /* In debug mode, timeout */
502     struct timespec timeout = {
503         (mdate() / 1000000) + THREAD_COND_TIMEOUT,
504         0 /* 1Hz precision is sufficient here :-) */
505     };
506
507     i_result = pthread_cond_timedwait( &p_condvar->cond, &p_mutex->mutex,
508                                        &timeout );
509
510     if( i_result == ETIMEDOUT )
511     {
512         errno = ETIMEDOUT;
513         msg_Dbg( p_condvar->p_this,
514                   "thread %li: possible condition deadlock "
515                   "at %s:%d (%m)", CAST_PTHREAD_TO_INT(pthread_self()),
516                   psz_file, i_line );
517
518         i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
519     }
520
521 #   else
522     i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
523 #   endif
524
525     if ( i_result )
526     {
527         i_thread = CAST_PTHREAD_TO_INT(pthread_self());
528         errno = i_result;
529     }
530
531 #elif defined( HAVE_CTHREADS_H )
532     condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
533     i_result = 0;
534
535 #endif
536
537     if( i_result )
538     {
539         msg_Err( p_condvar->p_this,
540                  "thread %li: cond_wait failed at %s:%d (%d:%m)",
541                  i_thread, psz_file, i_line, i_result );
542     }
543
544     return i_result;
545 }
546
547
548 /*****************************************************************************
549  * vlc_cond_timedwait: wait until condition completion or expiration
550  *****************************************************************************
551  * Returns 0 if object signaled, an error code in case of timeout or error.
552  *****************************************************************************/
553 #define vlc_cond_timedwait( P_COND, P_MUTEX, DEADLINE )                      \
554     __vlc_cond_timedwait( __FILE__, __LINE__, P_COND, P_MUTEX, DEADLINE  )
555
556 static inline int __vlc_cond_timedwait( const char * psz_file, int i_line,
557                                         vlc_cond_t *p_condvar,
558                                         vlc_mutex_t *p_mutex,
559                                         mtime_t deadline )
560 {
561     int i_res;
562     unsigned long int i_thread = 0;
563
564 #if defined( PTH_INIT_IN_PTH_H )
565 #   error Unimplemented
566 #elif defined( ST_INIT_IN_ST_H )
567 #   error Unimplemented
568 #elif defined( UNDER_CE )
569     mtime_t delay_ms = (deadline - mdate())/1000;
570
571     DWORD result;
572     if( delay_ms < 0 )
573         delay_ms = 0;
574
575     p_condvar->i_waiting_threads++;
576     LeaveCriticalSection( &p_mutex->csection );
577     result = WaitForSingleObject( p_condvar->event, delay_ms );
578     p_condvar->i_waiting_threads--;
579
580     /* Reacquire the mutex before returning. */
581     vlc_mutex_lock( p_mutex );
582
583     i_res = (int)result;
584
585 #elif defined( WIN32 )
586     DWORD result;
587
588     mtime_t delay_ms = (deadline - mdate())/1000;
589     if( delay_ms < 0 )
590         delay_ms = 0;
591
592     if( !p_condvar->semaphore )
593     {
594         /* Increase our wait count */
595         p_condvar->i_waiting_threads++;
596
597         if( p_mutex->mutex )
598         {
599             /* It is only possible to atomically release the mutex and
600              * initiate the waiting on WinNT/2K/XP. Win9x doesn't have
601              * SignalObjectAndWait(). */
602             result = p_condvar->SignalObjectAndWait( p_mutex->mutex,
603                                             p_condvar->event,
604                                             delay_ms, FALSE );
605         }
606         else
607         {
608             LeaveCriticalSection( &p_mutex->csection );
609             result = WaitForSingleObject( p_condvar->event, delay_ms );
610         }
611
612         p_condvar->i_waiting_threads--;
613     }
614     else if( p_condvar->i_win9x_cv == 1 )
615     {
616         int i_waiting_threads;
617
618         /* Wait for the gate to be open */
619         result = WaitForSingleObject( p_condvar->event, delay_ms );
620
621         /* recaculate remaining delay */
622         delay_ms = (deadline - mdate())/1000;
623         if( delay_ms < 0 )
624             delay_ms = 0;
625
626         /* Increase our wait count */
627         p_condvar->i_waiting_threads++;
628
629         LeaveCriticalSection( &p_mutex->csection );
630         if( !result )
631             result = WaitForSingleObject( p_condvar->semaphore, delay_ms );
632
633         /* Decrement and test must be atomic */
634         EnterCriticalSection( &p_condvar->csection );
635
636         /* Decrease our wait count */
637         i_waiting_threads = --p_condvar->i_waiting_threads;
638
639         LeaveCriticalSection( &p_condvar->csection );
640
641         /* Reopen the gate if we were the last waiting thread */
642         if( !i_waiting_threads )
643             SetEvent( p_condvar->event );
644     }
645     else
646     {
647         int i_waiting_threads;
648
649         /* Increase our wait count */
650         p_condvar->i_waiting_threads++;
651
652         LeaveCriticalSection( &p_mutex->csection );
653         result = WaitForSingleObject( p_condvar->semaphore, delay_ms );
654
655         /* Decrement and test must be atomic */
656         EnterCriticalSection( &p_condvar->csection );
657
658         /* Decrease our wait count */
659         i_waiting_threads = --p_condvar->i_waiting_threads;
660
661         LeaveCriticalSection( &p_condvar->csection );
662
663         /* Signal that the last waiting thread just went through */
664         if( !i_waiting_threads )
665             SetEvent( p_condvar->event );
666     }
667
668     /* Reacquire the mutex before returning. */
669     vlc_mutex_lock( p_mutex );
670
671     i_res = (int)result;
672
673 #elif defined( HAVE_KERNEL_SCHEDULER_H )
674 #   error Unimplemented
675 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
676     lldiv_t d = lldiv( deadline, 1000000 );
677     struct timespec ts = { d.quot, d.rem * 1000 };
678
679     i_res = pthread_cond_timedwait( &p_condvar->cond, &p_mutex->mutex, &ts );
680     if( i_res == ETIMEDOUT )
681         return ETIMEDOUT; /* this error is perfectly normal */
682     else
683     if ( i_res ) /* other errors = bug */
684     {
685         i_thread = CAST_PTHREAD_TO_INT(pthread_self());
686         errno = i_res;
687     }
688
689 #elif defined( HAVE_CTHREADS_H )
690 #   error Unimplemented
691 #endif
692
693     if( i_res )
694     {
695         msg_Err( p_condvar->p_this,
696                  "thread %li: cond_wait failed at %s:%d (%d:%m)",
697                  i_thread, psz_file, i_line, i_res );
698     }
699
700     return i_res;
701 }
702
703 /*****************************************************************************
704  * vlc_cond_destroy: destroy a condition
705  *****************************************************************************/
706 #define vlc_cond_destroy( P_COND )                                          \
707     __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
708
709 /*****************************************************************************
710  * vlc_threadvar_create: create a thread-local variable
711  *****************************************************************************/
712 #define vlc_threadvar_create( PTHIS, P_TLS )                                 \
713    __vlc_threadvar_create( PTHIS, P_TLS )
714
715 /*****************************************************************************
716  * vlc_threadvar_set: create: set the value of a thread-local variable
717  *****************************************************************************/
718 static inline int vlc_threadvar_set( vlc_threadvar_t * p_tls, void *p_value )
719 {
720     int i_ret;
721
722 #if defined( PTH_INIT_IN_PTH_H )
723     return pth_key_setdata( p_tls->handle, p_value );
724 #elif  defined( ST_INIT_IN_ST_H )
725     return st_thread_setspecific( p_tls->handle, p_value );
726 #elif defined( HAVE_KERNEL_SCHEDULER_H )
727     return -1;
728
729 #elif defined( UNDER_CE ) || defined( WIN32 )
730     i_ret = ( TlsSetValue( p_tls->handle, p_value ) != 0 );
731
732 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
733     i_ret = pthread_setspecific( p_tls->handle, p_value );
734
735 #elif defined( HAVE_CTHREADS_H )
736     i_ret = cthread_setspecific( p_tls->handle, p_value );
737 #endif
738
739     return i_ret;
740 }
741
742 /*****************************************************************************
743  * vlc_threadvar_get: create: get the value of a thread-local variable
744  *****************************************************************************/
745 static inline void* vlc_threadvar_get( vlc_threadvar_t * p_tls )
746 {
747     void* p_ret;
748
749 #if defined( PTH_INIT_IN_PTH_H )
750     p_ret = pth_key_getdata( p_handle->key );
751 #elif defined( ST_INIT_IN_ST_H )
752     p_ret = st_thread_getspecific( p_handle->key );
753 #elif defined( HAVE_KERNEL_SCHEDULER_H )
754     p_ret = NULL;
755 #elif defined( UNDER_CE ) || defined( WIN32 )
756     p_ret = TlsGetValue( p_tls->handle );
757
758 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
759     p_ret = pthread_getspecific( p_tls->handle );
760
761 #elif defined( HAVE_CTHREADS_H )
762     if ( !cthread_getspecific( p_tls->handle, &p_ret ) )
763     {
764         p_ret = NULL;
765     }
766 #endif
767
768     return p_ret;
769 }
770
771 /*****************************************************************************
772  * vlc_thread_create: create a thread
773  *****************************************************************************/
774 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT )         \
775     __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, PRIORITY, WAIT )
776
777 /*****************************************************************************
778  * vlc_thread_set_priority: set the priority of the calling thread
779  *****************************************************************************/
780 #define vlc_thread_set_priority( P_THIS, PRIORITY )                         \
781     __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
782
783 /*****************************************************************************
784  * vlc_thread_ready: tell the parent thread we were successfully spawned
785  *****************************************************************************/
786 #define vlc_thread_ready( P_THIS )                                          \
787     __vlc_thread_ready( VLC_OBJECT(P_THIS) )
788
789 /*****************************************************************************
790  * vlc_thread_join: wait until a thread exits
791  *****************************************************************************/
792 #define vlc_thread_join( P_THIS )                                           \
793     __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )
794
795 #endif