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_WORKER_THREAD_H_ 17 #define PANDA_RUNTIME_SCHEDULER_WORKER_THREAD_H_ 18 19 #include "runtime/include/thread.h" 20 21 namespace panda::scheduler { 22 23 class Task; 24 25 // Worker thread is a physical OS thread. 26 class WorkerThread : public panda::Thread { 27 public: 28 explicit WorkerThread(PandaVM *vm); 29 // We need to override this to call appropriate destructor in thread manager 30 ~WorkerThread() override = default; 31 GetCurrent()32 static WorkerThread *GetCurrent() 33 { 34 auto thread = Thread::GetCurrent(); 35 ASSERT(thread != nullptr); 36 ASSERT(thread->GetThreadType() == Thread::ThreadType::THREAD_TYPE_WORKER_THREAD); 37 return static_cast<WorkerThread *>(thread); 38 } 39 40 static WorkerThread *Create(PandaVM *vm); 41 void ProcessCreatedThread(); 42 static WorkerThread *AttachThread(); DetachThread()43 static void DetachThread() 44 { 45 trace::ScopedTrace scoped_trace(__FUNCTION__); 46 auto thread = GetCurrent(); 47 thread->Detach(); 48 } 49 50 void Destroy(); SetTask(Task * tk)51 void SetTask(Task *tk) 52 { 53 task = tk; 54 } 55 56 private: 57 Task *task = nullptr; Detach()58 void Detach() 59 { 60 LOG(DEBUG, RUNTIME) << "Detaching worker thread"; 61 Destroy(); 62 } 63 64 NO_COPY_SEMANTIC(WorkerThread); 65 NO_MOVE_SEMANTIC(WorkerThread); 66 }; 67 68 } // namespace panda::scheduler 69 70 #endif // PANDA_RUNTIME_SCHEDULER_WORKER_THREAD_H_ 71