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 VM_THREAD_H_ | 5 #ifndef VM_THREAD_H_ |
6 #define VM_THREAD_H_ | 6 #define VM_THREAD_H_ |
7 | 7 |
8 #include "platform/assert.h" | 8 #include "platform/assert.h" |
| 9 #include "platform/thread.h" |
9 #include "vm/allocation.h" | 10 #include "vm/allocation.h" |
10 #include "vm/globals.h" | 11 #include "vm/globals.h" |
11 #include "vm/isolate.h" | 12 #include "vm/isolate.h" |
12 | 13 |
13 // Declare the OS-specific types ahead of defining the generic classes. | |
14 #if defined(TARGET_OS_LINUX) | |
15 #include "vm/thread_linux.h" | |
16 #elif defined(TARGET_OS_MACOS) | |
17 #include "vm/thread_macos.h" | |
18 #elif defined(TARGET_OS_WINDOWS) | |
19 #include "vm/thread_win.h" | |
20 #else | |
21 #error Unknown target os. | |
22 #endif | |
23 | |
24 namespace dart { | 14 namespace dart { |
25 | 15 |
26 class Thread { | |
27 public: | |
28 // Function to be called on thread start. | |
29 typedef void (*ThreadStartFunction) (uword parameter); | |
30 | |
31 // TODO(iposva): Define the proper interface for spawning and killing threads. | |
32 Thread(ThreadStartFunction function, uword parameters); | |
33 ~Thread(); | |
34 | |
35 private: | |
36 ThreadData data_; | |
37 | |
38 DISALLOW_COPY_AND_ASSIGN(Thread); | |
39 }; | |
40 | |
41 | |
42 class Mutex { | |
43 public: | |
44 Mutex(); | |
45 ~Mutex(); | |
46 | |
47 void Lock(); | |
48 bool TryLock(); | |
49 void Unlock(); | |
50 | |
51 private: | |
52 MutexData data_; | |
53 | |
54 DISALLOW_COPY_AND_ASSIGN(Mutex); | |
55 }; | |
56 | |
57 | |
58 class Monitor { | |
59 public: | |
60 enum WaitResult { | |
61 kNotified, | |
62 kTimedOut | |
63 }; | |
64 | |
65 static const int64_t kNoTimeout = 0; | |
66 | |
67 Monitor(); | |
68 ~Monitor(); | |
69 | |
70 void Enter(); | |
71 void Exit(); | |
72 | |
73 // Wait for notification or timeout. | |
74 WaitResult Wait(int64_t millis); | |
75 | |
76 // Notify waiting threads. | |
77 void Notify(); | |
78 void NotifyAll(); | |
79 | |
80 private: | |
81 MonitorData data_; // OS-specific data. | |
82 | |
83 DISALLOW_COPY_AND_ASSIGN(Monitor); | |
84 }; | |
85 | |
86 | |
87 class MutexLocker : public StackResource { | 16 class MutexLocker : public StackResource { |
88 public: | 17 public: |
89 explicit MutexLocker(Mutex* mutex) : | 18 explicit MutexLocker(Mutex* mutex) : |
90 StackResource(Isolate::Current()), | 19 StackResource(Isolate::Current()), |
91 mutex_(mutex) { | 20 mutex_(mutex) { |
92 ASSERT(mutex != NULL); | 21 ASSERT(mutex != NULL); |
93 // TODO(iposva): Consider adding a no GC scope here. | 22 // TODO(iposva): Consider adding a no GC scope here. |
94 mutex_->Lock(); | 23 mutex_->Lock(); |
95 } | 24 } |
96 | 25 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 private: | 65 private: |
137 Monitor* const monitor_; | 66 Monitor* const monitor_; |
138 | 67 |
139 DISALLOW_COPY_AND_ASSIGN(MonitorLocker); | 68 DISALLOW_COPY_AND_ASSIGN(MonitorLocker); |
140 }; | 69 }; |
141 | 70 |
142 } // namespace dart | 71 } // namespace dart |
143 | 72 |
144 | 73 |
145 #endif // VM_THREAD_H_ | 74 #endif // VM_THREAD_H_ |
OLD | NEW |