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 co->ProcessPresentAndAnnouncedCallbacks();
122 } else if (co->HasNativeEntrypoint()) {
123 // profiling: jump to the NATIVE EP, will end the SCH_ALL there
124 co->GetNativeEntrypoint()(co->GetNativeEntrypointParam());
125 }
126 SetStatus(Coroutine::Status::TERMINATING);
127 coroutineManager->TerminateCoroutine(co);
128 }
129
SwitchTo(StackfulCoroutineContext * target)130 bool StackfulCoroutineContext::SwitchTo(StackfulCoroutineContext *target)
131 {
132 ASSERT(target != nullptr);
133 fibers::SwitchContext(&context_, &target->context_);
134 // maybe eventually we will check the return value of SwitchContext() and return false in case of error...
135 return true;
136 }
137
RequestSuspend(bool getsBlocked)138 void StackfulCoroutineContext::RequestSuspend(bool getsBlocked)
139 {
140 SetStatus(getsBlocked ? Coroutine::Status::BLOCKED : Coroutine::Status::RUNNABLE);
141 }
142
RequestResume()143 void StackfulCoroutineContext::RequestResume()
144 {
145 UpdateId(os::thread::GetCurrentThreadId(), GetCoroutine());
146 SetStatus(Coroutine::Status::RUNNING);
147 }
148
RequestUnblock()149 void StackfulCoroutineContext::RequestUnblock()
150 {
151 SetStatus(Coroutine::Status::RUNNABLE);
152 }
153
MainThreadFinished()154 void StackfulCoroutineContext::MainThreadFinished()
155 {
156 SetStatus(Coroutine::Status::TERMINATING);
157 }
158
EnterAwaitLoop()159 void StackfulCoroutineContext::EnterAwaitLoop()
160 {
161 SetStatus(Coroutine::Status::AWAIT_LOOP);
162 }
163
164 } // namespace ark
165