summaryrefslogtreecommitdiff
path: root/media/libcubeb/src/cubeb_utils_unix.h
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /media/libcubeb/src/cubeb_utils_unix.h
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloaduxp-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
Add m-esr52 at 52.6.0
Diffstat (limited to 'media/libcubeb/src/cubeb_utils_unix.h')
-rw-r--r--media/libcubeb/src/cubeb_utils_unix.h89
1 files changed, 89 insertions, 0 deletions
diff --git a/media/libcubeb/src/cubeb_utils_unix.h b/media/libcubeb/src/cubeb_utils_unix.h
new file mode 100644
index 0000000000..80219d58b1
--- /dev/null
+++ b/media/libcubeb/src/cubeb_utils_unix.h
@@ -0,0 +1,89 @@
+/*
+ * 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>
+#include <errno.h>
+#include <stdio.h>
+
+/* This wraps a critical section to track the owner in debug mode. */
+class owned_critical_section
+{
+public:
+ owned_critical_section()
+ {
+ pthread_mutexattr_t attr;
+ pthread_mutexattr_init(&attr);
+#ifndef NDEBUG
+ pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
+#else
+ pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
+#endif
+
+#ifndef NDEBUG
+ int r =
+#endif
+ pthread_mutex_init(&mutex, &attr);
+#ifndef NDEBUG
+ assert(r == 0);
+#endif
+
+ pthread_mutexattr_destroy(&attr);
+ }
+
+ ~owned_critical_section()
+ {
+#ifndef NDEBUG
+ int r =
+#endif
+ pthread_mutex_destroy(&mutex);
+#ifndef NDEBUG
+ assert(r == 0);
+#endif
+ }
+
+ void enter()
+ {
+#ifndef NDEBUG
+ int r =
+#endif
+ pthread_mutex_lock(&mutex);
+#ifndef NDEBUG
+ assert(r == 0 && "Deadlock");
+#endif
+ }
+
+ void leave()
+ {
+#ifndef NDEBUG
+ int r =
+#endif
+ pthread_mutex_unlock(&mutex);
+#ifndef NDEBUG
+ assert(r == 0 && "Unlocking unlocked mutex");
+#endif
+ }
+
+ void assert_current_thread_owns()
+ {
+#ifndef NDEBUG
+ int r = pthread_mutex_lock(&mutex);
+ assert(r == EDEADLK);
+#endif
+ }
+
+private:
+ pthread_mutex_t mutex;
+
+ // Disallow copy and assignment because pthread_mutex_t cannot be copied.
+ owned_critical_section(const owned_critical_section&);
+ owned_critical_section& operator=(const owned_critical_section&);
+};
+
+#endif /* CUBEB_UTILS_UNIX */