1 /**
2 * Copyright (c) 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 "plugins/ets/runtime/types/ets_job.h"
17 #include "plugins/ets/runtime/ets_coroutine.h"
18 #include "plugins/ets/runtime/ets_platform_types.h"
19 #include "plugins/ets/runtime/ets_vm.h"
20
21 namespace ark::ets {
22
23 /*static*/
Create(EtsCoroutine * coro)24 EtsJob *EtsJob::Create(EtsCoroutine *coro)
25 {
26 [[maybe_unused]] EtsHandleScope scope(coro);
27 auto *klass = PlatformTypes(coro)->coreJob;
28 auto hJobObject = EtsObject::Create(coro, klass);
29 ASSERT(hJobObject != nullptr);
30 auto hJob = EtsHandle<EtsJob>(coro, EtsJob::FromEtsObject(hJobObject));
31 auto *mutex = EtsMutex::Create(coro);
32 hJob->SetMutex(coro, mutex);
33 auto *event = EtsEvent::Create(coro);
34 hJob->SetEvent(coro, event);
35 hJob->state_ = STATE_RUNNING;
36 return hJob.GetPtr();
37 }
38
39 /*static*/
EtsJobFinish(EtsJob * job,EtsObject * value)40 void EtsJob::EtsJobFinish(EtsJob *job, EtsObject *value)
41 {
42 EtsCoroutine *coro = EtsCoroutine::GetCurrent();
43 if (job == nullptr) {
44 LanguageContext ctx = Runtime::GetCurrent()->GetLanguageContext(panda_file::SourceLang::ETS);
45 ThrowNullPointerException(ctx, coro);
46 return;
47 }
48 [[maybe_unused]] EtsHandleScope scope(coro);
49 EtsHandle<EtsJob> hJob(coro, job);
50 ASSERT(hJob.GetPtr() != nullptr);
51 EtsHandle<EtsObject> hvalue(coro, value);
52 EtsMutex::LockHolder lh(hJob);
53 if (!hJob->IsRunning()) {
54 return;
55 }
56 hJob->Finish(coro, hvalue.GetPtr());
57 }
58
59 /*static*/
EtsJobFail(EtsJob * job,EtsObject * error)60 void EtsJob::EtsJobFail(EtsJob *job, EtsObject *error)
61 {
62 EtsCoroutine *coro = EtsCoroutine::GetCurrent();
63 if (job == nullptr) {
64 LanguageContext ctx = Runtime::GetCurrent()->GetLanguageContext(panda_file::SourceLang::ETS);
65 ThrowNullPointerException(ctx, coro);
66 return;
67 }
68 [[maybe_unused]] EtsHandleScope scope(coro);
69 EtsHandle<EtsJob> hJob(coro, job);
70 ASSERT(hJob.GetPtr() != nullptr);
71 EtsHandle<EtsObject> herror(coro, error);
72 EtsMutex::LockHolder lh(hJob);
73 if (!hJob->IsRunning()) {
74 return;
75 }
76 hJob->Fail(coro, herror.GetPtr());
77 }
78
79 } // namespace ark::ets
80