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 "runtime/monitor_object_lock.h"
17
18 #include "libpandabase/os/thread.h"
19 #include "runtime/include/thread.h"
20 #include "runtime/handle_scope-inl.h"
21
22 namespace panda {
23
ObjectLock(ObjectHeader * obj)24 ObjectLock::ObjectLock(ObjectHeader *obj)
25 : scope_(HandleScope<ObjectHeader *>(ManagedThread::GetCurrent())),
26 obj_handler_(VMHandle<ObjectHeader>(ManagedThread::GetCurrent(), obj))
27 {
28 [[maybe_unused]] auto res = Monitor::MonitorEnter(obj_handler_.GetPtr());
29 ASSERT(res == Monitor::State::OK);
30 }
31
Wait(bool ignore_interruption)32 void ObjectLock::Wait(bool ignore_interruption)
33 {
34 Monitor::State state = Monitor::Wait(obj_handler_.GetPtr(), ThreadStatus::IS_WAITING, 0, 0, ignore_interruption);
35 LOG_IF(state == Monitor::State::ILLEGAL, FATAL, RUNTIME) << "Monitor::Wait() failed";
36 }
37
TimedWait(uint64_t timeout)38 void ObjectLock::TimedWait(uint64_t timeout)
39 {
40 Monitor::State state = Monitor::Wait(obj_handler_.GetPtr(), ThreadStatus::IS_TIMED_WAITING, timeout, 0);
41 LOG_IF(state == Monitor::State::ILLEGAL, FATAL, RUNTIME) << "Monitor::Wait() failed";
42 }
43
Notify()44 void ObjectLock::Notify()
45 {
46 Monitor::State state = Monitor::Notify(obj_handler_.GetPtr());
47 LOG_IF(state != Monitor::State::OK, FATAL, RUNTIME) << "Monitor::Notify() failed";
48 }
49
NotifyAll()50 void ObjectLock::NotifyAll()
51 {
52 Monitor::State state = Monitor::NotifyAll(obj_handler_.GetPtr());
53 LOG_IF(state != Monitor::State::OK, FATAL, RUNTIME) << "Monitor::NotifyAll() failed";
54 }
55
~ObjectLock()56 ObjectLock::~ObjectLock()
57 {
58 [[maybe_unused]] auto res = Monitor::MonitorExit(obj_handler_.GetPtr());
59 ASSERT(res == Monitor::State::OK);
60 }
61
62 } // namespace panda
63