aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorPaul Adenot <[email protected]>2016-07-25 11:33:27 +0200
committerPaul Adenot <[email protected]>2016-07-27 10:46:57 +0200
commit76d768f79a1f645852cf7ebe0a7bc1130b4bb601 (patch)
tree5723505b451237b2d12777fc502217becba55091
parent5f3d3f506c564e11fdc2446380dc340d44de2c6c (diff)
downloadcubeb-76d768f79a1f645852cf7ebe0a7bc1130b4bb601.tar.gz
cubeb-76d768f79a1f645852cf7ebe0a7bc1130b4bb601.zip
Add files for mutex implementation on Windows and UNIX
-rw-r--r--src/cubeb_utils.h19
-rw-r--r--src/cubeb_utils_unix.h56
-rw-r--r--src/cubeb_utils_win.h64
-rw-r--r--src/cubeb_wasapi.cpp67
4 files changed, 139 insertions, 67 deletions
diff --git a/src/cubeb_utils.h b/src/cubeb_utils.h
index 8c992ff..ba38b45 100644
--- a/src/cubeb_utils.h
+++ b/src/cubeb_utils.h
@@ -11,6 +11,11 @@
#include <stdint.h>
#include <string.h>
#include <assert.h>
+#if defined(WIN32)
+#include "cubeb_utils_win.h"
+#else
+#include "cubeb_utils_unix.h"
+#endif
/** Similar to memcpy, but accounts for the size of an element. */
template<typename T>
@@ -189,4 +194,18 @@ private:
size_t length_;
};
+struct auto_lock {
+ auto_lock(owned_critical_section * lock)
+ : lock(lock)
+ {
+ lock->enter();
+ }
+ ~auto_lock()
+ {
+ lock->leave();
+ }
+private:
+ owned_critical_section * lock;
+};
+
#endif /* CUBEB_UTILS */
diff --git a/src/cubeb_utils_unix.h b/src/cubeb_utils_unix.h
new file mode 100644
index 0000000..675c4da
--- /dev/null
+++ b/src/cubeb_utils_unix.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright © 2016 Mozilla Foundation
+ *
+ * This program is made available under an ISC-style license. See the
+ * accompanying file LICENSE for details.
+ */
+
+#if !defined(CUBEB_UTILS_UNIX)
+#define CUBEB_UTILS_UNIX
+
+#include <pthread.h>
+
+/* This wraps a critical section to track the owner in debug mode. */
+class owned_critical_section
+{
+public:
+ owned_critical_section()
+ {
+ int r;
+ pthread_mutexattr_t attr;
+ pthread_mutexattr_init(&attr);
+ pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
+ r = pthread_mutex_init(&mutex, &attr);
+ assert(r == 0);
+ pthread_mutexattr_destroy(&attr);
+ }
+
+ ~owned_critical_section()
+ {
+ int r = pthread_mutex_destroy(&mutex);
+ assert(r == 0);
+ }
+
+ void enter()
+ {
+ pthread_mutex_lock(&mutex);
+ }
+
+ void leave()
+ {
+ pthread_mutex_unlock(&mutex);
+ }
+
+ void assert_current_thread_owns()
+ {
+#ifdef DEBUG
+ int r = pthread_mutex_lock(&mutex);
+ assert(r == EDEADLK);
+#endif
+ }
+
+private:
+ pthread_mutex_t mutex;
+};
+
+#endif /* CUBEB_UTILS_UNIX */
diff --git a/src/cubeb_utils_win.h b/src/cubeb_utils_win.h
new file mode 100644
index 0000000..77dfde3
--- /dev/null
+++ b/src/cubeb_utils_win.h
@@ -0,0 +1,64 @@
+/*
+ * Copyright © 2016 Mozilla Foundation
+ *
+ * This program is made available under an ISC-style license. See the
+ * accompanying file LICENSE for details.
+ */
+
+#if !defined(CUBEB_UTILS_WIN)
+#define CUBEB_UTILS_WIN
+
+/* This wraps a critical section to track the owner in debug mode, adapted from
+ NSPR and http://blogs.msdn.com/b/oldnewthing/archive/2013/07/12/10433554.aspx */
+class owned_critical_section
+{
+public:
+ owned_critical_section()
+#ifdef DEBUG
+ : owner(0)
+#endif
+ {
+ InitializeCriticalSection(&critical_section);
+ }
+
+ ~owned_critical_section()
+ {
+ DeleteCriticalSection(&critical_section);
+ }
+
+ void enter()
+ {
+ EnterCriticalSection(&critical_section);
+#ifdef DEBUG
+ XASSERT(owner != GetCurrentThreadId() && "recursive locking");
+ owner = GetCurrentThreadId();
+#endif
+ }
+
+ void leave()
+ {
+#ifdef DEBUG
+ /* GetCurrentThreadId cannot return 0: it is not a the valid thread id */
+ owner = 0;
+#endif
+ LeaveCriticalSection(&critical_section);
+ }
+
+ /* This is guaranteed to have the good behaviour if it succeeds. The behaviour
+ is undefined otherwise. */
+ void assert_current_thread_owns()
+ {
+#ifdef DEBUG
+ /* This implies owner != 0, because GetCurrentThreadId cannot return 0. */
+ XASSERT(owner == GetCurrentThreadId());
+#endif
+ }
+
+private:
+ CRITICAL_SECTION critical_section;
+#ifdef DEBUG
+ DWORD owner;
+#endif
+};
+
+#endif /* CUBEB_UTILS_WIN */
diff --git a/src/cubeb_wasapi.cpp b/src/cubeb_wasapi.cpp
index b0ec043..c1d21aa 100644
--- a/src/cubeb_wasapi.cpp
+++ b/src/cubeb_wasapi.cpp
@@ -79,73 +79,6 @@ void SafeRelease(T * ptr)
}
}
-/* This wraps a critical section to track the owner in debug mode, adapted from
- NSPR and http://blogs.msdn.com/b/oldnewthing/archive/2013/07/12/10433554.aspx */
-class owned_critical_section
-{
-public:
- owned_critical_section()
-#ifdef DEBUG
- : owner(0)
-#endif
- {
- InitializeCriticalSection(&critical_section);
- }
-
- ~owned_critical_section()
- {
- DeleteCriticalSection(&critical_section);
- }
-
- void enter()
- {
- EnterCriticalSection(&critical_section);
-#ifdef DEBUG
- XASSERT(owner != GetCurrentThreadId() && "recursive locking");
- owner = GetCurrentThreadId();
-#endif
- }
-
- void leave()
- {
-#ifdef DEBUG
- /* GetCurrentThreadId cannot return 0: it is not a the valid thread id */
- owner = 0;
-#endif
- LeaveCriticalSection(&critical_section);
- }
-
- /* This is guaranteed to have the good behaviour if it succeeds. The behaviour
- is undefined otherwise. */
- void assert_current_thread_owns()
- {
-#ifdef DEBUG
- /* This implies owner != 0, because GetCurrentThreadId cannot return 0. */
- XASSERT(owner == GetCurrentThreadId());
-#endif
- }
-
-private:
- CRITICAL_SECTION critical_section;
-#ifdef DEBUG
- DWORD owner;
-#endif
-};
-
-struct auto_lock {
- auto_lock(owned_critical_section * lock)
- : lock(lock)
- {
- lock->enter();
- }
- ~auto_lock()
- {
- lock->leave();
- }
-private:
- owned_critical_section * lock;
-};
-
struct auto_com {
auto_com() {
result = CoInitializeEx(NULL, COINIT_MULTITHREADED);