commit 7d981f383b6bacf2e334619d27d6bee967b4b156
parent 84f068238f70b690dfef63feffb4d3b1a03c3853
Author: vaplv <vaplv@free.fr>
Date: Sun, 19 Nov 2017 16:02:46 +0100
Reintroduce the Read/Write mutex API
RW mutex is currently implemented with the pthread API only: there is no
implementation with the Win32 thread API and consequently RW mutex is
not available on CL compiler.
Diffstat:
3 files changed, 92 insertions(+), 0 deletions(-)
diff --git a/src/mutex.h b/src/mutex.h
@@ -26,6 +26,13 @@ RSYS_API void mutex_destroy(struct mutex* mutex);
RSYS_API void mutex_lock(struct mutex* mutex);
RSYS_API void mutex_unlock(struct mutex* mutex);
+struct mutex_rw;
+RSYS_API struct mutex_rw* mutex_rw_create(void);/* NULL <=> error */
+RSYS_API void mutex_rw_destroy(struct mutex_rw* mutex);
+RSYS_API void mutex_rw_rlock(struct mutex_rw* mutex);
+RSYS_API void mutex_rw_wlock(struct mutex_rw* mutex);
+RSYS_API void mutex_rw_unlock(struct mutex_rw* mutex);
+
END_DECLS
#endif /* MUTEX_H */
diff --git a/src/pthread/pthread_mutex.c b/src/pthread/pthread_mutex.c
@@ -24,6 +24,9 @@
#define PTHREAD(Func) ASSERT(pthread_##Func == 0)
#endif
+/*******************************************************************************
+ * Mutex
+ ******************************************************************************/
struct mutex*
mutex_create(void)
{
@@ -55,5 +58,46 @@ mutex_unlock(struct mutex* mutex)
PTHREAD(mutex_unlock((pthread_mutex_t*)mutex));
}
+/*******************************************************************************
+ * Read Write mutex
+ ******************************************************************************/
+struct mutex_rw*
+mutex_rw_create(void)
+{
+ pthread_rwlock_t* mutex = mem_alloc(sizeof(pthread_rwlock_t));
+ if(mutex)
+ PTHREAD(rwlock_init(mutex, NULL));
+ return (struct mutex_rw*)mutex;
+}
+
+void
+mutex_rw_destroy(struct mutex_rw* mutex)
+{
+ ASSERT(mutex);
+ PTHREAD(rwlock_destroy((pthread_rwlock_t*)mutex));
+ mem_rm(mutex);
+}
+
+void
+mutex_rw_rlock(struct mutex_rw* mutex)
+{
+ ASSERT(mutex);
+ PTHREAD(rwlock_rdlock((pthread_rwlock_t*)mutex));
+}
+
+void
+mutex_rw_wlock(struct mutex_rw* mutex)
+{
+ ASSERT(mutex);
+ PTHREAD(rwlock_wrlock((pthread_rwlock_t*)mutex));
+}
+
+void
+mutex_rw_unlock(struct mutex_rw* mutex)
+{
+ ASSERT(mutex);
+ PTHREAD(rwlock_unlock((pthread_rwlock_t*)mutex));
+}
+
#undef PTHREAD
diff --git a/src/win32/win32_mutex.c b/src/win32/win32_mutex.c
@@ -17,6 +17,9 @@
#include "../mutex.h"
#include <Windows.h>
+/*******************************************************************************
+ * Mutex
+ ******************************************************************************/
struct mutex*
mutex_create(void)
{
@@ -48,3 +51,41 @@ mutex_unlock(struct mutex* mutex)
LeaveCriticalSection((LPCRITICAL_SECTION)mutex);
}
+/*******************************************************************************
+ * Read Write mutex
+ ******************************************************************************/
+struct mutex_rw*
+mutex_rw_create(void)
+{
+ FATAL("Missing Read/Write mutex implementation with Win32 thread API.\n");
+ return NULL;
+}
+
+void
+mutex_rw_destroy(struct mutex_rw* mutex)
+{
+ (void)mutex;
+ FATAL("Missing Read/Write mutex implementation with Win32 thread API.\n");
+}
+
+void
+mutex_rw_rlock(struct mutex_rw* mutex)
+{
+ (void)mutex;
+ FATAL("Missing Read/Write mutex implementation with Win32 thread API.\n");
+}
+
+void
+mutex_rw_wlock(struct mutex_rw* mutex)
+{
+ (void)mutex;
+ FATAL("Missing Read/Write mutex implementation with Win32 thread API.\n");
+}
+
+void
+mutex_rw_unlock(struct mutex_rw* mutex)
+{
+ (void)mutex;
+ FATAL("Missing Read/Write mutex implementation with Win32 thread API.\n");
+}
+