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 #ifndef FFRT_API_C_LOOP_H 17 #define FFRT_API_C_LOOP_H 18 19 #include "queue.h" 20 #include "type_def.h" 21 22 typedef void* ffrt_loop_t; 23 24 /** 25 * @brief Creates a loop. 26 * 27 * @param queue Indicates a queue. 28 * @return Returns a non-null loop handle if the loop is created; 29 returns a null pointer otherwise. 30 * @since 12 31 * @version 1.0 32 */ 33 FFRT_C_API ffrt_loop_t ffrt_loop_create(ffrt_queue_t queue); 34 35 /** 36 * @brief Destroys a loop. 37 * 38 * @param loop Indicates a loop handle. 39 * @return returns 0 if the loop is destroyed; 40 returns -1 otherwise. 41 * @since 12 42 * @version 1.0 43 */ 44 FFRT_C_API int ffrt_loop_destroy(ffrt_loop_t loop); 45 46 /** 47 * @brief start loop run. 48 * 49 * @param loop Indicates a loop handle. 50 * @return returns -1 if loop run fail; 51 returns 0 otherwise. 52 * @since 12 53 * @version 1.0 54 */ 55 FFRT_C_API int ffrt_loop_run(ffrt_loop_t loop); 56 57 /** 58 * @brief stop loop run. 59 * 60 * @param loop Indicates a loop handle. 61 * @since 12 62 * @version 1.0 63 */ 64 FFRT_C_API void ffrt_loop_stop(ffrt_loop_t loop); 65 66 /** 67 * @brief control an epoll file descriptor on ffrt loop 68 * 69 * @param loop Indicates a loop handle. 70 * @param op Indicates operation on the target file descriptor. 71 * @param fd Indicates the target file descriptor on which to perform the operation. 72 * @param events Indicates the event type associated with the target file descriptor. 73 * @param data Indicates user data used in cb. 74 * @param cb Indicates user cb which will be executed when the target fd is polled. 75 * @return Returns 0 if success; 76 returns -1 otherwise. 77 * @since 12 78 * @version 1.0 79 */ 80 FFRT_C_API int ffrt_loop_epoll_ctl(ffrt_loop_t loop, int op, int fd, uint32_t events, void *data, ffrt_poller_cb cb); 81 82 /** 83 * @brief Start a timer on ffrt loop 84 * 85 * @param loop Indicates a loop handle. 86 * @param timeout Indicates the number of milliseconds that specifies timeout. 87 * @param data Indicates user data used in cb. 88 * @param cb Indicates user cb which will be executed when timeout. 89 * @param repeat Indicates whether to repeat this timer. 90 * @return Returns a timer handle. 91 * @since 12 92 * @version 1.0 93 */ 94 FFRT_C_API ffrt_timer_t ffrt_loop_timer_start( 95 ffrt_loop_t loop, uint64_t timeout, void* data, ffrt_timer_cb cb, bool repeat); 96 97 /** 98 * @brief Stop a target timer on ffrt loop 99 * 100 * @param loop Indicates a loop handle. 101 * @param handle Indicates the target timer handle. 102 * @return Returns 0 if success; 103 returns -1 otherwise. 104 * @since 12 105 * @version 1.0 106 */ 107 FFRT_C_API int ffrt_loop_timer_stop(ffrt_loop_t loop, ffrt_timer_t handle); 108 109 #endif