• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * @addtogroup FFRT
18  * @{
19  *
20  * @brief Provides FFRT C++ APIs.
21  *
22  * @since 10
23  */
24 
25 /**
26  * @file task.h
27  *
28  * @brief Declares the task interfaces in C++.
29  *
30  * @library libffrt.z.so
31  * @kit FunctionFlowRuntimeKit
32  * @syscap SystemCapability.Resourceschedule.Ffrt.Core
33  * @since 10
34  */
35 
36 #ifndef FFRT_API_CPP_TASK_H
37 #define FFRT_API_CPP_TASK_H
38 
39 #include <string>
40 #include <vector>
41 #include <functional>
42 #include "c/task.h"
43 
44 namespace ffrt {
45 /**
46  * @class task_attr
47  * @brief Represents the attributes of a task, such as its name, QoS level, delay, priority, and timeout.
48  *
49  * @since 10
50  */
51 class task_attr : public ffrt_task_attr_t {
52 public:
53 #if __has_builtin(__builtin_FUNCTION)
54     /**
55      * @brief Constructs a task_attr object with an optional function name as its name.
56      *
57      * If supported, the function name is automatically set as the task name.
58      *
59      * @since 10
60      */
61     task_attr(const char* func = __builtin_FUNCTION())
62     {
63         ffrt_task_attr_init(this);
64         ffrt_task_attr_set_name(this, func);
65     }
66 #else
67     /**
68      * @brief Constructs a task_attr object.
69      *
70      * @since 10
71      */
72     task_attr()
73     {
74         ffrt_task_attr_init(this);
75     }
76 #endif
77 
78     /**
79      * @brief Destroys the task_attr object, releasing its resources.
80      *
81      * @since 10
82      */
~task_attr()83     ~task_attr()
84     {
85         ffrt_task_attr_destroy(this);
86     }
87 
88     /**
89      * @brief Deleted copy constructor to prevent copying of the task_attr object.
90      */
91     task_attr(const task_attr&) = delete;
92 
93     /**
94      * @brief Deleted copy assignment operator to prevent assignment of the task_attr object.
95      */
96     task_attr& operator=(const task_attr&) = delete;
97 
98     /**
99      * @brief Sets a task name.
100      *
101      * @param name Indicates a pointer to the task name.
102      * @since 10
103      */
name(const char * name)104     inline task_attr& name(const char* name)
105     {
106         ffrt_task_attr_set_name(this, name);
107         return *this;
108     }
109 
110     /**
111      * @brief Obtains the task name.
112      *
113      * @return Returns a pointer to the task name.
114      * @since 10
115      */
name()116     inline const char* name() const
117     {
118         return ffrt_task_attr_get_name(this);
119     }
120 
121     /**
122      * @brief Sets the QoS for this task.
123      *
124      * @param qos Indicates the QoS.
125      * @since 10
126      */
qos(qos qos_)127     inline task_attr& qos(qos qos_)
128     {
129         ffrt_task_attr_set_qos(this, qos_);
130         return *this;
131     }
132 
133     /**
134      * @brief Obtains the QoS of this task.
135      *
136      * @return Returns the QoS.
137      * @since 10
138      */
qos()139     inline int qos() const
140     {
141         return ffrt_task_attr_get_qos(this);
142     }
143 
144     /**
145      * @brief Sets the delay time for this task.
146      *
147      * @param delay_us Indicates the delay time, in microseconds.
148      * @since 10
149      */
delay(uint64_t delay_us)150     inline task_attr& delay(uint64_t delay_us)
151     {
152         ffrt_task_attr_set_delay(this, delay_us);
153         return *this;
154     }
155 
156     /**
157      * @brief Obtains the delay time of this task.
158      *
159      * @return Returns the delay time.
160      * @since 10
161      */
delay()162     inline uint64_t delay() const
163     {
164         return ffrt_task_attr_get_delay(this);
165     }
166 
167     /**
168      * @brief Sets the priority for this task.
169      *
170      * @param priority Indicates the execute priority of concurrent queue task.
171      * @since 12
172      */
priority(ffrt_queue_priority_t prio)173     inline task_attr& priority(ffrt_queue_priority_t prio)
174     {
175         ffrt_task_attr_set_queue_priority(this, prio);
176         return *this;
177     }
178 
179     /**
180      * @brief Obtains the priority of this task.
181      *
182      * @return Returns the priority of concurrent queue task.
183      * @since 12
184      */
priority()185     inline ffrt_queue_priority_t priority() const
186     {
187         return ffrt_task_attr_get_queue_priority(this);
188     }
189 
190     /**
191      * @brief Sets the stack size for this task.
192      *
193      * @param size Indicates the task stack size, unit is byte.
194      * @since 12
195      */
stack_size(uint64_t size)196     inline task_attr& stack_size(uint64_t size)
197     {
198         ffrt_task_attr_set_stack_size(this, size);
199         return *this;
200     }
201 
202     /**
203      * @brief Obtains the stack size of this task.
204      *
205      * @return Returns task stack size, unit is byte.
206      * @since 12
207      */
stack_size()208     inline uint64_t stack_size() const
209     {
210         return ffrt_task_attr_get_stack_size(this);
211     }
212 
213     /**
214      * @brief Sets the task schedule timeout.
215      *
216      * The lower limit of timeout value is 1 ms, if the value is less than 1 ms, it will be set to 1 ms.
217      *
218      * @param timeout_us task scheduler timeout.
219      */
timeout(uint64_t timeout_us)220     inline task_attr& timeout(uint64_t timeout_us)
221     {
222         ffrt_task_attr_set_timeout(this, timeout_us);
223         return *this;
224     }
225 
226     /**
227      * @brief Obtains the task schedule timeout.
228      *
229      * @return Returns task scheduler timeout.
230      */
timeout()231     inline uint64_t timeout() const
232     {
233         return ffrt_task_attr_get_timeout(this);
234     }
235 };
236 
237 /**
238  * @class task_handle
239  * @brief Represents a handle for a submitted task, allowing operations such as
240  *        querying task IDs and managing task resources.
241  *
242  * @since 10
243  */
244 class task_handle {
245 public:
246     /**
247      * @brief Default constructor for task_handle.
248      *
249      * @since 10
250      */
task_handle()251     task_handle() : p(nullptr)
252     {
253     }
254 
255     /**
256      * @brief Constructs a task_handle object from a raw task handle pointer.
257      *
258      * @param p The raw task handle pointer.
259      * @since 10
260      */
task_handle(ffrt_task_handle_t p)261     task_handle(ffrt_task_handle_t p) : p(p)
262     {
263     }
264 
265     /**
266      * @brief Destroys the task_handle object, releasing any associated resources.
267      *
268      * @since 10
269      */
~task_handle()270     ~task_handle()
271     {
272         if (p) {
273             ffrt_task_handle_destroy(p);
274         }
275     }
276 
277     /**
278      * @brief Deleted copy constructor to prevent copying of the task_handle object.
279      */
280     task_handle(task_handle const&) = delete;
281 
282     /**
283      * @brief Deleted copy assignment operator to prevent assignment of the task_handle object.
284      */
285     task_handle& operator=(task_handle const&) = delete;
286 
287     /**
288      * @brief Move constructor for task_handle.
289      *
290      * @param h The task_handle object to move from.
291      * @since 10
292      */
task_handle(task_handle && h)293     inline task_handle(task_handle&& h)
294     {
295         *this = std::move(h);
296     }
297 
298     /**
299      * @brief get gid from task handle.
300      *
301      * @return Return gid.
302      */
get_id()303     inline uint64_t get_id() const
304     {
305         return ffrt_task_handle_get_id(p);
306     }
307 
308     /**
309      * @brief Move assignment operator for task_handle.
310      *
311      * @param h The task_handle object to move from.
312      * @return Returns the current task_handle object.
313      * @since 10
314      */
315     inline task_handle& operator=(task_handle&& h)
316     {
317         if (this != &h) {
318             if (p) {
319                 ffrt_task_handle_destroy(p);
320             }
321             p = h.p;
322             h.p = nullptr;
323         }
324         return *this;
325     }
326 
327     /**
328      * @brief Implicit conversion to a void pointer.
329      *
330      * @return Returns the raw task handle pointer.
331      * @since 10
332      */
333     inline operator void* () const
334     {
335         return p;
336     }
337 
338 private:
339     ffrt_task_handle_t p = nullptr; ///< Handle to the underlying task.
340 };
341 
342 /**
343  * @struct dependence
344  * @brief Represents a dependency for a task, which can either be a data dependency or a task dependency.
345  */
346 struct dependence : ffrt_dependence_t {
347     /**
348      * @brief Constructs a data dependency.
349      *
350      * @param d A pointer to the data dependency.
351      * @since 10
352      */
dependencedependence353     dependence(const void* d)
354     {
355         type = ffrt_dependence_data;
356         ptr = d;
357     }
358 
359     /**
360      * @brief Constructs a task dependency.
361      *
362      * @param h A reference to a task_handle representing the dependency.
363      * @since 10
364      */
dependencedependence365     dependence(const task_handle& h)
366     {
367         type = ffrt_dependence_task;
368         ptr = h;
369         ffrt_task_handle_inc_ref(const_cast<ffrt_task_handle_t>(ptr));
370     }
371 
372     /**
373      * @brief Copy constructor for dependence.
374      *
375      * @param other The dependence object to copy from.
376      * @since 10
377      */
dependencedependence378     dependence(const dependence& other)
379     {
380         (*this) = other;
381     }
382 
383     /**
384      * @brief Move constructor for dependence.
385      *
386      * @param other The dependence object to move from.
387      * @since 10
388      */
dependencedependence389     dependence(dependence&& other)
390     {
391         (*this) = std::move(other);
392     }
393 
394     /**
395      * @brief Copy assignment operator for dependence.
396      *
397      * @param other The dependence object to copy from.
398      * @return Returns the current dependence object.
399      * @since 10
400      */
401     dependence& operator=(const dependence& other)
402     {
403         if (this != &other) {
404             type = other.type;
405             ptr = other.ptr;
406             if (type == ffrt_dependence_task) {
407                 ffrt_task_handle_inc_ref(const_cast<ffrt_task_handle_t>(ptr));
408             }
409         }
410         return *this;
411     }
412 
413     /**
414      * @brief Move assignment operator for dependence.
415      *
416      * @param other The dependence object to move from.
417      * @return Returns the current dependence object.
418      * @since 10
419      */
420     dependence& operator=(dependence&& other)
421     {
422         if (this != &other) {
423             type = other.type;
424             ptr = other.ptr;
425             other.ptr = nullptr;
426         }
427         return *this;
428     }
429 
430     /**
431      * @brief Destructor for dependence.
432      *
433      * @since 10
434      */
~dependencedependence435     ~dependence()
436     {
437         if (type == ffrt_dependence_task && ptr) {
438             ffrt_task_handle_dec_ref(const_cast<ffrt_task_handle_t>(ptr));
439         }
440     }
441 };
442 
443 /**
444  * @struct function
445  * @brief Represents a function wrapper for task execution.
446  *
447  * This template struct is used to wrap a function closure for task execution.
448  *
449  * @tparam T The type of the function closure.
450  * @since 10
451  */
452 template<class T>
453 struct function {
454     ffrt_function_header_t header;
455     T closure;
456 };
457 
458 /**
459  * @brief Executes a function wrapper.
460  *
461  * This function is used to execute a function wrapper.
462  *
463  * @tparam T The type of the function closure.
464  * @param t A pointer to the function wrapper.
465  * @since 10
466  */
467 template<class T>
exec_function_wrapper(void * t)468 void exec_function_wrapper(void* t)
469 {
470     auto f = reinterpret_cast<function<std::decay_t<T>>*>(t);
471     f->closure();
472 }
473 
474 /**
475  * @brief Destroys a function wrapper.
476  *
477  * This function is used to destroy a function wrapper.
478  *
479  * @tparam T The type of the function closure.
480  * @param t A pointer to the function wrapper.
481  * @since 10
482  */
483 template<class T>
destroy_function_wrapper(void * t)484 void destroy_function_wrapper(void* t)
485 {
486     auto f = reinterpret_cast<function<std::decay_t<T>>*>(t);
487     f->closure = nullptr;
488 }
489 
490 /**
491  * @brief Creates a function wrapper.
492  *
493  * This function is used to create a function wrapper for task submission.
494  *
495  * @tparam T The type of the function closure.
496  * @param func The function closure.
497  * @param kind The function kind (optional).
498  * @return Returns a pointer to the function wrapper header.
499  * @since 10
500  */
501 template<class T>
502 inline ffrt_function_header_t* create_function_wrapper(T&& func,
503     ffrt_function_kind_t kind = ffrt_function_kind_general)
504 {
505     using function_type = function<std::decay_t<T>>;
506     static_assert(sizeof(function_type) <= ffrt_auto_managed_function_storage_size,
507         "size of function must be less than ffrt_auto_managed_function_storage_size");
508 
509     auto p = ffrt_alloc_auto_managed_function_storage_base(kind);
510     auto f = new (p)function_type;
511     f->header.exec = exec_function_wrapper<T>;
512     f->header.destroy = destroy_function_wrapper<T>;
513     f->header.reserve[0] = 0;
514     f->closure = std::forward<T>(func);
515     return reinterpret_cast<ffrt_function_header_t*>(f);
516 }
517 
518 /**
519  * @brief Submits a task without input and output dependencies.
520  *
521  * @param func Indicates a task executor function closure.
522  * @param attr Indicates a task attribute.
523  * @since 10
524  */
525 static inline void submit(std::function<void()>&& func, const task_attr& attr = {})
526 {
527     return ffrt_submit_base(create_function_wrapper(std::move(func)), nullptr, nullptr, &attr);
528 }
529 
530 /**
531  * @brief Submits a task with input dependencies only.
532  *
533  * @param func Indicates a task executor function closure.
534  * @param in_deps Indicates a pointer to the input dependencies.
535  * @param attr Indicates a task attribute.
536  * @since 10
537  */
538 static inline void submit(std::function<void()>&& func, std::initializer_list<dependence> in_deps,
539     const task_attr& attr = {})
540 {
541     ffrt_deps_t in{static_cast<uint32_t>(in_deps.size()), in_deps.begin()};
542     return ffrt_submit_base(create_function_wrapper(std::move(func)), &in, nullptr, &attr);
543 }
544 
545 /**
546  * @brief Submits a task with input and output dependencies.
547  *
548  * @param func Indicates a task executor function closure.
549  * @param in_deps Indicates a pointer to the input dependencies.
550  * @param out_deps Indicates a pointer to the output dependencies.
551  * @param attr Indicates a task attribute.
552  * @since 10
553  */
554 static inline void submit(std::function<void()>&& func, std::initializer_list<dependence> in_deps,
555     std::initializer_list<dependence> out_deps, const task_attr& attr = {})
556 {
557     ffrt_deps_t in{static_cast<uint32_t>(in_deps.size()), in_deps.begin()};
558     ffrt_deps_t out{static_cast<uint32_t>(out_deps.size()), out_deps.begin()};
559     return ffrt_submit_base(create_function_wrapper(std::move(func)), &in, &out, &attr);
560 }
561 
562 /**
563  * @brief Submits a task with input dependencies only.
564  *
565  * @param func Indicates a task executor function closure.
566  * @param in_deps Indicates a pointer to the input dependencies.
567  * @param attr Indicates a task attribute.
568  * @since 10
569  */
570 static inline void submit(std::function<void()>&& func, const std::vector<dependence>& in_deps,
571     const task_attr& attr = {})
572 {
573     ffrt_deps_t in{static_cast<uint32_t>(in_deps.size()), in_deps.data()};
574     return ffrt_submit_base(create_function_wrapper(std::move(func)), &in, nullptr, &attr);
575 }
576 
577 /**
578  * @brief Submits a task with input and output dependencies.
579  *
580  * @param func Indicates a task executor function closure.
581  * @param in_deps Indicates a pointer to the input dependencies.
582  * @param out_deps Indicates a pointer to the output dependencies.
583  * @param attr Indicates a task attribute.
584  * @since 10
585  */
586 static inline void submit(std::function<void()>&& func, const std::vector<dependence>& in_deps,
587     const std::vector<dependence>& out_deps, const task_attr& attr = {})
588 {
589     ffrt_deps_t in{static_cast<uint32_t>(in_deps.size()), in_deps.data()};
590     ffrt_deps_t out{static_cast<uint32_t>(out_deps.size()), out_deps.data()};
591     return ffrt_submit_base(create_function_wrapper(std::move(func)), &in, &out, &attr);
592 }
593 
594 /**
595  * @brief Submits a task without input and output dependencies.
596  *
597  * @param func Indicates a task executor function closure.
598  * @param attr Indicates a task attribute.
599  * @since 10
600  */
601 static inline void submit(const std::function<void()>& func, const task_attr& attr = {})
602 {
603     return ffrt_submit_base(create_function_wrapper(func), nullptr, nullptr, &attr);
604 }
605 
606 /**
607  * @brief Submits a task with input dependencies only.
608  *
609  * @param func Indicates a task executor function closure.
610  * @param in_deps Indicates a pointer to the input dependencies.
611  * @param attr Indicates a task attribute.
612  * @since 10
613  */
614 static inline void submit(const std::function<void()>& func, std::initializer_list<dependence> in_deps,
615     const task_attr& attr = {})
616 {
617     ffrt_deps_t in{static_cast<uint32_t>(in_deps.size()), in_deps.begin()};
618     return ffrt_submit_base(create_function_wrapper(func), &in, nullptr, &attr);
619 }
620 
621 /**
622  * @brief Submits a task with input and output dependencies.
623  *
624  * @param func Indicates a task executor function closure.
625  * @param in_deps Indicates a pointer to the input dependencies.
626  * @param out_deps Indicates a pointer to the output dependencies.
627  * @param attr Indicates a task attribute.
628  * @since 10
629  */
630 static inline void submit(const std::function<void()>& func, std::initializer_list<dependence> in_deps,
631     std::initializer_list<dependence> out_deps, const task_attr& attr = {})
632 {
633     ffrt_deps_t in{static_cast<uint32_t>(in_deps.size()), in_deps.begin()};
634     ffrt_deps_t out{static_cast<uint32_t>(out_deps.size()), out_deps.begin()};
635     return ffrt_submit_base(create_function_wrapper(func), &in, &out, &attr);
636 }
637 
638 /**
639  * @brief Submits a task with input dependencies only.
640  *
641  * @param func Indicates a task executor function closure.
642  * @param in_deps Indicates a pointer to the input dependencies.
643  * @param attr Indicates a task attribute.
644  * @since 10
645  */
646 static inline void submit(const std::function<void()>& func, const std::vector<dependence>& in_deps,
647     const task_attr& attr = {})
648 {
649     ffrt_deps_t in{static_cast<uint32_t>(in_deps.size()), in_deps.data()};
650     return ffrt_submit_base(create_function_wrapper(func), &in, nullptr, &attr);
651 }
652 
653 /**
654  * @brief Submits a task with input and output dependencies.
655  *
656  * @param func Indicates a task executor function closure.
657  * @param in_deps Indicates a pointer to the input dependencies.
658  * @param out_deps Indicates a pointer to the output dependencies.
659  * @param attr Indicates a task attribute.
660  * @since 10
661  */
662 static inline void submit(const std::function<void()>& func, const std::vector<dependence>& in_deps,
663     const std::vector<dependence>& out_deps, const task_attr& attr = {})
664 {
665     ffrt_deps_t in{static_cast<uint32_t>(in_deps.size()), in_deps.data()};
666     ffrt_deps_t out{static_cast<uint32_t>(out_deps.size()), out_deps.data()};
667     return ffrt_submit_base(create_function_wrapper(func), &in, &out, &attr);
668 }
669 
670 /**
671  * @brief Submits a task without input and output dependencies, and obtains a task handle.
672  *
673  * @param func Indicates a task executor function closure.
674  * @param attr Indicates a task attribute.
675  * @return Returns a non-null task handle if the task is submitted;
676            returns a null pointer otherwise.
677  * @since 10
678  */
679 static inline task_handle submit_h(std::function<void()>&& func, const task_attr& attr = {})
680 {
681     return ffrt_submit_h_base(create_function_wrapper(std::move(func)), nullptr, nullptr, &attr);
682 }
683 
684 /**
685  * @brief Submits a task with input dependencies only, and obtains a task handle.
686  *
687  * @param func Indicates a task executor function closure.
688  * @param in_deps Indicates a pointer to the input dependencies.
689  * @param attr Indicates a task attribute.
690  * @return Returns a non-null task handle if the task is submitted;
691            returns a null pointer otherwise.
692  * @since 10
693  */
694 static inline task_handle submit_h(std::function<void()>&& func, std::initializer_list<dependence> in_deps,
695     const task_attr& attr = {})
696 {
697     ffrt_deps_t in{static_cast<uint32_t>(in_deps.size()), in_deps.begin()};
698     return ffrt_submit_h_base(create_function_wrapper(std::move(func)), &in, nullptr, &attr);
699 }
700 
701 /**
702  * @brief Submits a task with input and output dependencies, and obtains a task handle.
703  *
704  * @param func Indicates a task executor function closure.
705  * @param in_deps Indicates a pointer to the input dependencies.
706  * @param out_deps Indicates a pointer to the output dependencies.
707  * @param attr Indicates a task attribute.
708  * @return Returns a non-null task handle if the task is submitted;
709            returns a null pointer otherwise.
710  * @since 10
711  */
712 static inline task_handle submit_h(std::function<void()>&& func, std::initializer_list<dependence> in_deps,
713     std::initializer_list<dependence> out_deps, const task_attr& attr = {})
714 {
715     ffrt_deps_t in{static_cast<uint32_t>(in_deps.size()), in_deps.begin()};
716     ffrt_deps_t out{static_cast<uint32_t>(out_deps.size()), out_deps.begin()};
717     return ffrt_submit_h_base(create_function_wrapper(std::move(func)), &in, &out, &attr);
718 }
719 
720 /**
721  * @brief Submits a task with input dependencies only, and obtains a task handle.
722  *
723  * @param func Indicates a task executor function closure.
724  * @param in_deps Indicates a pointer to the input dependencies.
725  * @param attr Indicates a task attribute.
726  * @return Returns a non-null task handle if the task is submitted;
727            returns a null pointer otherwise.
728  * @since 10
729  */
730 static inline task_handle submit_h(std::function<void()>&& func, const std::vector<dependence>& in_deps,
731     const task_attr& attr = {})
732 {
733     ffrt_deps_t in{static_cast<uint32_t>(in_deps.size()), in_deps.data()};
734     return ffrt_submit_h_base(create_function_wrapper(std::move(func)), &in, nullptr, &attr);
735 }
736 
737 /**
738  * @brief Submits a task with input and output dependencies, and obtains a task handle.
739  *
740  * @param func Indicates a task executor function closure.
741  * @param in_deps Indicates a pointer to the input dependencies.
742  * @param out_deps Indicates a pointer to the output dependencies.
743  * @param attr Indicates a task attribute.
744  * @return Returns a non-null task handle if the task is submitted;
745            returns a null pointer otherwise.
746  * @since 10
747  */
748 static inline task_handle submit_h(std::function<void()>&& func, const std::vector<dependence>& in_deps,
749     const std::vector<dependence>& out_deps, const task_attr& attr = {})
750 {
751     ffrt_deps_t in{static_cast<uint32_t>(in_deps.size()), in_deps.data()};
752     ffrt_deps_t out{static_cast<uint32_t>(out_deps.size()), out_deps.data()};
753     return ffrt_submit_h_base(create_function_wrapper(std::move(func)), &in, &out, &attr);
754 }
755 
756 /**
757  * @brief Submits a task without input and output dependencies, and obtains a task handle.
758  *
759  * @param func Indicates a task executor function closure.
760  * @param attr Indicates a task attribute.
761  * @return Returns a non-null task handle if the task is submitted;
762            returns a null pointer otherwise.
763  * @since 10
764  */
765 static inline task_handle submit_h(const std::function<void()>& func, const task_attr& attr = {})
766 {
767     return ffrt_submit_h_base(create_function_wrapper(func), nullptr, nullptr, &attr);
768 }
769 
770 /**
771  * @brief Submits a task with input dependencies only, and obtains a task handle.
772  *
773  * @param func Indicates a task executor function closure.
774  * @param in_deps Indicates a pointer to the input dependencies.
775  * @param attr Indicates a task attribute.
776  * @return Returns a non-null task handle if the task is submitted;
777            returns a null pointer otherwise.
778  * @since 10
779  */
780 static inline task_handle submit_h(const std::function<void()>& func, std::initializer_list<dependence> in_deps,
781     const task_attr& attr = {})
782 {
783     ffrt_deps_t in{static_cast<uint32_t>(in_deps.size()), in_deps.begin()};
784     return ffrt_submit_h_base(create_function_wrapper(func), &in, nullptr, &attr);
785 }
786 
787 /**
788  * @brief Submits a task with input and output dependencies, and obtains a task handle.
789  *
790  * @param func Indicates a task executor function closure.
791  * @param in_deps Indicates a pointer to the input dependencies.
792  * @param out_deps Indicates a pointer to the output dependencies.
793  * @param attr Indicates a task attribute.
794  * @return Returns a non-null task handle if the task is submitted;
795            returns a null pointer otherwise.
796  * @since 10
797  */
798 static inline task_handle submit_h(const std::function<void()>& func, std::initializer_list<dependence> in_deps,
799     std::initializer_list<dependence> out_deps, const task_attr& attr = {})
800 {
801     ffrt_deps_t in{static_cast<uint32_t>(in_deps.size()), in_deps.begin()};
802     ffrt_deps_t out{static_cast<uint32_t>(out_deps.size()), out_deps.begin()};
803     return ffrt_submit_h_base(create_function_wrapper(func), &in, &out, &attr);
804 }
805 
806 /**
807  * @brief Submits a task with input dependencies only, and obtains a task handle.
808  *
809  * @param func Indicates a task executor function closure.
810  * @param in_deps Indicates a pointer to the input dependencies.
811  * @param attr Indicates a task attribute.
812  * @return Returns a non-null task handle if the task is submitted;
813            returns a null pointer otherwise.
814  * @since 10
815  */
816 static inline task_handle submit_h(const std::function<void()>& func, const std::vector<dependence>& in_deps,
817     const task_attr& attr = {})
818 {
819     ffrt_deps_t in{static_cast<uint32_t>(in_deps.size()), in_deps.data()};
820     return ffrt_submit_h_base(create_function_wrapper(func), &in, nullptr, &attr);
821 }
822 
823 /**
824  * @brief Submits a task with input and output dependencies, and obtains a task handle.
825  *
826  * @param func Indicates a task executor function closure.
827  * @param in_deps Indicates a pointer to the input dependencies.
828  * @param out_deps Indicates a pointer to the output dependencies.
829  * @param attr Indicates a task attribute.
830  * @return Returns a non-null task handle if the task is submitted;
831            returns a null pointer otherwise.
832  * @since 10
833  */
834 static inline task_handle submit_h(const std::function<void()>& func, const std::vector<dependence>& in_deps,
835     const std::vector<dependence>& out_deps, const task_attr& attr = {})
836 {
837     ffrt_deps_t in{static_cast<uint32_t>(in_deps.size()), in_deps.data()};
838     ffrt_deps_t out{static_cast<uint32_t>(out_deps.size()), out_deps.data()};
839     return ffrt_submit_h_base(create_function_wrapper(func), &in, &out, &attr);
840 }
841 
842 /**
843  * @brief Waits until all submitted tasks are complete.
844  *
845  * @since 10
846  */
wait()847 static inline void wait()
848 {
849     ffrt_wait();
850 }
851 
852 /**
853  * @brief Waits until dependent tasks are complete.
854  *
855  * @param deps Indicates a pointer to the dependent tasks.
856  * @since 10
857  */
wait(std::initializer_list<dependence> deps)858 static inline void wait(std::initializer_list<dependence> deps)
859 {
860     ffrt_deps_t d{static_cast<uint32_t>(deps.size()), deps.begin()};
861     ffrt_wait_deps(&d);
862 }
863 
864 /**
865  * @brief Waits until dependent tasks are complete.
866  *
867  * @param deps Indicates a pointer to the dependent tasks.
868  * @since 10
869  */
wait(const std::vector<dependence> & deps)870 static inline void wait(const std::vector<dependence>& deps)
871 {
872     ffrt_deps_t d{static_cast<uint32_t>(deps.size()), deps.data()};
873     ffrt_wait_deps(&d);
874 }
875 
876 /**
877  * @brief Sets the thread stack size of a specified QoS level.
878  *
879  * @param qos_ Indicates the QoS.
880  * @param stack_size Indicates the thread stack size.
881  * @return Returns ffrt_success if the stack size set success;
882            returns ffrt_error_inval if qos_ or stack_size invalid;
883            returns ffrt_error otherwise.
884  */
set_worker_stack_size(qos qos_,size_t stack_size)885 static inline ffrt_error_t set_worker_stack_size(qos qos_, size_t stack_size)
886 {
887     return ffrt_set_worker_stack_size(qos_, stack_size);
888 }
889 
890 /**
891  * @namespace ffrt::this_task
892  * @brief Contains utility functions for the currently executing task.
893  */
894 namespace this_task {
895 /**
896  * @brief Updates the QoS level of the currently executing task.
897  *
898  * @param qos_ The new QoS level.
899  * @return Returns the updated QoS level.
900  * @since 10
901  */
update_qos(qos qos_)902 static inline int update_qos(qos qos_)
903 {
904     return ffrt_this_task_update_qos(qos_);
905 }
906 
907 /**
908  * @brief Obtains the ID of this task.
909  *
910  * @return Returns the task ID.
911  * @since 10
912  */
get_id()913 static inline uint64_t get_id()
914 {
915     return ffrt_this_task_get_id();
916 }
917 } // namespace this_task
918 } // namespace ffrt
919 
920 #endif // FFRT_API_CPP_TASK_H
921 /** @} */