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
16 /**
17 * @file task.h
18 *
19 * @brief Declares the task inner interfaces in C++.
20 *
21 * @since 10
22 */
23 #ifndef FFRT_INNER_API_CPP_TASK_H
24 #define FFRT_INNER_API_CPP_TASK_H
25 #include <cstdint>
26 #include "c/task_ext.h"
27 #include "cpp/task.h"
28
29 namespace ffrt {
30 /**
31 * @brief Skips a task.
32 *
33 * @param handle Indicates a task handle.
34 * @return Returns <b>0</b> if the task is skipped;
35 returns <b>-1</b> otherwise.
36 * @since 10
37 */
skip(task_handle & handle)38 static inline int skip(task_handle &handle)
39 {
40 return ffrt_skip(handle);
41 }
42
43 void sync_io(int fd);
44
45 void set_trace_tag(const char* name);
46
47 void clear_trace_tag();
48
set_cgroup_attr(qos qos_,ffrt_os_sched_attr * attr)49 static inline int set_cgroup_attr(qos qos_, ffrt_os_sched_attr *attr)
50 {
51 return ffrt_set_cgroup_attr(qos_, attr);
52 }
53
restore_qos_config()54 static inline void restore_qos_config()
55 {
56 ffrt_restore_qos_config();
57 }
58
set_cpu_worker_max_num(qos qos_,uint32_t num)59 static inline int set_cpu_worker_max_num(qos qos_, uint32_t num)
60 {
61 return ffrt_set_cpu_worker_max_num(qos_, num);
62 }
63
64 /**
65 * @brief Notifies a specified number of workers at a specified QoS level.
66 *
67 * @param qos_ Indicates the QoS.
68 * @param number Indicates the number of workers to be notified.
69 */
notify_workers(qos qos_,int number)70 static inline void notify_workers(qos qos_, int number)
71 {
72 return ffrt_notify_workers(qos_, number);
73 }
74
75 /**
76 * @brief Obtains the ID of this queue.
77 *
78 * @return Returns the queue ID.
79 */
get_queue_id()80 static inline int64_t get_queue_id()
81 {
82 return ffrt_this_queue_get_id();
83 }
84
85 /**
86 * @brief Enable the worker escape function (When all the worker threads under a QoS level fully block, the system will
87 * temporarily exceed the limit on the number of worker threads and create new worker threads to execute tasks).
88 * Delay penalty is added for escape function. As the number of threads increases, the thread creation delay increases.
89 * Calling this function does not take effect when the escape function is enabled.
90 *
91 * @param one_stage_interval_ms Indicates the interval for creating threads in one-stage, default value is 10ms.
92 * If input parameter value is smaller than the default value, the setting fails.
93 * @param two_stage_interval_ms Indicates the interval for creating threads in two-stage, default value is 100ms.
94 * If input parameter value is smaller than the default value, the setting fails.
95 * @param three_stage_interval_ms Indicates the interval for creating threads in three-stage, default value is 1000ms.
96 * If input parameter value is smaller than the default value, the setting fails.
97 * @param one_stage_worker_num Indicates the number of workers in one-stage.
98 * @param two_stage_worker_num Indicates the number of workers in two-stage.
99 * @return Returns 0 if the parameters are valid and the escape function is enabled successfully;
100 * returns 1 otherwise.
101 */
102 static inline int enable_worker_escape(uint64_t one_stage_interval_ms = 10, uint64_t two_stage_interval_ms = 100,
103 uint64_t three_stage_interval_ms = 1000, uint64_t one_stage_worker_num = 128, uint64_t two_stage_worker_num = 256)
104 {
105 return ffrt_enable_worker_escape(one_stage_interval_ms, two_stage_interval_ms,
106 three_stage_interval_ms, one_stage_worker_num, two_stage_worker_num);
107 }
108
109 /**
110 * @brief Disable the worker escape function (When all the worker threads under a QoS level fully block, the system will
111 * temporarily exceed the limit on the number of worker threads and create new worker threads to execute tasks).
112 *
113 */
disable_worker_escape()114 static inline void disable_worker_escape()
115 {
116 ffrt_disable_worker_escape();
117 }
118 } // namespace ffrt
119 #endif
120