• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-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 
16 #include "runtime/scheduler/task.h"
17 #include "runtime/scheduler/worker_thread.h"
18 #include "runtime/include/runtime.h"
19 #include "runtime/include/panda_vm.h"
20 
21 namespace ark::scheduler {
22 
23 // NOTE(xuliang): task id
Task(PandaVM * vm,ObjectHeader * obj)24 Task::Task(PandaVM *vm, ObjectHeader *obj)
25     : ManagedThread(-1, Runtime::GetCurrent()->GetInternalAllocator(), vm, Thread::ThreadType::THREAD_TYPE_TASK)
26 {
27     future_ = obj;
28 }
29 
Create(PandaVM * vm,ObjectHeader * obj)30 Task *Task::Create(PandaVM *vm, ObjectHeader *obj)
31 {
32     auto allocator = Runtime::GetCurrent()->GetInternalAllocator();
33     auto *task = allocator->New<Task>(vm, obj);
34     task->Initialize();
35     return task;
36 }
37 
Initialize()38 void Task::Initialize()
39 {
40     trace::ScopedTrace scopedTrace2("ThreadManager::RegisterThread");
41     // NOTE(xuliang): RegisterThread
42 }
43 
Destroy()44 void Task::Destroy()
45 {
46     ASSERT(this == Task::GetCurrent());
47     // NOTE(xuliang): should be done in UnregisterExitedThread.
48     GetVM()->GetGC()->OnThreadTerminate(this, mem::BuffersKeepingFlag::DELETE);
49     auto allocator = Runtime::GetCurrent()->GetInternalAllocator();
50     auto wt = workerThread_;
51     allocator->Delete(this);
52     Thread::SetCurrent(wt);
53 }
54 
SwitchFromWorkerThread()55 void Task::SwitchFromWorkerThread()
56 {
57     workerThread_ = WorkerThread::GetCurrent();
58     workerThread_->SetTask(this);
59     Thread::SetCurrent(this);
60 }
61 
SuspendCurrent()62 void Task::SuspendCurrent()
63 {
64     auto task = Task::GetCurrent();
65     auto wt = task->workerThread_;
66     ASSERT(wt != nullptr);
67     Thread::SetCurrent(wt);
68     wt->SetTask(nullptr);
69 }
70 
EndCurrent()71 void Task::EndCurrent()
72 {
73     auto task = Task::GetCurrent();
74     auto wt = task->workerThread_;
75     ASSERT(wt != nullptr);
76     task->Destroy();
77     Thread::SetCurrent(wt);
78     wt->SetTask(nullptr);
79 }
80 
81 }  // namespace ark::scheduler
82