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