]> git.sesse.net Git - vlc/blobdiff - include/vlc_threads.h
Move stuff around
[vlc] / include / vlc_threads.h
index 2eac7f91dfa4d6781a91173175db5d2ab49ff155..63e61d126ee759100cba717cc761d4c8ea845db6 100644 (file)
@@ -3,7 +3,7 @@
  * This header provides portable declarations for mutexes & conditions
  *****************************************************************************
  * Copyright (C) 1999, 2002 the VideoLAN team
- * $Id$
+ * Copyright © 2007-2008 Rémi Denis-Courmont
  *
  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
  *          Samuel Hocevar <sam@via.ecp.fr>
@@ -122,7 +122,13 @@ typedef pthread_cond_t  vlc_cond_t;
 typedef pthread_key_t   vlc_threadvar_t;
 
 #elif defined( WIN32 ) || defined( UNDER_CE )
-typedef HANDLE  vlc_thread_t;
+typedef struct
+{
+    HANDLE handle;
+    void  *(*entry) (void *);
+    void  *data;
+} *vlc_thread_t;
+
 typedef HANDLE  vlc_mutex_t;
 typedef HANDLE  vlc_cond_t;
 typedef DWORD   vlc_threadvar_t;
@@ -170,6 +176,19 @@ VLC_EXPORT( int,  __vlc_thread_create, ( vlc_object_t *, const char *, int, cons
 VLC_EXPORT( int,  __vlc_thread_set_priority, ( vlc_object_t *, const char *, int, int ) );
 VLC_EXPORT( void, __vlc_thread_join,   ( vlc_object_t *, const char *, int ) );
 
+VLC_EXPORT( int, vlc_clone, (vlc_thread_t *, void * (*) (void *), void *, int) );
+VLC_EXPORT( void, vlc_cancel, (vlc_thread_t) );
+VLC_EXPORT( int, vlc_join, (vlc_thread_t, void **) );
+VLC_EXPORT (void, vlc_control_cancel, (int cmd, ...));
+
+#ifndef LIBVLC_USE_PTHREAD
+enum {
+    VLC_SAVE_CANCEL,
+    VLC_RESTORE_CANCEL,
+    VLC_TEST_CANCEL,
+};
+#endif
+
 #define vlc_thread_ready vlc_object_signal
 
 /*****************************************************************************
@@ -252,6 +271,51 @@ static inline void __vlc_mutex_unlock( const char * psz_file, int i_line,
 #define vlc_mutex_destroy( P_MUTEX )                                        \
     __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
 
+/**
+ * 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_cond_init: initialize a condition
  *****************************************************************************/