1 /**
2 * Copyright (c) 2021-2024 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 ark {
ObjectLock(ObjectHeader * obj)23 ObjectLock::ObjectLock(ObjectHeader *obj)
24 : scope_(HandleScope<ObjectHeader *>(ManagedThread::GetCurrent())),
25 objHandler_(VMHandle<ObjectHeader>(ManagedThread::GetCurrent(), obj))
26 {
27 [[maybe_unused]] auto res = Monitor::MonitorEnter(objHandler_.GetPtr());
28 ASSERT(res == Monitor::State::OK);
29 }
30
Wait(bool ignoreInterruption)31 bool ObjectLock::Wait(bool ignoreInterruption)
32 {
33 Monitor::State state = Monitor::Wait(objHandler_.GetPtr(), ThreadStatus::IS_WAITING, 0, 0, ignoreInterruption);
34 LOG_IF(state == Monitor::State::ILLEGAL, FATAL, RUNTIME) << "Monitor::Wait() failed";
35 return state != Monitor::State::INTERRUPTED;
36 }
37
TimedWait(uint64_t timeout)38 bool ObjectLock::TimedWait(uint64_t timeout)
39 {
40 Monitor::State state = Monitor::Wait(objHandler_.GetPtr(), ThreadStatus::IS_TIMED_WAITING, timeout, 0);
41 LOG_IF(state == Monitor::State::ILLEGAL, FATAL, RUNTIME) << "Monitor::Wait() failed";
42 return state != Monitor::State::INTERRUPTED;
43 }
44
Notify()45 void ObjectLock::Notify()
46 {
47 Monitor::State state = Monitor::Notify(objHandler_.GetPtr());
48 LOG_IF(state != Monitor::State::OK, FATAL, RUNTIME) << "Monitor::Notify() failed";
49 }
50
NotifyAll()51 void ObjectLock::NotifyAll()
52 {
53 Monitor::State state = Monitor::NotifyAll(objHandler_.GetPtr());
54 LOG_IF(state != Monitor::State::OK, FATAL, RUNTIME) << "Monitor::NotifyAll() failed";
55 }
56
~ObjectLock()57 ObjectLock::~ObjectLock()
58 {
59 [[maybe_unused]] auto res = Monitor::MonitorExit(objHandler_.GetPtr());
60 ASSERT(res == Monitor::State::OK);
61 }
62 } // namespace ark
63