]> git.sesse.net Git - vlc/blob - include/vlc_threads.h
Cleanup helper for unlocking
[vlc] / include / vlc_threads.h
1 /*****************************************************************************
2  * vlc_threads.h : threads implementation for the VideoLAN client
3  * This header provides portable declarations for mutexes & conditions
4  *****************************************************************************
5  * Copyright (C) 1999, 2002 the VideoLAN team
6  * Copyright © 2007-2008 Rémi Denis-Courmont
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 #ifndef VLC_THREADS_H_
29 #define VLC_THREADS_H_
30
31 /**
32  * \file
33  * This file defines structures and functions for handling threads in vlc
34  *
35  */
36
37 #if defined( UNDER_CE )
38                                                                 /* WinCE API */
39 #elif defined( WIN32 )
40 #   include <process.h>                                         /* Win32 API */
41 #   include <errno.h>
42
43 #elif defined( SYS_BEOS )                                            /* BeOS */
44 #   include <kernel/OS.h>
45 #   include <kernel/scheduler.h>
46 #   include <byteorder.h>
47
48 #else                                         /* pthreads (like Linux & BSD) */
49 #   define LIBVLC_USE_PTHREAD 1
50 #   define _APPLE_C_SOURCE    1 /* Proper pthread semantics on OSX */
51
52 #   include <stdlib.h> /* lldiv_t definition (only in C99) */
53 #   include <unistd.h> /* _POSIX_SPIN_LOCKS */
54 #   include <pthread.h>
55     /* Needed for pthread_cond_timedwait */
56 #   include <errno.h>
57 #   include <time.h>
58
59 #endif
60
61 /*****************************************************************************
62  * Constants
63  *****************************************************************************/
64
65 /* Thread priorities */
66 #ifdef __APPLE__
67 #   define VLC_THREAD_PRIORITY_LOW      0
68 #   define VLC_THREAD_PRIORITY_INPUT   22
69 #   define VLC_THREAD_PRIORITY_AUDIO   22
70 #   define VLC_THREAD_PRIORITY_VIDEO    0
71 #   define VLC_THREAD_PRIORITY_OUTPUT  22
72 #   define VLC_THREAD_PRIORITY_HIGHEST 22
73
74 #elif defined(SYS_BEOS)
75 #   define VLC_THREAD_PRIORITY_LOW 5
76 #   define VLC_THREAD_PRIORITY_INPUT 10
77 #   define VLC_THREAD_PRIORITY_AUDIO 10
78 #   define VLC_THREAD_PRIORITY_VIDEO 5
79 #   define VLC_THREAD_PRIORITY_OUTPUT 15
80 #   define VLC_THREAD_PRIORITY_HIGHEST 15
81
82 #elif defined(LIBVLC_USE_PTHREAD)
83 #   define VLC_THREAD_PRIORITY_LOW      0
84 #   define VLC_THREAD_PRIORITY_INPUT   10
85 #   define VLC_THREAD_PRIORITY_AUDIO    5
86 #   define VLC_THREAD_PRIORITY_VIDEO    0
87 #   define VLC_THREAD_PRIORITY_OUTPUT  15
88 #   define VLC_THREAD_PRIORITY_HIGHEST 20
89
90 #elif defined(WIN32) || defined(UNDER_CE)
91 /* Define different priorities for WinNT/2K/XP and Win9x/Me */
92 #   define VLC_THREAD_PRIORITY_LOW 0
93 #   define VLC_THREAD_PRIORITY_INPUT \
94         (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
95 #   define VLC_THREAD_PRIORITY_AUDIO \
96         (IS_WINNT ? THREAD_PRIORITY_HIGHEST : 0)
97 #   define VLC_THREAD_PRIORITY_VIDEO \
98         (IS_WINNT ? 0 : THREAD_PRIORITY_BELOW_NORMAL )
99 #   define VLC_THREAD_PRIORITY_OUTPUT \
100         (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
101 #   define VLC_THREAD_PRIORITY_HIGHEST \
102         (IS_WINNT ? THREAD_PRIORITY_TIME_CRITICAL : 0)
103
104 #else
105 #   define VLC_THREAD_PRIORITY_LOW 0
106 #   define VLC_THREAD_PRIORITY_INPUT 0
107 #   define VLC_THREAD_PRIORITY_AUDIO 0
108 #   define VLC_THREAD_PRIORITY_VIDEO 0
109 #   define VLC_THREAD_PRIORITY_OUTPUT 0
110 #   define VLC_THREAD_PRIORITY_HIGHEST 0
111
112 #endif
113
114 /*****************************************************************************
115  * Type definitions
116  *****************************************************************************/
117
118 #if defined (LIBVLC_USE_PTHREAD)
119 typedef pthread_t       vlc_thread_t;
120 typedef pthread_mutex_t vlc_mutex_t;
121 typedef pthread_cond_t  vlc_cond_t;
122 typedef pthread_key_t   vlc_threadvar_t;
123
124 #elif defined( WIN32 ) || defined( UNDER_CE )
125 typedef struct
126 {
127     HANDLE handle;
128     void  *(*entry) (void *);
129     void  *data;
130 } *vlc_thread_t;
131
132 typedef HANDLE  vlc_mutex_t;
133 typedef HANDLE  vlc_cond_t;
134 typedef DWORD   vlc_threadvar_t;
135
136 #elif defined( SYS_BEOS )
137 /* This is the BeOS implementation of the vlc threads, note that the mutex is
138  * not a real mutex and the cond_var is not like a pthread cond_var but it is
139  * enough for what we need */
140
141 typedef thread_id vlc_thread_t;
142
143 typedef struct
144 {
145     int32_t         init;
146     sem_id          lock;
147 } vlc_mutex_t;
148
149 typedef struct
150 {
151     int32_t         init;
152     thread_id       thread;
153 } vlc_cond_t;
154
155 typedef struct
156 {
157 } vlc_threadvar_t;
158
159 #endif
160
161 #if defined( WIN32 ) && !defined ETIMEDOUT
162 #  define ETIMEDOUT 10060 /* This is the value in winsock.h. */
163 #endif
164
165 /*****************************************************************************
166  * Function definitions
167  *****************************************************************************/
168 VLC_EXPORT( int,  vlc_mutex_init,    ( vlc_mutex_t * ) );
169 VLC_EXPORT( int,  vlc_mutex_init_recursive, ( vlc_mutex_t * ) );
170 VLC_EXPORT( void,  __vlc_mutex_destroy, ( const char *, int, vlc_mutex_t * ) );
171 VLC_EXPORT( int,  __vlc_cond_init,     ( vlc_cond_t * ) );
172 VLC_EXPORT( void,  __vlc_cond_destroy,  ( const char *, int, vlc_cond_t * ) );
173 VLC_EXPORT( int, vlc_threadvar_create, (vlc_threadvar_t * , void (*) (void *) ) );
174 VLC_EXPORT( void, vlc_threadvar_delete, (vlc_threadvar_t *) );
175 VLC_EXPORT( int,  __vlc_thread_create, ( vlc_object_t *, const char *, int, const char *, void * ( * ) ( vlc_object_t * ), int, bool ) );
176 VLC_EXPORT( int,  __vlc_thread_set_priority, ( vlc_object_t *, const char *, int, int ) );
177 VLC_EXPORT( void, __vlc_thread_join,   ( vlc_object_t *, const char *, int ) );
178
179 VLC_EXPORT( int, vlc_clone, (vlc_thread_t *, void * (*) (void *), void *, int) );
180 VLC_EXPORT( void, vlc_cancel, (vlc_thread_t) );
181 VLC_EXPORT( int, vlc_join, (vlc_thread_t, void **) );
182 VLC_EXPORT (void, vlc_control_cancel, (int cmd, ...));
183
184 #ifndef LIBVLC_USE_PTHREAD
185 enum {
186     VLC_SAVE_CANCEL,
187     VLC_RESTORE_CANCEL,
188     VLC_TEST_CANCEL,
189     VLC_DO_CANCEL,
190     VLC_CLEANUP_PUSH,
191     VLC_CLEANUP_POP,
192 };
193 #endif
194
195 #define vlc_thread_ready vlc_object_signal
196
197 /*****************************************************************************
198  * vlc_mutex_lock: lock a mutex
199  *****************************************************************************/
200 #define vlc_mutex_lock( P_MUTEX )                                           \
201     __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
202
203 VLC_EXPORT(void, vlc_pthread_fatal, (const char *action, int error, const char *file, unsigned line));
204
205 #if defined(LIBVLC_USE_PTHREAD)
206 # define VLC_THREAD_ASSERT( action ) \
207     if (val) \
208         vlc_pthread_fatal (action, val, psz_file, i_line)
209 #else
210 # define VLC_THREAD_ASSERT ((void)(val))
211 #endif
212
213 static inline void __vlc_mutex_lock( const char * psz_file, int i_line,
214                                     vlc_mutex_t * p_mutex )
215 {
216 #if defined(LIBVLC_USE_PTHREAD)
217 #   define vlc_assert_locked( m ) \
218            assert (pthread_mutex_lock (m) == EDEADLK)
219     int val = pthread_mutex_lock( p_mutex );
220     VLC_THREAD_ASSERT ("locking mutex");
221
222 #elif defined( UNDER_CE )
223     (void)psz_file; (void)i_line;
224
225     EnterCriticalSection( &p_mutex->csection );
226
227 #elif defined( WIN32 )
228     (void)psz_file; (void)i_line;
229
230     WaitForSingleObject( *p_mutex, INFINITE );
231
232 #elif defined( SYS_BEOS )
233     acquire_sem( p_mutex->lock );
234
235 #endif
236 }
237
238 #ifndef vlc_assert_locked
239 # define vlc_assert_locked( m ) (void)m
240 #endif
241
242 /*****************************************************************************
243  * vlc_mutex_unlock: unlock a mutex
244  *****************************************************************************/
245 #define vlc_mutex_unlock( P_MUTEX )                                         \
246     __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
247
248 static inline void __vlc_mutex_unlock( const char * psz_file, int i_line,
249                                       vlc_mutex_t *p_mutex )
250 {
251 #if defined(LIBVLC_USE_PTHREAD)
252     int val = pthread_mutex_unlock( p_mutex );
253     VLC_THREAD_ASSERT ("unlocking mutex");
254
255 #elif defined( UNDER_CE )
256     (void)psz_file; (void)i_line;
257
258     LeaveCriticalSection( &p_mutex->csection );
259
260 #elif defined( WIN32 )
261     (void)psz_file; (void)i_line;
262
263     ReleaseMutex( *p_mutex );
264
265 #elif defined( SYS_BEOS )
266     release_sem( p_mutex->lock );
267
268 #endif
269 }
270
271 /*****************************************************************************
272  * vlc_mutex_destroy: destroy a mutex
273  *****************************************************************************/
274 #define vlc_mutex_destroy( P_MUTEX )                                        \
275     __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
276
277 /**
278  * Save the cancellation state and disable cancellation for the calling thread.
279  * This function must be called before entering a piece of code that is not
280  * cancellation-safe.
281  * @return Previous cancellation state (opaque value).
282  */
283 static inline int vlc_savecancel (void)
284 {
285     int state;
286 #if defined (LIBVLC_USE_PTHREAD)
287     (void) pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &state);
288 #else
289     vlc_control_cancel (VLC_SAVE_CANCEL, &state);
290 #endif
291     return state;
292 }
293
294 /**
295  * Restore the cancellation state for the calling thread.
296  * @param state previous state as returned by vlc_savecancel().
297  * @return Nothing, always succeeds.
298  */
299 static inline void vlc_restorecancel (int state)
300 {
301 #if defined (LIBVLC_USE_PTHREAD)
302     (void) pthread_setcancelstate (state, NULL);
303 #else
304     vlc_control_cancel (VLC_RESTORE_CANCEL, state);
305 #endif
306 }
307
308 /**
309  * Issues an explicit deferred cancellation point.
310  * This has no effect if thread cancellation is disabled.
311  * This can be called when there is a rather slow non-sleeping operation.
312  */
313 static inline void vlc_testcancel (void)
314 {
315 #if defined (LIBVLC_USE_PTHREAD)
316     pthread_testcancel ();
317 #else
318     vlc_control_cancel (VLC_TEST_CANCEL);
319 #endif
320 }
321
322 #if defined (LIBVLC_USE_PTHREAD)
323 /**
324  * Registers a new procedure to run if the thread is cancelled (or otherwise
325  * exits prematurely). Any call to vlc_cleanup_push() <b>must</b> paired with a
326  * call to either vlc_cleanup_pop() or vlc_cleanup_run(). Branching into or out
327  * of the block between these two function calls is not allowed (read: it will
328  * likely crash the whole process). If multiple procedures are registered,
329  * they are handled in last-in first-out order.
330  *
331  * @param routine procedure to call if the thread ends
332  * @param arg argument for the procedure
333  */
334 # define vlc_cleanup_push( routine, arg ) pthread_cleanup_push (routine, arg)
335
336 /**
337  * Removes a cleanup procedure that was previously registered with
338  * vlc_cleanup_push().
339  */
340 # define vlc_cleanup_pop( ) pthread_cleanup_pop (0)
341
342 /**
343  * Removes a cleanup procedure that was previously registered with
344  * vlc_cleanup_push(), and executes it.
345  */
346 # define vlc_cleanup_run( ) pthread_cleanup_pop (1)
347 #else
348 typedef struct vlc_cleanup_t vlc_cleanup_t;
349
350 struct vlc_cleanup_t
351 {
352     vlc_cleanup_t *next;
353     void         (*proc) (void *);
354     void          *data;
355 };
356
357 /* This macros opens a code block on purpose. This is needed for multiple
358  * calls within a single function. This also prevent Win32 developpers from
359  * writing code that would break on POSIX (POSIX opens a block as well). */
360 # define vlc_cleanup_push( routine, arg ) \
361     do { \
362         vlc_cleanup_t vlc_cleanup_data = { NULL, routine, arg, }; \
363         vlc_control_cancel (VLC_CLEANUP_PUSH, &vlc_cleanup_data)
364
365 # define vlc_cleanup_pop( ) \
366         vlc_control_cancel (VLC_CLEANUP_POP); \
367     } while (0)
368
369 # define vlc_cleanup_run( ) \
370         vlc_control_cancel (VLC_CLEANUP_POP); \
371         vlc_cleanup_data.proc (vlc_cleanup_data.data); \
372     } while (0)
373
374 #endif /* LIBVLC_USE_PTHREAD */
375
376 static inline void vlc_cleanup_lock (void *lock)
377 {
378     vlc_mutex_unlock ((vlc_mutex_t *)lock);
379 }
380 #define mutex_cleanup_push( lock ) vlc_cleanup_push (vlc_cleanup_lock, lock)
381
382 /*****************************************************************************
383  * vlc_cond_init: initialize a condition
384  *****************************************************************************/
385 #define vlc_cond_init( P_THIS, P_COND )                                     \
386     __vlc_cond_init( P_COND )
387
388 /*****************************************************************************
389  * vlc_cond_signal: start a thread on condition completion
390  *****************************************************************************/
391 #define vlc_cond_signal( P_COND )                                           \
392     __vlc_cond_signal( __FILE__, __LINE__, P_COND )
393
394 static inline void __vlc_cond_signal( const char * psz_file, int i_line,
395                                       vlc_cond_t *p_condvar )
396 {
397 #if defined(LIBVLC_USE_PTHREAD)
398     int val = pthread_cond_signal( p_condvar );
399     VLC_THREAD_ASSERT ("signaling condition variable");
400
401 #elif defined( UNDER_CE ) || defined( WIN32 )
402     (void)psz_file; (void)i_line;
403
404     /* Release one waiting thread if one is available. */
405     /* For this trick to work properly, the vlc_cond_signal must be surrounded
406      * by a mutex. This will prevent another thread from stealing the signal */
407     /* PulseEvent() only works if none of the waiting threads is suspended.
408      * This is particularily problematic under a debug session.
409      * as documented in http://support.microsoft.com/kb/q173260/ */
410     PulseEvent( *p_condvar );
411
412 #elif defined( SYS_BEOS )
413     while( p_condvar->thread != -1 )
414     {
415         thread_info info;
416         if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
417             return;
418
419         if( info.state != B_THREAD_SUSPENDED )
420         {
421             /* The  waiting thread is not suspended so it could
422              * have been interrupted beetwen the unlock and the
423              * suspend_thread line. That is why we sleep a little
424              * before retesting p_condver->thread. */
425             snooze( 10000 );
426         }
427         else
428         {
429             /* Ok, we have to wake up that thread */
430             resume_thread( p_condvar->thread );
431         }
432     }
433
434 #endif
435 }
436
437 /*****************************************************************************
438  * vlc_cond_wait: wait until condition completion
439  *****************************************************************************/
440 #define vlc_cond_wait( P_COND, P_MUTEX )                                     \
441     __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX  )
442
443 static inline void __vlc_cond_wait( const char * psz_file, int i_line,
444                                     vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
445 {
446 #if defined(LIBVLC_USE_PTHREAD)
447     int val = pthread_cond_wait( p_condvar, p_mutex );
448     VLC_THREAD_ASSERT ("waiting on condition");
449
450 #elif defined( UNDER_CE )
451     LeaveCriticalSection( &p_mutex->csection );
452     WaitForSingleObject( *p_condvar, INFINITE );
453
454     /* Reacquire the mutex before returning. */
455     vlc_mutex_lock( p_mutex );
456
457 #elif defined( WIN32 )
458     DWORD result;
459
460     do
461     {
462         vlc_testcancel ();
463         result = SignalObjectAndWait (*p_mutex, *p_condvar, INFINITE, TRUE);
464
465         /* Reacquire the mutex before returning. */
466         vlc_mutex_lock( p_mutex );
467     }
468     while (result == WAIT_IO_COMPLETION);
469
470     vlc_testcancel ();
471
472     (void)psz_file; (void)i_line;
473
474 #elif defined( SYS_BEOS )
475     /* The p_condvar->thread var is initialized before the unlock because
476      * it enables to identify when the thread is interrupted beetwen the
477      * unlock line and the suspend_thread line */
478     p_condvar->thread = find_thread( NULL );
479     vlc_mutex_unlock( p_mutex );
480     suspend_thread( p_condvar->thread );
481     p_condvar->thread = -1;
482
483     vlc_mutex_lock( p_mutex );
484
485 #endif
486 }
487
488
489 /*****************************************************************************
490  * vlc_cond_timedwait: wait until condition completion or expiration
491  *****************************************************************************
492  * Returns 0 if object signaled, an error code in case of timeout or error.
493  *****************************************************************************/
494 #define vlc_cond_timedwait( P_COND, P_MUTEX, DEADLINE )                      \
495     __vlc_cond_timedwait( __FILE__, __LINE__, P_COND, P_MUTEX, DEADLINE  )
496
497 static inline int __vlc_cond_timedwait( const char * psz_file, int i_line,
498                                         vlc_cond_t *p_condvar,
499                                         vlc_mutex_t *p_mutex,
500                                         mtime_t deadline )
501 {
502 #if defined(LIBVLC_USE_PTHREAD)
503     lldiv_t d = lldiv( deadline, 1000000 );
504     struct timespec ts = { d.quot, d.rem * 1000 };
505
506     int val = pthread_cond_timedwait (p_condvar, p_mutex, &ts);
507     if (val != ETIMEDOUT)
508         VLC_THREAD_ASSERT ("timed-waiting on condition");
509     return val;
510
511 #elif defined( UNDER_CE )
512     mtime_t delay_ms = (deadline - mdate())/1000;
513     DWORD result;
514     if( delay_ms < 0 )
515         delay_ms = 0;
516
517     LeaveCriticalSection( &p_mutex->csection );
518     result = WaitForSingleObject( *p_condvar, delay_ms );
519
520     /* Reacquire the mutex before returning. */
521     vlc_mutex_lock( p_mutex );
522
523     (void)psz_file; (void)i_line;
524
525     return (result == WAIT_TIMEOUT) ? ETIMEDOUT : 0;
526
527 #elif defined( WIN32 )
528     mtime_t total;
529     DWORD result = WAIT_TIMEOUT;
530
531     (void)psz_file; (void)i_line;
532
533     vlc_testcancel ();
534     while ((total = (deadline - mdate ()) > 0))
535     {
536         DWORD delay = (total > 0x7fffffff) ? 0x7fffffff : total;
537         result = SignalObjectAndWait( *p_mutex, *p_condvar,
538                                       delay, TRUE );
539
540         /* Reacquire the mutex before return/cancel. */
541         vlc_mutex_lock (p_mutex);
542         if (result == WAIT_OBJECT_0)
543             return 0; /* Condition signaled! */
544         vlc_testcancel ();
545     }
546     return ETIMEDOUT;
547
548 #elif defined( SYS_BEOS )
549 #   error Unimplemented
550
551 #endif
552 }
553
554 /*****************************************************************************
555  * vlc_cond_destroy: destroy a condition
556  *****************************************************************************/
557 #define vlc_cond_destroy( P_COND )                                          \
558     __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
559
560 /*****************************************************************************
561  * vlc_threadvar_set: create: set the value of a thread-local variable
562  *****************************************************************************/
563 static inline int vlc_threadvar_set( vlc_threadvar_t * p_tls, void *p_value )
564 {
565     int i_ret;
566
567 #if defined(LIBVLC_USE_PTHREAD)
568     i_ret = pthread_setspecific( *p_tls, p_value );
569
570 #elif defined( SYS_BEOS )
571     i_ret = EINVAL;
572
573 #elif defined( UNDER_CE ) || defined( WIN32 )
574     i_ret = TlsSetValue( *p_tls, p_value ) ? EINVAL : 0;
575
576 #endif
577
578     return i_ret;
579 }
580
581 /*****************************************************************************
582  * vlc_threadvar_get: create: get the value of a thread-local variable
583  *****************************************************************************/
584 static inline void* vlc_threadvar_get( vlc_threadvar_t * p_tls )
585 {
586     void *p_ret;
587
588 #if defined(LIBVLC_USE_PTHREAD)
589     p_ret = pthread_getspecific( *p_tls );
590
591 #elif defined( SYS_BEOS )
592     p_ret = NULL;
593
594 #elif defined( UNDER_CE ) || defined( WIN32 )
595     p_ret = TlsGetValue( *p_tls );
596
597 #endif
598
599     return p_ret;
600 }
601
602 # if defined (_POSIX_SPIN_LOCKS) && ((_POSIX_SPIN_LOCKS - 0) > 0)
603 typedef pthread_spinlock_t vlc_spinlock_t;
604
605 /**
606  * Initializes a spinlock.
607  */
608 static inline int vlc_spin_init (vlc_spinlock_t *spin)
609 {
610     return pthread_spin_init (spin, PTHREAD_PROCESS_PRIVATE);
611 }
612
613 /**
614  * Acquires a spinlock.
615  */
616 static inline void vlc_spin_lock (vlc_spinlock_t *spin)
617 {
618     pthread_spin_lock (spin);
619 }
620
621 /**
622  * Releases a spinlock.
623  */
624 static inline void vlc_spin_unlock (vlc_spinlock_t *spin)
625 {
626     pthread_spin_unlock (spin);
627 }
628
629 /**
630  * Deinitializes a spinlock.
631  */
632 static inline void vlc_spin_destroy (vlc_spinlock_t *spin)
633 {
634     pthread_spin_destroy (spin);
635 }
636
637 #elif defined( WIN32 )
638
639 typedef CRITICAL_SECTION vlc_spinlock_t;
640
641 /**
642  * Initializes a spinlock.
643  */
644 static inline int vlc_spin_init (vlc_spinlock_t *spin)
645 {
646     return !InitializeCriticalSectionAndSpinCount(spin, 4000);
647 }
648
649 /**
650  * Acquires a spinlock.
651  */
652 static inline void vlc_spin_lock (vlc_spinlock_t *spin)
653 {
654     EnterCriticalSection(spin);
655 }
656
657 /**
658  * Releases a spinlock.
659  */
660 static inline void vlc_spin_unlock (vlc_spinlock_t *spin)
661 {
662     LeaveCriticalSection(spin);
663 }
664
665 /**
666  * Deinitializes a spinlock.
667  */
668 static inline void vlc_spin_destroy (vlc_spinlock_t *spin)
669 {
670     DeleteCriticalSection(spin);
671 }
672
673 #else
674
675 /* Fallback to plain mutexes if spinlocks are not available */
676 typedef vlc_mutex_t vlc_spinlock_t;
677
678 static inline int vlc_spin_init (vlc_spinlock_t *spin)
679 {
680     return vlc_mutex_init (spin);
681 }
682
683 # define vlc_spin_lock    vlc_mutex_lock
684 # define vlc_spin_unlock  vlc_mutex_unlock
685 # define vlc_spin_destroy vlc_mutex_destroy
686 #endif
687
688 /**
689  * Issues a full memory barrier.
690  */
691 #if defined (__APPLE__)
692 # include <libkern/OSAtomic.h> /* OSMemoryBarrier() */
693 #endif
694 static inline void barrier (void)
695 {
696 #if defined (__GNUC__) && (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
697     __sync_synchronize ();
698 #elif defined(__APPLE__)
699     OSMemoryBarrier ();
700 #elif defined(__powerpc__)
701     asm volatile ("sync":::"memory");
702 #elif defined(__i386__)
703     asm volatile ("mfence":::"memory");
704 #else
705     vlc_spinlock_t spin;
706     vlc_spin_init (&spin);
707     vlc_spin_lock (&spin);
708     vlc_spin_unlock (&spin);
709     vlc_spin_destroy (&spin);
710 #endif
711 }
712
713 /*****************************************************************************
714  * vlc_thread_create: create a thread
715  *****************************************************************************/
716 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT )         \
717     __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, FUNC, PRIORITY, WAIT )
718
719 /*****************************************************************************
720  * vlc_thread_set_priority: set the priority of the calling thread
721  *****************************************************************************/
722 #define vlc_thread_set_priority( P_THIS, PRIORITY )                         \
723     __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
724
725 /*****************************************************************************
726  * vlc_thread_join: wait until a thread exits
727  *****************************************************************************/
728 #define vlc_thread_join( P_THIS )                                           \
729     __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )
730
731 #endif /* !_VLC_THREADS_H */