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 interfaces in C++.
20 *
21 * @since 10
22 * @version 1.0
23 */
24 #ifndef FFRT_API_CPP_TASK_H
25 #define FFRT_API_CPP_TASK_H
26 #include <vector>
27 #include <string>
28 #include <functional>
29 #include <memory>
30 #include "c/task.h"
31
32 namespace ffrt {
33 class task_attr : public ffrt_task_attr_t {
34 public:
task_attr()35 task_attr()
36 {
37 ffrt_task_attr_init(this);
38 }
39
~task_attr()40 ~task_attr()
41 {
42 ffrt_task_attr_destroy(this);
43 }
44
45 task_attr(const task_attr&) = delete;
46 task_attr& operator=(const task_attr&) = delete;
47
48 /**
49 * @brief Sets a task name.
50 *
51 * @param name Indicates a pointer to the task name.
52 * @since 10
53 * @version 1.0
54 */
name(const char * name)55 inline task_attr& name(const char* name)
56 {
57 ffrt_task_attr_set_name(this, name);
58 return *this;
59 }
60
61 /**
62 * @brief Obtains the task name.
63 *
64 * @return Returns a pointer to the task name.
65 * @since 10
66 * @version 1.0
67 */
name()68 inline const char* name() const
69 {
70 return ffrt_task_attr_get_name(this);
71 }
72
73 /**
74 * @brief Sets the QoS for this task.
75 *
76 * @param qos Indicates the QoS.
77 * @since 10
78 * @version 1.0
79 */
qos(qos qos_)80 inline task_attr& qos(qos qos_)
81 {
82 ffrt_task_attr_set_qos(this, qos_);
83 return *this;
84 }
85
86 /**
87 * @brief Obtains the QoS of this task.
88 *
89 * @return Returns the QoS.
90 * @since 10
91 * @version 1.0
92 */
qos()93 inline int qos() const
94 {
95 return ffrt_task_attr_get_qos(this);
96 }
97
98 /**
99 * @brief Sets the delay time for this task.
100 *
101 * @param delay_us Indicates the delay time, in microseconds.
102 * @since 10
103 * @version 1.0
104 */
delay(uint64_t delay_us)105 inline task_attr& delay(uint64_t delay_us)
106 {
107 ffrt_task_attr_set_delay(this, delay_us);
108 return *this;
109 }
110
111 /**
112 * @brief Obtains the delay time of this task.
113 *
114 * @return Returns the delay time.
115 * @since 10
116 * @version 1.0
117 */
delay()118 inline uint64_t delay() const
119 {
120 return ffrt_task_attr_get_delay(this);
121 }
122 };
123
124 class task_handle {
125 public:
task_handle()126 task_handle() : p(nullptr)
127 {
128 }
task_handle(ffrt_task_handle_t p)129 task_handle(ffrt_task_handle_t p) : p(p)
130 {
131 }
132
~task_handle()133 ~task_handle()
134 {
135 if (p) {
136 ffrt_task_handle_destroy(p);
137 }
138 }
139
140 task_handle(task_handle const&) = delete;
141 void operator=(task_handle const&) = delete;
142
task_handle(task_handle && h)143 inline task_handle(task_handle&& h)
144 {
145 *this = std::move(h);
146 }
147
148 inline task_handle& operator=(task_handle&& h)
149 {
150 if (p) {
151 ffrt_task_handle_destroy(p);
152 }
153 p = h.p;
154 h.p = nullptr;
155 return *this;
156 }
157
158 inline operator void* () const
159 {
160 return p;
161 }
162
163 private:
164 ffrt_task_handle_t p = nullptr;
165 };
166
167 struct dependence : ffrt_dependence_t {
dependencedependence168 dependence(const void* d)
169 {
170 type = ffrt_dependence_data;
171 ptr = d;
172 }
dependencedependence173 dependence(const task_handle& h)
174 {
175 type = ffrt_dependence_task;
176 ptr = h;
177 }
178 };
179
180 template<class T>
181 struct function {
182 template<class CT>
functionfunction183 function(ffrt_function_header_t h, CT&& c) : header(h), closure(std::forward<CT>(c)) {}
184 ffrt_function_header_t header;
185 T closure;
186 };
187
188 template<class T>
exec_function_wrapper(void * t)189 void exec_function_wrapper(void* t)
190 {
191 auto f = reinterpret_cast<function<std::decay_t<T>>*>(t);
192 f->closure();
193 }
194
195 template<class T>
destroy_function_wrapper(void * t)196 void destroy_function_wrapper(void* t)
197 {
198 auto f = reinterpret_cast<function<std::decay_t<T>>*>(t);
199 f->closure = nullptr;
200 }
201
202 template<class T>
203 inline ffrt_function_header_t* create_function_wrapper(T&& func,
204 ffrt_function_kind_t kind = ffrt_function_kind_general)
205 {
206 using function_type = function<std::decay_t<T>>;
207 static_assert(sizeof(function_type) <= ffrt_auto_managed_function_storage_size,
208 "size of function must be less than ffrt_auto_managed_function_storage_size");
209
210 auto p = ffrt_alloc_auto_managed_function_storage_base(kind);
211 auto f =
212 new (p)function_type({ exec_function_wrapper<T>, destroy_function_wrapper<T>, { 0 } }, std::forward<T>(func));
213 return reinterpret_cast<ffrt_function_header_t*>(f);
214 }
215
216 /**
217 * @brief Submits a task without input and output dependencies.
218 *
219 * @param func Indicates a task executor function closure.
220 * @since 10
221 * @version 1.0
222 */
submit(std::function<void ()> && func)223 static inline void submit(std::function<void()>&& func)
224 {
225 return ffrt_submit_base(create_function_wrapper(std::move(func)), nullptr, nullptr, nullptr);
226 }
227
228 /**
229 * @brief Submits a task with input dependencies only.
230 *
231 * @param func Indicates a task executor function closure.
232 * @param in_deps Indicates a pointer to the input dependencies.
233 * @since 10
234 * @version 1.0
235 */
submit(std::function<void ()> && func,std::initializer_list<dependence> in_deps)236 static inline void submit(std::function<void()>&& func, std::initializer_list<dependence> in_deps)
237 {
238 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.begin()};
239 return ffrt_submit_base(create_function_wrapper(std::move(func)), &in, nullptr, nullptr);
240 }
241
242 /**
243 * @brief Submits a task with input and output dependencies.
244 *
245 * @param func Indicates a task executor function closure.
246 * @param in_deps Indicates a pointer to the input dependencies.
247 * @param out_deps Indicates a pointer to the output dependencies.
248 * @since 10
249 * @version 1.0
250 */
submit(std::function<void ()> && func,std::initializer_list<dependence> in_deps,std::initializer_list<dependence> out_deps)251 static inline void submit(std::function<void()>&& func, std::initializer_list<dependence> in_deps,
252 std::initializer_list<dependence> out_deps)
253 {
254 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.begin()};
255 ffrt_deps_t out {static_cast<uint32_t>(out_deps.size()), out_deps.begin()};
256 return ffrt_submit_base(create_function_wrapper(std::move(func)), &in, &out, nullptr);
257 }
258
259 /**
260 * @brief Submits a task with a specified attribute and input and output dependencies.
261 *
262 * @param func Indicates a task executor function closure.
263 * @param in_deps Indicates a pointer to the input dependencies.
264 * @param out_deps Indicates a pointer to the output dependencies.
265 * @param attr Indicates a task attribute.
266 * @since 10
267 * @version 1.0
268 */
submit(std::function<void ()> && func,std::initializer_list<dependence> in_deps,std::initializer_list<dependence> out_deps,const task_attr & attr)269 static inline void submit(std::function<void()>&& func, std::initializer_list<dependence> in_deps,
270 std::initializer_list<dependence> out_deps, const task_attr& attr)
271 {
272 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.begin()};
273 ffrt_deps_t out {static_cast<uint32_t>(out_deps.size()), out_deps.begin()};
274 return ffrt_submit_base(create_function_wrapper(std::move(func)), &in, &out, &attr);
275 }
276
277 /**
278 * @brief Submits a task with input dependencies only.
279 *
280 * @param func Indicates a task executor function closure.
281 * @param in_deps Indicates a pointer to the input dependencies.
282 * @since 10
283 * @version 1.0
284 */
submit(std::function<void ()> && func,const std::vector<dependence> & in_deps)285 static inline void submit(std::function<void()>&& func, const std::vector<dependence>& in_deps)
286 {
287 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.data()};
288 return ffrt_submit_base(create_function_wrapper(std::move(func)), &in, nullptr, nullptr);
289 }
290
291 /**
292 * @brief Submits a task with input and output dependencies.
293 *
294 * @param func Indicates a task executor function closure.
295 * @param in_deps Indicates a pointer to the input dependencies.
296 * @param out_deps Indicates a pointer to the output dependencies.
297 * @since 10
298 * @version 1.0
299 */
submit(std::function<void ()> && func,const std::vector<dependence> & in_deps,const std::vector<dependence> & out_deps)300 static inline void submit(std::function<void()>&& func, const std::vector<dependence>& in_deps,
301 const std::vector<dependence>& out_deps)
302 {
303 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.data()};
304 ffrt_deps_t out {static_cast<uint32_t>(out_deps.size()), out_deps.data()};
305 return ffrt_submit_base(create_function_wrapper(std::move(func)), &in, &out, nullptr);
306 }
307
308 /**
309 * @brief Submits a task with a specified attribute and input and output dependencies.
310 *
311 * @param func Indicates a task executor function closure.
312 * @param in_deps Indicates a pointer to the input dependencies.
313 * @param out_deps Indicates a pointer to the output dependencies.
314 * @param attr Indicates a task attribute.
315 * @since 10
316 * @version 1.0
317 */
submit(std::function<void ()> && func,const std::vector<dependence> & in_deps,const std::vector<dependence> & out_deps,const task_attr & attr)318 static inline void submit(std::function<void()>&& func, const std::vector<dependence>& in_deps,
319 const std::vector<dependence>& out_deps, const task_attr& attr)
320 {
321 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.data()};
322 ffrt_deps_t out {static_cast<uint32_t>(out_deps.size()), out_deps.data()};
323 return ffrt_submit_base(create_function_wrapper(std::move(func)), &in, &out, &attr);
324 }
325
326 /**
327 * @brief Submits a task without input and output dependencies.
328 *
329 * @param func Indicates a task executor function closure.
330 * @since 10
331 * @version 1.0
332 */
submit(const std::function<void ()> & func)333 static inline void submit(const std::function<void()>& func)
334 {
335 return ffrt_submit_base(create_function_wrapper(func), nullptr, nullptr, nullptr);
336 }
337
338 /**
339 * @brief Submits a task with input dependencies only.
340 *
341 * @param func Indicates a task executor function closure.
342 * @param in_deps Indicates a pointer to the input dependencies.
343 * @since 10
344 * @version 1.0
345 */
submit(const std::function<void ()> & func,std::initializer_list<dependence> in_deps)346 static inline void submit(const std::function<void()>& func, std::initializer_list<dependence> in_deps)
347 {
348 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.begin()};
349 return ffrt_submit_base(create_function_wrapper(func), &in, nullptr, nullptr);
350 }
351
352 /**
353 * @brief Submits a task with input and output dependencies.
354 *
355 * @param func Indicates a task executor function closure.
356 * @param in_deps Indicates a pointer to the input dependencies.
357 * @param out_deps Indicates a pointer to the output dependencies.
358 * @since 10
359 * @version 1.0
360 */
submit(const std::function<void ()> & func,std::initializer_list<dependence> in_deps,std::initializer_list<dependence> out_deps)361 static inline void submit(const std::function<void()>& func, std::initializer_list<dependence> in_deps,
362 std::initializer_list<dependence> out_deps)
363 {
364 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.begin()};
365 ffrt_deps_t out {static_cast<uint32_t>(out_deps.size()), out_deps.begin()};
366 return ffrt_submit_base(create_function_wrapper(func), &in, &out, nullptr);
367 }
368
369 /**
370 * @brief Submits a task with a specified attribute and input and output dependencies.
371 *
372 * @param func Indicates a task executor function closure.
373 * @param in_deps Indicates a pointer to the input dependencies.
374 * @param out_deps Indicates a pointer to the output dependencies.
375 * @param attr Indicates a task attribute.
376 * @since 10
377 * @version 1.0
378 */
submit(const std::function<void ()> & func,std::initializer_list<dependence> in_deps,std::initializer_list<dependence> out_deps,const task_attr & attr)379 static inline void submit(const std::function<void()>& func, std::initializer_list<dependence> in_deps,
380 std::initializer_list<dependence> out_deps, const task_attr& attr)
381 {
382 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.begin()};
383 ffrt_deps_t out {static_cast<uint32_t>(out_deps.size()), out_deps.begin()};
384 return ffrt_submit_base(create_function_wrapper(func), &in, &out, &attr);
385 }
386
387 /**
388 * @brief Submits a task with input dependencies only.
389 *
390 * @param func Indicates a task executor function closure.
391 * @param in_deps Indicates a pointer to the input dependencies.
392 * @since 10
393 * @version 1.0
394 */
submit(const std::function<void ()> & func,const std::vector<dependence> & in_deps)395 static inline void submit(const std::function<void()>& func, const std::vector<dependence>& in_deps)
396 {
397 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.data()};
398 return ffrt_submit_base(create_function_wrapper(func), &in, nullptr, nullptr);
399 }
400
401 /**
402 * @brief Submits a task with input and output dependencies.
403 *
404 * @param func Indicates a task executor function closure.
405 * @param in_deps Indicates a pointer to the input dependencies.
406 * @param out_deps Indicates a pointer to the output dependencies.
407 * @since 10
408 * @version 1.0
409 */
submit(const std::function<void ()> & func,const std::vector<dependence> & in_deps,const std::vector<dependence> & out_deps)410 static inline void submit(const std::function<void()>& func, const std::vector<dependence>& in_deps,
411 const std::vector<dependence>& out_deps)
412 {
413 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.data()};
414 ffrt_deps_t out {static_cast<uint32_t>(out_deps.size()), out_deps.data()};
415 return ffrt_submit_base(create_function_wrapper(func), &in, &out, nullptr);
416 }
417
418 /**
419 * @brief Submits a task with a specified attribute and input and output dependencies.
420 *
421 * @param func Indicates a task executor function closure.
422 * @param in_deps Indicates a pointer to the input dependencies.
423 * @param out_deps Indicates a pointer to the output dependencies.
424 * @param attr Indicates a task attribute.
425 * @since 10
426 * @version 1.0
427 */
submit(const std::function<void ()> & func,const std::vector<dependence> & in_deps,const std::vector<dependence> & out_deps,const task_attr & attr)428 static inline void submit(const std::function<void()>& func, const std::vector<dependence>& in_deps,
429 const std::vector<dependence>& out_deps, const task_attr& attr)
430 {
431 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.data()};
432 ffrt_deps_t out {static_cast<uint32_t>(out_deps.size()), out_deps.data()};
433 return ffrt_submit_base(create_function_wrapper(func), &in, &out, &attr);
434 }
435
436 /**
437 * @brief Submits a task without input and output dependencies, and obtains a task handle.
438 *
439 * @param func Indicates a task executor function closure.
440 * @return Returns a non-null task handle if the task is submitted;
441 returns a null pointer otherwise.
442 * @since 10
443 * @version 1.0
444 */
submit_h(std::function<void ()> && func)445 static inline task_handle submit_h(std::function<void()>&& func)
446 {
447 return ffrt_submit_h_base(create_function_wrapper(std::move(func)), nullptr, nullptr, nullptr);
448 }
449
450 /**
451 * @brief Submits a task with input dependencies only, and obtains a task handle.
452 *
453 * @param func Indicates a task executor function closure.
454 * @param in_deps Indicates a pointer to the input dependencies.
455 * @return Returns a non-null task handle if the task is submitted;
456 returns a null pointer otherwise.
457 * @since 10
458 * @version 1.0
459 */
submit_h(std::function<void ()> && func,std::initializer_list<dependence> in_deps)460 static inline task_handle submit_h(std::function<void()>&& func, std::initializer_list<dependence> in_deps)
461 {
462 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.begin()};
463 return ffrt_submit_h_base(create_function_wrapper(std::move(func)), &in, nullptr, nullptr);
464 }
465
466 /**
467 * @brief Submits a task with input and output dependencies, and obtains a task handle.
468 *
469 * @param func Indicates a task executor function closure.
470 * @param in_deps Indicates a pointer to the input dependencies.
471 * @param out_deps Indicates a pointer to the output dependencies.
472 * @return Returns a non-null task handle if the task is submitted;
473 returns a null pointer otherwise.
474 * @since 10
475 * @version 1.0
476 */
submit_h(std::function<void ()> && func,std::initializer_list<dependence> in_deps,std::initializer_list<dependence> out_deps)477 static inline task_handle submit_h(std::function<void()>&& func, std::initializer_list<dependence> in_deps,
478 std::initializer_list<dependence> out_deps)
479 {
480 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.begin()};
481 ffrt_deps_t out {static_cast<uint32_t>(out_deps.size()), out_deps.begin()};
482 return ffrt_submit_h_base(create_function_wrapper(std::move(func)), &in, &out, nullptr);
483 }
484
485 /**
486 * @brief Submits a task with a specified attribute and input and output dependencies, and obtains a task handle.
487 *
488 * @param func Indicates a task executor function closure.
489 * @param in_deps Indicates a pointer to the input dependencies.
490 * @param out_deps Indicates a pointer to the output dependencies.
491 * @param attr Indicates a task attribute.
492 * @return Returns a non-null task handle if the task is submitted;
493 returns a null pointer otherwise.
494 * @since 10
495 * @version 1.0
496 */
submit_h(std::function<void ()> && func,std::initializer_list<dependence> in_deps,std::initializer_list<dependence> out_deps,const task_attr & attr)497 static inline task_handle submit_h(std::function<void()>&& func, std::initializer_list<dependence> in_deps,
498 std::initializer_list<dependence> out_deps, const task_attr& attr)
499 {
500 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.begin()};
501 ffrt_deps_t out {static_cast<uint32_t>(out_deps.size()), out_deps.begin()};
502 return ffrt_submit_h_base(create_function_wrapper(std::move(func)), &in, &out, &attr);
503 }
504
505 /**
506 * @brief Submits a task with input dependencies only, and obtains a task handle.
507 *
508 * @param func Indicates a task executor function closure.
509 * @param in_deps Indicates a pointer to the input dependencies.
510 * @return Returns a non-null task handle if the task is submitted;
511 returns a null pointer otherwise.
512 * @since 10
513 * @version 1.0
514 */
submit_h(std::function<void ()> && func,const std::vector<dependence> & in_deps)515 static inline task_handle submit_h(std::function<void()>&& func, const std::vector<dependence>& in_deps)
516 {
517 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.data()};
518 return ffrt_submit_h_base(create_function_wrapper(std::move(func)), &in, nullptr, nullptr);
519 }
520
521 /**
522 * @brief Submits a task with input and output dependencies, and obtains a task handle.
523 *
524 * @param func Indicates a task executor function closure.
525 * @param in_deps Indicates a pointer to the input dependencies.
526 * @param out_deps Indicates a pointer to the output dependencies.
527 * @return Returns a non-null task handle if the task is submitted;
528 returns a null pointer otherwise.
529 * @since 10
530 * @version 1.0
531 */
submit_h(std::function<void ()> && func,const std::vector<dependence> & in_deps,const std::vector<dependence> & out_deps)532 static inline task_handle submit_h(std::function<void()>&& func, const std::vector<dependence>& in_deps,
533 const std::vector<dependence>& out_deps)
534 {
535 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.data()};
536 ffrt_deps_t out {static_cast<uint32_t>(out_deps.size()), out_deps.data()};
537 return ffrt_submit_h_base(create_function_wrapper(std::move(func)), &in, &out, nullptr);
538 }
539
540 /**
541 * @brief Submits a task with a specified attribute and input and output dependencies, and obtains a task handle.
542 *
543 * @param func Indicates a task executor function closure.
544 * @param in_deps Indicates a pointer to the input dependencies.
545 * @param out_deps Indicates a pointer to the output dependencies.
546 * @param attr Indicates a task attribute.
547 * @return Returns a non-null task handle if the task is submitted;
548 returns a null pointer otherwise.
549 * @since 10
550 * @version 1.0
551 */
submit_h(std::function<void ()> && func,const std::vector<dependence> & in_deps,const std::vector<dependence> & out_deps,const task_attr & attr)552 static inline task_handle submit_h(std::function<void()>&& func, const std::vector<dependence>& in_deps,
553 const std::vector<dependence>& out_deps, const task_attr& attr)
554 {
555 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.data()};
556 ffrt_deps_t out {static_cast<uint32_t>(out_deps.size()), out_deps.data()};
557 return ffrt_submit_h_base(create_function_wrapper(std::move(func)), &in, &out, &attr);
558 }
559
560 /**
561 * @brief Submits a task without input and output dependencies, and obtains a task handle.
562 *
563 * @param func Indicates a task executor function closure.
564 * @return Returns a non-null task handle if the task is submitted;
565 returns a null pointer otherwise.
566 * @since 10
567 * @version 1.0
568 */
submit_h(const std::function<void ()> & func)569 static inline task_handle submit_h(const std::function<void()>& func)
570 {
571 return ffrt_submit_h_base(create_function_wrapper(func), nullptr, nullptr, nullptr);
572 }
573
574 /**
575 * @brief Submits a task with input dependencies only, and obtains a task handle.
576 *
577 * @param func Indicates a task executor function closure.
578 * @param in_deps Indicates a pointer to the input dependencies.
579 * @return Returns a non-null task handle if the task is submitted;
580 returns a null pointer otherwise.
581 * @since 10
582 * @version 1.0
583 */
submit_h(const std::function<void ()> & func,std::initializer_list<dependence> in_deps)584 static inline task_handle submit_h(const std::function<void()>& func, std::initializer_list<dependence> in_deps)
585 {
586 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.begin()};
587 return ffrt_submit_h_base(create_function_wrapper(func), &in, nullptr, nullptr);
588 }
589
590 /**
591 * @brief Submits a task with input and output dependencies, and obtains a task handle.
592 *
593 * @param func Indicates a task executor function closure.
594 * @param in_deps Indicates a pointer to the input dependencies.
595 * @param out_deps Indicates a pointer to the output dependencies.
596 * @return Returns a non-null task handle if the task is submitted;
597 returns a null pointer otherwise.
598 * @since 10
599 * @version 1.0
600 */
submit_h(const std::function<void ()> & func,std::initializer_list<dependence> in_deps,std::initializer_list<dependence> out_deps)601 static inline task_handle submit_h(const std::function<void()>& func, std::initializer_list<dependence> in_deps,
602 std::initializer_list<dependence> out_deps)
603 {
604 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.begin()};
605 ffrt_deps_t out {static_cast<uint32_t>(out_deps.size()), out_deps.begin()};
606 return ffrt_submit_h_base(create_function_wrapper(func), &in, &out, nullptr);
607 }
608
609 /**
610 * @brief Submits a task with a specified attribute and input and output dependencies, and obtains a task handle.
611 *
612 * @param func Indicates a task executor function closure.
613 * @param in_deps Indicates a pointer to the input dependencies.
614 * @param out_deps Indicates a pointer to the output dependencies.
615 * @param attr Indicates a task attribute.
616 * @return Returns a non-null task handle if the task is submitted;
617 returns a null pointer otherwise.
618 * @since 10
619 * @version 1.0
620 */
submit_h(const std::function<void ()> & func,std::initializer_list<dependence> in_deps,std::initializer_list<dependence> out_deps,const task_attr & attr)621 static inline task_handle submit_h(const std::function<void()>& func, std::initializer_list<dependence> in_deps,
622 std::initializer_list<dependence> out_deps, const task_attr& attr)
623 {
624 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.begin()};
625 ffrt_deps_t out {static_cast<uint32_t>(out_deps.size()), out_deps.begin()};
626 return ffrt_submit_h_base(create_function_wrapper(func), &in, &out, &attr);
627 }
628
629 /**
630 * @brief Submits a task with input dependencies only, and obtains a task handle.
631 *
632 * @param func Indicates a task executor function closure.
633 * @param in_deps Indicates a pointer to the input dependencies.
634 * @return Returns a non-null task handle if the task is submitted;
635 returns a null pointer otherwise.
636 * @since 10
637 * @version 1.0
638 */
submit_h(const std::function<void ()> & func,const std::vector<dependence> & in_deps)639 static inline task_handle submit_h(const std::function<void()>& func, const std::vector<dependence>& in_deps)
640 {
641 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.data()};
642 return ffrt_submit_h_base(create_function_wrapper(func), &in, nullptr, nullptr);
643 }
644
645 /**
646 * @brief Submits a task with input and output dependencies, and obtains a task handle.
647 *
648 * @param func Indicates a task executor function closure.
649 * @param in_deps Indicates a pointer to the input dependencies.
650 * @param out_deps Indicates a pointer to the output dependencies.
651 * @return Returns a non-null task handle if the task is submitted;
652 returns a null pointer otherwise.
653 * @since 10
654 * @version 1.0
655 */
submit_h(const std::function<void ()> & func,const std::vector<dependence> & in_deps,const std::vector<dependence> & out_deps)656 static inline task_handle submit_h(const std::function<void()>& func, const std::vector<dependence>& in_deps,
657 const std::vector<dependence>& out_deps)
658 {
659 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.data()};
660 ffrt_deps_t out {static_cast<uint32_t>(out_deps.size()), out_deps.data()};
661 return ffrt_submit_h_base(create_function_wrapper(func), &in, &out, nullptr);
662 }
663
664 /**
665 * @brief Submits a task with a specified attribute and input and output dependencies, and obtains a task handle.
666 *
667 * @param func Indicates a task executor function closure.
668 * @param in_deps Indicates a pointer to the input dependencies.
669 * @param out_deps Indicates a pointer to the output dependencies.
670 * @param attr Indicates a task attribute.
671 * @return Returns a non-null task handle if the task is submitted;
672 returns a null pointer otherwise.
673 * @since 10
674 * @version 1.0
675 */
submit_h(const std::function<void ()> & func,const std::vector<dependence> & in_deps,const std::vector<dependence> & out_deps,const task_attr & attr)676 static inline task_handle submit_h(const std::function<void()>& func, const std::vector<dependence>& in_deps,
677 const std::vector<dependence>& out_deps, const task_attr& attr)
678 {
679 ffrt_deps_t in {static_cast<uint32_t>(in_deps.size()), in_deps.data()};
680 ffrt_deps_t out {static_cast<uint32_t>(out_deps.size()), out_deps.data()};
681 return ffrt_submit_h_base(create_function_wrapper(func), &in, &out, &attr);
682 }
683
684 /**
685 * @brief Waits until all submitted tasks are complete.
686 *
687 * @since 10
688 * @version 1.0
689 */
wait()690 static inline void wait()
691 {
692 ffrt_wait();
693 }
694
695 /**
696 * @brief Waits until dependent tasks are complete.
697 *
698 * @param deps Indicates a pointer to the dependent tasks.
699 * @since 10
700 * @version 1.0
701 */
wait(std::initializer_list<dependence> deps)702 static inline void wait(std::initializer_list<dependence> deps)
703 {
704 ffrt_deps_t d {static_cast<uint32_t>(deps.size()), deps.begin()};
705 ffrt_wait_deps(&d);
706 }
707
708 /**
709 * @brief Waits until dependent tasks are complete.
710 *
711 * @param deps Indicates a pointer to the dependent tasks.
712 * @since 10
713 * @version 1.0
714 */
wait(const std::vector<dependence> & deps)715 static inline void wait(const std::vector<dependence>& deps)
716 {
717 ffrt_deps_t d {static_cast<uint32_t>(deps.size()), deps.data()};
718 ffrt_wait_deps(&d);
719 }
720
721 namespace this_task {
update_qos(qos qos_)722 static inline int update_qos(qos qos_)
723 {
724 return ffrt_this_task_update_qos(qos_);
725 }
726
727 /**
728 * @brief Obtains the ID of this task.
729 *
730 * @return Returns the task ID.
731 * @since 10
732 * @version 1.0
733 */
get_id()734 static inline uint64_t get_id()
735 {
736 return ffrt_this_task_get_id();
737 }
738 } // namespace this_task
739 } // namespace ffrt
740 #endif
741