OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #ifndef VM_THREAD_H_ | |
6 #define VM_THREAD_H_ | |
7 | |
8 #include "platform/globals.h" | |
9 | |
10 // Declare the OS-specific types ahead of defining the generic classes. | |
11 #if defined(TARGET_OS_ANDROID) | |
12 #include "vm/thread_android.h" | |
13 #elif defined(TARGET_OS_LINUX) | |
14 #include "vm/thread_linux.h" | |
15 #elif defined(TARGET_OS_MACOS) | |
16 #include "vm/thread_macos.h" | |
17 #elif defined(TARGET_OS_WINDOWS) | |
18 #include "vm/thread_win.h" | |
19 #else | |
20 #error Unknown target os. | |
21 #endif | |
22 | |
23 namespace dart { | |
24 | |
25 class Isolate; | |
26 | |
27 class Thread { | |
28 public: | |
29 static ThreadLocalKey kUnsetThreadLocalKey; | |
30 static ThreadId kInvalidThreadId; | |
31 | |
32 typedef void (*ThreadStartFunction) (uword parameter); | |
33 | |
34 // Start a thread running the specified function. Returns 0 if the | |
35 // thread started successfuly and a system specific error code if | |
36 // the thread failed to start. | |
37 static int Start(ThreadStartFunction function, uword parameters); | |
38 | |
39 static ThreadLocalKey CreateThreadLocal(); | |
40 static void DeleteThreadLocal(ThreadLocalKey key); | |
41 static uword GetThreadLocal(ThreadLocalKey key) { | |
42 return ThreadInlineImpl::GetThreadLocal(key); | |
43 } | |
44 static void SetThreadLocal(ThreadLocalKey key, uword value); | |
45 static intptr_t GetMaxStackSize(); | |
46 static ThreadId GetCurrentThreadId(); | |
47 static bool Join(ThreadId id); | |
48 static intptr_t ThreadIdToIntPtr(ThreadId id); | |
49 static bool Compare(ThreadId a, ThreadId b); | |
50 static void GetThreadCpuUsage(ThreadId thread_id, int64_t* cpu_usage); | |
51 }; | |
52 | |
53 | |
54 class Mutex { | |
55 public: | |
56 Mutex(); | |
57 ~Mutex(); | |
58 | |
59 void Lock(); | |
60 bool TryLock(); | |
61 void Unlock(); | |
62 | |
63 #if defined(DEBUG) | |
64 Isolate* Owner() const { return owner_; } | |
65 #endif // defined(DEBUG) | |
66 | |
67 private: | |
68 MutexData data_; | |
69 #if defined(DEBUG) | |
70 Isolate* owner_; | |
71 #endif // defined(DEBUG) | |
72 | |
73 DISALLOW_COPY_AND_ASSIGN(Mutex); | |
74 }; | |
75 | |
76 | |
77 class Monitor { | |
78 public: | |
79 enum WaitResult { | |
80 kNotified, | |
81 kTimedOut | |
82 }; | |
83 | |
84 static const int64_t kNoTimeout = 0; | |
85 | |
86 Monitor(); | |
87 ~Monitor(); | |
88 | |
89 void Enter(); | |
90 void Exit(); | |
91 | |
92 // Wait for notification or timeout. | |
93 WaitResult Wait(int64_t millis); | |
94 WaitResult WaitMicros(int64_t micros); | |
95 | |
96 // Notify waiting threads. | |
97 void Notify(); | |
98 void NotifyAll(); | |
99 | |
100 private: | |
101 MonitorData data_; // OS-specific data. | |
102 | |
103 DISALLOW_COPY_AND_ASSIGN(Monitor); | |
104 }; | |
105 | |
106 | |
107 } // namespace dart | |
108 | |
109 | |
110 #endif // VM_THREAD_H_ | |
OLD | NEW |