typedef pthread_mutex_t vlc_mutex_t;
#define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
typedef pthread_cond_t vlc_cond_t;
+typedef pthread_rwlock_t vlc_rwlock_t;
typedef pthread_key_t vlc_threadvar_t;
typedef struct vlc_timer_t vlc_timer_t;
#define VLC_STATIC_MUTEX { 0, }
typedef HANDLE vlc_cond_t;
+
+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;
+
typedef DWORD vlc_threadvar_t;
typedef struct vlc_timer_t vlc_timer_t;
struct vlc_timer_t
VLC_EXPORT( void, vlc_cond_broadcast, (vlc_cond_t *) );
VLC_EXPORT( void, vlc_cond_wait, (vlc_cond_t *, vlc_mutex_t *) );
VLC_EXPORT( int, vlc_cond_timedwait, (vlc_cond_t *, vlc_mutex_t *, mtime_t) );
+VLC_EXPORT( void, vlc_rwlock_init, (vlc_rwlock_t *) );
+VLC_EXPORT( void, vlc_rwlock_destroy, (vlc_rwlock_t *) );
+VLC_EXPORT( void, vlc_rwlock_rdlock, (vlc_rwlock_t *) );
+VLC_EXPORT( void, vlc_rwlock_wrlock, (vlc_rwlock_t *) );
+VLC_EXPORT( void, vlc_rwlock_unlock, (vlc_rwlock_t *) );
VLC_EXPORT( int, vlc_threadvar_create, (vlc_threadvar_t * , void (*) (void *) ) );
VLC_EXPORT( void, vlc_threadvar_delete, (vlc_threadvar_t *) );
VLC_EXPORT( int, vlc_threadvar_set, (vlc_threadvar_t, void *) );
vlc_rand_bytes
vlc_release
vlc_restorecancel
+vlc_rwlock_destroy
+vlc_rwlock_init
+vlc_rwlock_rdlock
+vlc_rwlock_unlock
+vlc_rwlock_wrlock
vlc_savecancel
vlc_sd_Create
vlc_sd_GetNames
return val;
}
+/**
+ * Initializes a read/write lock.
+ */
+void vlc_rwlock_init (vlc_rwlock_t *lock)
+{
+ if (pthread_rwlock_init (lock, NULL))
+ abort ();
+}
+
+/**
+ * Destroys an initialized unused read/write lock.
+ */
+void vlc_rwlock_destroy (vlc_rwlock_t *lock)
+{
+ int val = pthread_rwlock_destroy (lock);
+ VLC_THREAD_ASSERT ("destroying R/W lock");
+}
+
+/**
+ * Acquires a read/write lock for reading. Recursion is allowed.
+ */
+void vlc_rwlock_rdlock (vlc_rwlock_t *lock)
+{
+ int val = pthread_rwlock_rdlock (lock);
+ VLC_THREAD_ASSERT ("acquiring R/W lock for reading");
+}
+
+/**
+ * Acquires a read/write lock for writing. Recursion is not allowed.
+ */
+void vlc_rwlock_wrlock (vlc_rwlock_t *lock)
+{
+ int val = pthread_rwlock_wrlock (lock);
+ VLC_THREAD_ASSERT ("acquiring R/W lock for writing");
+}
+
+/**
+ * Releases a read/write lock.
+ */
+void vlc_rwlock_unlock (vlc_rwlock_t *lock)
+{
+ int val = pthread_rwlock_unlock (lock);
+ VLC_THREAD_ASSERT ("releasing R/W lock");
+}
+
/**
* Allocates a thread-specific variable.
* @param key where to store the thread-specific variable handle
#include "libvlc.h"
#include <stdarg.h>
#include <assert.h>
+#include <limits.h>
static vlc_threadvar_t cancel_key;
return (result == WAIT_OBJECT_0) ? 0 : ETIMEDOUT;
}
+/*** 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->writers > 0) /* Favor writers to avoid starving */
+ 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->writer = GetCurrentThreadId ();
+ 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 */
+ assert (lock->writers > 0);
+ lock->writers--;
+ }
+
+ 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);
+}
+
/*** Thread-specific variables (TLS) ***/
int vlc_threadvar_create (vlc_threadvar_t *p_tls, void (*destr) (void *))
{