1 /**
2 * Copyright (c) 2024-2025 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 "intrinsics.h"
17 #include "plugins/ets/runtime/types/ets_sync_primitives.h"
18 #include "plugins/ets/runtime/ets_platform_types.h"
19 #include "plugins/ets/runtime/ets_utils.h"
20
21 namespace ark::ets::intrinsics {
22
EtsMutexCreate()23 EtsObject *EtsMutexCreate()
24 {
25 return EtsMutex::Create(EtsCoroutine::GetCurrent());
26 }
27
EtsMutexLock(EtsObject * mutex)28 void EtsMutexLock(EtsObject *mutex)
29 {
30 ASSERT(mutex->GetClass() == PlatformTypes()->coreMutex);
31 EtsMutex::FromEtsObject(mutex)->Lock();
32 }
33
EtsMutexUnlock(EtsObject * mutex)34 void EtsMutexUnlock(EtsObject *mutex)
35 {
36 ASSERT(mutex->GetClass() == PlatformTypes()->coreMutex);
37 EtsMutex::FromEtsObject(mutex)->Unlock();
38 }
39
EtsEventCreate()40 EtsObject *EtsEventCreate()
41 {
42 return EtsEvent::Create(EtsCoroutine::GetCurrent());
43 }
44
EtsEventWait(EtsObject * event)45 void EtsEventWait(EtsObject *event)
46 {
47 ASSERT(event->GetClass() == PlatformTypes()->coreEvent);
48 EtsEvent::FromEtsObject(event)->Wait();
49 }
50
EtsEventFire(EtsObject * event)51 void EtsEventFire(EtsObject *event)
52 {
53 ASSERT(event->GetClass() == PlatformTypes()->coreEvent);
54 EtsEvent::FromEtsObject(event)->Fire();
55 }
56
EtsCondVarCreate()57 EtsObject *EtsCondVarCreate()
58 {
59 return EtsCondVar::Create(EtsCoroutine::GetCurrent());
60 }
61
EtsCondVarWait(EtsObject * condVar,EtsObject * mutex)62 void EtsCondVarWait(EtsObject *condVar, EtsObject *mutex)
63 {
64 auto *coro = EtsCoroutine::GetCurrent();
65 ASSERT(condVar->GetClass() == PlatformTypes(coro)->coreCondVar);
66 ASSERT(mutex->GetClass() == PlatformTypes(coro)->coreMutex);
67 EtsHandleScope scope(coro);
68 EtsHandle<EtsMutex> hMutex(coro, EtsMutex::FromEtsObject(mutex));
69 EtsCondVar::FromEtsObject(condVar)->Wait(hMutex);
70 }
71
EtsCondVarNotifyOne(EtsObject * condVar,EtsObject * mutex)72 void EtsCondVarNotifyOne(EtsObject *condVar, EtsObject *mutex)
73 {
74 ASSERT(condVar->GetClass() == PlatformTypes()->coreCondVar);
75 ASSERT(mutex->GetClass() == PlatformTypes()->coreMutex);
76 EtsCondVar::FromEtsObject(condVar)->NotifyOne(EtsMutex::FromEtsObject(mutex));
77 }
78
EtsCondVarNotifyAll(EtsObject * condVar,EtsObject * mutex)79 void EtsCondVarNotifyAll(EtsObject *condVar, EtsObject *mutex)
80 {
81 ASSERT(condVar->GetClass() == PlatformTypes()->coreCondVar);
82 ASSERT(mutex->GetClass() == PlatformTypes()->coreMutex);
83 EtsCondVar::FromEtsObject(condVar)->NotifyAll(EtsMutex::FromEtsObject(mutex));
84 }
85
EtsQueueSpinlockCreate()86 EtsObject *EtsQueueSpinlockCreate()
87 {
88 return EtsQueueSpinlock::Create(EtsCoroutine::GetCurrent());
89 }
90
EtsQueueSpinlockGuard(EtsObject * spinlock,EtsObject * callback)91 void EtsQueueSpinlockGuard(EtsObject *spinlock, EtsObject *callback)
92 {
93 ASSERT(spinlock->GetClass() == PlatformTypes()->coreQueueSpinlock);
94 auto *coro = EtsCoroutine::GetCurrent();
95 EtsHandleScope scope(coro);
96 EtsHandle<EtsQueueSpinlock> hSpinlock(coro, EtsQueueSpinlock::FromEtsObject(spinlock));
97 EtsHandle<EtsObject> hCallback(coro, callback);
98 EtsQueueSpinlock::Guard guard(hSpinlock);
99 LambdaUtils::InvokeVoid(coro, hCallback.GetPtr());
100 }
101
102 } // namespace ark::ets::intrinsics
103