Bug 1874226 - DebugFilesAutoLock to lock/unlock in ctor/dtor instead of via Scoped<> inheritance. r=xpcom-reviewers,emilio

Differential Revision: https://phabricator.services.mozilla.com/D200249
This commit is contained in:
Kelsey Gilbert 2024-02-07 18:30:22 +00:00
parent 7745990dfe
commit b2be0b2dcd

View file

@ -6,7 +6,6 @@
#include "mozilla/Maybe.h"
#include "mozilla/Mutex.h"
#include "mozilla/Scoped.h"
#include "mozilla/UniquePtr.h"
#include <algorithm>
@ -38,41 +37,18 @@ inline mozilla::Maybe<intptr_t> FileDescriptorToHandle(int aFd) {
namespace {
struct DebugFilesAutoLockTraits {
typedef PRLock* type;
typedef const PRLock* const_type;
static const_type empty() { return nullptr; }
static void release(type aL) { PR_Unlock(aL); }
};
class DebugFilesAutoLock : public mozilla::Scoped<DebugFilesAutoLockTraits> {
static PRLock* Lock;
class DebugFilesAutoLock final {
public:
static PRLock* getDebugFileIDsLock() {
// On windows this static is not thread safe, but we know that the first
// call is from
// * An early registration of a debug FD or
// * The call to InitWritePoisoning.
// Since the early debug FDs are logs created early in the main thread
// and no writes are trapped before InitWritePoisoning, we are safe.
if (!Lock) {
Lock = PR_NewLock();
}
// We have to use something lower level than a mutex. If we don't, we
// can get recursive in here when called from logging a call to free.
return Lock;
static PRLock* sLock = PR_NewLock();
return sLock;
}
DebugFilesAutoLock()
: mozilla::Scoped<DebugFilesAutoLockTraits>(getDebugFileIDsLock()) {
PR_Lock(get());
}
DebugFilesAutoLock() { PR_Lock(getDebugFileIDsLock()); }
~DebugFilesAutoLock() { PR_Unlock(getDebugFileIDsLock()); }
};
PRLock* DebugFilesAutoLock::Lock;
// The ChunkedList<T> class implements, at the high level, a non-iterable
// list of instances of T. Its goal is to be somehow minimalist for the
// use case of storing the debug files handles here, with the property of