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 #ifndef FFRT_API_C_EXECUTOR_TASK_H
16 #define FFRT_API_C_EXECUTOR_TASK_H
17 
18 #include <stdint.h>
19 #include <stdbool.h>
20 #include <sys/epoll.h>
21 #include "type_def_ext.h"
22 #include "c/timer.h"
23 
24 typedef struct ffrt_executor_task {
25     uintptr_t reserved[2];
26     uintptr_t type; // 0: TaskCtx, 1~: Dynamicly Define Task, User Space Address: libuv work
27     void* wq[2];
28 } ffrt_executor_task_t;
29 
30 typedef enum {
31     ffrt_normal_task = 0,
32     ffrt_io_task = 1,
33     ffrt_uv_task, // only used to register func for libuv
34     ffrt_queue_task,
35     ffrt_invalid_task
36 } ffrt_executor_task_type_t;
37 
38 typedef void (*ffrt_executor_task_func)(ffrt_executor_task_t* data, ffrt_qos_t qos);
39 FFRT_C_API void ffrt_executor_task_register_func(ffrt_executor_task_func func, ffrt_executor_task_type_t type);
40 FFRT_C_API void ffrt_executor_task_submit(ffrt_executor_task_t* task, const ffrt_task_attr_t* attr);
41 FFRT_C_API int ffrt_executor_task_cancel(ffrt_executor_task_t* task, const ffrt_qos_t qos);
42 
43 // poller
44 FFRT_C_API void ffrt_poller_wakeup(ffrt_qos_t qos);
45 
46 FFRT_C_API uint8_t ffrt_epoll_get_count(ffrt_qos_t qos);
47 
48 FFRT_C_API ffrt_timer_query_t ffrt_timer_query(ffrt_qos_t qos, ffrt_timer_t handle);
49 
50 FFRT_C_API int ffrt_epoll_ctl(ffrt_qos_t qos, int op, int fd, uint32_t events, void* data, ffrt_poller_cb cb);
51 
52 FFRT_C_API int ffrt_epoll_wait(ffrt_qos_t qos, struct epoll_event* events, int max_events, int timeout);
53 
54 FFRT_C_API uint64_t ffrt_epoll_get_wait_time(void* taskHandle);
55 
56 // ffrt_executor_task
57 FFRT_C_API void ffrt_submit_coroutine(void* co, ffrt_coroutine_ptr_t exec, ffrt_function_t destroy,
58     const ffrt_deps_t* in_deps, const ffrt_deps_t* out_deps, const ffrt_task_attr_t* attr);
59 
60 // waker
61 FFRT_C_API void ffrt_wake_coroutine(void* task);
62 
63 // get
64 FFRT_C_API void* ffrt_get_current_task();
65 
66 /**
67  * @brief Obtains current coroutine stack address and size.
68  *
69  * @param stack_addr Coroutine stack address.
70  * @param size Coroutine stack size.
71  * @return Returns <b>0</b> if the stack is obtained;
72  *         returns <b>-1</b> otherwise.
73  * @since 12
74  * @version 1.0
75  */
76 FFRT_C_API bool ffrt_get_current_coroutine_stack(void** stack_addr, size_t* size);
77 
78 /**
79  * @brief Obtains current task.
80  *
81  * @param none.
82  * @return Returns current task.
83  * @since 12
84  * @version 1.0
85  */
86 FFRT_C_API void* ffrt_get_cur_task();
87 
88 /**
89  * @brief Set the taskLocal flag in ffrt_task_attr.
90  *
91  * @param attr The ffrt_task_attr struct.
92  * @param task_local The bool value to be set.
93  * @return none.
94  * @since 12
95  * @version 1.0
96  */
97 FFRT_C_API void ffrt_task_attr_set_local(ffrt_task_attr_t* attr, bool task_local);
98 
99 /**
100  * @brief Obtains the taskLocal flag in ffrt_task_attr.
101  *
102  * @param attr The ffrt_task_attr struct.
103  * @return The bool value of task_local.
104  * @since 12
105  * @version 1.0
106  */
107 FFRT_C_API bool ffrt_task_attr_get_local(ffrt_task_attr_t* attr);
108 
109 /**
110  * @brief Obtains the thread id of the input task handle.
111  *
112  * @param task_handle The task pointer.
113  * @return The thread id of the input task handle.
114  * @version 1.0
115  */
116 FFRT_C_API pthread_t ffrt_task_get_tid(void* task_handle);
117 
118 /**
119  * @brief Obtains the task id cached by the current thread.
120  *
121  * @return Returns the task id.
122  * @version 1.0
123  */
124 FFRT_C_API uint64_t ffrt_get_cur_cached_task_id();
125 #endif