blob: d6ae2b53481a231485383a50cc8504b857c0af3c [file] [log] [blame]
// 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.
#ifndef VM_OS_THREAD_H_
#define VM_OS_THREAD_H_
#include "platform/globals.h"
#include "vm/globals.h"
// Declare the OS-specific types ahead of defining the generic classes.
#if defined(TARGET_OS_ANDROID)
#include "vm/os_thread_android.h"
#elif defined(TARGET_OS_LINUX)
#include "vm/os_thread_linux.h"
#elif defined(TARGET_OS_MACOS)
#include "vm/os_thread_macos.h"
#elif defined(TARGET_OS_WINDOWS)
#include "vm/os_thread_win.h"
#error Unknown target os.
namespace dart {
// Low-level operations on OS platform threads.
// TODO(koda): Move to runtime/platform.
class OSThread {
static ThreadLocalKey kUnsetThreadLocalKey;
static ThreadId kInvalidThreadId;
static ThreadJoinId kInvalidThreadJoinId;
typedef void (*ThreadStartFunction) (uword parameter);
typedef void (*ThreadDestructor) (void* parameter);
// Start a thread running the specified function. Returns 0 if the
// thread started successfuly and a system specific error code if
// the thread failed to start.
static int Start(ThreadStartFunction function, uword parameters);
// NOTE: Destructor currently ignored on Windows (issue 23474).
static ThreadLocalKey CreateThreadLocal(ThreadDestructor destructor = NULL);
static void DeleteThreadLocal(ThreadLocalKey key);
static uword GetThreadLocal(ThreadLocalKey key) {
return ThreadInlineImpl::GetThreadLocal(key);
static void SetThreadLocal(ThreadLocalKey key, uword value);
static intptr_t GetMaxStackSize();
static ThreadId GetCurrentThreadId();
static ThreadId GetCurrentThreadTraceId();
static intptr_t CurrentCurrentThreadIdAsIntPtr() {
return ThreadIdToIntPtr(GetCurrentThreadId());
static ThreadJoinId GetCurrentThreadJoinId();
static void Join(ThreadJoinId id);
static intptr_t ThreadIdToIntPtr(ThreadId id);
static ThreadId ThreadIdFromIntPtr(intptr_t id);
static bool Compare(ThreadId a, ThreadId b);
static void GetThreadCpuUsage(ThreadId thread_id, int64_t* cpu_usage);
class Mutex {
void Lock();
bool TryLock();
void Unlock();
#if defined(DEBUG)
bool IsOwnedByCurrentThread() const {
return owner_ == OSThread::GetCurrentThreadId();
bool IsOwnedByCurrentThread() const {
return false;
MutexData data_;
#if defined(DEBUG)
ThreadId owner_;
#endif // defined(DEBUG)
class Monitor {
enum WaitResult {
static const int64_t kNoTimeout = 0;
void Enter();
void Exit();
// Wait for notification or timeout.
WaitResult Wait(int64_t millis);
WaitResult WaitMicros(int64_t micros);
// Notify waiting threads.
void Notify();
void NotifyAll();
MonitorData data_; // OS-specific data.
} // namespace dart
#endif // VM_OS_THREAD_H_