• 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 #include "le_task.h"
16 #include <time.h>
17 #include <sys/eventfd.h>
18 
19 #include "le_loop.h"
20 
21 #define MILLION_MICROSECOND 1000000
22 #define THOUSAND_MILLISECOND 1000
23 
DoAsyncEvent_(const LoopHandle loopHandle,AsyncEventTask * asyncTask)24 static void DoAsyncEvent_(const LoopHandle loopHandle, AsyncEventTask *asyncTask)
25 {
26     LE_CHECK(loopHandle != NULL && asyncTask != NULL, return, "Invalid parameters");
27 #ifdef LOOP_DEBUG
28     struct timespec startTime = {0};
29     struct timespec endTime = {0};
30     long long diff;
31     clock_gettime(CLOCK_MONOTONIC, &(startTime));
32 #endif
33     StreamTask *task = &asyncTask->stream;
34     ListNode *node = task->buffHead.next;
35     if (node != &task->buffHead) {
36         LE_Buffer *buffer = ListEntry(node, LE_Buffer, node);
37         uint64_t eventId = *(uint64_t*)(buffer->data);
38         if (asyncTask->processAsyncEvent) {
39             asyncTask->processAsyncEvent((TaskHandle)asyncTask, eventId,
40                 (uint8_t *)(buffer->data + sizeof(uint64_t)), buffer->dataSize);
41         }
42         OH_ListRemove(&buffer->node);
43         free(buffer);
44 #ifdef LOOP_DEBUG
45         clock_gettime(CLOCK_MONOTONIC, &(endTime));
46         diff = (long long)((endTime.tv_sec - startTime.tv_sec) * MILLION_MICROSECOND);
47         if (endTime.tv_nsec > startTime.tv_nsec) {
48             diff += (endTime.tv_nsec - startTime.tv_nsec) / THOUSAND_MILLISECOND; // 1000 ms
49         } else {
50             diff -= (endTime.tv_nsec - startTime.tv_nsec) / THOUSAND_MILLISECOND; // 1000 ms
51         }
52         LE_LOGI("DoAsyncEvent_ diff %ld",  diff);
53 #endif
54     }
55 }
56 
57 #ifdef STARTUP_INIT_TEST
LE_DoAsyncEvent(const LoopHandle loopHandle,const TaskHandle taskHandle)58 void LE_DoAsyncEvent(const LoopHandle loopHandle, const TaskHandle taskHandle)
59 {
60     AsyncEventTask *asyncTask = (AsyncEventTask *)taskHandle;
61     while (!IsBufferEmpty(&asyncTask->stream)) {
62         DoAsyncEvent_(loopHandle, (AsyncEventTask *)taskHandle);
63     }
64 }
65 #endif
HandleAsyncEvent_(const LoopHandle loopHandle,const TaskHandle taskHandle,uint32_t oper)66 static LE_STATUS HandleAsyncEvent_(const LoopHandle loopHandle, const TaskHandle taskHandle, uint32_t oper)
67 {
68     LE_LOGV("HandleAsyncEvent_ fd: %d oper 0x%x", GetSocketFd(taskHandle), oper);
69     EventLoop *loop = (EventLoop *)loopHandle;
70     AsyncEventTask *asyncTask = (AsyncEventTask *)taskHandle;
71     if (LE_TEST_FLAGS(oper, EVENT_READ)) {
72         uint64_t eventId = 0;
73         int ret = read(GetSocketFd(taskHandle), &eventId, sizeof(eventId));
74         LE_LOGV("HandleAsyncEvent_ read fd:%d ret: %d eventId %llu", GetSocketFd(taskHandle), ret, eventId);
75         DoAsyncEvent_(loopHandle, asyncTask);
76         if (!IsBufferEmpty(&asyncTask->stream)) {
77             loop->modEvent(loop, (const BaseTask *)taskHandle, EVENT_WRITE);
78             return LE_SUCCESS;
79         }
80     } else {
81         static uint64_t eventId = 0;
82         (void)write(GetSocketFd(taskHandle), &eventId, sizeof(eventId));
83         loop->modEvent(loop, (const BaseTask *)taskHandle, EVENT_READ);
84         eventId++;
85     }
86     return LE_SUCCESS;
87 }
88 
HandleAsyncTaskClose_(const LoopHandle loopHandle,const TaskHandle taskHandle)89 static void HandleAsyncTaskClose_(const LoopHandle loopHandle, const TaskHandle taskHandle)
90 {
91     BaseTask *task = (BaseTask *)taskHandle;
92     CloseTask(loopHandle, task);
93     DelTask((EventLoop *)loopHandle, task);
94     close(task->taskId.fd);
95 }
96 
DumpEventTaskInfo_(const TaskHandle task)97 static void DumpEventTaskInfo_(const TaskHandle task)
98 {
99     INIT_CHECK(task != NULL, return);
100     BaseTask *baseTask = (BaseTask *)task;
101     AsyncEventTask *eventTask = (AsyncEventTask *)baseTask;
102     printf("\tfd: %d \n", eventTask->stream.base.taskId.fd);
103     printf("\t  TaskType: %s\n", "EventTask");
104 }
105 
LE_CreateAsyncTask(const LoopHandle loopHandle,TaskHandle * taskHandle,LE_ProcessAsyncEvent processAsyncEvent)106 LE_STATUS LE_CreateAsyncTask(const LoopHandle loopHandle,
107     TaskHandle *taskHandle, LE_ProcessAsyncEvent processAsyncEvent)
108 {
109     LE_CHECK(loopHandle != NULL && taskHandle != NULL, return LE_INVALID_PARAM, "Invalid parameters");
110     LE_CHECK(processAsyncEvent != NULL, return LE_INVALID_PARAM, "Invalid parameters processAsyncEvent ");
111 
112     int fd = eventfd(1, EFD_NONBLOCK | EFD_CLOEXEC);
113     LE_CHECK(fd > 0, return LE_FAILURE, "Failed to event fd ");
114     LE_BaseInfo baseInfo = {TASK_EVENT | TASK_ASYNC_EVENT, NULL};
115     AsyncEventTask *task = (AsyncEventTask *)CreateTask(loopHandle, fd, &baseInfo, sizeof(AsyncEventTask));
116     LE_CHECK(task != NULL, close(fd);
117         return LE_NO_MEMORY, "Failed to create task");
118     task->stream.base.handleEvent = HandleAsyncEvent_;
119     task->stream.base.innerClose = HandleAsyncTaskClose_;
120     task->stream.base.dumpTaskInfo = DumpEventTaskInfo_;
121     OH_ListInit(&task->stream.buffHead);
122     LoopMutexInit(&task->stream.mutex);
123     task->processAsyncEvent = processAsyncEvent;
124     EventLoop *loop = (EventLoop *)loopHandle;
125     loop->addEvent(loop, (const BaseTask *)task, EVENT_READ);
126     *taskHandle = (TaskHandle)task;
127     return LE_SUCCESS;
128 }
129 
LE_StartAsyncEvent(const LoopHandle loopHandle,const TaskHandle taskHandle,uint64_t eventId,const uint8_t * data,uint32_t buffLen)130 LE_STATUS LE_StartAsyncEvent(const LoopHandle loopHandle,
131     const TaskHandle taskHandle, uint64_t eventId, const uint8_t *data, uint32_t buffLen)
132 {
133     LE_CHECK(loopHandle != NULL && taskHandle != NULL, return LE_INVALID_PARAM, "Invalid parameters");
134     BufferHandle handle = LE_CreateBuffer(loopHandle, buffLen + 1 + sizeof(eventId));
135     char *buff = (char *)LE_GetBufferInfo(handle, NULL, NULL);
136     int ret = memcpy_s(buff, sizeof(eventId), &eventId, sizeof(eventId));
137     LE_CHECK(ret == 0, return -1, "Failed to copy data");
138     if (data != NULL && buffLen > 0) {
139         ret = memcpy_s(buff + sizeof(eventId), buffLen, data, buffLen);
140         LE_CHECK(ret == 0, return -1, "Failed to copy data");
141         buff[sizeof(eventId) + buffLen] = '\0';
142     }
143     return LE_Send(loopHandle, taskHandle, handle, buffLen);
144 }
145 
LE_StopAsyncTask(LoopHandle loopHandle,TaskHandle taskHandle)146 void LE_StopAsyncTask(LoopHandle loopHandle, TaskHandle taskHandle)
147 {
148     LE_CHECK(loopHandle != NULL && taskHandle != NULL, return, "Invalid parameters");
149     LE_CloseTask(loopHandle, taskHandle);
150 }