• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2023 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 panda {
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 }
94 
95 /* static */
CoroThreadProc(void * ctx)96 void StackfulCoroutineContext::CoroThreadProc(void *ctx)
97 {
98     static_cast<StackfulCoroutineContext *>(ctx)->ThreadProcImpl();
99 }
100 
ThreadProcImpl()101 void StackfulCoroutineContext::ThreadProcImpl()
102 {
103     auto *co = GetCoroutine();
104     co->NativeCodeBegin();
105     SetStatus(Coroutine::Status::RUNNING);
106     if (co->HasManagedEntrypoint()) {
107         ScopedManagedCodeThread s(co);
108         PandaVector<Value> args = std::move(co->GetManagedEntrypointArguments());
109         Value result = co->GetManagedEntrypoint()->Invoke(co, args.data());
110         co->RequestCompletion(result);
111     } else if (co->HasNativeEntrypoint()) {
112         co->GetNativeEntrypoint()(co->GetNativeEntrypointParam());
113     }
114     SetStatus(Coroutine::Status::TERMINATING);
115 
116     auto *threadManager = static_cast<CoroutineManager *>(co->GetVM()->GetThreadManager());
117     threadManager->TerminateCoroutine(co);
118 }
119 
SwitchTo(StackfulCoroutineContext * target)120 bool StackfulCoroutineContext::SwitchTo(StackfulCoroutineContext *target)
121 {
122     ASSERT(target != nullptr);
123     fibers::SwitchContext(&context_, &target->context_);
124     // maybe eventually we will check the return value of SwitchContext() and return false in case of error...
125     return true;
126 }
127 
RequestSuspend(bool getsBlocked)128 void StackfulCoroutineContext::RequestSuspend(bool getsBlocked)
129 {
130     SetStatus(getsBlocked ? Coroutine::Status::BLOCKED : Coroutine::Status::RUNNABLE);
131 }
132 
RequestResume()133 void StackfulCoroutineContext::RequestResume()
134 {
135     SetStatus(Coroutine::Status::RUNNING);
136 }
137 
RequestUnblock()138 void StackfulCoroutineContext::RequestUnblock()
139 {
140     SetStatus(Coroutine::Status::RUNNABLE);
141 }
142 
MainThreadFinished()143 void StackfulCoroutineContext::MainThreadFinished()
144 {
145     SetStatus(Coroutine::Status::TERMINATING);
146 }
147 
EnterAwaitLoop()148 void StackfulCoroutineContext::EnterAwaitLoop()
149 {
150     SetStatus(Coroutine::Status::AWAIT_LOOP);
151 }
152 
153 }  // namespace panda
154