1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "semaphore.h"
33 #include "sys/types.h"
34 #include "map_error.h"
35 #include "time_posix.h"
36
37
38 /* Initialize semaphore to value, shared is not supported in Huawei LiteOS. */
sem_init(sem_t * sem,int shared,unsigned int value)39 int sem_init(sem_t *sem, int shared, unsigned int value)
40 {
41 UINT32 semHandle = 0;
42 UINT32 ret;
43
44 (VOID)shared;
45 if ((sem == NULL) || (value > OS_SEM_COUNT_MAX)) {
46 errno = EINVAL;
47 return -1;
48 }
49
50 ret = LOS_SemCreate(value, &semHandle);
51 if (map_errno(ret) != ENOERR) {
52 return -1;
53 }
54
55 sem->sem = GET_SEM(semHandle);
56
57 return 0;
58 }
59
sem_destroy(sem_t * sem)60 int sem_destroy(sem_t *sem)
61 {
62 UINT32 ret;
63
64 if ((sem == NULL) || (sem->sem == NULL)) {
65 errno = EINVAL;
66 return -1;
67 }
68
69 ret = LOS_SemDelete(sem->sem->semID);
70 if (map_errno(ret) != ENOERR) {
71 return -1;
72 }
73 return 0;
74 }
75
76 /* Decrement value if >0 or wait for a post. */
sem_wait(sem_t * sem)77 int sem_wait(sem_t *sem)
78 {
79 UINT32 ret;
80
81 if ((sem == NULL) || (sem->sem == NULL)) {
82 errno = EINVAL;
83 return -1;
84 }
85
86 ret = LOS_SemPend(sem->sem->semID, LOS_WAIT_FOREVER);
87 if (map_errno(ret) == ENOERR) {
88 return 0;
89 } else {
90 return -1;
91 }
92 }
93
94 /* Decrement value if >0, return -1 if not. */
sem_trywait(sem_t * sem)95 int sem_trywait(sem_t *sem)
96 {
97 UINT32 ret;
98
99 if ((sem == NULL) || (sem->sem == NULL)) {
100 errno = EINVAL;
101 return -1;
102 }
103
104 ret = LOS_SemPend(sem->sem->semID, LOS_NO_WAIT);
105 if (map_errno(ret) == ENOERR) {
106 return 0;
107 } else {
108 if ((errno != EINVAL) || (ret == LOS_ERRNO_SEM_UNAVAILABLE)) {
109 errno = EAGAIN;
110 }
111 return -1;
112 }
113 }
114
sem_timedwait(sem_t * sem,const struct timespec * timeout)115 int sem_timedwait(sem_t *sem, const struct timespec *timeout)
116 {
117 UINT32 ret;
118 UINT32 tickCnt;
119
120 if ((sem == NULL) || (sem->sem == NULL)) {
121 errno = EINVAL;
122 return -1;
123 }
124
125 if (!ValidTimeSpec(timeout)) {
126 errno = EINVAL;
127 return -1;
128 }
129
130 tickCnt = OsTimeSpec2Tick(timeout);
131 ret = LOS_SemPend(sem->sem->semID, tickCnt);
132 if (map_errno(ret) == ENOERR) {
133 return 0;
134 } else {
135 return -1;
136 }
137 }
138
sem_post(sem_t * sem)139 int sem_post(sem_t *sem)
140 {
141 UINT32 ret;
142
143 if ((sem == NULL) || (sem->sem == NULL)) {
144 errno = EINVAL;
145 return -1;
146 }
147
148 ret = LOS_SemPost(sem->sem->semID);
149 if (map_errno(ret) != ENOERR) {
150 return -1;
151 }
152
153 return 0;
154 }
155
sem_getvalue(sem_t * sem,int * currVal)156 int sem_getvalue(sem_t *sem, int *currVal)
157 {
158 INT32 val;
159
160 if ((sem == NULL) || (currVal == NULL)) {
161 errno = EINVAL;
162 return -1;
163 }
164 val = sem->sem->semCount;
165 if (val < 0) {
166 val = 0;
167 }
168
169 *currVal = val;
170 return 0;
171 }
172
sem_open(const char * name,int openFlag,...)173 sem_t *sem_open(const char *name, int openFlag, ...)
174 {
175 (VOID)name;
176 (VOID)openFlag;
177 errno = ENOSYS;
178 return NULL;
179 }
180
sem_close(sem_t * sem)181 int sem_close(sem_t *sem)
182 {
183 (VOID)sem;
184 errno = ENOSYS;
185 return -1;
186 }
187
sem_unlink(const char * name)188 int sem_unlink(const char *name)
189 {
190 (VOID)name;
191 errno = ENOSYS;
192 return -1;
193 }
194
195