]> git.sesse.net Git - vlc/commitdiff
Add inline API for spinlocks.
authorRémi Denis-Courmont <rem@videolan.org>
Mon, 21 Jan 2008 19:18:38 +0000 (19:18 +0000)
committerRémi Denis-Courmont <rem@videolan.org>
Mon, 21 Jan 2008 19:18:38 +0000 (19:18 +0000)
include/vlc_threads_funcs.h

index 265f11c545e64cd6018f664541d81e7f4c1693dc..5eb2ff3834f34e8df16bf5a3bd9240d522ffc95b 100644 (file)
@@ -802,6 +802,52 @@ static inline void* vlc_threadvar_get( vlc_threadvar_t * p_tls )
     return p_ret;
 }
 
+# if defined (_POSIX_SPIN_LOCKS) && ((_POSIX_SPIN_LOCKS - 0) > 0)
+typedef struct
+{
+    pthread_spinlock_t  spin;
+} vlc_spinlock_t;
+
+/**
+ * Initializes a spinlock.
+ */
+static inline int vlc_spin_init (vlc_spinlock_t *spin)
+{
+    return pthread_spin_init (&spin, PTHREAD_PROCESS_PRIVATE);
+}
+
+/**
+ * Acquires a spinlock.
+ */
+static inline int vlc_spin_lock (vlc_spinlock_t *spin)
+{
+    return pthread_spin_lock (&spin->spin);
+}
+
+/**
+ * Releases a spinlock.
+ */
+static inline int vlc_spin_unlock (vlc_spinlock_t *spin)
+{
+    return pthread_spin_unlock (&spin->spin);
+}
+
+/**
+ * Deinitializes a spinlock.
+ */
+static inline int vlc_spin_destroy (vlc_spinlock_t *spin)
+{
+    return pthread_spin_destroy (&spin->spin);
+}
+#else
+/* Fallback to plain mutexes if spinlocks are not available */
+typedef vlc_mutex_t vlc_spinlock_t;
+# define vlc_spin_init    vlc_mutex_init
+# define vlc_spin_lock    vlc_mutex_lock
+# define vlc_spin_unlock  vlc_mutex_unlock
+# define vlc_spin_destroy vlc_mutex_destroy
+#endif
+
 /*****************************************************************************
  * vlc_thread_create: create a thread
  *****************************************************************************/