• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef PANDA_RUNTIME_COROUTINES_COROUTINE_CONTEXT_H
16 #define PANDA_RUNTIME_COROUTINES_COROUTINE_CONTEXT_H
17 
18 #include "runtime/coroutines/coroutine.h"
19 
20 namespace ark {
21 
22 /**
23  * @brief Native context of a coroutine.
24  *
25  * Implementation dependent: may contain register state or OS thread pointer or something else.
26  * Does not contain managed state (contains native state only).
27  */
28 class CoroutineContext {
29 public:
30     NO_COPY_SEMANTIC(CoroutineContext);
31     NO_MOVE_SEMANTIC(CoroutineContext);
32 
33     CoroutineContext() = default;
34     virtual ~CoroutineContext() = default;
35 
AttachToCoroutine(Coroutine * co)36     virtual void AttachToCoroutine(Coroutine *co)
37     {
38         co_ = co;
39     }
40     /// Get linked coroutine instance (contains managed portion of state)
GetCoroutine()41     Coroutine *GetCoroutine()
42     {
43         return co_;
44     }
45     // access to status might require implementation-dependent synchronization, so we have to store it
46     // in the native context
47     virtual Coroutine::Status GetStatus() const = 0;
48     virtual void SetStatus(Coroutine::Status newStatus) = 0;
49 
50     virtual void Destroy() = 0;
51     virtual void CleanUp() = 0;
52     virtual void RequestSuspend(bool getsBlocked) = 0;
53     virtual void RequestResume() = 0;
54     virtual void RequestUnblock() = 0;
55 
56     /**
57      * Writes coroutine stack parameters into the provided stack_addr, stack_size, guard_size variables
58      * and returns true on successful operation
59      */
60     virtual bool RetrieveStackInfo(void *&stackAddr, size_t &stackSize, size_t &guardSize) = 0;
61 
62 protected:
UpdateId(os::thread::ThreadId id,Coroutine * co)63     static void UpdateId(os::thread::ThreadId id, Coroutine *co)
64     {
65         co->UpdateId(id);
66     }
67 
68 private:
69     Coroutine *co_ = nullptr;
70 };
71 
72 }  // namespace ark
73 
74 #endif  // PANDA_RUNTIME_COROUTINES_COROUTINE_CONTEXT_H
75