6#if (defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_ANDROID)) && \
7 !defined(DART_USE_ABSL)
13#include <sys/resource.h>
22#define VALIDATE_PTHREAD_RESULT(result) \
24 const int kBufferSize = 1024; \
25 char error_buf[kBufferSize]; \
26 FATAL("pthread error: %d (%s)", result, \
27 Utils::StrError(result, error_buf, kBufferSize)); \
31#define RETURN_ON_PTHREAD_FAILURE(result) \
33 const int kBufferSize = 1024; \
34 char error_buf[kBufferSize]; \
35 fprintf(stderr, "%s:%d: pthread error: %d (%s)\n", __FILE__, __LINE__, \
36 result, Utils::StrError(result, error_buf, kBufferSize)); \
40#define RETURN_ON_PTHREAD_FAILURE(result) \
46static void ComputeTimeSpecMicros(
struct timespec* ts, int64_t micros) {
50 int result = clock_gettime(CLOCK_MONOTONIC, ts);
60class ThreadStartData {
62 ThreadStartData(
const char*
name,
65 : name_(
name), function_(
function), parameter_(parameter) {}
67 const char*
name()
const {
return name_; }
69 uword parameter()
const {
return parameter_; }
82static void* ThreadStart(
void* data_ptr) {
83 ThreadStartData*
data =
reinterpret_cast<ThreadStartData*
>(data_ptr);
92 char truncated_name[16];
93 snprintf(truncated_name,
sizeof(truncated_name),
"%s",
name);
94 pthread_setname_np(pthread_self(), truncated_name);
106 int result = pthread_attr_init(&attr);
107 RETURN_ON_PTHREAD_FAILURE(
result);
109 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
110 RETURN_ON_PTHREAD_FAILURE(
result);
113 RETURN_ON_PTHREAD_FAILURE(
result);
118 result = pthread_create(&tid, &attr, ThreadStart,
data);
119 RETURN_ON_PTHREAD_FAILURE(
result);
121 result = pthread_attr_destroy(&attr);
122 RETURN_ON_PTHREAD_FAILURE(
result);
135 return pthread_self();
139 return (pthread_equal(
a,
b) != 0);
143 pthread_mutexattr_t attr;
144 int result = pthread_mutexattr_init(&attr);
145 VALIDATE_PTHREAD_RESULT(
result);
148 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
149 VALIDATE_PTHREAD_RESULT(
result);
152 result = pthread_mutex_init(data_.mutex(), &attr);
154 VALIDATE_PTHREAD_RESULT(
result);
156 result = pthread_mutexattr_destroy(&attr);
157 VALIDATE_PTHREAD_RESULT(
result);
161 int result = pthread_mutex_destroy(data_.mutex());
163 VALIDATE_PTHREAD_RESULT(
result);
167 int result = pthread_mutex_lock(data_.mutex());
175 int result = pthread_mutex_trylock(data_.mutex());
187 int result = pthread_mutex_unlock(data_.mutex());
194 pthread_mutexattr_t mutex_attr;
195 int result = pthread_mutexattr_init(&mutex_attr);
196 VALIDATE_PTHREAD_RESULT(
result);
199 result = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK);
200 VALIDATE_PTHREAD_RESULT(
result);
203 result = pthread_mutex_init(data_.mutex(), &mutex_attr);
204 VALIDATE_PTHREAD_RESULT(
result);
206 result = pthread_mutexattr_destroy(&mutex_attr);
207 VALIDATE_PTHREAD_RESULT(
result);
209 pthread_condattr_t cond_attr;
210 result = pthread_condattr_init(&cond_attr);
211 VALIDATE_PTHREAD_RESULT(
result);
213 result = pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC);
214 VALIDATE_PTHREAD_RESULT(
result);
216 result = pthread_cond_init(data_.cond(), &cond_attr);
217 VALIDATE_PTHREAD_RESULT(
result);
219 result = pthread_condattr_destroy(&cond_attr);
220 VALIDATE_PTHREAD_RESULT(
result);
224 int result = pthread_mutex_destroy(data_.mutex());
225 VALIDATE_PTHREAD_RESULT(
result);
227 result = pthread_cond_destroy(data_.cond());
228 VALIDATE_PTHREAD_RESULT(
result);
232 int result = pthread_mutex_lock(data_.mutex());
233 VALIDATE_PTHREAD_RESULT(
result);
239 int result = pthread_mutex_unlock(data_.mutex());
240 VALIDATE_PTHREAD_RESULT(
result);
252 int result = pthread_cond_wait(data_.cond(), data_.mutex());
253 VALIDATE_PTHREAD_RESULT(
result);
256 ComputeTimeSpecMicros(&ts, micros);
257 int result = pthread_cond_timedwait(data_.cond(), data_.mutex(), &ts);
259 if (
result == ETIMEDOUT) {
268 int result = pthread_cond_signal(data_.cond());
269 VALIDATE_PTHREAD_RESULT(
result);
274 int result = pthread_cond_broadcast(data_.cond());
275 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]