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/semaphore.h"
17 #include <fcntl.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <sys/eventfd.h>
21 #include "platform/include/mutex.h"
22 #include "platform/include/platform_def.h"
23 #include "securec.h"
24 
25 #if !defined(EFD_SEMAPHORE)
26 #define EFD_SEMAPHORE (1 << 0)
27 #endif
28 
29 typedef struct Semaphore {
30     Mutex *mutex;
31     int fd;
32 } SemaphoreInternal;
33 
SemaphoreCreate(uint32_t val)34 Semaphore *SemaphoreCreate(uint32_t val)
35 {
36     int efd = eventfd(val, EFD_SEMAPHORE);
37     if (efd == -1) {
38         LOG_ERROR("SemaphoreCreate: create eventfd failed, error no: %{public}d.", errno);
39         return NULL;
40     }
41 
42     Mutex *mutex = MutexCreate();
43     if (mutex == NULL) {
44         close(efd);
45         LOG_ERROR("SemaphoreCreate: create Mutex failed.");
46         return NULL;
47     }
48 
49     Semaphore *semaphore = (Semaphore *)malloc(sizeof(Semaphore));
50     if (semaphore == NULL) {
51         close(efd);
52         MutexDelete(mutex);
53         LOG_ERROR("SemaphoreCreate: create Semaphore failed.");
54         return NULL;
55     }
56     (void)memset_s(semaphore, sizeof(Semaphore), 0, sizeof(Semaphore));
57     semaphore->fd = efd;
58     semaphore->mutex = mutex;
59 
60     return semaphore;
61 }
62 
SemaphoreDelete(Semaphore * sem)63 void SemaphoreDelete(Semaphore *sem)
64 {
65     if (sem == NULL) {
66         return;
67     }
68 
69     if (sem->fd != -1) {
70         close(sem->fd);
71     }
72 
73     MutexDelete(sem->mutex);
74     free(sem);
75 }
76 
SemaphoreWait(const Semaphore * sem)77 void SemaphoreWait(const Semaphore *sem)
78 {
79     ASSERT(sem);
80 
81     eventfd_t val;
82     eventfd_read(sem->fd, &val);
83 }
84 
SemaphoreTryWait(Semaphore * sem)85 int32_t SemaphoreTryWait(Semaphore *sem)
86 {
87     ASSERT(sem);
88 
89     MutexLock(sem->mutex);
90     int flags = fcntl(sem->fd, F_GETFL);
91     if (flags == -1) {
92         goto ERROR;
93     }
94 
95     if (fcntl(sem->fd, F_SETFL, flags | EFD_NONBLOCK) == -1) {
96         goto ERROR;
97     }
98 
99     eventfd_t val;
100     if (eventfd_read(sem->fd, &val) == -1) {
101         goto ERROR;
102     }
103 
104     if (fcntl(sem->fd, F_SETFL, flags) == -1) {
105         goto ERROR;
106     }
107 
108     MutexUnlock(sem->mutex);
109     return 0;
110 
111 ERROR:
112     MutexUnlock(sem->mutex);
113     return -1;
114 }
115 
SemaphorePost(const Semaphore * sem)116 void SemaphorePost(const Semaphore *sem)
117 {
118     ASSERT(sem);
119 
120     eventfd_write(sem->fd, 1);
121 }
122 
SemaphoreTryPost(Semaphore * sem)123 int32_t SemaphoreTryPost(Semaphore *sem)
124 {
125     ASSERT(sem);
126 
127     MutexLock(sem->mutex);
128     int flags = fcntl(sem->fd, F_GETFL);
129     if (flags == -1) {
130         goto ERROR;
131     }
132 
133     if (fcntl(sem->fd, F_SETFL, flags | EFD_NONBLOCK) == -1) {
134         goto ERROR;
135     }
136 
137     if (eventfd_write(sem->fd, 1) == -1) {
138         goto ERROR;
139     }
140 
141     if (fcntl(sem->fd, F_SETFL, flags) == -1) {
142         goto ERROR;
143     }
144 
145     MutexUnlock(sem->mutex);
146     return 0;
147 
148 ERROR:
149     MutexUnlock(sem->mutex);
150     return -1;
151 }
152 
SemaphoreGetfd(const Semaphore * sem)153 int SemaphoreGetfd(const Semaphore *sem)
154 {
155     ASSERT(sem);
156 
157     return sem->fd;
158 }
159