OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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_INTERRUPTER_H_ | 5 #ifndef VM_THREAD_INTERRUPTER_H_ |
6 #define VM_THREAD_INTERRUPTER_H_ | 6 #define VM_THREAD_INTERRUPTER_H_ |
7 | 7 |
8 #include "vm/allocation.h" | 8 #include "vm/allocation.h" |
9 #include "vm/signal_handler.h" | 9 #include "vm/signal_handler.h" |
10 #include "vm/os_thread.h" | 10 #include "vm/os_thread.h" |
11 | 11 #include "vm/thread.h" |
12 | 12 |
13 namespace dart { | 13 namespace dart { |
14 | 14 |
15 struct InterruptedThreadState { | |
16 ThreadId tid; | |
17 uintptr_t pc; | |
18 uintptr_t csp; | |
19 uintptr_t dsp; | |
20 uintptr_t fp; | |
21 uintptr_t lr; | |
22 }; | |
23 | |
24 // When a thread is interrupted the thread specific interrupt callback will be | |
25 // invoked. Each callback is given an InterruptedThreadState and the user data | |
26 // pointer. When inside a thread interrupt callback doing any of the following | |
27 // is forbidden: | |
28 // * Accessing TLS. | |
29 // * Allocating memory. | |
30 // * Taking a lock. | |
31 typedef void (*ThreadInterruptCallback)(const InterruptedThreadState& state, | |
32 void* data); | |
33 | |
34 // State stored per registered thread. | |
35 class InterruptableThreadState { | |
36 public: | |
37 ThreadId id; | |
38 ThreadInterruptCallback callback; | |
39 void* data; | |
40 }; | |
41 | |
42 class ThreadInterrupter : public AllStatic { | 15 class ThreadInterrupter : public AllStatic { |
43 public: | 16 public: |
44 static void InitOnce(); | 17 static void InitOnce(); |
45 | 18 |
46 static void Startup(); | 19 static void Startup(); |
47 static void Shutdown(); | 20 static void Shutdown(); |
48 | 21 |
49 // Delay between interrupts. | 22 // Delay between interrupts. |
50 static void SetInterruptPeriod(intptr_t period); | 23 static void SetInterruptPeriod(intptr_t period); |
51 | 24 |
52 // Wake up the thread interrupter thread. | 25 // Wake up the thread interrupter thread. |
53 static void WakeUp(); | 26 static void WakeUp(); |
54 | 27 |
55 // Register the currently running thread for interrupts. If the current thread | 28 // Register the currently running thread for interrupts. If the current thread |
56 // is already registered, callback and data will be updated. | 29 // is already registered, callback and data will be updated. |
57 static InterruptableThreadState* Register(ThreadInterruptCallback callback, | 30 static void Register(ThreadInterruptCallback callback, void* data); |
58 void* data); | 31 |
59 // Unregister the currently running thread for interrupts. | 32 // Unregister the currently running thread for interrupts. |
60 static void Unregister(); | 33 static void Unregister(); |
61 | 34 |
62 // Get the current thread state. Will create a thread state if one hasn't | |
63 // been allocated. | |
64 static InterruptableThreadState* GetCurrentThreadState(); | |
65 // Get the current thread state. Will not create one if one doesn't exist. | |
66 static InterruptableThreadState* CurrentThreadState(); | |
67 | |
68 // Interrupt a thread. | 35 // Interrupt a thread. |
69 static void InterruptThread(InterruptableThreadState* thread_state); | 36 static void InterruptThread(Thread* thread); |
70 | 37 |
71 private: | 38 private: |
72 static const intptr_t kMaxThreads = 4096; | 39 static const intptr_t kMaxThreads = 4096; |
73 static bool initialized_; | 40 static bool initialized_; |
74 static bool shutdown_; | 41 static bool shutdown_; |
75 static bool thread_running_; | 42 static bool thread_running_; |
76 static ThreadId interrupter_thread_id_; | 43 static ThreadId interrupter_thread_id_; |
77 static Monitor* monitor_; | 44 static Monitor* monitor_; |
78 static intptr_t interrupt_period_; | 45 static intptr_t interrupt_period_; |
79 static intptr_t current_wait_time_; | 46 static intptr_t current_wait_time_; |
80 | 47 |
81 static bool InDeepSleep() { | 48 static bool InDeepSleep() { |
82 return current_wait_time_ == Monitor::kNoTimeout; | 49 return current_wait_time_ == Monitor::kNoTimeout; |
83 } | 50 } |
84 | 51 |
85 static InterruptableThreadState* _EnsureThreadStateCreated(); | |
86 static void UpdateStateObject(ThreadInterruptCallback callback, void* data); | 52 static void UpdateStateObject(ThreadInterruptCallback callback, void* data); |
87 | 53 |
88 static void SetCurrentThreadState(InterruptableThreadState* state); | |
89 | |
90 static void ThreadMain(uword parameters); | 54 static void ThreadMain(uword parameters); |
91 | 55 |
92 static void InstallSignalHandler(); | 56 static void InstallSignalHandler(); |
93 | 57 |
94 static void RemoveSignalHandler(); | 58 static void RemoveSignalHandler(); |
95 | 59 |
96 friend class ThreadInterrupterVisitIsolates; | 60 friend class ThreadInterrupterVisitIsolates; |
97 }; | 61 }; |
98 | 62 |
99 void ThreadInterruptNoOp(const InterruptedThreadState& state, void* data); | 63 void ThreadInterruptNoOp(const InterruptedThreadState& state, void* data); |
100 | 64 |
101 } // namespace dart | 65 } // namespace dart |
102 | 66 |
103 #endif // VM_THREAD_INTERRUPTER_H_ | 67 #endif // VM_THREAD_INTERRUPTER_H_ |
OLD | NEW |