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 "platform/include/event.h"
17 #include <pthread.h>
18 #include <stdlib.h>
19 #include <sys/time.h>
20 #include "platform/include/platform_def.h"
21 #include "log.h"
22
23 typedef struct Event {
24 pthread_mutex_t mutex;
25 pthread_cond_t cond;
26 bool signal;
27 bool autoClear;
28 } EventInternal;
29
EventCreate(bool isAutoClear)30 Event *EventCreate(bool isAutoClear)
31 {
32 Event *event = (Event *)malloc(sizeof(Event));
33 if (event != NULL) {
34 pthread_mutex_init(&event->mutex, NULL);
35 pthread_cond_init(&event->cond, 0);
36 event->autoClear = isAutoClear;
37 event->signal = false;
38 }
39 return event;
40 }
41
EventDelete(Event * event)42 void EventDelete(Event *event)
43 {
44 if (event == NULL) {
45 return;
46 }
47
48 while (EBUSY == pthread_cond_destroy(&event->cond)) {
49 EventClear(event);
50 };
51 pthread_mutex_destroy(&event->mutex);
52 free(event);
53 }
54
EventSet(Event * event)55 void EventSet(Event *event)
56 {
57 ASSERT(event);
58
59 pthread_mutex_lock(&event->mutex);
60 if (event->signal) {
61 pthread_mutex_unlock(&event->mutex);
62 return;
63 }
64
65 event->signal = true;
66 pthread_cond_broadcast(&event->cond);
67 pthread_mutex_unlock(&event->mutex);
68 }
69
EventClear(Event * event)70 void EventClear(Event *event)
71 {
72 ASSERT(event);
73
74 pthread_mutex_lock(&event->mutex);
75 event->signal = false;
76 pthread_cond_broadcast(&event->cond);
77 pthread_mutex_unlock(&event->mutex);
78 }
79
EventWaitInternal(Event * event,int64_t ms)80 static int32_t EventWaitInternal(Event *event, int64_t ms)
81 {
82 int ret;
83 if (ms < 0) {
84 ret = pthread_cond_wait(&event->cond, &event->mutex);
85 } else {
86 struct timeval now;
87 struct timespec timeout;
88 gettimeofday(&now, NULL);
89 timeout.tv_sec = now.tv_sec + ms / MS_PER_SECOND;
90 timeout.tv_nsec = now.tv_usec * MS_PER_SECOND + (ms % MS_PER_SECOND) * NS_PER_MS;
91 ret = pthread_cond_timedwait(&event->cond, &event->mutex, &timeout);
92 }
93
94 return ret;
95 }
96
EventWait(Event * event,int64_t ms)97 int32_t EventWait(Event *event, int64_t ms)
98 {
99 ASSERT(event);
100
101 int32_t ret = 0;
102 pthread_mutex_lock(&event->mutex);
103
104 if (event->signal == false) {
105 int32_t err;
106 err = EventWaitInternal(event, ms);
107 if (err == ETIMEDOUT) {
108 HILOGE("EventWait result is timeout");
109 ret = EVENT_WAIT_TIMEOUT_ERR;
110 } else if (err) {
111 HILOGE("EventWait result is wait err");
112 ret = EVENT_WAIT_OTHER_ERR;
113 }
114 if (event->autoClear && event->signal) {
115 event->signal = false;
116 }
117 } else {
118 if (event->autoClear) {
119 event->signal = false;
120 }
121 }
122
123 pthread_mutex_unlock(&event->mutex);
124 return ret;
125 }