• 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)46 static void TaskNodeFree(const HashNode *node)
47 {
48     BaseTask *task = HASHMAP_ENTRY(node, BaseTask, hashNode);
49     CloseTask(LE_GetDefaultLoop(), task);
50 }
51 
CreateLoop_(EventLoop ** loop,uint32_t maxevents,uint32_t timeout)52 static LE_STATUS CreateLoop_(EventLoop **loop, uint32_t maxevents, uint32_t timeout)
53 {
54 #ifdef LOOP_EVENT_USE_EPOLL
55     LE_STATUS ret = CreateEpollLoop(loop, maxevents, timeout);
56     LE_CHECK(ret == LE_SUCCESS, return ret, "Failed to create epoll loop");
57 #endif
58     (*loop)->maxevents = maxevents;
59     (*loop)->timeout = timeout;
60     (*loop)->stop = 0;
61     LoopMutexInit(&(*loop)->mutex);
62 
63     HashInfo info = {
64         TaskNodeCompare,
65         TaskKeyCompare,
66         TaskGetNodeHasCode,
67         TaskGetKeyHasCode,
68         TaskNodeFree,
69         128
70     };
71     return OH_HashMapCreate(&(*loop)->taskMap, &info);
72 }
73 
CloseLoop(EventLoop * loop)74 LE_STATUS CloseLoop(EventLoop *loop)
75 {
76     if (!loop->stop) {
77         return LE_SUCCESS;
78     }
79     OH_HashMapDestory(loop->taskMap);
80     if (loop->close) {
81         loop->close(loop);
82     }
83     return LE_SUCCESS;
84 }
85 
ProcessEvent(const EventLoop * loop,int fd,uint32_t oper)86 LE_STATUS ProcessEvent(const EventLoop *loop, int fd, uint32_t oper)
87 {
88     BaseTask *task = GetTaskByFd((EventLoop *)loop, fd);
89     if (task != NULL) {
90         if (oper & Event_Error) {
91             task->flags |= TASK_FLAGS_INVALID;
92         }
93         task->handleEvent((LoopHandle)loop, (TaskHandle)task, oper);
94     } else {
95         loop->delEvent(loop, fd, oper);
96     }
97     return LE_SUCCESS;
98 }
99 
AddTask(EventLoop * loop,BaseTask * task)100 LE_STATUS AddTask(EventLoop *loop, BaseTask *task)
101 {
102     LoopMutexLock(&loop->mutex);
103     OH_HashMapAdd(loop->taskMap, &task->hashNode);
104     LoopMutexUnlock(&loop->mutex);
105     return LE_SUCCESS;
106 }
107 
GetTaskByFd(EventLoop * loop,int fd)108 BaseTask *GetTaskByFd(EventLoop *loop, int fd)
109 {
110     BaseTask *task = NULL;
111     LoopMutexLock(&loop->mutex);
112     TaskId id = {0, {fd}};
113     HashNode *node = OH_HashMapGet(loop->taskMap, &id);
114     if (node != NULL) {
115         task = HASHMAP_ENTRY(node, BaseTask, hashNode);
116     }
117     LoopMutexUnlock(&loop->mutex);
118     return task;
119 }
120 
DelTask(EventLoop * loop,BaseTask * task)121 void DelTask(EventLoop *loop, BaseTask *task)
122 {
123     loop->delEvent(loop, task->taskId.fd,
124         Event_Read | Event_Write | Event_Error | Event_Free | Event_Timeout | Event_Signal);
125     LoopMutexLock(&loop->mutex);
126     OH_HashMapRemove(loop->taskMap, (TaskId *)task);
127     LoopMutexUnlock(&loop->mutex);
128     return;
129 }
130 
131 static EventLoop *g_defaultLoop = NULL;
LE_GetDefaultLoop(void)132 LoopHandle LE_GetDefaultLoop(void)
133 {
134     if (g_defaultLoop == NULL) {
135         LE_CreateLoop((LoopHandle *)&g_defaultLoop);
136     }
137     return (LoopHandle)g_defaultLoop;
138 }
139 
LE_CreateLoop(LoopHandle * handlle)140 LE_STATUS LE_CreateLoop(LoopHandle *handlle)
141 {
142     EventLoop *loop = NULL;
143     LE_STATUS ret = CreateLoop_(&loop, LOOP_MAX_SOCKET, DEFAULT_TIMEOUT);
144     *handlle = (LoopHandle)loop;
145     return ret;
146 }
147 
LE_RunLoop(const LoopHandle handle)148 void LE_RunLoop(const LoopHandle handle)
149 {
150     LE_CHECK(handle != NULL, return, "Invalid handle");
151     EventLoop *loop = (EventLoop *)handle;
152     loop->runLoop(loop);
153 }
154 
LE_CloseLoop(const LoopHandle loopHandle)155 void LE_CloseLoop(const LoopHandle loopHandle)
156 {
157     LE_CHECK(loopHandle != NULL, return, "Invalid handle");
158     CloseLoop((EventLoop *)loopHandle);
159 }
160 
LE_StopLoop(const LoopHandle handle)161 void LE_StopLoop(const LoopHandle handle)
162 {
163     LE_CHECK(handle != NULL, return, "Invalid handle");
164     EventLoop *loop = (EventLoop *)handle;
165     loop->stop = 1;
166 }