+/**
+ * Issues a full memory barrier.
+ */
+#if defined (__APPLE__)
+# include <libkern/OSAtomic.h> /* OSMemoryBarrier() */
+#endif
+static inline void barrier (void)
+{
+#if defined (__GNUC__) && (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
+ __sync_synchronize ();
+#elif defined(__APPLE__)
+ OSMemoryBarrier ();
+#elif defined(__powerpc__)
+ asm volatile ("sync":::"memory");
+#elif defined(__i386__)
+ asm volatile ("mfence":::"memory");
+#else
+ vlc_spinlock_t spin;
+ vlc_spin_init (&spin);
+ vlc_spin_lock (&spin);
+ vlc_spin_unlock (&spin);
+ vlc_spin_destroy (&spin);
+#endif
+}
+
+#ifndef LIBVLC_USE_PTHREAD
+enum {
+ VLC_SAVE_CANCEL,
+ VLC_RESTORE_CANCEL,
+ VLC_TEST_CANCEL,
+};
+#endif
+
+VLC_EXPORT (void, vlc_control_cancel, (int cmd, ...));
+
+/**
+ * Save the cancellation state and disable cancellation for the calling thread.
+ * This function must be called before entering a piece of code that is not
+ * cancellation-safe.
+ * @return Previous cancellation state (opaque value).
+ */
+static inline int vlc_savecancel (void)
+{
+ int state;
+#if defined (LIBVLC_USE_PTHREAD)
+ (void) pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &state);
+#else
+ vlc_control_cancel (VLC_SAVE_CANCEL, &state);
+#endif
+ return state;
+}
+
+/**
+ * Restore the cancellation state for the calling thread.
+ * @param state previous state as returned by vlc_savecancel().
+ * @return Nothing, always succeeds.
+ */
+static inline void vlc_restorecancel (int state)
+{
+#if defined (LIBVLC_USE_PTHREAD)
+ (void) pthread_setcancelstate (state, NULL);
+#else
+ vlc_control_cancel (VLC_RESTORE_CANCEL, state);
+#endif
+}
+
+/**
+ * Issues an explicit deferred cancellation point.
+ * This has no effect if thread cancellation is disabled.
+ * This can be called when there is a rather slow non-sleeping operation.
+ */
+static inline void vlc_testcancel (void)
+{
+#if defined (LIBVLC_USE_PTHREAD)
+ pthread_testcancel ();
+#else
+ vlc_control_cancel (VLC_TEST_CANCEL);
+#endif
+}
+
+/*****************************************************************************
+ * vlc_thread_create: create a thread
+ *****************************************************************************/
+#define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT ) \
+ __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, FUNC, PRIORITY, WAIT )
+
+/*****************************************************************************
+ * vlc_thread_set_priority: set the priority of the calling thread
+ *****************************************************************************/
+#define vlc_thread_set_priority( P_THIS, PRIORITY ) \
+ __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
+
+/*****************************************************************************
+ * vlc_thread_join: wait until a thread exits
+ *****************************************************************************/
+#define vlc_thread_join( P_THIS ) \
+ __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )
+
+#endif /* !_VLC_THREADS_H */