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 #include "runtime/scheduler/worker_thread.h" 17 #include "runtime/include/runtime.h" 18 19 namespace panda::scheduler { 20 WorkerThread(PandaVM * vm)21WorkerThread::WorkerThread(PandaVM *vm) : Thread(vm, Thread::ThreadType::THREAD_TYPE_WORKER_THREAD) {} 22 23 /* static - creation of the initial Worker thread */ Create(PandaVM * vm)24WorkerThread *WorkerThread::Create(PandaVM *vm) 25 { 26 ASSERT(Thread::GetCurrent() == nullptr); 27 auto allocator = Runtime::GetCurrent()->GetInternalAllocator(); 28 auto *thread = allocator->New<WorkerThread>(vm); 29 thread->ProcessCreatedThread(); 30 return thread; 31 } 32 33 /* Common actions for creation of the thread. */ ProcessCreatedThread()34void WorkerThread::ProcessCreatedThread() 35 { 36 Thread::SetCurrent(this); 37 // Runtime takes ownership of the thread 38 trace::ScopedTrace scoped_trace2("ThreadManager::RegisterThread"); 39 // TODO(xuliang): RegisterThread 40 } 41 AttachThread()42WorkerThread *WorkerThread::AttachThread() 43 { 44 trace::ScopedTrace scoped_trace(__FUNCTION__); 45 46 return WorkerThread::Create(Runtime::GetCurrent()->GetPandaVM()); 47 } 48 Destroy()49void WorkerThread::Destroy() 50 { 51 ASSERT(this == WorkerThread::GetCurrent()); 52 // TODO(xuliang): should be done in UnregisterExitedThread. 53 auto allocator = Runtime::GetCurrent()->GetInternalAllocator(); 54 allocator->Delete(this); 55 Thread::SetCurrent(nullptr); 56 } 57 58 } // namespace panda::scheduler 59