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 12 23 */ 24 25 /** 26 * @file loop.h 27 * 28 * @brief Declares the loop interfaces in C. 29 * 30 * @library libffrt.z.so 31 * @kit FunctionFlowRuntimeKit 32 * @syscap SystemCapability.Resourceschedule.Ffrt.Core 33 * @since 12 34 */ 35 36 #ifndef FFRT_API_C_LOOP_H 37 #define FFRT_API_C_LOOP_H 38 39 #include <stdbool.h> 40 #include "type_def.h" 41 #include "queue.h" 42 43 /** 44 * @brief Defines the loop handle, which identifies different loops. 45 * 46 * @since 12 47 */ 48 typedef void* ffrt_loop_t; 49 50 /** 51 * @brief Creates a loop. 52 * 53 * @param queue Indicates a queue. 54 * @return Returns a non-null loop handle if the loop is created; 55 returns a null pointer otherwise. 56 * @since 12 57 */ 58 FFRT_C_API ffrt_loop_t ffrt_loop_create(ffrt_queue_t queue); 59 60 /** 61 * @brief Destroys a loop, the user needs to invoke this interface. 62 * 63 * @param loop Indicates a loop handle. 64 * @return Returns 0 if the loop is destroyed; 65 returns -1 otherwise. 66 * @since 12 67 */ 68 FFRT_C_API int ffrt_loop_destroy(ffrt_loop_t loop); 69 70 /** 71 * @brief Starts a loop run. 72 * 73 * @param loop Indicates a loop handle. 74 * @return Returns -1 if loop run fail; 75 returns 0 otherwise. 76 * @since 12 77 */ 78 FFRT_C_API int ffrt_loop_run(ffrt_loop_t loop); 79 80 /** 81 * @brief Stops a loop run. 82 * 83 * @param loop Indicates a loop handle. 84 * @since 12 85 */ 86 FFRT_C_API void ffrt_loop_stop(ffrt_loop_t loop); 87 88 /** 89 * @brief Controls an epoll file descriptor on ffrt loop. 90 * 91 * @warning Do not call `exit` in `cb` - this my cause unexpected behavior. 92 * 93 * @param loop Indicates a loop handle. 94 * @param op Indicates operation on the target file descriptor. 95 * @param fd Indicates the target file descriptor on which to perform the operation. 96 * @param events Indicates the event type associated with the target file descriptor. 97 * @param data Indicates user data used in cb. 98 * @param cb Indicates user cb which will be executed when the target fd is polled. 99 * @return Returns 0 if success; 100 returns -1 otherwise. 101 * @since 12 102 */ 103 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); 104 105 /** 106 * @brief Starts a timer on ffrt loop. 107 * 108 * @warning Do not call `exit` in `cb` - this my cause unexpected behavior. 109 * 110 * @param loop Indicates a loop handle. 111 * @param timeout Indicates the number of milliseconds that specifies timeout. 112 * @param data Indicates user data used in cb. 113 * @param cb Indicates user cb which will be executed when timeout. 114 * @param repeat Indicates whether to repeat this timer. 115 * @return Returns a timer handle. 116 * @since 12 117 */ 118 FFRT_C_API ffrt_timer_t ffrt_loop_timer_start( 119 ffrt_loop_t loop, uint64_t timeout, void* data, ffrt_timer_cb cb, bool repeat); 120 121 /** 122 * @brief Stops a timer on ffrt loop. 123 * 124 * @param loop Indicates a loop handle. 125 * @param handle Indicates the target timer handle. 126 * @return Returns 0 if success; 127 returns -1 otherwise. 128 * @since 12 129 */ 130 FFRT_C_API int ffrt_loop_timer_stop(ffrt_loop_t loop, ffrt_timer_t handle); 131 132 #endif // FFRT_API_C_LOOP_H 133 /** @} */