зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1290625 - Make libcubeb mutex recurse again, to avoid deadlock when getting microphone. r=achronop
MozReview-Commit-ID: AvYa8ylsdOf --HG-- extra : rebase_source : 51d340d1607c78002fda6c665932fb966c66a298
This commit is contained in:
Родитель
e92959a3bd
Коммит
dd02c97e85
|
@ -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 */
|
Загрузка…
Ссылка в новой задаче