-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathrmt_mutex.cpp
More file actions
105 lines (83 loc) · 2.98 KB
/
Copy pathrmt_mutex.cpp
File metadata and controls
105 lines (83 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//=============================================================================
// Copyright (c) 2019-2025 Advanced Micro Devices, Inc. All rights reserved.
/// @author AMD Developer Tools Team
/// @file
/// @brief Implementations of the mutex abstraction.
//=============================================================================
#ifndef _WIN32
#include <new>
#include <mutex>
#endif // #ifndef _WIN32
#include "rmt_assert.h"
#include "rmt_mutex.h"
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif // #ifdef _WIN32
// structure for a mutex
typedef struct RmtMutexInternal
{
#ifdef _WIN32
HANDLE handle;
#else
std::mutex* mutex; // pointer to mutex object
std::mutex buffer; // buffer to store the mutex object (using placement new)
#endif // #ifdef _WIN32
} RmtMutexInternal;
// create a mutex
RmtErrorCode RmtMutexCreate(RmtMutex* mutex, const char* name)
{
RMT_ASSERT_MESSAGE(mutex, "Parameter mutex is NULL.");
RMT_RETURN_ON_ERROR(mutex, kRmtErrorInvalidPointer);
RmtMutexInternal* mutex_internal = (RmtMutexInternal*)mutex;
// if this fails it means the external opaque structure isn't large enough for an internal mutex.
RMT_STATIC_ASSERT(sizeof(RmtMutex) >= sizeof(RmtMutexInternal));
#ifdef _WIN32
/* create the mutex for win32 */
mutex_internal->handle = CreateMutexA(NULL, FALSE, name);
RMT_RETURN_ON_ERROR(mutex_internal->handle != NULL, kRmtErrorPlatformFunctionFailed);
#else
RMT_UNUSED(name);
mutex_internal->mutex = new (&mutex_internal->buffer) std::mutex;
#endif // #ifdef _WIN32
return kRmtOk;
}
RmtErrorCode RmtMutexLock(RmtMutex* mutex)
{
RMT_ASSERT_MESSAGE(mutex, "Parameter mutex is NULL.");
RMT_RETURN_ON_ERROR(mutex, kRmtErrorInvalidPointer);
RmtMutexInternal* mutex_internal = (RmtMutexInternal*)mutex;
#ifdef _WIN32
const DWORD error_code = WaitForSingleObject(mutex_internal->handle, INFINITE);
RMT_RETURN_ON_ERROR(error_code == WAIT_OBJECT_0, kRmtErrorPlatformFunctionFailed);
#else
mutex_internal->mutex->lock();
#endif // #ifdef _WIN32
return kRmtOk;
}
RmtErrorCode RmtMutexUnlock(RmtMutex* mutex)
{
RMT_ASSERT_MESSAGE(mutex, "Parameter mutex is NULL.");
RMT_RETURN_ON_ERROR(mutex, kRmtErrorInvalidPointer);
RmtMutexInternal* mutex_internal = (RmtMutexInternal*)mutex;
#ifdef _WIN32
BOOL error_code = ReleaseMutex(mutex_internal->handle);
RMT_RETURN_ON_ERROR(error_code, kRmtErrorPlatformFunctionFailed);
#else
mutex_internal->mutex->unlock();
#endif // #ifdef _WIN32
return kRmtOk;
}
RmtErrorCode RmtMutexDestroy(RmtMutex* mutex)
{
RMT_ASSERT_MESSAGE(mutex, "Parameter mutex is NULL.");
RMT_RETURN_ON_ERROR(mutex, kRmtErrorInvalidPointer);
RmtMutexInternal* mutex_internal = (RmtMutexInternal*)mutex;
#ifdef _WIN32
BOOL error_code = CloseHandle(mutex_internal->handle);
RMT_RETURN_ON_ERROR(error_code, kRmtErrorPlatformFunctionFailed);
#else
mutex_internal->mutex->~mutex();
#endif // #ifdef _WIN32
return kRmtOk;
}