| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef PLATFORM_THREAD_H_ | 5 #ifndef PLATFORM_THREAD_H_ |
| 6 #define PLATFORM_THREAD_H_ | 6 #define PLATFORM_THREAD_H_ |
| 7 | 7 |
| 8 #include "platform/globals.h" | 8 #include "platform/globals.h" |
| 9 | 9 |
| 10 // Declare the OS-specific types ahead of defining the generic classes. | 10 // Declare the OS-specific types ahead of defining the generic classes. |
| 11 #if defined(TARGET_OS_ANDROID) | 11 #if defined(TARGET_OS_ANDROID) |
| 12 #include "platform/thread_android.h" | 12 #include "platform/thread_android.h" |
| 13 #elif defined(TARGET_OS_LINUX) | 13 #elif defined(TARGET_OS_LINUX) |
| 14 #include "platform/thread_linux.h" | 14 #include "platform/thread_linux.h" |
| 15 #elif defined(TARGET_OS_MACOS) | 15 #elif defined(TARGET_OS_MACOS) |
| 16 #include "platform/thread_macos.h" | 16 #include "platform/thread_macos.h" |
| 17 #elif defined(TARGET_OS_WINDOWS) | 17 #elif defined(TARGET_OS_WINDOWS) |
| 18 #include "platform/thread_win.h" | 18 #include "platform/thread_win.h" |
| 19 #else | 19 #else |
| 20 #error Unknown target os. | 20 #error Unknown target os. |
| 21 #endif | 21 #endif |
| 22 | 22 |
| 23 namespace dart { | 23 namespace dart { |
| 24 | 24 |
| 25 class Thread { | 25 class Thread { |
| 26 public: | 26 public: |
| 27 static ThreadLocalKey kUnsetThreadLocalKey; | 27 static ThreadLocalKey kUnsetThreadLocalKey; |
| 28 static ThreadId kInvalidThreadId; |
| 28 | 29 |
| 29 typedef void (*ThreadStartFunction) (uword parameter); | 30 typedef void (*ThreadStartFunction) (uword parameter); |
| 30 | 31 |
| 31 // Start a thread running the specified function. Returns 0 if the | 32 // Start a thread running the specified function. Returns 0 if the |
| 32 // thread started successfuly and a system specific error code if | 33 // thread started successfuly and a system specific error code if |
| 33 // the thread failed to start. | 34 // the thread failed to start. |
| 34 static int Start(ThreadStartFunction function, uword parameters); | 35 static int Start(ThreadStartFunction function, uword parameters); |
| 35 | 36 |
| 36 static ThreadLocalKey CreateThreadLocal(); | 37 static ThreadLocalKey CreateThreadLocal(); |
| 37 static void DeleteThreadLocal(ThreadLocalKey key); | 38 static void DeleteThreadLocal(ThreadLocalKey key); |
| 38 static uword GetThreadLocal(ThreadLocalKey key) { | 39 static uword GetThreadLocal(ThreadLocalKey key) { |
| 39 return ThreadInlineImpl::GetThreadLocal(key); | 40 return ThreadInlineImpl::GetThreadLocal(key); |
| 40 } | 41 } |
| 41 static void SetThreadLocal(ThreadLocalKey key, uword value); | 42 static void SetThreadLocal(ThreadLocalKey key, uword value); |
| 42 static intptr_t GetMaxStackSize(); | 43 static intptr_t GetMaxStackSize(); |
| 43 static ThreadId GetCurrentThreadId(); | 44 static ThreadId GetCurrentThreadId(); |
| 45 static intptr_t ThreadIdToIntPtr(ThreadId id); |
| 46 static bool Compare(ThreadId a, ThreadId b); |
| 44 static void GetThreadCpuUsage(ThreadId thread_id, int64_t* cpu_usage); | 47 static void GetThreadCpuUsage(ThreadId thread_id, int64_t* cpu_usage); |
| 45 }; | 48 }; |
| 46 | 49 |
| 47 | 50 |
| 48 class Mutex { | 51 class Mutex { |
| 49 public: | 52 public: |
| 50 Mutex(); | 53 Mutex(); |
| 51 ~Mutex(); | 54 ~Mutex(); |
| 52 | 55 |
| 53 void Lock(); | 56 void Lock(); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 84 void Notify(); | 87 void Notify(); |
| 85 void NotifyAll(); | 88 void NotifyAll(); |
| 86 | 89 |
| 87 private: | 90 private: |
| 88 MonitorData data_; // OS-specific data. | 91 MonitorData data_; // OS-specific data. |
| 89 | 92 |
| 90 DISALLOW_COPY_AND_ASSIGN(Monitor); | 93 DISALLOW_COPY_AND_ASSIGN(Monitor); |
| 91 }; | 94 }; |
| 92 | 95 |
| 93 | 96 |
| 94 class ScopedMutex { | |
| 95 public: | |
| 96 explicit ScopedMutex(Mutex* mutex) : mutex_(mutex) { | |
| 97 ASSERT(mutex_ != NULL); | |
| 98 mutex_->Lock(); | |
| 99 } | |
| 100 | |
| 101 ~ScopedMutex() { | |
| 102 mutex_->Unlock(); | |
| 103 } | |
| 104 | |
| 105 private: | |
| 106 Mutex* const mutex_; | |
| 107 | |
| 108 DISALLOW_COPY_AND_ASSIGN(ScopedMutex); | |
| 109 }; | |
| 110 | |
| 111 | |
| 112 class ScopedMonitor { | |
| 113 public: | |
| 114 explicit ScopedMonitor(Monitor* monitor) : monitor_(monitor) { | |
| 115 ASSERT(monitor_ != NULL); | |
| 116 monitor_->Enter(); | |
| 117 } | |
| 118 | |
| 119 ~ScopedMonitor() { | |
| 120 monitor_->Exit(); | |
| 121 } | |
| 122 | |
| 123 Monitor::WaitResult Wait(int64_t millis = dart::Monitor::kNoTimeout) { | |
| 124 return monitor_->Wait(millis); | |
| 125 } | |
| 126 | |
| 127 Monitor::WaitResult WaitMicros(int64_t micros = dart::Monitor::kNoTimeout) { | |
| 128 return monitor_->WaitMicros(micros); | |
| 129 } | |
| 130 | |
| 131 void Notify() { | |
| 132 monitor_->Notify(); | |
| 133 } | |
| 134 | |
| 135 void NotifyAll() { | |
| 136 monitor_->NotifyAll(); | |
| 137 } | |
| 138 | |
| 139 private: | |
| 140 Monitor* const monitor_; | |
| 141 | |
| 142 DISALLOW_COPY_AND_ASSIGN(ScopedMonitor); | |
| 143 }; | |
| 144 | |
| 145 | |
| 146 } // namespace dart | 97 } // namespace dart |
| 147 | 98 |
| 148 | 99 |
| 149 #endif // PLATFORM_THREAD_H_ | 100 #endif // PLATFORM_THREAD_H_ |
| OLD | NEW |