This repository was archived by the owner on Nov 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 503
Expand file tree
/
Copy pathpal_threading.cpp
More file actions
206 lines (168 loc) · 5.5 KB
/
pal_threading.cpp
File metadata and controls
206 lines (168 loc) · 5.5 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include "pal_common.h"
#include "pal_threading.h"
#include "pal_time.h"
#include <limits.h>
#include <sched.h>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LowLevelMutex
extern "C" void CoreLibNative_LowLevelMutex_Acquire(LowLevelMutex *mutex)
{
assert(mutex != nullptr);
mutex->Acquire();
}
extern "C" void CoreLibNative_LowLevelMutex_Release(LowLevelMutex *mutex)
{
assert(mutex != nullptr);
mutex->Release();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LowLevelMonitor
#if !(HAVE_MACH_ABSOLUTE_TIME || HAVE_PTHREAD_CONDATTR_SETCLOCK && HAVE_CLOCK_MONOTONIC)
#error Don't know how to perform timed wait on this platform
#endif
LowLevelMonitor::LowLevelMonitor(bool abortOnFailure, bool *successRef) : LowLevelMutex(abortOnFailure, successRef)
{
if (successRef != nullptr && !*successRef)
{
assert(!abortOnFailure);
return;
}
#if HAVE_MACH_ABSOLUTE_TIME
// Older versions of OSX don't support CLOCK_MONOTONIC, so we don't use pthread_condattr_setclock. See
// Wait(int32_t timeoutMilliseconds).
int initError = pthread_cond_init(&m_condition, nullptr);
#else
int error;
pthread_condattr_t conditionAttributes;
error = pthread_condattr_init(&conditionAttributes);
if (error != 0)
{
if (abortOnFailure)
{
abort();
}
LowLevelMutex::~LowLevelMutex();
*successRef = false;
return;
}
error = pthread_condattr_setclock(&conditionAttributes, CLOCK_MONOTONIC);
assert(error == 0);
int initError = pthread_cond_init(&m_condition, &conditionAttributes);
error = pthread_condattr_destroy(&conditionAttributes);
assert(error == 0);
#endif
if (initError != 0)
{
if (abortOnFailure)
{
abort();
}
LowLevelMutex::~LowLevelMutex();
*successRef = false;
return;
}
if (successRef != nullptr)
{
*successRef = true;
}
}
// Returns false upon timeout or unexpected error, and true when the thread is woken up (could be a spurious wakeup, depending
// on implementation)
bool LowLevelMonitor::Wait(int32_t timeoutMilliseconds)
{
assert(timeoutMilliseconds >= -1);
if (timeoutMilliseconds < 0)
{
Wait();
return true;
}
SetIsLocked(false);
int error;
// Calculate the time at which a timeout should occur, and wait. Older versions of OSX don't support clock_gettime with
// CLOCK_MONOTONIC, so we instead compute the relative timeout duration, and use a relative variant of the timed wait.
timespec timeoutTimeSpec;
#if HAVE_MACH_ABSOLUTE_TIME
MillisecondsToTimeSpec(timeoutMilliseconds, &timeoutTimeSpec);
error = pthread_cond_timedwait_relative_np(&m_condition, &m_mutex, &timeoutTimeSpec);
#else
error = clock_gettime(CLOCK_MONOTONIC, &timeoutTimeSpec);
assert(error == 0);
AddMillisecondsToTimeSpec(timeoutMilliseconds, &timeoutTimeSpec);
error = pthread_cond_timedwait(&m_condition, &m_mutex, &timeoutTimeSpec);
#endif
assert(error == 0 || error == ETIMEDOUT);
SetIsLocked(true);
return error == 0;
}
extern "C" LowLevelMonitor *CoreLibNative_LowLevelMonitor_New()
{
void *monitorBuffer = malloc(sizeof(LowLevelMonitor));
if (monitorBuffer == nullptr)
{
return nullptr;
}
bool success;
LowLevelMonitor *monitor = new(monitorBuffer) LowLevelMonitor(false /* abortOnFailure */, &success);
assert(monitor == monitorBuffer);
if (!success)
{
free(monitorBuffer);
return nullptr;
}
return monitor;
}
extern "C" void CoreLibNative_LowLevelMonitor_Delete(LowLevelMonitor *monitor)
{
assert(monitor != nullptr);
monitor->~LowLevelMonitor();
free(monitor);
}
extern "C" void CoreLibNative_LowLevelMonitor_Wait(LowLevelMonitor *monitor)
{
assert(monitor != nullptr);
monitor->Wait();
}
// TODO: Change return type to 'bool' once marshaling support is added for it
extern "C" int32_t CoreLibNative_LowLevelMonitor_TimedWait(LowLevelMonitor *monitor, int32_t timeoutMilliseconds)
{
assert(monitor != nullptr);
return static_cast<int32_t>(monitor->Wait(timeoutMilliseconds));
}
extern "C" void CoreLibNative_LowLevelMonitor_Signal_Release(LowLevelMonitor *monitor)
{
assert(monitor != nullptr);
monitor->Signal();
monitor->Release();
}
extern "C" bool CoreLibNative_RuntimeThread_CreateThread(size_t stackSize, void *(*startAddress)(void*), void *parameter)
{
bool result = false;
pthread_attr_t attrs;
int error = pthread_attr_init(&attrs);
if (error != 0)
{
// Do not call pthread_attr_destroy
return false;
}
error = pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);
assert(error == 0);
if (stackSize > 0)
{
if (stackSize < PTHREAD_STACK_MIN)
{
stackSize = PTHREAD_STACK_MIN;
}
error = pthread_attr_setstacksize(&attrs, stackSize);
if (error != 0) goto CreateThreadExit;
}
pthread_t threadId;
error = pthread_create(&threadId, &attrs, startAddress, parameter);
if (error != 0) goto CreateThreadExit;
result = true;
CreateThreadExit:
error = pthread_attr_destroy(&attrs);
assert(error == 0);
return result;
}