OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 #include "platform/assert.h" | 5 #include "platform/assert.h" |
6 #include "vm/lockers.h" | 6 #include "vm/lockers.h" |
7 #include "vm/safepoint.h" | 7 #include "vm/safepoint.h" |
8 | 8 |
9 namespace dart { | 9 namespace dart { |
10 | 10 |
11 | 11 |
| 12 static void updateThreadState(Thread* thread) { |
| 13 // First try a fast update of the thread state to indicate it is not at a |
| 14 // safepoint anymore. |
| 15 uword old_state = Thread::SetAtSafepoint(true, 0); |
| 16 uword addr = |
| 17 reinterpret_cast<uword>(thread) + Thread::safepoint_state_offset(); |
| 18 if (AtomicOperations::CompareAndSwapWord( |
| 19 reinterpret_cast<uword*>(addr), old_state, 0) != old_state) { |
| 20 // Fast update failed which means we could potentially be in the middle |
| 21 // of a safepoint operation and need to block for it. |
| 22 SafepointHandler* handler = thread->isolate()->safepoint_handler(); |
| 23 handler->ExitSafepointUsingLock(thread); |
| 24 } |
| 25 thread->set_execution_state(Thread::kThreadInVM); |
| 26 } |
| 27 |
| 28 |
12 Monitor::WaitResult MonitorLocker::WaitWithSafepointCheck(Thread* thread, | 29 Monitor::WaitResult MonitorLocker::WaitWithSafepointCheck(Thread* thread, |
13 int64_t millis) { | 30 int64_t millis) { |
14 ASSERT(thread == Thread::Current()); | 31 ASSERT(thread == Thread::Current()); |
15 thread->set_execution_state(Thread::kThreadInBlockedState); | 32 thread->set_execution_state(Thread::kThreadInBlockedState); |
16 thread->EnterSafepoint(); | 33 thread->EnterSafepoint(); |
17 Monitor::WaitResult result = monitor_->Wait(millis); | 34 Monitor::WaitResult result = monitor_->Wait(millis); |
18 // First try a fast update of the thread state to indicate it is not at a | 35 // First try a fast update of the thread state to indicate it is not at a |
19 // safepoint anymore. | 36 // safepoint anymore. |
20 uword old_state = Thread::SetAtSafepoint(true, 0); | 37 uword old_state = Thread::SetAtSafepoint(true, 0); |
21 uword addr = | 38 uword addr = |
(...skipping 14 matching lines...) Expand all Loading... |
36 | 53 |
37 SafepointMutexLocker::SafepointMutexLocker(Mutex* mutex) : mutex_(mutex) { | 54 SafepointMutexLocker::SafepointMutexLocker(Mutex* mutex) : mutex_(mutex) { |
38 ASSERT(mutex != NULL); | 55 ASSERT(mutex != NULL); |
39 if (!mutex_->TryLock()) { | 56 if (!mutex_->TryLock()) { |
40 // We did not get the lock and could potentially block, so transition | 57 // We did not get the lock and could potentially block, so transition |
41 // accordingly. | 58 // accordingly. |
42 Thread* thread = Thread::Current(); | 59 Thread* thread = Thread::Current(); |
43 thread->set_execution_state(Thread::kThreadInBlockedState); | 60 thread->set_execution_state(Thread::kThreadInBlockedState); |
44 thread->EnterSafepoint(); | 61 thread->EnterSafepoint(); |
45 mutex->Lock(); | 62 mutex->Lock(); |
46 // First try a fast update of the thread state to indicate it is not at a | 63 // Update thread state and block if a safepoint operation is in progress. |
47 // safepoint anymore. | 64 updateThreadState(thread); |
48 uword old_state = Thread::SetAtSafepoint(true, 0); | |
49 uword addr = | |
50 reinterpret_cast<uword>(thread) + Thread::safepoint_state_offset(); | |
51 if (AtomicOperations::CompareAndSwapWord( | |
52 reinterpret_cast<uword*>(addr), old_state, 0) != old_state) { | |
53 // Fast update failed which means we could potentially be in the middle | |
54 // of a safepoint operation and need to block for it. | |
55 SafepointHandler* handler = thread->isolate()->safepoint_handler(); | |
56 handler->ExitSafepointUsingLock(thread); | |
57 } | |
58 thread->set_execution_state(Thread::kThreadInVM); | |
59 } | 65 } |
60 } | 66 } |
61 | 67 |
| 68 |
| 69 SafepointMonitorLocker::SafepointMonitorLocker(Monitor* monitor) |
| 70 : monitor_(monitor) { |
| 71 ASSERT(monitor_ != NULL); |
| 72 if (!monitor_->TryEnter()) { |
| 73 // We did not get the lock and could potentially block, so transition |
| 74 // accordingly. |
| 75 Thread* thread = Thread::Current(); |
| 76 thread->set_execution_state(Thread::kThreadInBlockedState); |
| 77 thread->EnterSafepoint(); |
| 78 monitor_->Enter(); |
| 79 // Update thread state and block if a safepoint operation is in progress. |
| 80 updateThreadState(thread); |
| 81 } |
| 82 } |
| 83 |
| 84 |
| 85 Monitor::WaitResult SafepointMonitorLocker::Wait(int64_t millis) { |
| 86 Thread* thread = Thread::Current(); |
| 87 thread->set_execution_state(Thread::kThreadInBlockedState); |
| 88 thread->EnterSafepoint(); |
| 89 Monitor::WaitResult result = monitor_->Wait(millis); |
| 90 // First try a fast update of the thread state to indicate it is not at a |
| 91 // safepoint anymore. |
| 92 uword old_state = Thread::SetAtSafepoint(true, 0); |
| 93 uword addr = |
| 94 reinterpret_cast<uword>(thread) + Thread::safepoint_state_offset(); |
| 95 if (AtomicOperations::CompareAndSwapWord( |
| 96 reinterpret_cast<uword*>(addr), old_state, 0) != old_state) { |
| 97 // Fast update failed which means we could potentially be in the middle |
| 98 // of a safepoint operation and need to block for it. |
| 99 monitor_->Exit(); |
| 100 SafepointHandler* handler = thread->isolate()->safepoint_handler(); |
| 101 handler->ExitSafepointUsingLock(thread); |
| 102 monitor_->Enter(); |
| 103 } |
| 104 thread->set_execution_state(Thread::kThreadInVM); |
| 105 return result; |
| 106 } |
| 107 |
62 } // namespace dart | 108 } // namespace dart |
OLD | NEW |