-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathos_thread_android.cc
235 lines (191 loc) · 6.66 KB
/
os_thread_android.cc
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "platform/globals.h" // NOLINT
#if defined(DART_HOST_OS_ANDROID) && !defined(DART_USE_ABSL)
#include "vm/os_thread.h"
#include <errno.h> // NOLINT
#include <stdio.h>
#include <sys/prctl.h>
#include <sys/resource.h> // NOLINT
#include <sys/time.h> // NOLINT
#include "platform/address_sanitizer.h"
#include "platform/assert.h"
#include "platform/safe_stack.h"
#include "platform/signal_blocker.h"
#include "platform/utils.h"
#include "vm/flags.h"
namespace dart {
DEFINE_FLAG(int,
worker_thread_priority,
kMinInt,
"The thread priority the VM should use for new worker threads.");
class ThreadStartData {
public:
ThreadStartData(const char* name,
OSThread::ThreadStartFunction function,
uword parameter)
: name_(name), function_(function), parameter_(parameter) {}
const char* name() const { return name_; }
OSThread::ThreadStartFunction function() const { return function_; }
uword parameter() const { return parameter_; }
private:
const char* name_;
OSThread::ThreadStartFunction function_;
uword parameter_;
DISALLOW_COPY_AND_ASSIGN(ThreadStartData);
};
// TODO(bkonyi): remove this call once the prebuilt SDK is updated.
// Spawned threads inherit their spawner's signal mask. We sometimes spawn
// threads for running Dart code from a thread that is blocking SIGPROF.
// This function explicitly unblocks SIGPROF so the profiler continues to
// sample this thread.
static void UnblockSIGPROF() {
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGPROF);
int r = pthread_sigmask(SIG_UNBLOCK, &set, nullptr);
USE(r);
ASSERT(r == 0);
ASSERT(!CHECK_IS_BLOCKING(SIGPROF));
}
// Dispatch to the thread start function provided by the caller. This trampoline
// is used to ensure that the thread is properly destroyed if the thread just
// exits.
static void* ThreadStart(void* data_ptr) {
if (FLAG_worker_thread_priority != kMinInt) {
if (setpriority(PRIO_PROCESS, gettid(), FLAG_worker_thread_priority) ==
-1) {
FATAL("Setting thread priority to %d failed: errno = %d\n",
FLAG_worker_thread_priority, errno);
}
}
ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr);
const char* name = data->name();
OSThread::ThreadStartFunction function = data->function();
uword parameter = data->parameter();
delete data;
// Set the thread name. There is 16 bytes limit on the name (including \0).
// pthread_setname_np ignores names that are too long rather than truncating.
char truncated_name[16];
snprintf(truncated_name, ARRAY_SIZE(truncated_name), "%s", name);
pthread_setname_np(pthread_self(), truncated_name);
// Create new OSThread object and set as TLS for new thread.
OSThread* thread = OSThread::CreateOSThread();
if (thread != nullptr) {
OSThread::SetCurrent(thread);
thread->SetName(name);
UnblockSIGPROF();
// Call the supplied thread start function handing it its parameters.
function(parameter);
}
return nullptr;
}
int OSThread::TryStart(const char* name,
ThreadStartFunction function,
uword parameter) {
pthread_attr_t attr;
int result = pthread_attr_init(&attr);
RETURN_ON_PTHREAD_FAILURE(result);
result = pthread_attr_setstacksize(&attr, OSThread::GetMaxStackSize());
RETURN_ON_PTHREAD_FAILURE(result);
ThreadStartData* data = new ThreadStartData(name, function, parameter);
pthread_t tid;
result = pthread_create(&tid, &attr, ThreadStart, data);
RETURN_ON_PTHREAD_FAILURE(result);
result = pthread_attr_destroy(&attr);
RETURN_ON_PTHREAD_FAILURE(result);
return 0;
}
const ThreadJoinId OSThread::kInvalidThreadJoinId =
static_cast<ThreadJoinId>(0);
ThreadLocalKey OSThread::CreateThreadLocal(ThreadDestructor destructor) {
pthread_key_t key = kUnsetThreadLocalKey;
int result = pthread_key_create(&key, destructor);
VALIDATE_PTHREAD_RESULT(result);
ASSERT(key != kUnsetThreadLocalKey);
return key;
}
void OSThread::DeleteThreadLocal(ThreadLocalKey key) {
ASSERT(key != kUnsetThreadLocalKey);
int result = pthread_key_delete(key);
VALIDATE_PTHREAD_RESULT(result);
}
void OSThread::SetThreadLocal(ThreadLocalKey key, uword value) {
ASSERT(key != kUnsetThreadLocalKey);
int result = pthread_setspecific(key, reinterpret_cast<void*>(value));
VALIDATE_PTHREAD_RESULT(result);
}
intptr_t OSThread::GetMaxStackSize() {
const int kStackSize = (128 * kWordSize * KB);
return kStackSize;
}
#ifdef SUPPORT_TIMELINE
ThreadId OSThread::GetCurrentThreadTraceId() {
return GetCurrentThreadId();
}
#endif // SUPPORT_TIMELINE
char* OSThread::GetCurrentThreadName() {
const intptr_t kNameBufferSize = 16;
char* name = static_cast<char*>(malloc(kNameBufferSize));
prctl(PR_GET_NAME, name);
return name;
}
ThreadJoinId OSThread::GetCurrentThreadJoinId(OSThread* thread) {
ASSERT(thread != nullptr);
// Make sure we're filling in the join id for the current thread.
ASSERT(thread->id() == GetCurrentThreadId());
// Make sure the join_id_ hasn't been set, yet.
DEBUG_ASSERT(thread->join_id_ == kInvalidThreadJoinId);
pthread_t id = pthread_self();
#if defined(DEBUG)
thread->join_id_ = id;
#endif
return id;
}
void OSThread::Join(ThreadJoinId id) {
int result = pthread_join(id, nullptr);
VALIDATE_PTHREAD_RESULT(result);
}
void OSThread::Detach(ThreadJoinId id) {
int result = pthread_detach(id);
VALIDATE_PTHREAD_RESULT(result);
}
intptr_t OSThread::ThreadIdToIntPtr(ThreadId id) {
COMPILE_ASSERT(sizeof(id) <= sizeof(intptr_t));
return static_cast<intptr_t>(id);
}
ThreadId OSThread::ThreadIdFromIntPtr(intptr_t id) {
return static_cast<ThreadId>(id);
}
bool OSThread::GetCurrentStackBounds(uword* lower, uword* upper) {
pthread_attr_t attr;
if (pthread_getattr_np(pthread_self(), &attr) != 0) {
return false;
}
void* base;
size_t size;
int error = pthread_attr_getstack(&attr, &base, &size);
pthread_attr_destroy(&attr);
if (error != 0) {
return false;
}
*lower = reinterpret_cast<uword>(base);
*upper = *lower + size;
return true;
}
#if defined(USING_SAFE_STACK)
NO_SANITIZE_ADDRESS
NO_SANITIZE_SAFE_STACK
uword OSThread::GetCurrentSafestackPointer() {
#error "SAFE_STACK is unsupported on this platform"
return 0;
}
NO_SANITIZE_ADDRESS
NO_SANITIZE_SAFE_STACK
void OSThread::SetCurrentSafestackPointer(uword ssp) {
#error "SAFE_STACK is unsupported on this platform"
}
#endif
} // namespace dart
#endif // defined(DART_HOST_OS_ANDROID) && !defined(DART_USE_ABSL)