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 "ecmascript/js_thread.h"
20#include "ecmascript/platform/mutex.h"
21#include "libpandabase/utils/bit_field.h"
22
23namespace panda::ecmascript {
24class VmThreadControl {
25public:
26    static constexpr char VM_NEED_SUSPENSION = 1;
27
28    VmThreadControl(JSThread *thread)
29    {
30        thread_ = thread;
31    }
32
33    void SetVMNeedSuspension(bool flag);
34
35    void SetTerminationRequest(bool flag);
36
37    bool VMNeedSuspension() const;
38
39    void SuspendVM();
40
41    void ResumeVM();
42
43    bool NotifyVMThreadSuspension();
44
45    void RequestTerminateExecution();
46
47    void SetVMSuspended(bool flag);
48
49    bool IsSuspended() const;
50
51private:
52    JSThread *thread_;
53    Mutex vmThreadSuspensionMutex_;
54    ConditionVariable vmThreadNeedSuspensionCV_;
55    ConditionVariable vmThreadHasSuspendedCV_;
56};
57} // namespace panda::ecmascript
58#endif  // ECMASCRIPT_VM_THREAD_CONTROL_H