aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2013-01-10 09:16:22 +0100
committerMax Kellermann <max@duempel.org>2013-01-10 10:03:18 +0100
commit483ba5ea1ccebd4741fbcc5d92648ac6854c60fe (patch)
treef797f807c2f0b4ea0e48b82fdd92c51d980f509b
parent18076ac9b766485efc45931e62a164e5cc1a8542 (diff)
downloadmpd-483ba5ea1ccebd4741fbcc5d92648ac6854c60fe.tar.gz
mpd-483ba5ea1ccebd4741fbcc5d92648ac6854c60fe.tar.xz
mpd-483ba5ea1ccebd4741fbcc5d92648ac6854c60fe.zip
thread/GLibMutex: new Mutex implementation
Switch WIN32 to this implementation to be able to use condition variables, which is impossible with CriticalSection.
-rw-r--r--Makefile.am2
-rw-r--r--src/thread/GLibMutex.hxx (renamed from src/thread/CriticalSection.hxx)59
-rw-r--r--src/thread/Mutex.hxx4
3 files changed, 45 insertions, 20 deletions
diff --git a/Makefile.am b/Makefile.am
index c1b76fa4d..23f1a711c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -163,7 +163,7 @@ src_mpd_SOURCES = \
$(MIXER_API_SRC) \
src/thread/Mutex.hxx \
src/thread/PosixMutex.hxx \
- src/thread/CriticalSection.hxx \
+ src/thread/GLibMutex.hxx \
src/glib_socket.h \
src/clock.c src/clock.h \
src/notify.c \
diff --git a/src/thread/CriticalSection.hxx b/src/thread/GLibMutex.hxx
index f3faee943..f9bb5b4e9 100644
--- a/src/thread/CriticalSection.hxx
+++ b/src/thread/GLibMutex.hxx
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009-2013 Max Kellermann <max@duempel.org>
+ * Copyright (C) 2013 Max Kellermann <max@duempel.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -27,36 +27,61 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef MPD_THREAD_CRITICAL_SECTION_HXX
-#define MPD_THREAD_CRITICAL_SECTION_HXX
+#ifndef MPD_THREAD_GLIB_MUTEX_HXX
+#define MPD_THREAD_GLIB_MUTEX_HXX
-#include <windows.h>
+#include <glib.h>
-class CriticalSection {
- CRITICAL_SECTION critical_section;
+/**
+ * A wrapper for GMutex.
+ */
+class GLibMutex {
+#if GLIB_CHECK_VERSION(2,32,0)
+ GMutex mutex;
+#else
+ GMutex *mutex;
+#endif
public:
- CriticalSection() {
- ::InitializeCriticalSection(&critical_section);
+ GLibMutex() {
+#if GLIB_CHECK_VERSION(2,32,0)
+ g_mutex_init(&mutex);
+#else
+ mutex = g_mutex_new();
+#endif
}
- ~CriticalSection() {
- ::DeleteCriticalSection(&critical_section);
+ ~GLibMutex() {
+#if GLIB_CHECK_VERSION(2,32,0)
+ g_mutex_clear(&mutex);
+#else
+ g_mutex_free(mutex);
+#endif
}
- CriticalSection(const CriticalSection &other) = delete;
- CriticalSection &operator=(const CriticalSection &other) = delete;
+ GLibMutex(const GLibMutex &other) = delete;
+ GLibMutex &operator=(const GLibMutex &other) = delete;
+
+private:
+ GMutex *GetNative() {
+#if GLIB_CHECK_VERSION(2,32,0)
+ return &mutex;
+#else
+ return mutex;
+#endif
+ }
+public:
void lock() {
- ::EnterCriticalSection(&critical_section);
- };
+ g_mutex_lock(GetNative());
+ }
bool try_lock() {
- return ::TryEnterCriticalSection(&critical_section) != 0;
- };
+ return g_mutex_trylock(GetNative());
+ }
void unlock() {
- ::LeaveCriticalSection(&critical_section);
+ g_mutex_lock(GetNative());
}
};
diff --git a/src/thread/Mutex.hxx b/src/thread/Mutex.hxx
index 675af74b1..d2ebf4d1c 100644
--- a/src/thread/Mutex.hxx
+++ b/src/thread/Mutex.hxx
@@ -24,8 +24,8 @@
/* mingw-w64 4.6.3 lacks a std::mutex implementation */
-#include "CriticalSection.hxx"
-typedef CriticalSection Mutex;
+#include "GLibMutex.hxx"
+typedef GLibMutex Mutex;
#else