• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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")))
ffrt_loop_create(ffrt_queue_t queue)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         FFRT_SYSEVENT_LOGE("failed to set loop for handler");
36         delete innerLoop;
37         return nullptr;
38     }
39     return static_cast<ffrt_loop_t>(innerLoop);
40 }
41 
42 API_ATTRIBUTE((visibility("default")))
ffrt_loop_destroy(ffrt_loop_t loop)43 int ffrt_loop_destroy(ffrt_loop_t loop)
44 {
45     FFRT_COND_DO_ERR((loop == nullptr), return -1, "input invalid, loop is nullptr");
46     Loop* innerLoop = static_cast<Loop*>(loop);
47     delete innerLoop;
48     return 0;
49 }
50 
51 API_ATTRIBUTE((visibility("default")))
ffrt_loop_run(ffrt_loop_t loop)52 int ffrt_loop_run(ffrt_loop_t loop)
53 {
54     FFRT_COND_DO_ERR((loop == nullptr), return -1, "input invalid, loop is nullptr");
55     Loop* innerLoop = static_cast<Loop*>(loop);
56     innerLoop->Run();
57     return 0;
58 }
59 
60 
61 API_ATTRIBUTE((visibility("default")))
ffrt_loop_stop(ffrt_loop_t loop)62 void ffrt_loop_stop(ffrt_loop_t loop)
63 {
64     FFRT_COND_DO_ERR((loop == nullptr), return, "input invalid, loop is nullptr");
65     Loop* innerLoop = static_cast<Loop*>(loop);
66     innerLoop->Stop();
67 }
68 
69 API_ATTRIBUTE((visibility("default")))
ffrt_loop_epoll_ctl(ffrt_loop_t loop,int op,int fd,uint32_t events,void * data,ffrt_poller_cb cb)70 int ffrt_loop_epoll_ctl(ffrt_loop_t loop, int op, int fd, uint32_t events, void *data, ffrt_poller_cb cb)
71 {
72     FFRT_COND_DO_ERR((loop == nullptr), return -1, "input invalid, loop is nullptr");
73     Loop* innerLoop = static_cast<Loop*>(loop);
74     return innerLoop->EpollCtl(op, fd, events, data, cb);
75 }
76 
77 API_ATTRIBUTE((visibility("default")))
ffrt_loop_timer_start(ffrt_loop_t loop,uint64_t timeout,void * data,ffrt_timer_cb cb,bool repeat)78 ffrt_timer_t ffrt_loop_timer_start(ffrt_loop_t loop, uint64_t timeout, void* data, ffrt_timer_cb cb, bool repeat)
79 {
80     FFRT_COND_DO_ERR((loop == nullptr), return -1, "input invalid, loop is nullptr");
81     FFRT_COND_DO_ERR((cb == nullptr), return -1, "input invalid, cb is nullptr");
82     Loop* innerLoop = static_cast<Loop*>(loop);
83     return innerLoop->TimerStart(timeout, data, cb, repeat);
84 }
85 
86 API_ATTRIBUTE((visibility("default")))
ffrt_loop_timer_stop(ffrt_loop_t loop,ffrt_timer_t handle)87 int ffrt_loop_timer_stop(ffrt_loop_t loop, ffrt_timer_t handle)
88 {
89     FFRT_COND_DO_ERR((loop == nullptr), return -1, "input invalid, loop is nullptr");
90     Loop* innerLoop = static_cast<Loop*>(loop);
91     return innerLoop->TimerStop(handle);
92 }
93