Thread.h
3.75 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#pragma once
#include "il2cpp-config.h"
#include "os/ErrorCodes.h"
#include "os/Event.h"
#include "os/WaitStatus.h"
#include "utils/NonCopyable.h"
namespace il2cpp
{
namespace os
{
class ThreadImpl;
enum ThreadPriority
{
kThreadPriorityLowest = 0,
kThreadPriorityLow = 1,
kThreadPriorityNormal = 2,
kThreadPriorityHigh = 3,
kThreadPriorityHighest = 4
};
enum ApartmentState
{
kApartmentStateInSTA = 0,
kApartmentStateInMTA = 1,
kApartmentStateUnknown = 2,
kApartmentStateCoInitialized = 4,
};
class Thread : public il2cpp::utils::NonCopyable
{
public:
Thread();
~Thread();
typedef void (*StartFunc) (void* arg);
// Use STDCALL calling convention on Windows, as it will be called back directly from the OS. This is defined as nothing on other platforms.
typedef void (STDCALL * APCFunc)(void* context);
typedef size_t ThreadId;
typedef void (*CleanupFunc) (void* arg);
/// Initialize/Shutdown thread subsystem. Must be called on main thread.
static void Init();
static void Shutdown();
ErrorCode Run(StartFunc func, void* arg);
ThreadId Id();
/// Set thread name for debugging purposes. Won't do anything if not supported
/// by platform.
void SetName(const char* name);
void SetPriority(ThreadPriority priority);
ThreadPriority GetPriority();
void SetStackSize(size_t stackSize);
static int GetMaxStackSize();
void SetCleanupFunction(CleanupFunc cleanupFunc, void* arg)
{
m_CleanupFunc = cleanupFunc;
m_CleanupFuncArg = arg;
}
/// Interruptible, infinite wait join.
WaitStatus Join();
/// Interruptible, timed wait join.
WaitStatus Join(uint32_t ms);
/// Execute the given function on the thread the next time the thread executes
/// an interruptible blocking operation.
/// NOTE: The APC is allowed to raise exceptions!
void QueueUserAPC(APCFunc func, void* context);
// Explicit versions modify state without actually changing COM state.
// Used to set thread state before it's started.
ApartmentState GetApartment();
ApartmentState GetExplicitApartment();
ApartmentState SetApartment(ApartmentState state);
void SetExplicitApartment(ApartmentState state);
/// Interruptible, timed sleep.
static void Sleep(uint32_t ms, bool interruptible = false);
static ThreadId CurrentThreadId();
static Thread* GetCurrentThread();
static bool HasCurrentThread();
static Thread* GetOrCreateCurrentThread();
static void DetachCurrentThread();
static bool YieldInternal();
#if IL2CPP_HAS_NATIVE_THREAD_CLEANUP
typedef void (*ThreadCleanupFunc) (void* arg);
static void SetNativeThreadCleanup(ThreadCleanupFunc cleanupFunction);
static void RegisterCurrentThreadForCleanup(void* arg);
static void UnregisterCurrentThreadForCleanup();
void SignalExited();
#endif
static const uint64_t kInvalidThreadId = 0;
private:
enum ThreadState
{
kThreadCreated,
kThreadRunning,
kThreadWaiting,
kThreadExited
};
ThreadState m_State;
friend class ThreadImpl; // m_Thread
ThreadImpl* m_Thread;
/// Event that the thread signals when it finishes execution. Used for joins.
/// Supports interruption.
Event m_ThreadExitedEvent;
CleanupFunc m_CleanupFunc;
void* m_CleanupFuncArg;
Thread(ThreadImpl* thread);
static void RunWrapper(void* arg);
};
}
}