1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "ecmascript/dfx/vm_thread_control.h" 17 18 namespace panda::ecmascript { NotifyVMThreadSuspension()19bool VmThreadControl::NotifyVMThreadSuspension() // block caller thread 20 { 21 if (VMNeedSuspension()) { // only enable one thread to post suspension 22 return false; 23 } 24 SetVMNeedSuspension(true); 25 os::memory::LockHolder lock(vmThreadSuspensionMutex_); 26 while (!IsSuspended()) { 27 vmThreadNeedSuspensionCV_.Wait(&vmThreadSuspensionMutex_); 28 } 29 return true; 30 } 31 SuspendVM()32void VmThreadControl::SuspendVM() // block vm thread 33 { 34 os::memory::LockHolder lock(vmThreadSuspensionMutex_); 35 SetVMSuspended(true); 36 vmThreadNeedSuspensionCV_.Signal(); // wake up the thread who needs suspend vmthread 37 while (VMNeedSuspension()) { 38 vmThreadHasSuspendedCV_.Wait(&vmThreadSuspensionMutex_); 39 } 40 SetVMSuspended(false); 41 } 42 ResumeVM()43void VmThreadControl::ResumeVM() 44 { 45 os::memory::LockHolder lock(vmThreadSuspensionMutex_); 46 SetVMNeedSuspension(false); 47 vmThreadHasSuspendedCV_.Signal(); 48 } 49 } // namespace panda::ecmascript