1 /*
2 * Copyright (c) 2023 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 #include <pthread.h>
16 #include <random>
17 #include "tm/io_task.h"
18 #include "core/task_io.h"
19 #ifdef FFRT_CO_BACKTRACE_OH_ENABLE
20 #include <dlfcn.h>
21 #endif
22 #include "util/slab.h"
23 #include "util/ffrt_facade.h"
24 #include "util/spmc_queue.h"
25
26 #define ENABLE_LOCAL_QUEUE
27
28 namespace {
29 const int INSERT_GLOBAL_QUEUE_FREQ = 5;
30 }
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 API_ATTRIBUTE((visibility("default")))
ffrt_submit_coroutine(void * co,ffrt_coroutine_ptr_t exec,ffrt_function_t destroy,const ffrt_deps_t * in_deps,const ffrt_deps_t * out_deps,const ffrt_task_attr_t * attr)36 void ffrt_submit_coroutine(void* co, ffrt_coroutine_ptr_t exec, ffrt_function_t destroy, const ffrt_deps_t* in_deps,
37 const ffrt_deps_t* out_deps, const ffrt_task_attr_t* attr)
38 {
39 FFRT_COND_DO_ERR((exec == nullptr), return, "input invalid, exec == nullptr");
40 FFRT_COND_DO_ERR((destroy == nullptr), return, "input invalid, destroy == nullptr");
41
42 ffrt::task_attr_private *p = reinterpret_cast<ffrt::task_attr_private *>(const_cast<ffrt_task_attr_t *>(attr));
43
44 (void)in_deps;
45 (void)out_deps;
46 ffrt::ffrt_io_callable_t work;
47 work.exec = exec;
48 work.destroy = destroy;
49 work.data = co;
50 if (likely(attr == nullptr || ffrt_task_attr_get_delay(attr) == 0)) {
51 ffrt::FFRTFacade::GetDMInstance().onSubmitIO(work, p);
52 return;
53 }
54 FFRT_LOGE("io function does not support delay");
55 }
56
57 API_ATTRIBUTE((visibility("default")))
ffrt_get_current_task(void)58 void* ffrt_get_current_task(void)
59 {
60 return reinterpret_cast<void*>(ffrt::ExecuteCtx::Cur()->exec_task);
61 }
62
63 // API used to schedule stackless coroutine task
64 API_ATTRIBUTE((visibility("default")))
ffrt_wake_coroutine(void * task)65 void ffrt_wake_coroutine(void* task)
66 {
67 if (task == nullptr) {
68 FFRT_LOGE("Task is nullptr.");
69 return;
70 }
71
72 #ifdef FFRT_BBOX_ENABLE
73 TaskWakeCounterInc();
74 #endif
75
76 ffrt::IOTask* wakedTask = static_cast<ffrt::IOTask*>(task);
77 wakedTask->status = ffrt::ExecTaskStatus::ET_READY;
78
79 #ifdef FFRT_LOCAL_QUEUE_ENABLE
80 // in self-wakeup scenario, tasks are placed in local fifo to delay scheduling, implementing the yeild function
81 bool selfWakeup = (ffrt::ExecuteCtx::Cur()->exec_task == task);
82 if (!selfWakeup) {
83 if (ffrt::ExecuteCtx::Cur()->PushTaskToPriorityStack(wakedTask)) {
84 return;
85 }
86
87 if (rand() % INSERT_GLOBAL_QUEUE_FREQ) {
88 if (ffrt::ExecuteCtx::Cur()->localFifo != nullptr &&
89 ffrt::ExecuteCtx::Cur()->localFifo->PushTail(task) == 0) {
90 ffrt::FFRTFacade::GetEUInstance().NotifyLocalTaskAdded(wakedTask->qos_);
91 return;
92 }
93 }
94 }
95 #endif
96
97 if (!ffrt::FFRTFacade::GetSchedInstance()->InsertNode(&wakedTask->fq_we.node, wakedTask->qos_)) {
98 FFRT_LOGE("Submit io task failed!");
99 }
100 }
101 #ifdef __cplusplus
102 }
103 #endif
104