The queue.h file declares the queue APIs in C.
File to include: <ffrt/queue.h>
Library: libffrt.z.so
System capability: SystemCapability.Resourceschedule.Ffrt.Core
Since: 10
Related module: FFRT
| Name | typedef Keyword | Description |
|---|---|---|
| ffrt_queue_type_t | ffrt_queue_type_t | Enumerates the queue types. |
| Name | Description |
|---|---|
| FFRT_C_API int ffrt_queue_attr_init(ffrt_queue_attr_t* attr) | Initializes a queue attribute. When the queue attribute is no longer used, it must be destroyed by ffrt_queue_attr_destroy. |
| FFRT_C_API void ffrt_queue_attr_destroy(ffrt_queue_attr_t* attr) | Destroys a queue attribute. The queue attribute must have been initialized by ffrt_queue_attr_init. |
| FFRT_C_API void ffrt_queue_attr_set_qos(ffrt_queue_attr_t* attr, ffrt_qos_t qos) | Sets the QoS of a queue attribute. |
| FFRT_C_API ffrt_qos_t ffrt_queue_attr_get_qos(const ffrt_queue_attr_t* attr) | Obtains the QoS of a queue attribute. |
| FFRT_C_API void ffrt_queue_attr_set_timeout(ffrt_queue_attr_t* attr, uint64_t timeout_us) | Sets the task execution timeout of a queue attribute. |
| FFRT_C_API uint64_t ffrt_queue_attr_get_timeout(const ffrt_queue_attr_t* attr) | Obtains the task execution timeout of a queue attribute. |
| FFRT_C_API void ffrt_queue_attr_set_callback(ffrt_queue_attr_t* attr, ffrt_function_header_t* f) | Sets the timeout callback function of a queue attribute. The callback is triggered when the execution time of a task in the queue exceeds the timeout set by ffrt_queue_attr_set_timeout. |
| FFRT_C_API ffrt_function_header_t* ffrt_queue_attr_get_callback(const ffrt_queue_attr_t* attr) | Obtains the timeout callback function of a queue attribute. |
| FFRT_C_API void ffrt_queue_attr_set_max_concurrency(ffrt_queue_attr_t* attr, const int max_concurrency) | Sets the maximum concurrency of a concurrent queue attribute. |
| FFRT_C_API int ffrt_queue_attr_get_max_concurrency(const ffrt_queue_attr_t* attr) | Obtains the maximum concurrency of a concurrent queue attribute. |
| FFRT_C_API void ffrt_queue_attr_set_thread_mode(ffrt_queue_attr_t* attr, bool mode) | Sets the execution mode of a queue attribute. This API specifies whether tasks in the queue are executed in coroutine mode or thread mode. By default, tasks are executed in coroutine mode. Setting mode to true enables thread-based execution. |
| FFRT_C_API bool ffrt_queue_attr_get_thread_mode(const ffrt_queue_attr_t* attr) | Obtains the execution mode of a queue attribute. |
| FFRT_C_API ffrt_queue_t ffrt_queue_create(ffrt_queue_type_t type, const char* name, const ffrt_queue_attr_t* attr) | Creates a queue. When the queue is no longer used, it must be destroyed by ffrt_queue_destroy. |
| FFRT_C_API void ffrt_queue_destroy(ffrt_queue_t queue) | Destroys a queue. The queue must have been created by ffrt_queue_create. During destruction, tasks that have not started execution are canceled, and the function blocks until the tasks being executed are completed. |
| FFRT_C_API void ffrt_queue_submit(ffrt_queue_t queue, ffrt_function_header_t* f, const ffrt_task_attr_t* attr) | Submits a task to a queue. |
| FFRT_C_API ffrt_task_handle_t ffrt_queue_submit_h(ffrt_queue_t queue, ffrt_function_header_t* f, const ffrt_task_attr_t* attr) | Submits a task to a queue and obtains a task handle. |
| FFRT_C_API void ffrt_queue_submit_f(ffrt_queue_t queue, ffrt_function_t func, void* arg, const ffrt_task_attr_t* attr) | Submits a task to a queue. This is a simplified form of ffrt_queue_submit. This API wraps the given task function and its argument into a task wrapper (ffrt_function_kind_queue) for queue submission. The task destruction callback (after_func) used to handle post-execution cleanup is set to NULL, so any additional cleanup action is omitted. The generated task wrapper is then submitted to the specified queue through ffrt_queue_submit. |
| FFRT_C_API ffrt_task_handle_t ffrt_queue_submit_h_f(ffrt_queue_t queue, ffrt_function_t func, void* arg, const ffrt_task_attr_t* attr) | Submits a task to a queue and obtains a task handle. This is a simplified form of ffrt_queue_submit_h. This API wraps the given task function and its argument into a task wrapper (ffrt_function_kind_queue) for queue submission. The task destruction callback (after_func) used to handle post-execution cleanup is set to NULL, so any additional cleanup action is omitted. The generated task wrapper is then submitted to the specified queue through ffrt_queue_submit_h. |
| FFRT_C_API void ffrt_queue_wait(ffrt_task_handle_t handle) | Waits for the task in the queue to complete execution. |
| FFRT_C_API int ffrt_queue_cancel(ffrt_task_handle_t handle) | Cancels a task in the queue. A task that has started execution cannot be canceled. |
| FFRT_C_API ffrt_queue_t ffrt_get_main_queue(void) | Obtains the application main thread queue. |
| FFRT_C_API ffrt_queue_t ffrt_get_current_queue(void) | Obtains the application Worker (ArkTS) thread queue. |
enum ffrt_queue_type_t
Description
Enumerates the queue types.
Since: 12
| Enum Item | Description |
|---|---|
| ffrt_queue_serial | Serial queue. |
| ffrt_queue_concurrent | Concurrent queue. |
| ffrt_queue_max | Maximum valid queue type value, used as a sentinel (for example, for iteration). |
FFRT_C_API int ffrt_queue_attr_init(ffrt_queue_attr_t* attr)
Description
Initializes a queue attribute. When the queue attribute is no longer used, it must be destroyed by ffrt_queue_attr_destroy.
Since: 10
Parameters
| Name | Description |
|---|---|
| ffrt_queue_attr_t* attr | Pointer to the queue attribute. |
Returns
| Type | Description |
|---|---|
| FFRT_C_API int | 0 is returned if the queue attribute is initialized successfully; otherwise, -1 is returned. |
FFRT_C_API void ffrt_queue_attr_destroy(ffrt_queue_attr_t* attr)
Description
Destroys a queue attribute. The queue attribute must have been initialized by ffrt_queue_attr_init.
Since: 10
Parameters
| Name | Description |
|---|---|
| ffrt_queue_attr_t* attr | Pointer to the queue attribute. |
FFRT_C_API void ffrt_queue_attr_set_qos(ffrt_queue_attr_t* attr, ffrt_qos_t qos)
Description
Sets the QoS of a queue attribute.
Since: 10
Parameters
| Name | Description |
|---|---|
| ffrt_queue_attr_t* attr | Pointer to the queue attribute. |
| ffrt_qos_t qos | QoS level. For the value range, see the ffrt_qos_t enum definition. |
FFRT_C_API ffrt_qos_t ffrt_queue_attr_get_qos(const ffrt_queue_attr_t* attr)
Description
Obtains the QoS of a queue attribute.
Since: 10
Parameters
| Name | Description |
|---|---|
| const ffrt_queue_attr_t* attr | Pointer to the queue attribute. |
Returns
| Type | Description |
|---|---|
| FFRT_C_API ffrt_qos_t | QoS level. For the value range, see the ffrt_qos_t enum definition. |
FFRT_C_API void ffrt_queue_attr_set_timeout(ffrt_queue_attr_t* attr, uint64_t timeout_us)
Description
Sets the task execution timeout of a queue attribute.
Since: 10
Parameters
| Name | Description |
|---|---|
| ffrt_queue_attr_t* attr | Pointer to the queue attribute. |
| uint64_t timeout_us | Execution timeout of the queue task, in microseconds. The lower limit is 1000 microseconds (1 millisecond). Values less than 1000 are forcibly set to 1000. |
FFRT_C_API uint64_t ffrt_queue_attr_get_timeout(const ffrt_queue_attr_t* attr)
Description
Obtains the task execution timeout of a queue attribute.
Since: 10
Parameters
| Name | Description |
|---|---|
| const ffrt_queue_attr_t* attr | Pointer to the queue attribute. |
Returns
| Type | Description |
|---|---|
| FFRT_C_API uint64_t | Execution timeout of a queue task, in microseconds. |
FFRT_C_API void ffrt_queue_attr_set_callback(ffrt_queue_attr_t* attr, ffrt_function_header_t* f)
Description
Sets the timeout callback function of a queue attribute. The callback is triggered when the execution time of a task in the queue exceeds the timeout set by ffrt_queue_attr_set_timeout.
Since: 10
Parameters
| Name | Description |
|---|---|
| ffrt_queue_attr_t* attr | Pointer to the queue attribute. |
| ffrt_function_header_t* f | Pointer to the queue timeout callback function. |
FFRT_C_API ffrt_function_header_t* ffrt_queue_attr_get_callback(const ffrt_queue_attr_t* attr)
Description
Obtains the timeout callback function of a queue attribute.
Since: 10
Parameters
| Name | Description |
|---|---|
| const ffrt_queue_attr_t* attr | Pointer to the queue attribute. |
Returns
| Type | Description |
|---|---|
| FFRT_C_API ffrt_function_header_t* | Timeout callback function for a queue task. |
FFRT_C_API void ffrt_queue_attr_set_max_concurrency(ffrt_queue_attr_t* attr, const int max_concurrency)
Description
Sets the maximum concurrency of a concurrent queue attribute.
Since: 12
Parameters
| Name | Description |
|---|---|
| ffrt_queue_attr_t* attr | Pointer to the queue attribute. |
| const int max_concurrency | Maximum number of tasks that the queue can execute concurrently. |
FFRT_C_API int ffrt_queue_attr_get_max_concurrency(const ffrt_queue_attr_t* attr)
Description
Obtains the maximum concurrency of a queue attribute.
Since: 12
Parameters
| Name | Description |
|---|---|
| const ffrt_queue_attr_t* attr | Pointer to the queue attribute. |
Returns
| Type | Description |
|---|---|
| FFRT_C_API int | Maximum concurrency of the queue. |
FFRT_C_API void ffrt_queue_attr_set_thread_mode(ffrt_queue_attr_t* attr, bool mode)
Description
Sets the execution mode of a queue attribute. This API specifies whether tasks in the queue are executed in coroutine mode or thread mode. By default, tasks are executed in coroutine mode. When mode is set to true, thread-based execution is enabled.
Since: 20
Parameters
| Name | Description |
|---|---|
| ffrt_queue_attr_t* attr | Pointer to the queue attribute. |
| bool mode | Whether to enable the thread-based execution mode. - true: tasks are executed by native threads (thread mode). - false: tasks are executed by coroutines (default). |
FFRT_C_API bool ffrt_queue_attr_get_thread_mode(const ffrt_queue_attr_t* attr)
Description
Obtains the execution mode of a queue attribute.
Since: 20
Parameters
| Name | Description |
|---|---|
| const ffrt_queue_attr_t* attr | Pointer to the queue attribute. |
Returns
| Type | Description |
|---|---|
| FFRT_C_API bool | true if the task is executed in a native thread (thread mode); false if the task is executed in a coroutine (default). |
FFRT_C_API ffrt_queue_t ffrt_queue_create(ffrt_queue_type_t type, const char* name, const ffrt_queue_attr_t* attr)
Description
Creates a queue. When the queue is no longer used, it must be destroyed by ffrt_queue_destroy.
Since: 10
Parameters
| Name | Description |
|---|---|
| ffrt_queue_type_t type | Queue type. ffrt_queue_serial applies to scenarios where tasks must be executed in order; ffrt_queue_concurrent applies to scenarios where tasks can be executed concurrently to improve throughput. |
| const char* name | Pointer to the queue name. |
| const ffrt_queue_attr_t* attr | Pointer to the queue attribute. |
Returns
| Type | Description |
|---|---|
| FFRT_C_API ffrt_queue_t | A non-null queue handle is returned when the queue is created successfully; otherwise, a null pointer is returned. |
FFRT_C_API void ffrt_queue_destroy(ffrt_queue_t queue)
Description
Destroys a queue. The queue must have been created by ffrt_queue_create. When the queue is destroyed, tasks that have not started execution are canceled, and the API blocks until the tasks being executed are complete.
Since: 10
Parameters
| Name | Description |
|---|---|
| ffrt_queue_t queue | Queue handle. |
FFRT_C_API void ffrt_queue_submit(ffrt_queue_t queue, ffrt_function_header_t* f, const ffrt_task_attr_t* attr)
Description
Submits a task to a queue.
Since: 10
Parameters
| Name | Description |
|---|---|
| ffrt_queue_t queue | Queue handle. |
| ffrt_function_header_t* f | Pointer to the task execution body. |
| const ffrt_task_attr_t* attr | Pointer to the task attribute. |
Reference
FFRT_C_API ffrt_task_handle_t ffrt_queue_submit_h(ffrt_queue_t queue, ffrt_function_header_t* f, const ffrt_task_attr_t* attr)
Description
Submits a task to a queue and obtains the task handle.
Since: 10
Parameters
| Name | Description |
|---|---|
| ffrt_queue_t queue | Queue handle. |
| ffrt_function_header_t* f | Pointer to the task execution body. |
| const ffrt_task_attr_t* attr | Pointer to the task attribute. |
Returns
| Type | Description |
|---|---|
| FFRT_C_API ffrt_task_handle_t | Non-null task handle if the task is submitted successfully; otherwise, a null pointer. |
Reference
FFRT_C_API void ffrt_queue_submit_f(ffrt_queue_t queue, ffrt_function_t func, void* arg, const ffrt_task_attr_t* attr)
Description
Submits a task to a queue. This API is a simplified form of ffrt_queue_submit. It wraps the given task function and its parameters into a task wrapper (ffrt_function_kind_queue) for queue submission. The task destruction callback (after_func) used to handle post-execution cleanup is set to NULL, so any additional cleanup action is omitted. The generated task wrapper is then submitted to the specified queue through ffrt_queue_submit.
Since: 20
Parameters
| Name | Description |
|---|---|
| ffrt_queue_t queue | Queue handle. |
| ffrt_function_t func | Task function to be executed. |
| void* arg | Pointer to the parameter or closure data passed to the task function. |
| const ffrt_task_attr_t* attr | Pointer to the task attribute. |
References
FFRT_C_API ffrt_task_handle_t ffrt_queue_submit_h_f(ffrt_queue_t queue, ffrt_function_t func, void* arg, const ffrt_task_attr_t* attr)
Description
Submits a task to a queue and obtains the task handle. This API is a simplified form of ffrt_queue_submit_h. It wraps the given task function and its arguments into a task wrapper (ffrt_function_kind_queue) for queue submission. The task destruction callback (after_func) used to handle post-execution cleanup is set to NULL, so any additional cleanup action is omitted. The generated task wrapper is then submitted to the specified queue through ffrt_queue_submit_h.
Since: 20
Parameters
| Name | Description |
|---|---|
| ffrt_queue_t queue | Queue handle. |
| ffrt_function_t func | Task function to be executed. |
| void* arg | Pointer to the parameter or closure data passed to the task function. |
| const ffrt_task_attr_t* attr | Pointer to the task attribute. |
Returns
| Type | Description |
|---|---|
| FFRT_C_API ffrt_task_handle_t | Non-null task handle when the task is submitted successfully; null pointer otherwise. |
References
FFRT_C_API void ffrt_queue_wait(ffrt_task_handle_t handle)
Description
Waits for the tasks in the queue to complete execution.
Since: 10
Parameters
| Name | Description |
|---|---|
| ffrt_task_handle_t handle | Task handle. |
FFRT_C_API int ffrt_queue_cancel(ffrt_task_handle_t handle)
Description
Cancels tasks in the queue. Tasks that have already started execution cannot be canceled.
Since: 10
Parameters
| Name | Description |
|---|---|
| ffrt_task_handle_t handle | Task handle. |
Returns
| Type | Description |
|---|---|
| FFRT_C_API int | Returns 0 if the task is canceled successfully; Returns 1 if the task has been executed or removed from the queue; Returns -1 if handle is null. |
FFRT_C_API ffrt_queue_t ffrt_get_main_queue(void)
Description
Obtains the main thread queue of the application.
Since: 12
Returns
| Type | Description |
|---|---|
| FFRT_C_API ffrt_queue_t | Application main thread queue. |
FFRT_C_API ffrt_queue_t ffrt_get_current_queue(void)
Description
Obtains the Worker (ArkTS) thread queue of the application.
Since: 12
Deprecated from: 18
Returns
| Type | Description |
|---|---|
| FFRT_C_API ffrt_queue_t | Application Worker (ArkTS) thread queue. |