From dd02c97e850b6eb244fc9e61948c09cdcd064059 Mon Sep 17 00:00:00 2001 From: Jan-Ivar Bruaroey Date: Fri, 29 Jul 2016 20:03:35 -0400 Subject: [PATCH] Bug 1290625 - Make libcubeb mutex recurse again, to avoid deadlock when getting microphone. r=achronop MozReview-Commit-ID: AvYa8ylsdOf --HG-- extra : rebase_source : 51d340d1607c78002fda6c665932fb966c66a298 --- media/libcubeb/src/cubeb_utils_unix.h | 81 +++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 media/libcubeb/src/cubeb_utils_unix.h diff --git a/media/libcubeb/src/cubeb_utils_unix.h b/media/libcubeb/src/cubeb_utils_unix.h new file mode 100644 index 000000000000..643306e14157 --- /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 +#include +#include + +/* 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 */