summaryrefslogtreecommitdiffstats
path: root/media/libcubeb/src/cubeb_utils_win.h
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
commit6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch)
treea68f146d7fa01f0134297619fbe7e33db084e0aa /media/libcubeb/src/cubeb_utils_win.h
parentInitial commit. (diff)
downloadthunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz
thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'media/libcubeb/src/cubeb_utils_win.h')
-rw-r--r--media/libcubeb/src/cubeb_utils_win.h67
1 files changed, 67 insertions, 0 deletions
diff --git a/media/libcubeb/src/cubeb_utils_win.h b/media/libcubeb/src/cubeb_utils_win.h
new file mode 100644
index 0000000000..48e7b1b6d0
--- /dev/null
+++ b/media/libcubeb/src/cubeb_utils_win.h
@@ -0,0 +1,67 @@
+/*
+ * 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_WIN)
+#define CUBEB_UTILS_WIN
+
+#include "cubeb-internal.h"
+#include <windows.h>
+
+/* This wraps an SRWLock to track the owner in debug mode, adapted from
+ NSPR and http://blogs.msdn.com/b/oldnewthing/archive/2013/07/12/10433554.aspx
+ */
+class owned_critical_section {
+public:
+ owned_critical_section()
+ : srwlock(SRWLOCK_INIT)
+#ifndef NDEBUG
+ ,
+ owner(0)
+#endif
+ {
+ }
+
+ void lock()
+ {
+ AcquireSRWLockExclusive(&srwlock);
+#ifndef NDEBUG
+ XASSERT(owner != GetCurrentThreadId() && "recursive locking");
+ owner = GetCurrentThreadId();
+#endif
+ }
+
+ void unlock()
+ {
+#ifndef NDEBUG
+ /* GetCurrentThreadId cannot return 0: it is not a the valid thread id */
+ owner = 0;
+#endif
+ ReleaseSRWLockExclusive(&srwlock);
+ }
+
+ /* This is guaranteed to have the good behaviour if it succeeds. The behaviour
+ is undefined otherwise. */
+ void assert_current_thread_owns()
+ {
+#ifndef NDEBUG
+ /* This implies owner != 0, because GetCurrentThreadId cannot return 0. */
+ XASSERT(owner == GetCurrentThreadId());
+#endif
+ }
+
+private:
+ SRWLOCK srwlock;
+#ifndef NDEBUG
+ DWORD owner;
+#endif
+
+ // Disallow copy and assignment because SRWLock cannot be copied.
+ owned_critical_section(const owned_critical_section &);
+ owned_critical_section & operator=(const owned_critical_section &);
+};
+
+#endif /* CUBEB_UTILS_WIN */