• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "global_object_lock.h"
17 #include "runtime/include/thread_scopes.h"
18 
19 namespace ark {
20 // Use some global lock as fast solution.
21 static os::memory::RecursiveMutex g_mtx;    // NOLINT(fuchsia-statically-constructed-objects)
22 static os::memory::ConditionVariable g_cv;  // NOLINT(fuchsia-statically-constructed-objects)
23 
GlobalObjectLock(const ObjectHeader * obj)24 GlobalObjectLock::GlobalObjectLock([[maybe_unused]] const ObjectHeader *obj)
25 {
26     ScopedChangeThreadStatus s(ManagedThread::GetCurrent(), ThreadStatus::IS_BLOCKED);
27     g_mtx.Lock();
28 }
29 
Wait(bool ignoreInterruption) const30 bool GlobalObjectLock::Wait([[maybe_unused]] bool ignoreInterruption) const
31 {
32     ScopedChangeThreadStatus s(ManagedThread::GetCurrent(), ThreadStatus::IS_WAITING);
33     g_cv.Wait(&g_mtx);
34     return true;
35 }
36 
TimedWait(uint64_t timeout) const37 bool GlobalObjectLock::TimedWait(uint64_t timeout) const
38 {
39     ScopedChangeThreadStatus s(ManagedThread::GetCurrent(), ThreadStatus::IS_TIMED_WAITING);
40     g_cv.TimedWait(&g_mtx, timeout);
41     return true;
42 }
43 
Notify()44 void GlobalObjectLock::Notify()
45 {
46     g_cv.Signal();
47 }
48 
NotifyAll()49 void GlobalObjectLock::NotifyAll()
50 {
51     g_cv.SignalAll();
52 }
53 
~GlobalObjectLock()54 GlobalObjectLock::~GlobalObjectLock()
55 {
56     g_mtx.Unlock();
57 }
58 }  // namespace ark
59