[vlc-commits] Implement rw_locks, needed for Android 2.2 and below

Rémi Duraffort git at videolan.org
Sat Jan 28 00:01:45 CET 2012


vlc/vlc-2.0 | branch: master | Rémi Duraffort <ivoire at videolan.org> | Sun Nov  7 19:11:51 2010 +0100| [c8e7064d3f9583d2e4173ebdce00f3297beb1d8a] | committer: Jean-Baptiste Kempf

Implement rw_locks, needed for Android 2.2 and below

Signed-off-by: Jean-Baptiste Kempf <jb at videolan.org>

> http://git.videolan.org/gitweb.cgi/vlc/vlc-2.0.git/?a=commit;h=c8e7064d3f9583d2e4173ebdce00f3297beb1d8a
---

 include/vlc_threads.h |   22 ++++++++++++++
 src/posix/thread.c    |   76 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 98 insertions(+), 0 deletions(-)

diff --git a/include/vlc_threads.h b/include/vlc_threads.h
index 7a6c6af..181f273 100644
--- a/include/vlc_threads.h
+++ b/include/vlc_threads.h
@@ -123,8 +123,30 @@ typedef pthread_mutex_t vlc_mutex_t;
 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
 typedef pthread_cond_t  vlc_cond_t;
 #define VLC_STATIC_COND  PTHREAD_COND_INITIALIZER
+#ifndef __ANDROID__
 typedef pthread_rwlock_t vlc_rwlock_t;
 #define VLC_STATIC_RWLOCK PTHREAD_RWLOCK_INITIALIZER
+#else
+typedef unsigned long   DWORD;
+
+typedef struct
+{
+    vlc_mutex_t   mutex;
+    vlc_cond_t    read_wait;
+    vlc_cond_t    write_wait;
+    unsigned long readers;
+    unsigned long writers;
+    DWORD         writer;
+} vlc_rwlock_t;
+
+#define VLC_STATIC_RWLOCK {         \
+    .mutex      = VLC_STATIC_MUTEX, \
+    .read_wait  = VLC_STATIC_COND,  \
+    .readers    = 0,                \
+    .writers    = 0,                \
+    .writer     = 0,                \
+    }
+#endif
 typedef pthread_key_t   vlc_threadvar_t;
 typedef struct vlc_timer *vlc_timer_t;
 
diff --git a/src/posix/thread.c b/src/posix/thread.c
index b8211e5..4879729 100644
--- a/src/posix/thread.c
+++ b/src/posix/thread.c
@@ -581,6 +581,7 @@ void vlc_sem_wait (vlc_sem_t *sem)
     VLC_THREAD_ASSERT ("locking semaphore");
 }
 
+#ifndef __ANDROID__
 /**
  * Initializes a read/write lock.
  */
@@ -625,6 +626,81 @@ void vlc_rwlock_unlock (vlc_rwlock_t *lock)
     int val = pthread_rwlock_unlock (lock);
     VLC_THREAD_ASSERT ("releasing R/W lock");
 }
+#endif
+/*** Read/write locks */
+/* SRW (Slim Read Write) locks are available in Vista+ only */
+void vlc_rwlock_init (vlc_rwlock_t *lock)
+{
+    vlc_mutex_init (&lock->mutex);
+    vlc_cond_init (&lock->read_wait);
+    vlc_cond_init (&lock->write_wait);
+    lock->readers = 0; /* active readers */
+    lock->writers = 0; /* waiting or active writers */
+    lock->writer = 0; /* ID of active writer */
+}
+
+/**
+ * Destroys an initialized unused read/write lock.
+ */
+void vlc_rwlock_destroy (vlc_rwlock_t *lock)
+{
+    vlc_cond_destroy (&lock->read_wait);
+    vlc_cond_destroy (&lock->write_wait);
+    vlc_mutex_destroy (&lock->mutex);
+}
+
+/**
+ * Acquires a read/write lock for reading. Recursion is allowed.
+ */
+void vlc_rwlock_rdlock (vlc_rwlock_t *lock)
+{
+    vlc_mutex_lock (&lock->mutex);
+    while (lock->writer != 0)
+        vlc_cond_wait (&lock->read_wait, &lock->mutex);
+    if (lock->readers == ULONG_MAX)
+        abort ();
+    lock->readers++;
+    vlc_mutex_unlock (&lock->mutex);
+}
+
+/**
+ * Acquires a read/write lock for writing. Recursion is not allowed.
+ */
+void vlc_rwlock_wrlock (vlc_rwlock_t *lock)
+{
+    vlc_mutex_lock (&lock->mutex);
+    if (lock->writers == ULONG_MAX)
+        abort ();
+    lock->writers++;
+    while ((lock->readers > 0) || (lock->writer != 0))
+        vlc_cond_wait (&lock->write_wait, &lock->mutex);
+    lock->writers--;
+    lock->writer = 42;
+    vlc_mutex_unlock (&lock->mutex);
+}
+
+/**
+ * Releases a read/write lock.
+ */
+void vlc_rwlock_unlock (vlc_rwlock_t *lock)
+{
+    vlc_mutex_lock (&lock->mutex);
+    if (lock->readers > 0)
+        lock->readers--; /* Read unlock */
+    else
+        lock->writer = 0; /* Write unlock */
+
+    if (lock->writers > 0)
+    {
+        if (lock->readers == 0)
+            vlc_cond_signal (&lock->write_wait);
+    }
+    else
+        vlc_cond_broadcast (&lock->read_wait);
+    vlc_mutex_unlock (&lock->mutex);
+}
+
+
 
 /**
  * Allocates a thread-specific variable.



More information about the vlc-commits mailing list