blob: 5db6f04d035845b1bc24ecf3352da37974c80978 (
plain) (
blame)
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
|
#include <pthread.h>
#include "WAVM/Inline/BasicTypes.h"
#include "WAVM/Inline/Errors.h"
#include "WAVM/Platform/Defines.h"
#include "WAVM/Platform/RWMutex.h"
using namespace WAVM;
using namespace WAVM::Platform;
Platform::RWMutex::RWMutex()
{
static_assert(sizeof(LockData) >= sizeof(pthread_rwlock_t), "");
static_assert(alignof(LockData) >= alignof(pthread_rwlock_t), "");
#if WAVM_ENABLE_ASSERTS
static_assert(sizeof(exclusiveLockingThreadId) == sizeof(pthread_t), "");
static_assert(alignof(Uptr) >= alignof(pthread_t), "");
#endif
WAVM_ERROR_UNLESS(!pthread_rwlock_init((pthread_rwlock_t*)&lockData, nullptr));
}
Platform::RWMutex::~RWMutex()
{
WAVM_ERROR_UNLESS(!pthread_rwlock_destroy((pthread_rwlock_t*)&lockData));
}
void Platform::RWMutex::lock(LockShareability shareability)
{
if(shareability == LockShareability::exclusive)
{
WAVM_ERROR_UNLESS(!pthread_rwlock_wrlock((pthread_rwlock_t*)&lockData));
#if WAVM_ENABLE_ASSERTS
exclusiveLockingThreadId.store(reinterpret_cast<Uptr>(pthread_self()),
std::memory_order_relaxed);
#endif
}
else
{
WAVM_ERROR_UNLESS(!pthread_rwlock_rdlock((pthread_rwlock_t*)&lockData));
}
}
void Platform::RWMutex::unlock(LockShareability shareability)
{
if(shareability == LockShareability::exclusive)
{
WAVM_ERROR_UNLESS(!pthread_rwlock_unlock((pthread_rwlock_t*)&lockData));
#if WAVM_ENABLE_ASSERTS
exclusiveLockingThreadId.store(0, std::memory_order_relaxed);
#endif
}
else
{
WAVM_ERROR_UNLESS(!pthread_rwlock_unlock((pthread_rwlock_t*)&lockData));
}
}
#if WAVM_ENABLE_ASSERTS
bool Platform::RWMutex::isExclusivelyLockedByCurrentThread()
{
return reinterpret_cast<pthread_t>(exclusiveLockingThreadId.load(std::memory_order_relaxed))
== pthread_self();
}
#endif
|