PosixWaitObject.h
2.8 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
#pragma once
#if IL2CPP_TARGET_POSIX && !RUNTIME_TINY
#include <pthread.h>
#include <stdint.h>
#include <limits.h>
#include "utils/NonCopyable.h"
#include "os/WaitStatus.h"
#if (IL2CPP_USE_POSIX_COND_TIMEDWAIT_REL)
int pthread_cond_timedwait_relative_np(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *spec);
#endif
namespace il2cpp
{
namespace os
{
class ThreadImpl;
namespace posix
{
const uint32_t kNoTimeout = UINT_MAX;
////TODO: generalize this so that it can be used with c++11 condition variables
/// Base class for all synchronization primitives when running on POSIX.
///
/// To support interruption and timeouts for all synchronization primitives (events, mutexes, and
/// semaphores) we implement these primitives ourselves instead of using their standard POSIX/Mach
/// system counterparts. See PosixWaitObject.cpp for an explanation why.
class PosixWaitObject : public il2cpp::utils::NonCopyable
{
public:
~PosixWaitObject();
WaitStatus Wait(bool interruptible = false);
WaitStatus Wait(uint32_t ms, bool interruptible = false);
/// Cause an ongoing blocking wait on this object to exit and check for pending APCs.
/// If the object is not currently being waited on, will cause the next wait to exit
/// right away and check for APCs. After APCs have been handled, the object will go
/// back to waiting except if the wait timeout has expired.
void InterruptWait();
void* GetOSHandle();
static void LockWaitObjectDeletion();
static void UnlockWaitObjectDeletion();
protected:
enum Type
{
kMutex, /// All mutexes are recursive.
kManualResetEvent,
kAutoResetEvent,
kSemaphore
};
PosixWaitObject(Type type);
Type m_Type;
/// Always have to acquire this mutex to touch m_Count.
pthread_mutex_t m_Mutex;
/// Signal other threads of changes to m_Count.
pthread_cond_t m_Condition;
/// "Release" count for the primitive. Means different things depending on the type of primitive
/// but for all primitives, we wait until this is zero. Semaphores are the only primitive for which
/// this can go past 1.
uint32_t m_Count;
/// Number of threads waiting on this object. This is used to prevent unnecessary signals
/// on m_Condition.
uint32_t m_WaitingThreadCount;
bool HaveWaitingThreads() const { return (m_WaitingThreadCount != 0); }
};
struct AutoLockWaitObjectDeletion
{
AutoLockWaitObjectDeletion() { PosixWaitObject::LockWaitObjectDeletion(); }
~AutoLockWaitObjectDeletion() { PosixWaitObject::UnlockWaitObjectDeletion(); }
};
}
}
}
#endif // IL2CPP_TARGET_POSIX