1 /** 2 * Copyright (c) 2021-2022 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 #ifndef PANDA_RUNTIME_SCHEDULER_TASK_H_ 17 #define PANDA_RUNTIME_SCHEDULER_TASK_H_ 18 19 #include "runtime/include/managed_thread.h" 20 #include "runtime/scheduler/worker_thread.h" 21 22 namespace panda::scheduler { 23 24 // Task is a user-level light-weight thread. 25 class Task : public panda::ManagedThread { 26 public: 27 explicit Task(PandaVM *vm, ObjectHeader *obj = nullptr); 28 // We need to override this to call appropriate destructor in thread manager 29 ~Task() override = default; 30 GetCurrent()31 static Task *GetCurrent() 32 { 33 auto task = Thread::GetCurrent(); 34 ASSERT(task->GetThreadType() == Thread::ThreadType::THREAD_TYPE_TASK); 35 return static_cast<Task *>(task); 36 } 37 38 static Task *Create(PandaVM *vm, ObjectHeader *obj = nullptr); 39 void Initialize(); 40 void Destroy(); 41 FreeInternalMemory()42 void FreeInternalMemory() override 43 { 44 ManagedThread::FreeInternalMemory(); 45 } 46 VisitGCRoots(const ObjectVisitor & cb)47 void VisitGCRoots(const ObjectVisitor &cb) override 48 { 49 ManagedThread::VisitGCRoots(cb); 50 if (future_ != nullptr) { 51 cb(future_); 52 } 53 } 54 UpdateGCRoots()55 void UpdateGCRoots() override 56 { 57 ManagedThread::UpdateGCRoots(); 58 if ((future_ != nullptr) && (future_->IsForwarded())) { 59 future_ = ::panda::mem::GetForwardAddress(future_); 60 } 61 } 62 63 void SwitchFromWorkerThread(); 64 static void SuspendCurrent(); 65 static void EndCurrent(); 66 67 private: 68 WorkerThread *workerThread_ = nullptr; 69 ObjectHeader *future_ = nullptr; 70 71 NO_COPY_SEMANTIC(Task); 72 NO_MOVE_SEMANTIC(Task); 73 }; 74 75 } // namespace panda::scheduler 76 77 #endif // PANDA_RUNTIME_SCHEDULER_TASK_H_ 78