Mutex.h
2.46 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
106
107
108
#pragma once
#include "os/ErrorCodes.h"
#include "os/Handle.h"
#include "os/WaitStatus.h"
#include "utils/NonCopyable.h"
namespace il2cpp
{
namespace os
{
class MutexImpl;
class FastMutexImpl;
class Mutex : public il2cpp::utils::NonCopyable
{
public:
Mutex(bool initiallyOwned = false);
~Mutex();
void Lock(bool interruptible = false);
bool TryLock(uint32_t milliseconds = 0, bool interruptible = false);
void Unlock();
void* GetOSHandle();
private:
MutexImpl* m_Mutex;
};
struct AutoLock : public il2cpp::utils::NonCopyable
{
AutoLock(Mutex* mutex) : m_Mutex(mutex) { m_Mutex->Lock(); }
~AutoLock() { m_Mutex->Unlock(); }
private:
Mutex* m_Mutex;
};
class MutexHandle : public Handle
{
public:
MutexHandle(Mutex* mutex) : m_Mutex(mutex) {}
virtual ~MutexHandle() { delete m_Mutex; }
virtual bool Wait() { m_Mutex->Lock(true); return true; }
virtual bool Wait(uint32_t ms) { return m_Mutex->TryLock(ms, true); }
virtual WaitStatus Wait(bool interruptible) { m_Mutex->Lock(interruptible); return kWaitStatusSuccess; }
virtual WaitStatus Wait(uint32_t ms, bool interruptible) { return m_Mutex->TryLock(ms, interruptible) ? kWaitStatusSuccess : kWaitStatusFailure; }
virtual void Signal() { m_Mutex->Unlock(); }
virtual void* GetOSHandle() { return m_Mutex->GetOSHandle(); }
Mutex* Get() { return m_Mutex; }
private:
Mutex* m_Mutex;
};
/// Lightweight mutex that has no support for interruption or timed waits. Meant for
/// internal use only.
class FastMutex
{
public:
FastMutex();
~FastMutex();
void Lock();
void Unlock();
FastMutexImpl* GetImpl();
private:
FastMutexImpl* m_Impl;
};
struct FastAutoLock : public il2cpp::utils::NonCopyable
{
FastAutoLock(FastMutex* mutex)
: m_Mutex(mutex)
{
m_Mutex->Lock();
}
~FastAutoLock()
{
m_Mutex->Unlock();
}
private:
FastMutex* m_Mutex;
};
struct FastAutoUnlock : public il2cpp::utils::NonCopyable
{
FastAutoUnlock(FastMutex* mutex)
: m_Mutex(mutex)
{
m_Mutex->Unlock();
}
~FastAutoUnlock()
{
m_Mutex->Lock();
}
private:
FastMutex* m_Mutex;
};
}
}