1# loop.h 2 3<!--Kit: Function Flow Runtime Kit--> 4<!--Subsystem: Resourceschedule--> 5<!--Owner: @chuchihtung; @yanleo--> 6<!--Designer: @geoffrey_guo; @huangyouzhong--> 7<!--Tester: @lotsof; @sunxuhao--> 8<!--Adviser: @foryourself--> 9 10## Overview 11 12The **loop.h** file declares the loop APIs in C. 13 14**File to include**: <ffrt/loop.h> 15 16**Library**: libffrt.z.so 17 18**System capability**: SystemCapability.Resourceschedule.Ffrt.Core 19 20**Since**: 12 21 22**Related module**: [FFRT](capi-ffrt.md) 23 24## Summary 25 26### Structs 27 28| Name | Description| 29|-----------------|----| 30| [ffrt_loop_t](capi-ffrt-ffrt-loop-t.md) | Defines the handle to a loop. | 31 32 33### Function 34 35| Name| Description| 36| -- | -- | 37| [FFRT_C_API ffrt_loop_t ffrt_loop_create(ffrt_queue_t queue)](#ffrt_loop_create) | Creates a loop.| 38| [FFRT_C_API int ffrt_loop_destroy(ffrt_loop_t loop)](#ffrt_loop_destroy) | Destroys a loop.| 39| [FFRT_C_API int ffrt_loop_run(ffrt_loop_t loop)](#ffrt_loop_run) | Runs a loop.| 40| [FFRT_C_API void ffrt_loop_stop(ffrt_loop_t loop)](#ffrt_loop_stop) | Stops a loop.| 41| [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)](#ffrt_loop_epoll_ctl) | Manages listening events on a loop.| 42| [FFRT_C_API ffrt_timer_t ffrt_loop_timer_start(ffrt_loop_t loop, uint64_t timeout, void* data, ffrt_timer_cb cb, bool repeat)](#ffrt_loop_timer_start) | Starts the timer on a loop.| 43| [FFRT_C_API int ffrt_loop_timer_stop(ffrt_loop_t loop, ffrt_timer_t handle)](#ffrt_loop_timer_stop) | Stops the timer on a loop.| 44 45## Function Description 46 47### ffrt_loop_create() 48 49``` 50FFRT_C_API ffrt_loop_t ffrt_loop_create(ffrt_queue_t queue) 51``` 52 53**Description** 54 55Creates a loop. 56 57**Since**: 12 58 59 60**Parameters** 61 62| Name | Description| 63|------------------------| -- | 64| [ffrt_queue_t](capi-ffrt-ffrt-queue-t.md) queue | Concurrent queue.| 65 66**Returns** 67 68| Type | Description| 69|----------------------------| -- | 70| FFRT_C_API [ffrt_loop_t](capi-ffrt-ffrt-loop-t.md) | Returns the **ffrt_loop_t** object if the loop is created;<br> returns a null pointer otherwise.| 71 72### ffrt_loop_destroy() 73 74``` 75FFRT_C_API int ffrt_loop_destroy(ffrt_loop_t loop) 76``` 77 78**Description** 79 80Destroys a loop. 81 82**Since**: 12 83 84 85**Parameters** 86 87| Name| Description| 88| -- | -- | 89| [ffrt_loop_t](capi-ffrt-ffrt-loop-t.md) loop | Loop object.| 90 91**Returns** 92 93| Type| Description| 94| -- | -- | 95| FFRT_C_API int | Returns **0** if the loop is destroyed;<br> returns **-1** otherwise.| 96 97### ffrt_loop_run() 98 99``` 100FFRT_C_API int ffrt_loop_run(ffrt_loop_t loop) 101``` 102 103**Description** 104 105Runs a loop. 106 107**Since**: 12 108 109 110**Parameters** 111 112| Name| Description| 113| -- | -- | 114| [ffrt_loop_t](capi-ffrt-ffrt-loop-t.md) loop | Loop object.| 115 116**Returns** 117 118| Type| Description| 119| -- | -- | 120| FFRT_C_API int | Returns **0** if the loop runs successfully;<br> returns **-1** otherwise.| 121 122### ffrt_loop_stop() 123 124``` 125FFRT_C_API void ffrt_loop_stop(ffrt_loop_t loop) 126``` 127 128**Description** 129 130Stops a loop. 131 132**Since**: 12 133 134 135**Parameters** 136 137| Name| Description| 138| -- | -- | 139| [ffrt_loop_t](capi-ffrt-ffrt-loop-t.md) loop | Loop object.| 140 141### ffrt_loop_epoll_ctl() 142 143``` 144FFRT_C_API int ffrt_loop_epoll_ctl(ffrt_loop_t loop, int op, int fd, uint32_t events, void *data, ffrt_poller_cb cb) 145``` 146 147**Description** 148 149Manages listening events on a loop. 150 151**Since**: 12 152 153 154**Parameters** 155 156| Name| Description| 157| -- | -- | 158| [ffrt_loop_t](capi-ffrt-ffrt-loop-t.md) loop | Loop object.| 159| int op | Operation to be performed.| 160| int fd | File descriptor.| 161| uint32_t events | Event.| 162| void *data | Pointer to the input parameter in the callback function invoked upon event changes.| 163| [ffrt_poller_cb](capi-type-def-h.md#ffrt_poller_cb) cb | Callback function invoked upon event changes.| 164 165**Returns** 166 167| Type| Description| 168| -- | -- | 169| FFRT_C_API int | Returns **0** if the operation is successful;<br> returns **-1** otherwise.| 170 171### ffrt_loop_timer_start() 172 173``` 174FFRT_C_API ffrt_timer_t ffrt_loop_timer_start(ffrt_loop_t loop, uint64_t timeout, void* data, ffrt_timer_cb cb, bool repeat) 175``` 176 177**Description** 178 179Starts the timer on a loop. 180 181**Since**: 12 182 183 184**Parameters** 185 186| Name| Description| 187| -- | -- | 188| [ffrt_loop_t](capi-ffrt-ffrt-loop-t.md) loop | Loop object.| 189| uint64_t timeout | Timeout, in milliseconds.| 190| void* data | Pointer to the input parameter in the callback function invoked upon event changes.| 191| [ffrt_timer_cb](capi-type-def-h.md#ffrt_timer_cb) cb | Callback function invoked upon event changes.| 192| bool repeat | Whether to repeat the timer.| 193 194**Returns** 195 196| Type | Description| 197|--------------------------------------------------| -- | 198| FFRT_C_API [ffrt_timer_t](capi-type-def-h.md#variables)| Returns the timer handle.| 199 200### ffrt_loop_timer_stop() 201 202``` 203FFRT_C_API int ffrt_loop_timer_stop(ffrt_loop_t loop, ffrt_timer_t handle) 204``` 205 206**Description** 207 208Stops the timer on a loop. 209 210**Since**: 12 211 212 213**Parameters** 214 215| Name| Description| 216| -- | -- | 217| ffrt_loop_t loop | Loop object.| 218| ffrt_timer_t handle | Timer handle.| 219 220**Returns** 221 222| Type| Description| 223| -- | -- | 224| FFRT_C_API int | Returns **0** if the operation is successful;<br>returns **-1** otherwise.| 225