6#if defined(DART_HOST_OS_FUCHSIA) && !defined(DART_USE_ABSL)
13#include <zircon/status.h>
14#include <zircon/syscalls.h>
15#include <zircon/threads.h>
16#include <zircon/types.h>
24#define VALIDATE_PTHREAD_RESULT(result) \
26 const int kBufferSize = 1024; \
27 char error_buf[kBufferSize]; \
28 FATAL("pthread error: %d (%s)", result, \
29 Utils::StrError(result, error_buf, kBufferSize)); \
33#define RETURN_ON_PTHREAD_FAILURE(result) \
35 const int kBufferSize = 1024; \
36 char error_buf[kBufferSize]; \
37 fprintf(stderr, "%s:%d: pthread error: %d (%s)\n", __FILE__, __LINE__, \
38 result, Utils::StrError(result, error_buf, kBufferSize)); \
42#define RETURN_ON_PTHREAD_FAILURE(result) \
48static void ComputeTimeSpecMicros(
struct timespec* ts, int64_t micros) {
52 int result = clock_gettime(CLOCK_MONOTONIC, ts);
62class ThreadStartData {
64 ThreadStartData(
const char*
name,
67 : name_(
name), function_(
function), parameter_(parameter) {}
69 const char*
name()
const {
return name_; }
71 uword parameter()
const {
return parameter_; }
84static void* ThreadStart(
void* data_ptr) {
85 ThreadStartData*
data =
reinterpret_cast<ThreadStartData*
>(data_ptr);
93 char truncated_name[ZX_MAX_NAME_LEN];
94 snprintf(truncated_name, ZX_MAX_NAME_LEN,
"%s",
name);
95 zx_handle_t thread_handle = thrd_get_zx_handle(thrd_current());
96 zx_object_set_property(thread_handle, ZX_PROP_NAME, truncated_name,
109 int result = pthread_attr_init(&attr);
110 RETURN_ON_PTHREAD_FAILURE(
result);
112 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
113 RETURN_ON_PTHREAD_FAILURE(
result);
116 RETURN_ON_PTHREAD_FAILURE(
result);
121 result = pthread_create(&tid, &attr, ThreadStart,
data);
122 RETURN_ON_PTHREAD_FAILURE(
result);
124 result = pthread_attr_destroy(&attr);
125 RETURN_ON_PTHREAD_FAILURE(
result);
138 return pthread_self();
142 return (pthread_equal(
a,
b) != 0);
146 pthread_mutexattr_t attr;
147 int result = pthread_mutexattr_init(&attr);
148 VALIDATE_PTHREAD_RESULT(
result);
151 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
152 VALIDATE_PTHREAD_RESULT(
result);
155 result = pthread_mutex_init(data_.mutex(), &attr);
157 VALIDATE_PTHREAD_RESULT(
result);
159 result = pthread_mutexattr_destroy(&attr);
160 VALIDATE_PTHREAD_RESULT(
result);
164 int result = pthread_mutex_destroy(data_.mutex());
166 VALIDATE_PTHREAD_RESULT(
result);
170 int result = pthread_mutex_lock(data_.mutex());
178 int result = pthread_mutex_trylock(data_.mutex());
190 int result = pthread_mutex_unlock(data_.mutex());
197 pthread_mutexattr_t mutex_attr;
198 int result = pthread_mutexattr_init(&mutex_attr);
199 VALIDATE_PTHREAD_RESULT(
result);
202 result = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK);
203 VALIDATE_PTHREAD_RESULT(
result);
206 result = pthread_mutex_init(data_.mutex(), &mutex_attr);
207 VALIDATE_PTHREAD_RESULT(
result);
209 result = pthread_mutexattr_destroy(&mutex_attr);
210 VALIDATE_PTHREAD_RESULT(
result);
212 pthread_condattr_t cond_attr;
213 result = pthread_condattr_init(&cond_attr);
214 VALIDATE_PTHREAD_RESULT(
result);
216 result = pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC);
217 VALIDATE_PTHREAD_RESULT(
result);
219 result = pthread_cond_init(data_.cond(), &cond_attr);
220 VALIDATE_PTHREAD_RESULT(
result);
222 result = pthread_condattr_destroy(&cond_attr);
223 VALIDATE_PTHREAD_RESULT(
result);
227 int result = pthread_mutex_destroy(data_.mutex());
228 VALIDATE_PTHREAD_RESULT(
result);
230 result = pthread_cond_destroy(data_.cond());
231 VALIDATE_PTHREAD_RESULT(
result);
235 int result = pthread_mutex_lock(data_.mutex());
236 VALIDATE_PTHREAD_RESULT(
result);
242 int result = pthread_mutex_unlock(data_.mutex());
243 VALIDATE_PTHREAD_RESULT(
result);
255 int result = pthread_cond_wait(data_.cond(), data_.mutex());
256 VALIDATE_PTHREAD_RESULT(
result);
259 ComputeTimeSpecMicros(&ts, micros);
260 int result = pthread_cond_timedwait(data_.cond(), data_.mutex(), &ts);
262 if (
result == ETIMEDOUT) {
271 int result = pthread_cond_signal(data_.cond());
272 VALIDATE_PTHREAD_RESULT(
result);
277 int result = pthread_cond_broadcast(data_.cond());
278 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 intptr_t kNanosecondsPerSecond
constexpr intptr_t kWordSize
static int8_t data[kExtLength]