• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ECMASCRIPT_VM_THREAD_CONTROL_H
17 #define ECMASCRIPT_VM_THREAD_CONTROL_H
18 
19 #include "libpandabase/utils/bit_field.h"
20 #include "os/mutex.h"
21 
22 namespace panda::ecmascript {
23 class VmThreadControl {
24 public:
25     using VMNeedSuspensionBit = BitField<bool, 0, 1>;
26     using VMHasSuspendedBit = VMNeedSuspensionBit::NextFlag;
27 
SetVMNeedSuspension(bool flag)28     void SetVMNeedSuspension(bool flag)
29     {
30         uint64_t newVal = VMNeedSuspensionBit::Update(threadStateBitField_, flag);
31         threadStateBitField_ = newVal;
32     }
33 
VMNeedSuspension()34     bool VMNeedSuspension()
35     {
36         return VMNeedSuspensionBit::Decode(threadStateBitField_);
37     }
38 
39     bool CheckSafepoint();
40 
41     void SuspendVM();
42 
43     void ResumeVM();
44 
45     bool NotifyVMThreadSuspension();
46 
SetVMSuspended(bool flag)47     void SetVMSuspended(bool flag)
48     {
49         uint64_t newVal = VMHasSuspendedBit::Update(threadStateBitField_, flag);
50         threadStateBitField_ = newVal;
51     }
52 
IsSuspended()53     bool IsSuspended()
54     {
55         return VMHasSuspendedBit::Decode(threadStateBitField_);
56     }
57 
58 private:
59     std::atomic<uint8_t> threadStateBitField_ {0};
60     os::memory::Mutex vmThreadSuspensionMutex_;
61     os::memory::ConditionVariable vmThreadNeedSuspensionCV_;
62     os::memory::ConditionVariable vmThreadHasSuspendedCV_;
63 };
64 } // namespace panda::ecmascript
65 #endif  // ECMASCRIPT_VM_THREAD_CONTROL_H