]> git.sesse.net Git - vlc/blob - include/vlc_threads_funcs.h
Do not export thread init functions.
[vlc] / 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, 2002 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 /*****************************************************************************
50  * vlc_threads_init: initialize threads system
51  *****************************************************************************/
52 #define vlc_threads_init( P_THIS )                                          \
53     __vlc_threads_init( VLC_OBJECT(P_THIS) )
54
55 /*****************************************************************************
56  * vlc_threads_end: deinitialize threads system
57  *****************************************************************************/
58 #define vlc_threads_end( P_THIS )                                           \
59     __vlc_threads_end( VLC_OBJECT(P_THIS) )
60
61 /*****************************************************************************
62  * vlc_mutex_init: initialize a mutex
63  *****************************************************************************/
64 #define vlc_mutex_init( P_THIS, P_MUTEX )                                   \
65     __vlc_mutex_init( VLC_OBJECT(P_THIS), P_MUTEX )
66
67 /*****************************************************************************
68  * vlc_mutex_lock: lock a mutex
69  *****************************************************************************/
70 #define vlc_mutex_lock( P_MUTEX )                                           \
71     __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
72
73 #ifdef __APPLE__
74 #   define CAST_PTHREAD_TO_INT(t) (unsigned long int)(uintptr_t)(void *)t
75 #else
76 #   define CAST_PTHREAD_TO_INT(t) (unsigned long int)t
77 #endif
78
79 static inline int __vlc_mutex_lock( const char * psz_file, int i_line,
80                                     vlc_mutex_t * p_mutex )
81 {
82     int i_result;
83     /* In case of error : */
84     unsigned long int i_thread = 0;
85     const char * psz_error = "";
86
87 #if defined( PTH_INIT_IN_PTH_H )
88     i_result = ( pth_mutex_acquire( &p_mutex->mutex, FALSE, NULL ) == FALSE );
89
90 #elif defined( ST_INIT_IN_ST_H )
91     i_result = st_mutex_lock( p_mutex->mutex );
92
93 #elif defined( UNDER_CE )
94     EnterCriticalSection( &p_mutex->csection );
95     i_result = 0;
96
97 #elif defined( WIN32 )
98     if( p_mutex->mutex )
99     {
100         WaitForSingleObject( p_mutex->mutex, INFINITE );
101     }
102     else
103     {
104         EnterCriticalSection( &p_mutex->csection );
105     }
106     i_result = 0;
107
108 #elif defined( HAVE_KERNEL_SCHEDULER_H )
109     if( p_mutex == NULL )
110     {
111         i_result = B_BAD_VALUE;
112     }
113     else if( p_mutex->init < 2000 )
114     {
115         i_result = B_NO_INIT;
116     }
117     else
118     {
119         i_result = acquire_sem( p_mutex->lock );
120     }
121
122 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
123 #   define vlc_assert_locked( m ) \
124            assert (pthread_mutex_lock (&((m)->mutex)) == EDEADLK)
125
126     i_result = pthread_mutex_lock( &p_mutex->mutex );
127     if ( i_result )
128     {
129         i_thread = CAST_PTHREAD_TO_INT(pthread_self());
130         psz_error = strerror(i_result);
131     }
132
133 #elif defined( HAVE_CTHREADS_H )
134     mutex_lock( p_mutex->mutex );
135     i_result = 0;
136
137 #endif
138
139     if( i_result )
140     {
141         msg_Err( p_mutex->p_this,
142                  "thread %li: mutex_lock failed at %s:%d (%d:%s)",
143                  i_thread, psz_file, i_line, i_result, psz_error );
144     }
145     return i_result;
146 }
147
148 #ifndef vlc_assert_locked
149 # define vlc_assert_locked( m ) (void)0
150 #endif
151
152 /*****************************************************************************
153  * vlc_mutex_unlock: unlock a mutex
154  *****************************************************************************/
155 #define vlc_mutex_unlock( P_MUTEX )                                         \
156     __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
157
158 static inline int __vlc_mutex_unlock( const char * psz_file, int i_line,
159                                       vlc_mutex_t *p_mutex )
160 {
161     int i_result;
162     /* In case of error : */
163     unsigned long int i_thread = 0;
164     const char * psz_error = "";
165
166 #if defined( PTH_INIT_IN_PTH_H )
167     i_result = ( pth_mutex_release( &p_mutex->mutex ) == FALSE );
168
169 #elif defined( ST_INIT_IN_ST_H )
170     i_result = st_mutex_unlock( p_mutex->mutex );
171
172 #elif defined( UNDER_CE )
173     LeaveCriticalSection( &p_mutex->csection );
174     i_result = 0;
175
176 #elif defined( WIN32 )
177     if( p_mutex->mutex )
178     {
179         ReleaseMutex( p_mutex->mutex );
180     }
181     else
182     {
183         LeaveCriticalSection( &p_mutex->csection );
184     }
185     i_result = 0;
186
187 #elif defined( HAVE_KERNEL_SCHEDULER_H )
188     if( p_mutex == NULL )
189     {
190         i_result = B_BAD_VALUE;
191     }
192     else if( p_mutex->init < 2000 )
193     {
194         i_result = B_NO_INIT;
195     }
196     else
197     {
198         release_sem( p_mutex->lock );
199         return B_OK;
200     }
201
202 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
203     i_result = pthread_mutex_unlock( &p_mutex->mutex );
204     if ( i_result )
205     {
206         i_thread = CAST_PTHREAD_TO_INT(pthread_self());
207         psz_error = strerror(i_result);
208     }
209
210 #elif defined( HAVE_CTHREADS_H )
211     mutex_unlock( p_mutex );
212     i_result = 0;
213
214 #endif
215
216     if( i_result )
217     {
218         msg_Err( p_mutex->p_this,
219                  "thread %li: mutex_unlock failed at %s:%d (%d:%s)",
220                  i_thread, psz_file, i_line, i_result, psz_error );
221     }
222
223     return i_result;
224 }
225
226 /*****************************************************************************
227  * vlc_mutex_destroy: destroy a mutex
228  *****************************************************************************/
229 #define vlc_mutex_destroy( P_MUTEX )                                        \
230     __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
231
232 /*****************************************************************************
233  * vlc_cond_init: initialize a condition
234  *****************************************************************************/
235 #define vlc_cond_init( P_THIS, P_COND )                                     \
236     __vlc_cond_init( VLC_OBJECT(P_THIS), P_COND )
237
238 /*****************************************************************************
239  * vlc_cond_signal: start a thread on condition completion
240  *****************************************************************************/
241 #define vlc_cond_signal( P_COND )                                           \
242     __vlc_cond_signal( __FILE__, __LINE__, P_COND )
243
244 static inline int __vlc_cond_signal( const char * psz_file, int i_line,
245                                      vlc_cond_t *p_condvar )
246 {
247     int i_result;
248     /* In case of error : */
249     unsigned long int i_thread = 0;
250     const char * psz_error = "";
251
252 #if defined( PTH_INIT_IN_PTH_H )
253     i_result = ( pth_cond_notify( &p_condvar->cond, FALSE ) == FALSE );
254
255 #elif defined( ST_INIT_IN_ST_H )
256     i_result = st_cond_signal( p_condvar->cond );
257
258 #elif defined( UNDER_CE )
259     PulseEvent( p_condvar->event );
260     i_result = 0;
261
262 #elif defined( WIN32 )
263     /* Release one waiting thread if one is available. */
264     /* For this trick to work properly, the vlc_cond_signal must be surrounded
265      * by a mutex. This will prevent another thread from stealing the signal */
266     if( !p_condvar->semaphore )
267     {
268         PulseEvent( p_condvar->event );
269     }
270     else if( p_condvar->i_win9x_cv == 1 )
271     {
272         /* Wait for the gate to be open */
273         WaitForSingleObject( p_condvar->event, INFINITE );
274
275         if( p_condvar->i_waiting_threads )
276         {
277             /* Using a semaphore exposes us to a race condition. It is
278              * possible for another thread to start waiting on the semaphore
279              * just after we signaled it and thus steal the signal.
280              * We have to prevent new threads from entering the cond_wait(). */
281             ResetEvent( p_condvar->event );
282
283             /* A semaphore is used here because Win9x doesn't have
284              * SignalObjectAndWait() and thus a race condition exists
285              * during the time we release the mutex and the time we start
286              * waiting on the event (more precisely, the signal can sometimes
287              * be missed by the waiting thread if we use PulseEvent()). */
288             ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
289         }
290     }
291     else
292     {
293         if( p_condvar->i_waiting_threads )
294         {
295             ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
296
297             /* Wait for the last thread to be awakened */
298             WaitForSingleObject( p_condvar->event, INFINITE );
299         }
300     }
301     i_result = 0;
302
303 #elif defined( HAVE_KERNEL_SCHEDULER_H )
304     if( p_condvar == NULL )
305     {
306         i_result = B_BAD_VALUE;
307     }
308     else if( p_condvar->init < 2000 )
309     {
310         i_result = B_NO_INIT;
311     }
312     else
313     {
314         while( p_condvar->thread != -1 )
315         {
316             thread_info info;
317             if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
318             {
319                 return 0;
320             }
321
322             if( info.state != B_THREAD_SUSPENDED )
323             {
324                 /* The  waiting thread is not suspended so it could
325                  * have been interrupted beetwen the unlock and the
326                  * suspend_thread line. That is why we sleep a little
327                  * before retesting p_condver->thread. */
328                 snooze( 10000 );
329             }
330             else
331             {
332                 /* Ok, we have to wake up that thread */
333                 resume_thread( p_condvar->thread );
334                 return 0;
335             }
336         }
337         i_result = 0;
338     }
339
340 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
341     i_result = pthread_cond_signal( &p_condvar->cond );
342     if ( i_result )
343     {
344         i_thread = CAST_PTHREAD_TO_INT(pthread_self());
345         psz_error = strerror(i_result);
346     }
347
348 #elif defined( HAVE_CTHREADS_H )
349     /* condition_signal() */
350     if ( p_condvar->queue.head || p_condvar->implications )
351     {
352         cond_signal( (condition_t)p_condvar );
353     }
354     i_result = 0;
355
356 #endif
357
358     if( i_result )
359     {
360         msg_Err( p_condvar->p_this,
361                  "thread %li: cond_signal failed at %s:%d (%d:%s)",
362                  i_thread, psz_file, i_line, i_result, psz_error );
363     }
364
365     return i_result;
366 }
367
368 /*****************************************************************************
369  * vlc_cond_wait: wait until condition completion
370  *****************************************************************************/
371 #define vlc_cond_wait( P_COND, P_MUTEX )                                     \
372     __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX  )
373
374 static inline int __vlc_cond_wait( const char * psz_file, int i_line,
375                                    vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
376 {
377     int i_result;
378     /* In case of error : */
379     unsigned long int i_thread = 0;
380     const char * psz_error = "";
381
382 #if defined( PTH_INIT_IN_PTH_H )
383     i_result = ( pth_cond_await( &p_condvar->cond, &p_mutex->mutex, NULL )
384                  == FALSE );
385
386 #elif defined( ST_INIT_IN_ST_H )
387     st_mutex_unlock( p_mutex->mutex );
388     i_result = st_cond_wait( p_condvar->cond );
389     st_mutex_lock( p_mutex->mutex );
390
391 #elif defined( UNDER_CE )
392     p_condvar->i_waiting_threads++;
393     LeaveCriticalSection( &p_mutex->csection );
394     WaitForSingleObject( p_condvar->event, INFINITE );
395     p_condvar->i_waiting_threads--;
396
397     /* Reacquire the mutex before returning. */
398     vlc_mutex_lock( p_mutex );
399
400     i_result = 0;
401
402 #elif defined( WIN32 )
403     if( !p_condvar->semaphore )
404     {
405         /* Increase our wait count */
406         p_condvar->i_waiting_threads++;
407
408         if( p_mutex->mutex )
409         {
410             /* It is only possible to atomically release the mutex and
411              * initiate the waiting on WinNT/2K/XP. Win9x doesn't have
412              * SignalObjectAndWait(). */
413             p_condvar->SignalObjectAndWait( p_mutex->mutex,
414                                             p_condvar->event,
415                                             INFINITE, FALSE );
416         }
417         else
418         {
419             LeaveCriticalSection( &p_mutex->csection );
420             WaitForSingleObject( p_condvar->event, INFINITE );
421         }
422
423         p_condvar->i_waiting_threads--;
424     }
425     else if( p_condvar->i_win9x_cv == 1 )
426     {
427         int i_waiting_threads;
428
429         /* Wait for the gate to be open */
430         WaitForSingleObject( p_condvar->event, INFINITE );
431
432         /* Increase our wait count */
433         p_condvar->i_waiting_threads++;
434
435         LeaveCriticalSection( &p_mutex->csection );
436         WaitForSingleObject( p_condvar->semaphore, INFINITE );
437
438         /* Decrement and test must be atomic */
439         EnterCriticalSection( &p_condvar->csection );
440
441         /* Decrease our wait count */
442         i_waiting_threads = --p_condvar->i_waiting_threads;
443
444         LeaveCriticalSection( &p_condvar->csection );
445
446         /* Reopen the gate if we were the last waiting thread */
447         if( !i_waiting_threads )
448             SetEvent( p_condvar->event );
449     }
450     else
451     {
452         int i_waiting_threads;
453
454         /* Increase our wait count */
455         p_condvar->i_waiting_threads++;
456
457         LeaveCriticalSection( &p_mutex->csection );
458         WaitForSingleObject( p_condvar->semaphore, INFINITE );
459
460         /* Decrement and test must be atomic */
461         EnterCriticalSection( &p_condvar->csection );
462
463         /* Decrease our wait count */
464         i_waiting_threads = --p_condvar->i_waiting_threads;
465
466         LeaveCriticalSection( &p_condvar->csection );
467
468         /* Signal that the last waiting thread just went through */
469         if( !i_waiting_threads )
470             SetEvent( p_condvar->event );
471     }
472
473     /* Reacquire the mutex before returning. */
474     vlc_mutex_lock( p_mutex );
475
476     i_result = 0;
477
478 #elif defined( HAVE_KERNEL_SCHEDULER_H )
479     if( p_condvar == NULL )
480     {
481         i_result = B_BAD_VALUE;
482     }
483     else if( p_mutex == NULL )
484     {
485         i_result = B_BAD_VALUE;
486     }
487     else if( p_condvar->init < 2000 )
488     {
489         i_result = B_NO_INIT;
490     }
491
492     /* The p_condvar->thread var is initialized before the unlock because
493      * it enables to identify when the thread is interrupted beetwen the
494      * unlock line and the suspend_thread line */
495     p_condvar->thread = find_thread( NULL );
496     vlc_mutex_unlock( p_mutex );
497     suspend_thread( p_condvar->thread );
498     p_condvar->thread = -1;
499
500     vlc_mutex_lock( p_mutex );
501     i_result = 0;
502
503 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
504
505 #   ifdef DEBUG
506     /* In debug mode, timeout */
507     struct timeval now;
508     struct timespec timeout;
509
510     gettimeofday( &now, NULL );
511     timeout.tv_sec = now.tv_sec + THREAD_COND_TIMEOUT;
512     timeout.tv_nsec = now.tv_usec * 1000;
513
514     i_result = pthread_cond_timedwait( &p_condvar->cond, &p_mutex->mutex,
515                                        &timeout );
516
517     if( i_result == ETIMEDOUT )
518     {
519         msg_Dbg( p_condvar->p_this,
520                   "thread %li: possible condition deadlock "
521                   "at %s:%d (%s)", CAST_PTHREAD_TO_INT(pthread_self()),
522                   psz_file, i_line, strerror(i_result) );
523
524         i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
525     }
526
527 #   else
528     i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
529 #   endif
530
531     if ( i_result )
532     {
533         i_thread = CAST_PTHREAD_TO_INT(pthread_self());
534         psz_error = strerror(i_result);
535     }
536
537 #elif defined( HAVE_CTHREADS_H )
538     condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
539     i_result = 0;
540
541 #endif
542
543     if( i_result )
544     {
545         msg_Err( p_condvar->p_this,
546                  "thread %li: cond_wait failed at %s:%d (%d:%s)",
547                  i_thread, psz_file, i_line, i_result, psz_error );
548     }
549
550     return i_result;
551 }
552
553 /*****************************************************************************
554  * vlc_cond_destroy: destroy a condition
555  *****************************************************************************/
556 #define vlc_cond_destroy( P_COND )                                          \
557     __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
558
559 /*****************************************************************************
560  * vlc_threadvar_create: create a thread-local variable
561  *****************************************************************************/
562 #define vlc_threadvar_create( PTHIS, P_TLS )                                 \
563    __vlc_threadvar_create( PTHIS, P_TLS )
564
565 /*****************************************************************************
566  * vlc_threadvar_set: create: set the value of a thread-local variable
567  *****************************************************************************/
568 static inline int vlc_threadvar_set( vlc_threadvar_t * p_tls, void *p_value )
569 {
570     int i_ret;
571
572 #if defined( PTH_INIT_IN_PTH_H )
573     return pth_key_setdata( p_tls->handle, p_value );
574 #elif  defined( ST_INIT_IN_ST_H )
575     return st_thread_setspecific( p_tls->handle, p_value );
576 #elif defined( HAVE_KERNEL_SCHEDULER_H )
577     return -1;
578
579 #elif defined( UNDER_CE ) || defined( WIN32 )
580     i_ret = ( TlsSetValue( p_tls->handle, p_value ) != 0 );
581
582 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
583     i_ret = pthread_setspecific( p_tls->handle, p_value );
584
585 #elif defined( HAVE_CTHREADS_H )
586     i_ret = cthread_setspecific( p_tls->handle, p_value );
587 #endif
588
589     return i_ret;
590 }
591
592 /*****************************************************************************
593  * vlc_threadvar_get: create: get the value of a thread-local variable
594  *****************************************************************************/
595 static inline void* vlc_threadvar_get( vlc_threadvar_t * p_tls )
596 {
597     void* p_ret;
598
599 #if defined( PTH_INIT_IN_PTH_H )
600     p_ret = pth_key_getdata( p_handle->key );
601 #elif defined( ST_INIT_IN_ST_H )
602     p_ret = st_thread_getspecific( p_handle->key );
603 #elif defined( HAVE_KERNEL_SCHEDULER_H )
604     p_ret = NULL;
605 #elif defined( UNDER_CE ) || defined( WIN32 )
606     p_ret = TlsGetValue( p_tls->handle );
607
608 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
609     p_ret = pthread_getspecific( p_tls->handle );
610
611 #elif defined( HAVE_CTHREADS_H )
612     if ( !cthread_getspecific( p_tls->handle, &p_ret ) )
613     {
614         p_ret = NULL;
615     }
616 #endif
617
618     return p_ret;
619 }
620
621 /*****************************************************************************
622  * vlc_thread_create: create a thread
623  *****************************************************************************/
624 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT )         \
625     __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, PRIORITY, WAIT )
626
627 /*****************************************************************************
628  * vlc_thread_set_priority: set the priority of the calling thread
629  *****************************************************************************/
630 #define vlc_thread_set_priority( P_THIS, PRIORITY )                         \
631     __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
632
633 /*****************************************************************************
634  * vlc_thread_ready: tell the parent thread we were successfully spawned
635  *****************************************************************************/
636 #define vlc_thread_ready( P_THIS )                                          \
637     __vlc_thread_ready( VLC_OBJECT(P_THIS) )
638
639 /*****************************************************************************
640  * vlc_thread_join: wait until a thread exits
641  *****************************************************************************/
642 #define vlc_thread_join( P_THIS )                                           \
643     __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )
644
645 #endif