Bug 1290625 - Make libcubeb mutex recurse again, to avoid deadlock when getting microphone. r=achronop
☠☠ backed out by 2ba03ba34ece ☠ ☠
authorJan-Ivar Bruaroey <jib@mozilla.com>
Fri, 29 Jul 2016 20:03:35 -0400
changeset 332503 f60ec9e34d4d34958ff64d573862474dac8c0a6f
parent 332502 ff43d3e4c4fc8732b460ded61155c586e9e32573
child 332504 cf3d0b086060c8b81a3a5c9c33745e65e0096121
push id9858
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 14:37:10 +0000
treeherdermozilla-aurora@203106ef6cb6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersachronop
bugs1290625
milestone50.0a1
Bug 1290625 - Make libcubeb mutex recurse again, to avoid deadlock when getting microphone. r=achronop MozReview-Commit-ID: AvYa8ylsdOf
media/libcubeb/src/cubeb_utils_unix.h
new file mode 100644
--- /dev/null
+++ b/media/libcubeb/src/cubeb_utils_unix.h
@@ -0,0 +1,81 @@
+/*
+ * 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);
+    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
+
+#ifdef DEBUG
+    int r =
+#endif
+    pthread_mutex_init(&mutex, &attr);
+#ifdef DEBUG
+    assert(r == 0);
+#endif
+
+    pthread_mutexattr_destroy(&attr);
+  }
+
+  ~owned_critical_section()
+  {
+#ifdef DEBUG
+    int r =
+#endif
+    pthread_mutex_destroy(&mutex);
+#ifdef DEBUG
+    assert(r == 0);
+#endif
+  }
+
+  void enter()
+  {
+#ifdef DEBUG
+    int r =
+#endif
+    pthread_mutex_lock(&mutex);
+#ifdef DEBUG
+    assert(r == 0 && "Deadlock");
+#endif
+  }
+
+  void leave()
+  {
+#ifdef DEBUG
+    int r =
+#endif
+    pthread_mutex_unlock(&mutex);
+#ifdef DEBUG
+    assert(r == 0 && "Unlocking unlocked mutex");
+#endif
+  }
+
+  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 */