6#if defined(DART_USE_ABSL)
9#include <sys/resource.h>
20#define VALIDATE_PTHREAD_RESULT(result) \
22 const int kBufferSize = 1024; \
23 char error_buf[kBufferSize]; \
24 FATAL("pthread error: %d (%s)", result, \
25 Utils::StrError(result, error_buf, kBufferSize)); \
29#define RETURN_ON_PTHREAD_FAILURE(result) \
31 const int kBufferSize = 1024; \
32 char error_buf[kBufferSize]; \
33 fprintf(stderr, "%s:%d: pthread error: %d (%s)\n", __FILE__, __LINE__, \
34 result, Utils::StrError(result, error_buf, kBufferSize)); \
38#define RETURN_ON_PTHREAD_FAILURE(result) \
44class ThreadStartData {
46 ThreadStartData(
const char*
name,
49 : name_(
name), function_(
function), parameter_(parameter) {}
51 const char*
name()
const {
return name_; }
53 uword parameter()
const {
return parameter_; }
66static void* ThreadStart(
void* data_ptr) {
67 ThreadStartData*
data =
reinterpret_cast<ThreadStartData*
>(data_ptr);
74#if defined(DART_HOST_OS_ANDROID) || defined(DART_HOST_OS_LINUX)
77 char truncated_name[16];
78 snprintf(truncated_name,
sizeof(truncated_name),
"%s",
name);
79 pthread_setname_np(pthread_self(), truncated_name);
80#elif defined(DART_HOST_OS_MACOS)
82 pthread_setname_np(
name);
95 int result = pthread_attr_init(&attr);
96 RETURN_ON_PTHREAD_FAILURE(
result);
98 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
99 RETURN_ON_PTHREAD_FAILURE(
result);
102 RETURN_ON_PTHREAD_FAILURE(
result);
107 result = pthread_create(&tid, &attr, ThreadStart,
data);
108 RETURN_ON_PTHREAD_FAILURE(
result);
110 result = pthread_attr_destroy(&attr);
111 RETURN_ON_PTHREAD_FAILURE(
result);
124 return pthread_self();
128 return (pthread_equal(
a,
b) != 0);
135ABSL_NO_THREAD_SAFETY_ANALYSIS
137 data_.mutex()->Lock();
140ABSL_NO_THREAD_SAFETY_ANALYSIS
141bool Mutex::TryLock() {
142 if (!data_.mutex()->TryLock()) {
148ABSL_NO_THREAD_SAFETY_ANALYSIS
149void Mutex::Unlock() {
150 data_.mutex()->Unlock();
153Monitor::Monitor() : data_() {}
155Monitor::~Monitor() {}
157ABSL_NO_THREAD_SAFETY_ANALYSIS
158void Monitor::Enter() {
159 data_.mutex()->Lock();
162ABSL_NO_THREAD_SAFETY_ANALYSIS
163void Monitor::Exit() {
164 data_.mutex()->Unlock();
167Monitor::WaitResult Monitor::Wait(int64_t millis) {
171ABSL_NO_THREAD_SAFETY_ANALYSIS
172Monitor::WaitResult Monitor::WaitMicros(int64_t micros) {
173 Monitor::WaitResult retval = kNotified;
174 if (micros == kNoTimeout) {
176 data_.cond()->Wait(data_.mutex());
178 if (data_.cond()->WaitWithTimeout(data_.mutex(),
179 absl::Microseconds(micros))) {
186ABSL_NO_THREAD_SAFETY_ANALYSIS
187void Monitor::Notify() {
188 data_.cond()->Signal();
191ABSL_NO_THREAD_SAFETY_ANALYSIS
192void Monitor::NotifyAll() {
193 data_.cond()->SignalAll();
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()
constexpr uint64_t kMicrosecondsPerMillisecond
Dart_NativeFunction function
constexpr intptr_t kWordSize
static int8_t data[kExtLength]