1 /**
2 * Copyright (c) 2023-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/coroutines/coroutine_manager.h"
17 #include "runtime/coroutines/coroutine_events.h"
18 #include "plugins/ets/runtime/types/ets_promise.h"
19 #include "plugins/ets/runtime/ets_coroutine.h"
20 #include "plugins/ets/runtime/ets_vm.h"
21 #include "plugins/ets/runtime/types/ets_method.h"
22
23 namespace ark::ets {
24
25 /*static*/
Create(EtsCoroutine * coro)26 EtsPromise *EtsPromise::Create(EtsCoroutine *coro)
27 {
28 [[maybe_unused]] EtsHandleScope scope(coro);
29 auto *klass = coro->GetPandaVM()->GetClassLinker()->GetPromiseClass();
30 auto hPromise = EtsHandle<EtsPromise>(coro, EtsPromise::FromEtsObject(EtsObject::Create(coro, klass)));
31 auto *mutex = EtsMutex::Create(coro);
32 hPromise->SetMutex(coro, mutex);
33 auto *event = EtsEvent::Create(coro);
34 hPromise->SetEvent(coro, event);
35 return hPromise.GetPtr();
36 }
37
OnPromiseCompletion(EtsCoroutine * coro)38 void EtsPromise::OnPromiseCompletion(EtsCoroutine *coro)
39 {
40 // Unblock awaitee coros
41 GetEvent(coro)->Fire();
42
43 auto *cbQueue = GetCallbackQueue(coro);
44 auto *launchModeQueue = GetLaunchModeQueue(coro);
45 auto queueSize = GetQueueSize();
46
47 for (int idx = 0; idx < queueSize; ++idx) {
48 auto *thenCallback = cbQueue->Get(idx);
49 auto launchMode = static_cast<CoroutineLaunchMode>(launchModeQueue->Get(idx));
50 EtsPromise::LaunchCallback(coro, thenCallback, launchMode);
51 }
52 ClearQueues(coro);
53
54 auto resolver = GetPromiseResolver();
55 InvalidatePromiseResolver();
56 if (resolver == nullptr) {
57 return;
58 }
59 auto remotePromiseResolverAction =
60 IsResolved() ? RemotePromiseResolver::Action::RESOLVE : RemotePromiseResolver::Action::REJECT;
61 if (coro == coro->GetCoroutineManager()->GetMainThread()) {
62 resolver->ResolveInPlace(GetValue(coro), remotePromiseResolverAction);
63 } else {
64 resolver->ResolveViaCallback(GetValue(coro), remotePromiseResolverAction);
65 }
66 Runtime::GetCurrent()->GetInternalAllocator()->Delete(resolver);
67 }
68
69 /* static */
LaunchCallback(EtsCoroutine * coro,EtsObject * callback,CoroutineLaunchMode launchMode)70 void EtsPromise::LaunchCallback(EtsCoroutine *coro, EtsObject *callback, CoroutineLaunchMode launchMode)
71 {
72 // Post callback to js env
73 auto *jobQueue = coro->GetPandaVM()->GetJobQueue();
74 if (launchMode == CoroutineLaunchMode::MAIN_WORKER && jobQueue != nullptr) {
75 jobQueue->Post(callback);
76 return;
77 }
78 // Launch callback in its own coroutine
79 auto *coroManager = coro->GetCoroutineManager();
80 auto *event = Runtime::GetCurrent()->GetInternalAllocator()->New<CompletionEvent>(nullptr, coroManager);
81 auto *method = EtsMethod::ToRuntimeMethod(callback->GetClass()->GetMethod("invoke"));
82 ASSERT(method != nullptr);
83 auto args = PandaVector<Value> {Value(callback->GetCoreType())};
84 [[maybe_unused]] auto *launchedCoro = coroManager->Launch(event, method, std::move(args), launchMode);
85 ASSERT(launchedCoro != nullptr);
86 }
87
88 } // namespace ark::ets
89