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 #include "c/loop.h"
17 #include "loop.h"
18 #include "queue/queue_handler.h"
19 #include "internal_inc/osal.h"
20 #include "dfx/log/ffrt_log_api.h"
21 
22 using namespace ffrt;
23 
24 API_ATTRIBUTE((visibility("default")))
25 ffrt_loop_t ffrt_loop_create(ffrt_queue_t queue)
26 {
27     FFRT_COND_DO_ERR((queue == nullptr), return nullptr, "input invalid, queue is nullptr");
28     QueueHandler* handler = static_cast<QueueHandler*>(queue);
29     FFRT_COND_DO_ERR((!handler->IsValidForLoop()), return nullptr, "queue invalid for loop");
30 
31     Loop* innerLoop = new (std::nothrow) Loop(handler);
32     FFRT_COND_DO_ERR((innerLoop == nullptr), return nullptr, "failed to construct loop");
33 
34     if (!handler->SetLoop(innerLoop)) {
35         delete innerLoop;
36         return nullptr;
37     }
38     return static_cast<ffrt_loop_t>(innerLoop);
39 }
40 
41 API_ATTRIBUTE((visibility("default")))
42 int ffrt_loop_destroy(ffrt_loop_t loop)
43 {
44     FFRT_COND_DO_ERR((loop == nullptr), return -1, "input invalid, loop is nullptr");
45     Loop* innerLoop = static_cast<Loop*>(loop);
46     delete innerLoop;
47     return 0;
48 }
49 
50 API_ATTRIBUTE((visibility("default")))
51 int ffrt_loop_run(ffrt_loop_t loop)
52 {
53     FFRT_COND_DO_ERR((loop == nullptr), return -1, "input invalid, loop is nullptr");
54     Loop* innerLoop = static_cast<Loop*>(loop);
55     innerLoop->Run();
56     return 0;
57 }
58 
59 
60 API_ATTRIBUTE((visibility("default")))
61 void ffrt_loop_stop(ffrt_loop_t loop)
62 {
63     FFRT_COND_DO_ERR((loop == nullptr), return, "input invalid, loop is nullptr");
64     Loop* innerLoop = static_cast<Loop*>(loop);
65     innerLoop->Stop();
66 }
67 
68 API_ATTRIBUTE((visibility("default")))
69 int ffrt_loop_epoll_ctl(ffrt_loop_t loop, int op, int fd, uint32_t events, void *data, ffrt_poller_cb cb)
70 {
71     FFRT_COND_DO_ERR((loop == nullptr), return -1, "input invalid, loop is nullptr");
72     Loop* innerLoop = static_cast<Loop*>(loop);
73     return innerLoop->EpollCtl(op, fd, events, data, cb);
74 }
75 
76 API_ATTRIBUTE((visibility("default")))
77 ffrt_timer_t ffrt_loop_timer_start(ffrt_loop_t loop, uint64_t timeout, void* data, ffrt_timer_cb cb, bool repeat)
78 {
79     FFRT_COND_DO_ERR((loop == nullptr), return -1, "input invalid, loop is nullptr");
80     Loop* innerLoop = static_cast<Loop*>(loop);
81     return innerLoop->TimerStart(timeout, data, cb, repeat);
82 }
83 
84 API_ATTRIBUTE((visibility("default")))
85 int ffrt_loop_timer_stop(ffrt_loop_t loop, ffrt_timer_t handle)
86 {
87     FFRT_COND_DO_ERR((loop == nullptr), return -1, "input invalid, loop is nullptr");
88     Loop* innerLoop = static_cast<Loop*>(loop);
89     return innerLoop->TimerStop(handle);
90 }
91