6#if defined(DART_HOST_OS_MACOS) && !defined(DART_USE_ABSL)
11#include <mach/mach_host.h>
12#include <mach/mach_init.h>
13#include <mach/mach_port.h>
14#include <mach/mach_traps.h>
15#include <mach/task_info.h>
16#include <mach/thread_act.h>
17#include <mach/thread_info.h>
19#include <sys/sysctl.h>
28#define VALIDATE_PTHREAD_RESULT(result) \
30 const int kBufferSize = 1024; \
31 char error_message[kBufferSize]; \
32 Utils::StrError(result, error_message, kBufferSize); \
33 FATAL("pthread error: %d (%s)", result, error_message); \
37#define RETURN_ON_PTHREAD_FAILURE(result) \
39 const int kBufferSize = 1024; \
40 char error_message[kBufferSize]; \
41 Utils::StrError(result, error_message, kBufferSize); \
42 fprintf(stderr, "%s:%d: pthread error: %d (%s)\n", __FILE__, __LINE__, \
43 result, error_message); \
47#define RETURN_ON_PTHREAD_FAILURE(result) \
53class ThreadStartData {
55 ThreadStartData(
const char*
name,
58 : name_(
name), function_(
function), parameter_(parameter) {}
60 const char*
name()
const {
return name_; }
62 uword parameter()
const {
return parameter_; }
75static void* ThreadStart(
void* data_ptr) {
76 ThreadStartData*
data =
reinterpret_cast<ThreadStartData*
>(data_ptr);
86 char truncated_name[16];
87 snprintf(truncated_name,
sizeof(truncated_name),
"%s",
name);
88 pthread_setname_np(
name);
100 int result = pthread_attr_init(&attr);
101 RETURN_ON_PTHREAD_FAILURE(
result);
103 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
104 RETURN_ON_PTHREAD_FAILURE(
result);
107 RETURN_ON_PTHREAD_FAILURE(
result);
112 result = pthread_create(&tid, &attr, ThreadStart,
data);
113 RETURN_ON_PTHREAD_FAILURE(
result);
115 result = pthread_attr_destroy(&attr);
116 RETURN_ON_PTHREAD_FAILURE(
result);
129 return pthread_self();
133 return (pthread_equal(
a,
b) != 0);
137 pthread_mutexattr_t attr;
138 int result = pthread_mutexattr_init(&attr);
139 VALIDATE_PTHREAD_RESULT(
result);
142 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
143 VALIDATE_PTHREAD_RESULT(
result);
146 result = pthread_mutex_init(data_.mutex(), &attr);
148 VALIDATE_PTHREAD_RESULT(
result);
150 result = pthread_mutexattr_destroy(&attr);
151 VALIDATE_PTHREAD_RESULT(
result);
155 int result = pthread_mutex_destroy(data_.mutex());
157 VALIDATE_PTHREAD_RESULT(
result);
161 int result = pthread_mutex_lock(data_.mutex());
169 int result = pthread_mutex_trylock(data_.mutex());
181 int result = pthread_mutex_unlock(data_.mutex());
188 pthread_mutexattr_t attr;
189 int result = pthread_mutexattr_init(&attr);
190 VALIDATE_PTHREAD_RESULT(
result);
193 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
194 VALIDATE_PTHREAD_RESULT(
result);
197 result = pthread_mutex_init(data_.mutex(), &attr);
198 VALIDATE_PTHREAD_RESULT(
result);
200 result = pthread_mutexattr_destroy(&attr);
201 VALIDATE_PTHREAD_RESULT(
result);
203 result = pthread_cond_init(data_.cond(),
nullptr);
204 VALIDATE_PTHREAD_RESULT(
result);
208 int result = pthread_mutex_destroy(data_.mutex());
209 VALIDATE_PTHREAD_RESULT(
result);
211 result = pthread_cond_destroy(data_.cond());
212 VALIDATE_PTHREAD_RESULT(
result);
216 int result = pthread_mutex_lock(data_.mutex());
217 VALIDATE_PTHREAD_RESULT(
result);
223 int result = pthread_mutex_unlock(data_.mutex());
224 VALIDATE_PTHREAD_RESULT(
result);
236 int result = pthread_cond_wait(data_.cond(), data_.mutex());
237 VALIDATE_PTHREAD_RESULT(
result);
247 ts.tv_sec =
static_cast<int32_t
>(secs);
248 ts.tv_nsec =
static_cast<long>(nanos);
250 pthread_cond_timedwait_relative_np(data_.cond(), data_.mutex(), &ts);
252 if (
result == ETIMEDOUT) {
261 int result = pthread_cond_signal(data_.cond());
262 VALIDATE_PTHREAD_RESULT(
result);
267 int result = pthread_cond_broadcast(data_.cond());
268 VALIDATE_PTHREAD_RESULT(
result);
static constexpr int64_t kNoTimeout
WaitResult Wait(int64_t millis)
WaitResult WaitMicros(int64_t micros)
static const ThreadId kInvalidThreadId
void(* ThreadStartFunction)(uword parameter)
static intptr_t GetMaxStackSize()
static int Start(const char *name, ThreadStartFunction function, uword parameters)
static bool Compare(ThreadId a, ThreadId b)
static ThreadId GetCurrentThreadId()
Dart_NativeFunction function
constexpr intptr_t kMicrosecondsPerMillisecond
constexpr intptr_t kMicrosecondsPerSecond
constexpr intptr_t kNanosecondsPerMicrosecond
constexpr int32_t kMaxInt32
constexpr intptr_t kWordSize
static int8_t data[kExtLength]