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