• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "le_loop.h"
17 #include "le_epoll.h"
18 
19 
TaskNodeCompare(const HashNode * node1,const HashNode * node2)20 static int TaskNodeCompare(const HashNode *node1, const HashNode *node2)
21 {
22     BaseTask *task1 = HASHMAP_ENTRY(node1, BaseTask, hashNode);
23     BaseTask *task2 = HASHMAP_ENTRY(node2, BaseTask, hashNode);
24     return (int)task1->taskId.fd - (int)task2->taskId.fd;
25 }
26 
TaskKeyCompare(const HashNode * node,const void * key)27 static int TaskKeyCompare(const HashNode *node, const void *key)
28 {
29     BaseTask *task = HASHMAP_ENTRY(node, BaseTask, hashNode);
30     TaskId *taskId = (TaskId *)key;
31     return (int)task->taskId.fd - taskId->taskId.fd;
32 }
33 
TaskGetNodeHasCode(const HashNode * node)34 static int TaskGetNodeHasCode(const HashNode *node)
35 {
36     BaseTask *task = HASHMAP_ENTRY(node, BaseTask, hashNode);
37     return task->taskId.fd;
38 }
39 
TaskGetKeyHasCode(const void * key)40 static int TaskGetKeyHasCode(const void *key)
41 {
42     TaskId *taskId = (TaskId *)key;
43     return taskId->taskId.fd;
44 }
45 
TaskNodeFree(const HashNode * node,void * context)46 static void TaskNodeFree(const HashNode *node, void *context)
47 {
48     BaseTask *task = HASHMAP_ENTRY(node, BaseTask, hashNode);
49     CloseTask((const LoopHandle)context, task);
50     free(task);
51 }
52 
CreateLoop_(EventLoop ** loop,uint32_t maxevents,uint32_t timeout)53 static LE_STATUS CreateLoop_(EventLoop **loop, uint32_t maxevents, uint32_t timeout)
54 {
55 #ifdef LOOP_EVENT_USE_EPOLL
56     LE_STATUS ret = CreateEpollLoop(loop, maxevents, timeout);
57     LE_CHECK(ret == LE_SUCCESS, return ret, "Failed to create epoll loop");
58 #endif
59     (*loop)->maxevents = maxevents;
60     (*loop)->timeout = timeout;
61     (*loop)->stop = 0;
62     LoopMutexInit(&(*loop)->mutex);
63 
64     HashInfo info = {
65         TaskNodeCompare,
66         TaskKeyCompare,
67         TaskGetNodeHasCode,
68         TaskGetKeyHasCode,
69         TaskNodeFree,
70         128
71     };
72     return OH_HashMapCreate(&(*loop)->taskMap, &info);
73 }
74 
CloseLoop(EventLoop * loop)75 LE_STATUS CloseLoop(EventLoop *loop)
76 {
77     if (!loop->stop) {
78         return LE_SUCCESS;
79     }
80     OH_HashMapDestory(loop->taskMap, loop);
81     if (loop->close) {
82         loop->close(loop);
83     }
84     return LE_SUCCESS;
85 }
86 
ProcessEvent(const EventLoop * loop,int fd,uint32_t oper)87 LE_STATUS ProcessEvent(const EventLoop *loop, int fd, uint32_t oper)
88 {
89     BaseTask *task = GetTaskByFd((EventLoop *)loop, fd);
90     if (task != NULL) {
91         if (oper & Event_Error) {
92             task->flags |= TASK_FLAGS_INVALID;
93         }
94         task->handleEvent((LoopHandle)loop, (TaskHandle)task, oper);
95     } else {
96         loop->delEvent(loop, fd, oper);
97     }
98     return LE_SUCCESS;
99 }
100 
AddTask(EventLoop * loop,BaseTask * task)101 LE_STATUS AddTask(EventLoop *loop, BaseTask *task)
102 {
103     LoopMutexLock(&loop->mutex);
104     int ret = OH_HashMapAdd(loop->taskMap, &task->hashNode);
105     LoopMutexUnlock(&loop->mutex);
106 #ifndef STARTUP_INIT_TEST
107     return ret;
108 #else
109     ret = 0;
110     return ret;
111 #endif
112 }
113 
GetTaskByFd(EventLoop * loop,int fd)114 BaseTask *GetTaskByFd(EventLoop *loop, int fd)
115 {
116     BaseTask *task = NULL;
117     LoopMutexLock(&loop->mutex);
118     TaskId id = {0, {fd}};
119     HashNode *node = OH_HashMapGet(loop->taskMap, &id);
120     if (node != NULL) {
121         task = HASHMAP_ENTRY(node, BaseTask, hashNode);
122     }
123     LoopMutexUnlock(&loop->mutex);
124     return task;
125 }
126 
DelTask(EventLoop * loop,BaseTask * task)127 void DelTask(EventLoop *loop, BaseTask *task)
128 {
129     loop->delEvent(loop, task->taskId.fd,
130         Event_Read | Event_Write | Event_Error | Event_Free | Event_Timeout | Event_Signal);
131     LoopMutexLock(&loop->mutex);
132     OH_HashMapRemove(loop->taskMap, (TaskId *)task);
133     LoopMutexUnlock(&loop->mutex);
134     return;
135 }
136 
137 static EventLoop *g_defaultLoop = NULL;
LE_GetDefaultLoop(void)138 LoopHandle LE_GetDefaultLoop(void)
139 {
140     if (g_defaultLoop == NULL) {
141         LE_CreateLoop((LoopHandle *)&g_defaultLoop);
142     }
143     return (LoopHandle)g_defaultLoop;
144 }
145 
LE_CreateLoop(LoopHandle * handle)146 LE_STATUS LE_CreateLoop(LoopHandle *handle)
147 {
148     EventLoop *loop = NULL;
149     LE_STATUS ret = CreateLoop_(&loop, LOOP_MAX_SOCKET, DEFAULT_TIMEOUT);
150     *handle = (LoopHandle)loop;
151     return ret;
152 }
153 
LE_RunLoop(const LoopHandle handle)154 void LE_RunLoop(const LoopHandle handle)
155 {
156     LE_CHECK(handle != NULL, return, "Invalid handle");
157     EventLoop *loop = (EventLoop *)handle;
158     loop->runLoop(loop);
159 }
160 
LE_CloseLoop(const LoopHandle loopHandle)161 void LE_CloseLoop(const LoopHandle loopHandle)
162 {
163     LE_CHECK(loopHandle != NULL, return, "Invalid handle");
164     CloseLoop((EventLoop *)loopHandle);
165     if ((LoopHandle)g_defaultLoop == loopHandle) {
166         g_defaultLoop = NULL;
167     }
168 }
169 
LE_StopLoop(const LoopHandle handle)170 void LE_StopLoop(const LoopHandle handle)
171 {
172     LE_CHECK(handle != NULL, return, "Invalid handle");
173     EventLoop *loop = (EventLoop *)handle;
174     loop->stop = 1;
175 }