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.h"
17 #include "runtime/include/panda_vm.h"
18 #include "runtime/include/thread_scopes.h"
19 #include "runtime/coroutines/coroutine_manager.h"
20 #include "runtime/coroutines/stackful_coroutine.h"
21
22 namespace ark {
23
24 // clang-tidy cannot detect that we are going to initialize context_ via getcontext()
25 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
StackfulCoroutineContext(uint8_t * stack,size_t stackSizeBytes)26 StackfulCoroutineContext::StackfulCoroutineContext(uint8_t *stack, size_t stackSizeBytes)
27 : stack_(stack), stackSizeBytes_(stackSizeBytes)
28 {
29 fibers::GetCurrentContext(&context_);
30 }
31
AttachToCoroutine(Coroutine * co)32 void StackfulCoroutineContext::AttachToCoroutine(Coroutine *co)
33 {
34 CoroutineContext::AttachToCoroutine(co);
35 if (co->HasManagedEntrypoint() || co->HasNativeEntrypoint()) {
36 fibers::UpdateContext(&context_, CoroThreadProc, this, stack_, stackSizeBytes_);
37 }
38 auto *cm = static_cast<CoroutineManager *>(co->GetVM()->GetThreadManager());
39 cm->RegisterCoroutine(co);
40 SetStatus(Coroutine::Status::RUNNABLE);
41 }
42
RetrieveStackInfo(void * & stackAddr,size_t & stackSize,size_t & guardSize)43 bool StackfulCoroutineContext::RetrieveStackInfo(void *&stackAddr, size_t &stackSize, size_t &guardSize)
44 {
45 stackAddr = stack_;
46 stackSize = stackSizeBytes_;
47 guardSize = 0;
48 return true;
49 }
50
GetStatus() const51 Coroutine::Status StackfulCoroutineContext::GetStatus() const
52 {
53 return status_;
54 }
55
SetStatus(Coroutine::Status newStatus)56 void StackfulCoroutineContext::SetStatus(Coroutine::Status newStatus)
57 {
58 #ifndef NDEBUG
59 PandaString setter = (Thread::GetCurrent() == nullptr) ? "null" : Coroutine::GetCurrent()->GetName();
60 LOG(DEBUG, COROUTINES) << GetCoroutine()->GetName() << ": " << status_ << " -> " << newStatus << " by " << setter;
61 #endif
62 status_ = newStatus;
63 }
64
Destroy()65 void StackfulCoroutineContext::Destroy()
66 {
67 auto *co = GetCoroutine();
68 if (co->HasManagedEntrypoint()) {
69 // coroutines with an entry point should not be destroyed manually!
70 UNREACHABLE();
71 }
72 ASSERT(co == Coroutine::GetCurrent());
73 ASSERT(co->GetStatus() != ThreadStatus::FINISHED);
74
75 co->UpdateStatus(ThreadStatus::TERMINATING);
76
77 auto *threadManager = static_cast<CoroutineManager *>(co->GetVM()->GetThreadManager());
78 if (threadManager->TerminateCoroutine(co)) {
79 // detach
80 Coroutine::SetCurrent(nullptr);
81 }
82 }
83
CleanUp()84 void StackfulCoroutineContext::CleanUp()
85 {
86 #ifdef PANDA_ASAN_ON
87 void *contextStackP;
88 size_t contextStackSize;
89 size_t contextGuardSize;
90 RetrieveStackInfo(contextStackP, contextStackSize, contextGuardSize);
91 ASAN_UNPOISON_MEMORY_REGION(contextStackP, contextStackSize);
92 #endif // PANDA_ASAN_ON
93 worker_ = nullptr;
94 affinityMask_ = stackful_coroutines::AFFINITY_MASK_NONE;
95 }
96
97 /*static*/
CoroThreadProc(void * ctx)98 void StackfulCoroutineContext::CoroThreadProc(void *ctx)
99 {
100 static_cast<StackfulCoroutineContext *>(ctx)->ThreadProcImpl();
101 }
102
ThreadProcImpl()103 void StackfulCoroutineContext::ThreadProcImpl()
104 {
105 // profiling: the interval was started in the ctxswitch
106 GetWorker()->GetPerfStats().FinishInterval(CoroutineTimeStats::CTX_SWITCH);
107 // consider changing this to INIT later on...
108 GetWorker()->GetPerfStats().StartInterval(CoroutineTimeStats::SCH_ALL);
109
110 auto *co = GetCoroutine();
111 auto *coroutineManager = static_cast<CoroutineManager *>(co->GetVM()->GetThreadManager());
112 co->NativeCodeBegin();
113 SetStatus(Coroutine::Status::RUNNING);
114 if (co->HasManagedEntrypoint()) {
115 ScopedManagedCodeThread s(co);
116 PandaVector<Value> args = std::move(co->GetManagedEntrypointArguments());
117 // profiling
118 GetWorker()->GetPerfStats().FinishInterval(CoroutineTimeStats::SCH_ALL);
119 Value result = co->GetManagedEntrypoint()->Invoke(co, args.data());
120 co->RequestCompletion(result);
121 } else if (co->HasNativeEntrypoint()) {
122 // profiling: jump to the NATIVE EP, will end the SCH_ALL there
123 co->GetNativeEntrypoint()(co->GetNativeEntrypointParam());
124 }
125 SetStatus(Coroutine::Status::TERMINATING);
126 coroutineManager->TerminateCoroutine(co);
127 }
128
SwitchTo(StackfulCoroutineContext * target)129 bool StackfulCoroutineContext::SwitchTo(StackfulCoroutineContext *target)
130 {
131 ASSERT(target != nullptr);
132 fibers::SwitchContext(&context_, &target->context_);
133 // maybe eventually we will check the return value of SwitchContext() and return false in case of error...
134 return true;
135 }
136
RequestSuspend(bool getsBlocked)137 void StackfulCoroutineContext::RequestSuspend(bool getsBlocked)
138 {
139 SetStatus(getsBlocked ? Coroutine::Status::BLOCKED : Coroutine::Status::RUNNABLE);
140 }
141
RequestResume()142 void StackfulCoroutineContext::RequestResume()
143 {
144 UpdateId(os::thread::GetCurrentThreadId(), GetCoroutine());
145 SetStatus(Coroutine::Status::RUNNING);
146 }
147
RequestUnblock()148 void StackfulCoroutineContext::RequestUnblock()
149 {
150 SetStatus(Coroutine::Status::RUNNABLE);
151 }
152
MainThreadFinished()153 void StackfulCoroutineContext::MainThreadFinished()
154 {
155 SetStatus(Coroutine::Status::TERMINATING);
156 }
157
EnterAwaitLoop()158 void StackfulCoroutineContext::EnterAwaitLoop()
159 {
160 SetStatus(Coroutine::Status::AWAIT_LOOP);
161 }
162
163 } // namespace ark
164