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_API_C_LOOP_H 17484543d1Sopenharmony_ci#define FFRT_API_C_LOOP_H 18484543d1Sopenharmony_ci 19484543d1Sopenharmony_ci#include "queue.h" 20484543d1Sopenharmony_ci#include "type_def.h" 21484543d1Sopenharmony_ci 22484543d1Sopenharmony_citypedef void* ffrt_loop_t; 23484543d1Sopenharmony_ci 24484543d1Sopenharmony_ci/** 25484543d1Sopenharmony_ci * @brief Creates a loop. 26484543d1Sopenharmony_ci * 27484543d1Sopenharmony_ci * @param queue Indicates a queue. 28484543d1Sopenharmony_ci * @return Returns a non-null loop handle if the loop is created; 29484543d1Sopenharmony_ci returns a null pointer otherwise. 30484543d1Sopenharmony_ci * @since 12 31484543d1Sopenharmony_ci * @version 1.0 32484543d1Sopenharmony_ci */ 33484543d1Sopenharmony_ciFFRT_C_API ffrt_loop_t ffrt_loop_create(ffrt_queue_t queue); 34484543d1Sopenharmony_ci 35484543d1Sopenharmony_ci/** 36484543d1Sopenharmony_ci * @brief Destroys a loop. 37484543d1Sopenharmony_ci * 38484543d1Sopenharmony_ci * @param loop Indicates a loop handle. 39484543d1Sopenharmony_ci * @return returns 0 if the loop is destroyed; 40484543d1Sopenharmony_ci returns -1 otherwise. 41484543d1Sopenharmony_ci * @since 12 42484543d1Sopenharmony_ci * @version 1.0 43484543d1Sopenharmony_ci */ 44484543d1Sopenharmony_ciFFRT_C_API int ffrt_loop_destroy(ffrt_loop_t loop); 45484543d1Sopenharmony_ci 46484543d1Sopenharmony_ci/** 47484543d1Sopenharmony_ci * @brief start loop run. 48484543d1Sopenharmony_ci * 49484543d1Sopenharmony_ci * @param loop Indicates a loop handle. 50484543d1Sopenharmony_ci * @return returns -1 if loop run fail; 51484543d1Sopenharmony_ci returns 0 otherwise. 52484543d1Sopenharmony_ci * @since 12 53484543d1Sopenharmony_ci * @version 1.0 54484543d1Sopenharmony_ci */ 55484543d1Sopenharmony_ciFFRT_C_API int ffrt_loop_run(ffrt_loop_t loop); 56484543d1Sopenharmony_ci 57484543d1Sopenharmony_ci/** 58484543d1Sopenharmony_ci * @brief stop loop run. 59484543d1Sopenharmony_ci * 60484543d1Sopenharmony_ci * @param loop Indicates a loop handle. 61484543d1Sopenharmony_ci * @since 12 62484543d1Sopenharmony_ci * @version 1.0 63484543d1Sopenharmony_ci */ 64484543d1Sopenharmony_ciFFRT_C_API void ffrt_loop_stop(ffrt_loop_t loop); 65484543d1Sopenharmony_ci 66484543d1Sopenharmony_ci/** 67484543d1Sopenharmony_ci * @brief control an epoll file descriptor on ffrt loop 68484543d1Sopenharmony_ci * 69484543d1Sopenharmony_ci * @param loop Indicates a loop handle. 70484543d1Sopenharmony_ci * @param op Indicates operation on the target file descriptor. 71484543d1Sopenharmony_ci * @param fd Indicates the target file descriptor on which to perform the operation. 72484543d1Sopenharmony_ci * @param events Indicates the event type associated with the target file descriptor. 73484543d1Sopenharmony_ci * @param data Indicates user data used in cb. 74484543d1Sopenharmony_ci * @param cb Indicates user cb which will be executed when the target fd is polled. 75484543d1Sopenharmony_ci * @return Returns 0 if success; 76484543d1Sopenharmony_ci returns -1 otherwise. 77484543d1Sopenharmony_ci * @since 12 78484543d1Sopenharmony_ci * @version 1.0 79484543d1Sopenharmony_ci */ 80484543d1Sopenharmony_ciFFRT_C_API int ffrt_loop_epoll_ctl(ffrt_loop_t loop, int op, int fd, uint32_t events, void *data, ffrt_poller_cb cb); 81484543d1Sopenharmony_ci 82484543d1Sopenharmony_ci/** 83484543d1Sopenharmony_ci * @brief Start a timer on ffrt loop 84484543d1Sopenharmony_ci * 85484543d1Sopenharmony_ci * @param loop Indicates a loop handle. 86484543d1Sopenharmony_ci * @param timeout Indicates the number of milliseconds that specifies timeout. 87484543d1Sopenharmony_ci * @param data Indicates user data used in cb. 88484543d1Sopenharmony_ci * @param cb Indicates user cb which will be executed when timeout. 89484543d1Sopenharmony_ci * @param repeat Indicates whether to repeat this timer. 90484543d1Sopenharmony_ci * @return Returns a timer handle. 91484543d1Sopenharmony_ci * @since 12 92484543d1Sopenharmony_ci * @version 1.0 93484543d1Sopenharmony_ci */ 94484543d1Sopenharmony_ciFFRT_C_API ffrt_timer_t ffrt_loop_timer_start( 95484543d1Sopenharmony_ci ffrt_loop_t loop, uint64_t timeout, void* data, ffrt_timer_cb cb, bool repeat); 96484543d1Sopenharmony_ci 97484543d1Sopenharmony_ci/** 98484543d1Sopenharmony_ci * @brief Stop a target timer on ffrt loop 99484543d1Sopenharmony_ci * 100484543d1Sopenharmony_ci * @param loop Indicates a loop handle. 101484543d1Sopenharmony_ci * @param handle Indicates the target timer handle. 102484543d1Sopenharmony_ci * @return Returns 0 if success; 103484543d1Sopenharmony_ci returns -1 otherwise. 104484543d1Sopenharmony_ci * @since 12 105484543d1Sopenharmony_ci * @version 1.0 106484543d1Sopenharmony_ci */ 107484543d1Sopenharmony_ciFFRT_C_API int ffrt_loop_timer_stop(ffrt_loop_t loop, ffrt_timer_t handle); 108484543d1Sopenharmony_ci 109484543d1Sopenharmony_ci#endif