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