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