pineapple/externals/cubeb/src/cubeb_utils_win.h

69 lines
1.6 KiB
C
Raw Normal View History

2020-12-28 16:15:37 +01:00
/*
* 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"
2021-12-08 07:33:31 +01:00
#include <windows.h>
2020-12-28 16:15:37 +01:00
2022-07-17 09:10:25 +02:00
/* This wraps a critical section to track the owner in debug mode, adapted from
2021-12-08 07:33:31 +01:00
NSPR and http://blogs.msdn.com/b/oldnewthing/archive/2013/07/12/10433554.aspx
*/
class owned_critical_section {
2020-12-28 16:15:37 +01:00
public:
owned_critical_section()
#ifndef NDEBUG
2022-07-17 09:10:25 +02:00
: owner(0)
2020-12-28 16:15:37 +01:00
#endif
{
2022-07-17 09:10:25 +02:00
InitializeCriticalSection(&critical_section);
2020-12-28 16:15:37 +01:00
}
2022-07-17 09:10:25 +02:00
~owned_critical_section() { DeleteCriticalSection(&critical_section); }
2020-12-28 16:15:37 +01:00
void lock()
{
2022-07-17 09:10:25 +02:00
EnterCriticalSection(&critical_section);
2020-12-28 16:15:37 +01:00
#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
2022-07-17 09:10:25 +02:00
LeaveCriticalSection(&critical_section);
2020-12-28 16:15:37 +01:00
}
/* 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:
2022-07-17 09:10:25 +02:00
CRITICAL_SECTION critical_section;
2020-12-28 16:15:37 +01:00
#ifndef NDEBUG
DWORD owner;
#endif
2022-07-17 09:10:25 +02:00
// Disallow copy and assignment because CRICICAL_SECTION cannot be copied.
2021-12-08 07:33:31 +01:00
owned_critical_section(const owned_critical_section &);
owned_critical_section & operator=(const owned_critical_section &);
2020-12-28 16:15:37 +01:00
};
#endif /* CUBEB_UTILS_WIN */