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