1484543d1Sopenharmony_ci/*
2484543d1Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3484543d1Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4484543d1Sopenharmony_ci * you may not use this file except in compliance with the License.
5484543d1Sopenharmony_ci * You may obtain a copy of the License at
6484543d1Sopenharmony_ci *
7484543d1Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8484543d1Sopenharmony_ci *
9484543d1Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10484543d1Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11484543d1Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12484543d1Sopenharmony_ci * See the License for the specific language governing permissions and
13484543d1Sopenharmony_ci * limitations under the License.
14484543d1Sopenharmony_ci */
15484543d1Sopenharmony_ci
16484543d1Sopenharmony_ci#ifndef FFRT_INNER_API_C_QUEUE_EXT_H
17484543d1Sopenharmony_ci#define FFRT_INNER_API_C_QUEUE_EXT_H
18484543d1Sopenharmony_ci
19484543d1Sopenharmony_ci#include <stdbool.h>
20484543d1Sopenharmony_ci#include "c/queue.h"
21484543d1Sopenharmony_ci
22484543d1Sopenharmony_citypedef enum {
23484543d1Sopenharmony_ci    ffrt_queue_eventhandler_interactive = 3,
24484543d1Sopenharmony_ci    ffrt_queue_eventhandler_adapter = 4,
25484543d1Sopenharmony_ci    ffrt_queue_inner_max,
26484543d1Sopenharmony_ci} ffrt_inner_queue_type_t;
27484543d1Sopenharmony_ci
28484543d1Sopenharmony_citypedef enum {
29484543d1Sopenharmony_ci    /* highest priority, should be distributed until the tasks in the queue are completed */
30484543d1Sopenharmony_ci    ffrt_inner_queue_priority_vip = 0,
31484543d1Sopenharmony_ci    /* should be distributed at once if possible, handle time equals to send time, prior to high level */
32484543d1Sopenharmony_ci    ffrt_inner_queue_priority_immediate,
33484543d1Sopenharmony_ci    /* high priority, sorted by handle time, prior to low level. */
34484543d1Sopenharmony_ci    ffrt_inner_queue_priority_high,
35484543d1Sopenharmony_ci    /* low priority, sorted by handle time, prior to idle level. */
36484543d1Sopenharmony_ci    ffrt_inner_queue_priority_low,
37484543d1Sopenharmony_ci    /* lowest priority, sorted by handle time, only distribute when there is no other level inside queue. */
38484543d1Sopenharmony_ci    ffrt_inner_queue_priority_idle,
39484543d1Sopenharmony_ci} ffrt_inner_queue_priority_t;
40484543d1Sopenharmony_ci
41484543d1Sopenharmony_ci/**
42484543d1Sopenharmony_ci * @brief Submits a task to a queue, for tasks with the same delay, insert the header.
43484543d1Sopenharmony_ci *
44484543d1Sopenharmony_ci * @param queue Indicates a queue handle.
45484543d1Sopenharmony_ci * @param f Indicates a pointer to the task executor.
46484543d1Sopenharmony_ci * @param attr Indicates a pointer to the task attribute.
47484543d1Sopenharmony_ci * @version 1.0
48484543d1Sopenharmony_ci */
49484543d1Sopenharmony_ciFFRT_C_API void ffrt_queue_submit_head(ffrt_queue_t queue, ffrt_function_header_t* f, const ffrt_task_attr_t* attr);
50484543d1Sopenharmony_ci
51484543d1Sopenharmony_ci/**
52484543d1Sopenharmony_ci * @brief Submits a task to the queue, and obtains a task handle, for tasks with the same delay, insert the header.
53484543d1Sopenharmony_ci *
54484543d1Sopenharmony_ci * @param queue Indicates a queue handle.
55484543d1Sopenharmony_ci * @param f Indicates a pointer to the task executor.
56484543d1Sopenharmony_ci * @param attr Indicates a pointer to the task attribute.
57484543d1Sopenharmony_ci * @return Returns a non-null task handle if the task is submitted;
58484543d1Sopenharmony_ci           returns a null pointer otherwise.
59484543d1Sopenharmony_ci * @version 1.0
60484543d1Sopenharmony_ci */
61484543d1Sopenharmony_ciFFRT_C_API ffrt_task_handle_t ffrt_queue_submit_head_h(
62484543d1Sopenharmony_ci    ffrt_queue_t queue, ffrt_function_header_t* f, const ffrt_task_attr_t* attr);
63484543d1Sopenharmony_ci
64484543d1Sopenharmony_ci/**
65484543d1Sopenharmony_ci * @brief Checks whether a task with the given name can be found in the queue.
66484543d1Sopenharmony_ci *
67484543d1Sopenharmony_ci * @param queue Indicates a queue handle.
68484543d1Sopenharmony_ci * @param name Indicates name to be searched for, regular expressions are supported.
69484543d1Sopenharmony_ci * @return Returns whether the task is found.
70484543d1Sopenharmony_ci * @version 1.0
71484543d1Sopenharmony_ci */
72484543d1Sopenharmony_ciFFRT_C_API bool ffrt_queue_has_task(ffrt_queue_t queue, const char* name);
73484543d1Sopenharmony_ci
74484543d1Sopenharmony_ci/**
75484543d1Sopenharmony_ci * @brief Cancels all unexecuted tasks in the queue.
76484543d1Sopenharmony_ci *
77484543d1Sopenharmony_ci * @param queue Indicates a queue handle.
78484543d1Sopenharmony_ci * @version 1.0
79484543d1Sopenharmony_ci */
80484543d1Sopenharmony_ciFFRT_C_API void ffrt_queue_cancel_all(ffrt_queue_t queue);
81484543d1Sopenharmony_ci
82484543d1Sopenharmony_ci/**
83484543d1Sopenharmony_ci * @brief Cancels all unexecuted tasks and wait for running tasks in the queue.
84484543d1Sopenharmony_ci *
85484543d1Sopenharmony_ci * @param queue Indicates a queue handle.
86484543d1Sopenharmony_ci * @version 1.0
87484543d1Sopenharmony_ci */
88484543d1Sopenharmony_ciFFRT_C_API void ffrt_queue_cancel_and_wait(ffrt_queue_t queue);
89484543d1Sopenharmony_ci
90484543d1Sopenharmony_ci/**
91484543d1Sopenharmony_ci * @brief Cancels a task with the given name in the queue.
92484543d1Sopenharmony_ci *
93484543d1Sopenharmony_ci * @param queue Indicates a queue handle.
94484543d1Sopenharmony_ci * @param name Indicates name of the task to be canceled, regular expressions are supported.
95484543d1Sopenharmony_ci * @return Returns <b>0</b> if the task is canceled;
96484543d1Sopenharmony_ci           returns <b>1</b> otherwise.
97484543d1Sopenharmony_ci * @version 1.0
98484543d1Sopenharmony_ci */
99484543d1Sopenharmony_ciFFRT_C_API int ffrt_queue_cancel_by_name(ffrt_queue_t queue, const char* name);
100484543d1Sopenharmony_ci
101484543d1Sopenharmony_ci/**
102484543d1Sopenharmony_ci * @brief Checks whether the queue is idle.
103484543d1Sopenharmony_ci *
104484543d1Sopenharmony_ci * @param queue Indicates a queue handle.
105484543d1Sopenharmony_ci * @return Returns whether the queue is idle.
106484543d1Sopenharmony_ci * @version 1.0
107484543d1Sopenharmony_ci */
108484543d1Sopenharmony_ciFFRT_C_API bool ffrt_queue_is_idle(ffrt_queue_t queue);
109484543d1Sopenharmony_ci
110484543d1Sopenharmony_ci/**
111484543d1Sopenharmony_ci * @brief Dumps queue information;
112484543d1Sopenharmony_ci          including current execution, historical execution, and remaining unexecuted task information, etc.
113484543d1Sopenharmony_ci *
114484543d1Sopenharmony_ci * @param queue Indicates a queue handle.
115484543d1Sopenharmony_ci * @param tag Indicates tag prefix for dump information.
116484543d1Sopenharmony_ci * @param buf Indicates produce output, write to the character string buf.
117484543d1Sopenharmony_ci * @param len Indicates the size of the buffer (in bytes).
118484543d1Sopenharmony_ci * @param history_info Indicates whether dump history information.
119484543d1Sopenharmony_ci * @return Returns the number of characters printed (not including the terminating null byte '\0');
120484543d1Sopenharmony_ci           returns -1 if an error occurred, pay special attention to returning -1 when truncation occurs.
121484543d1Sopenharmony_ci * @version 1.0
122484543d1Sopenharmony_ci */
123484543d1Sopenharmony_ciFFRT_C_API int ffrt_queue_dump(ffrt_queue_t queue, const char* tag, char* buf, uint32_t len, bool history_info);
124484543d1Sopenharmony_ci
125484543d1Sopenharmony_ci/**
126484543d1Sopenharmony_ci * @brief Dumps queue task count with specified priority.
127484543d1Sopenharmony_ci *
128484543d1Sopenharmony_ci * @param queue Indicates a queue handle.
129484543d1Sopenharmony_ci * @param priority Indicates the execute priority of queue task.
130484543d1Sopenharmony_ci * @return Returns the count of tasks;
131484543d1Sopenharmony_ci           returns -1 if an error occurred.
132484543d1Sopenharmony_ci * @version 1.0
133484543d1Sopenharmony_ci */
134484543d1Sopenharmony_ciFFRT_C_API int ffrt_queue_size_dump(ffrt_queue_t queue, ffrt_inner_queue_priority_t priority);
135484543d1Sopenharmony_ci
136484543d1Sopenharmony_ci/**
137484543d1Sopenharmony_ci * @brief Binds an eventhandler object to the queue.
138484543d1Sopenharmony_ci *
139484543d1Sopenharmony_ci * @param queue Indicates a queue handle.
140484543d1Sopenharmony_ci * @param eventhandler Indicates an eventhandler pointer.
141484543d1Sopenharmony_ci * @version 1.0
142484543d1Sopenharmony_ci */
143484543d1Sopenharmony_ciFFRT_C_API void ffrt_queue_set_eventhandler(ffrt_queue_t queue, void* eventhandler);
144484543d1Sopenharmony_ci
145484543d1Sopenharmony_ci/**
146484543d1Sopenharmony_ci * @brief Obtains the handler bound to the queue that is being executed on the current worker.
147484543d1Sopenharmony_ci *
148484543d1Sopenharmony_ci * @return Returns a non-null eventhandler pointer;
149484543d1Sopenharmony_ci           returns a null pointer if the current task is not bound to an eventhandler.
150484543d1Sopenharmony_ci * @version 1.0
151484543d1Sopenharmony_ci */
152484543d1Sopenharmony_ciFFRT_C_API void* ffrt_get_current_queue_eventhandler();
153484543d1Sopenharmony_ci
154484543d1Sopenharmony_ci#endif