Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #include "platform/globals.h" | |
| 6 #if defined(TARGET_OS_LINUX) | |
| 7 | |
| 8 #include "vm/signal_handler.h" | |
| 9 #include "vm/thread_interrupter.h" | |
| 10 | |
| 11 namespace dart { | |
| 12 | |
| 13 DECLARE_FLAG(bool, thread_interrupter); | |
| 14 DECLARE_FLAG(bool, trace_thread_interrupter); | |
| 15 | |
| 16 class ThreadInterrupterLinux : public AllStatic { | |
| 17 public: | |
| 18 static void ThreadInterruptSignalHandler(int signal, siginfo_t* info, | |
| 19 void* context_) { | |
| 20 if (signal != SIGPROF) { | |
| 21 return; | |
| 22 } | |
| 23 ThreadInterrupter::ThreadState* state = | |
| 24 ThreadInterrupter::CurrentThreadState(); | |
| 25 if ((state == NULL) || (state->callback == NULL)) { | |
| 26 // No interrupter state or callback. | |
| 27 return; | |
| 28 } | |
| 29 ASSERT(Thread::Compare(state->id, Thread::GetCurrentThreadId())); | |
| 30 // Extract thread state. | |
| 31 ucontext_t* context = reinterpret_cast<ucontext_t*>(context_); | |
| 32 mcontext_t mcontext = context->uc_mcontext; | |
| 33 InterruptedThreadState its; | |
| 34 its.tid = state->id; | |
| 35 its.pc = SignalHandler::GetProgramCounter(mcontext); | |
| 36 its.fp = SignalHandler::GetFramePointer(mcontext); | |
| 37 its.sp = SignalHandler::GetStackPointer(mcontext); | |
| 38 state->callback(its, state->data); | |
| 39 } | |
| 40 }; | |
| 41 | |
| 42 | |
| 43 void ThreadInterrupter::InterruptThreads(int64_t current_time) { | |
| 44 for (intptr_t i = 0; i < threads_size_; i++) { | |
| 45 ThreadState* state = threads_[i]; | |
| 46 ASSERT(state->id != Thread::kInvalidThreadId); | |
| 47 if (FLAG_trace_thread_interrupter) { | |
| 48 OS::Print("ThreadInterrupter interrupting %p\n", | |
| 49 reinterpret_cast<void*>(state->id)); | |
| 50 } | |
| 51 pthread_kill(state->id, SIGPROF); | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 void ThreadInterrupter::ThreadMain(uword parameters) { | |
| 56 ASSERT(FLAG_thread_interrupter); | |
| 57 ASSERT(initialized_); | |
| 58 SignalHandler::Install(ThreadInterrupterLinux::ThreadInterruptSignalHandler); | |
|
siva
2013/12/11 02:52:21
The ThreadMain function in the 4 versions seem ide
Cutch
2013/12/11 17:44:56
Done.
| |
| 59 if (FLAG_trace_thread_interrupter) { | |
| 60 OS::Print("ThreadInterrupter Linux ready.\n"); | |
| 61 } | |
| 62 { | |
| 63 // Signal to main thread we are ready. | |
| 64 MonitorLocker startup_ml(start_stop_monitor_); | |
| 65 thread_running_ = true; | |
| 66 interrupter_thread_id_ = Thread::GetCurrentThreadId(); | |
| 67 startup_ml.Notify(); | |
| 68 } | |
| 69 MonitorLocker ml(monitor_); | |
| 70 while (!shutdown_) { | |
| 71 int64_t current_time = OS::GetCurrentTimeMicros(); | |
| 72 InterruptThreads(current_time); | |
| 73 ml.WaitMicros(interrupt_period_); | |
| 74 } | |
| 75 if (FLAG_trace_thread_interrupter) { | |
| 76 OS::Print("ThreadInterrupter Linux exiting.\n"); | |
| 77 } | |
| 78 { | |
| 79 // Signal to main thread we are exiting. | |
| 80 MonitorLocker shutdown_ml(start_stop_monitor_); | |
| 81 thread_running_ = false; | |
| 82 shutdown_ml.Notify(); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 | |
| 87 } // namespace dart | |
| 88 | |
| 89 #endif // defined(TARGET_OS_LINUX) | |
| OLD | NEW |