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 RUNTIME_BIN_THREAD_H_ |
6 | #define RUNTIME_BIN_THREAD_H_ |
7 | |
8 | #include "platform/globals.h" |
9 | |
10 | namespace dart { |
11 | namespace bin { |
12 | class Thread; |
13 | class Mutex; |
14 | class Monitor; |
15 | } // namespace bin |
16 | } // namespace dart |
17 | |
18 | // Declare the OS-specific types ahead of defining the generic classes. |
19 | #if defined(DART_USE_ABSL) |
20 | #include "bin/thread_absl.h" |
21 | #elif defined(DART_HOST_OS_ANDROID) |
22 | #include "bin/thread_android.h" |
23 | #elif defined(DART_HOST_OS_FUCHSIA) |
24 | #include "bin/thread_fuchsia.h" |
25 | #elif defined(DART_HOST_OS_LINUX) |
26 | #include "bin/thread_linux.h" |
27 | #elif defined(DART_HOST_OS_MACOS) |
28 | #include "bin/thread_macos.h" |
29 | #elif defined(DART_HOST_OS_WINDOWS) |
30 | #include "bin/thread_win.h" |
31 | #else |
32 | #error Unknown target os. |
33 | #endif |
34 | |
35 | namespace dart { |
36 | namespace bin { |
37 | |
38 | class Thread { |
39 | public: |
40 | static const ThreadId kInvalidThreadId; |
41 | |
42 | typedef void (*ThreadStartFunction)(uword parameter); |
43 | |
44 | // Start a thread running the specified function. Returns 0 if the |
45 | // thread started successfully and a system specific error code if |
46 | // the thread failed to start. |
47 | static int Start(const char* name, |
48 | ThreadStartFunction function, |
49 | uword parameters); |
50 | |
51 | static intptr_t GetMaxStackSize(); |
52 | static ThreadId GetCurrentThreadId(); |
53 | static bool Compare(ThreadId a, ThreadId b); |
54 | |
55 | static void InitOnce(); |
56 | |
57 | private: |
58 | DISALLOW_ALLOCATION(); |
59 | DISALLOW_IMPLICIT_CONSTRUCTORS(Thread); |
60 | }; |
61 | |
62 | class Mutex { |
63 | public: |
64 | Mutex(); |
65 | ~Mutex(); |
66 | |
67 | void Lock(); |
68 | bool TryLock(); |
69 | void Unlock(); |
70 | |
71 | private: |
72 | MutexData data_; |
73 | |
74 | DISALLOW_COPY_AND_ASSIGN(Mutex); |
75 | }; |
76 | |
77 | class Monitor { |
78 | public: |
79 | enum WaitResult { kNotified, kTimedOut }; |
80 | |
81 | static constexpr int64_t kNoTimeout = 0; |
82 | |
83 | Monitor(); |
84 | ~Monitor(); |
85 | |
86 | void Enter(); |
87 | void Exit(); |
88 | |
89 | // Wait for notification or timeout. |
90 | WaitResult Wait(int64_t millis); |
91 | WaitResult WaitMicros(int64_t micros); |
92 | |
93 | // Notify waiting threads. |
94 | void Notify(); |
95 | void NotifyAll(); |
96 | |
97 | private: |
98 | MonitorData data_; // OS-specific data. |
99 | |
100 | DISALLOW_COPY_AND_ASSIGN(Monitor); |
101 | }; |
102 | |
103 | } // namespace bin |
104 | } // namespace dart |
105 | |
106 | #endif // RUNTIME_BIN_THREAD_H_ |
107 | |