• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <errno.h>
33 #include <pthread.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <securec.h>
37 #include "los_config.h"
38 #include "los_task.h"
39 #include "los_swtmr.h"
40 #include "time_internal.h"
41 #include "los_atomic.h"
42 #include "los_event.h"
43 #include "los_mux.h"
44 
45 typedef struct {
46     volatile INT32 *realValue;
47     INT32 value;
48     UINT32 clearEvent;
49 } EventCond;
50 
51 #define INLINE inline
52 
53 #define BROADCAST_EVENT     1
54 #define COND_COUNTER_STEP   0x0004U
55 #define COND_FLAGS_MASK     0x0003U
56 #define COND_COUNTER_MASK   (~COND_FLAGS_MASK)
57 
pthread_condattr_getpshared(const pthread_condattr_t * attr,int * shared)58 int pthread_condattr_getpshared(const pthread_condattr_t *attr, int *shared)
59 {
60     if ((attr == NULL) || (shared == NULL)) {
61         return EINVAL;
62     }
63 
64     *shared = PTHREAD_PROCESS_PRIVATE;
65 
66     return 0;
67 }
68 
pthread_condattr_setpshared(pthread_condattr_t * attr,int shared)69 int pthread_condattr_setpshared(pthread_condattr_t *attr, int shared)
70 {
71     (VOID)attr;
72     if ((shared != PTHREAD_PROCESS_PRIVATE) && (shared != PTHREAD_PROCESS_SHARED)) {
73         return EINVAL;
74     }
75 
76     if (shared != PTHREAD_PROCESS_PRIVATE) {
77         return ENOSYS;
78     }
79 
80     return 0;
81 }
82 
pthread_condattr_destroy(pthread_condattr_t * attr)83 int pthread_condattr_destroy(pthread_condattr_t *attr)
84 {
85     if (attr == NULL) {
86         return EINVAL;
87     }
88 
89     (VOID)memset_s(attr, sizeof(pthread_condattr_t), 0, sizeof(pthread_condattr_t));
90     attr->clock = INT32_MAX;
91 
92     return 0;
93 }
94 
pthread_condattr_getclock(const pthread_condattr_t * attr,clockid_t * clock)95 int pthread_condattr_getclock(const pthread_condattr_t *attr, clockid_t *clock)
96 {
97     if ((attr == NULL) || (clock == NULL)) {
98         return -1;
99     }
100 
101     *clock = attr->clock;
102 
103     return 0;
104 }
105 
pthread_condattr_init(pthread_condattr_t * attr)106 int pthread_condattr_init(pthread_condattr_t *attr)
107 {
108     if (attr == NULL) {
109         return EINVAL;
110     }
111 
112     attr->clock = CLOCK_REALTIME;
113 
114     return 0;
115 }
116 
pthread_condattr_setclock(pthread_condattr_t * attr,clockid_t clk)117 int pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clk)
118 {
119     if ((attr == NULL) || (clk < 0)) {
120         return EINVAL;
121     }
122 
123     if ((clk != CLOCK_REALTIME) && (clk != CLOCK_MONOTONIC) &&
124         (clk != CLOCK_PROCESS_CPUTIME_ID) && (clk != CLOCK_THREAD_CPUTIME_ID)) {
125         return EINVAL;
126     }
127 
128     attr->clock = clk;
129 
130     return 0;
131 }
132 
CondInitCheck(const pthread_cond_t * cond)133 STATIC INLINE INT32 CondInitCheck(const pthread_cond_t *cond)
134 {
135     if ((cond->event.stEventList.pstPrev == NULL) &&
136         (cond->event.stEventList.pstNext == NULL)) {
137         return 1;
138     }
139 
140     return 0;
141 }
142 
pthread_cond_init(pthread_cond_t * cond,const pthread_condattr_t * attr)143 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
144 {
145     int ret = 0;
146     pthread_condattr_t condAttr;
147 
148     if (cond == NULL) {
149         return EINVAL;
150     }
151 
152     if (attr == NULL) {
153         pthread_condattr_init(&condAttr);
154         attr = &condAttr;
155     }
156 
157     (VOID)LOS_EventInit(&(cond->event));
158 
159     cond->mutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
160     if (cond->mutex == NULL) {
161         return ENOMEM;
162     }
163 
164     (VOID)pthread_mutex_init(cond->mutex, NULL);
165 
166     cond->value = 0;
167     (VOID)pthread_mutex_lock(cond->mutex);
168     cond->count = 0;
169     cond->clock = attr->clock;
170     (VOID)pthread_mutex_unlock(cond->mutex);
171 
172     return ret;
173 }
174 
pthread_cond_destroy(pthread_cond_t * cond)175 int pthread_cond_destroy(pthread_cond_t *cond)
176 {
177     if (cond == NULL) {
178         return EINVAL;
179     }
180 
181     if (CondInitCheck(cond)) {
182         return 0;
183     }
184 
185     if (LOS_EventDestroy(&cond->event) != LOS_OK) {
186         return EBUSY;
187     }
188     if (pthread_mutex_destroy(cond->mutex) != 0) {
189         PRINT_ERR("%s mutex destroy fail!\n", __FUNCTION__);
190         return EINVAL;
191     }
192     free(cond->mutex);
193     cond->mutex = NULL;
194 
195     return 0;
196 }
197 
PthreadCountSub(pthread_cond_t * cond)198 STATIC VOID PthreadCountSub(pthread_cond_t *cond)
199 {
200     (VOID)pthread_mutex_lock(cond->mutex);
201     if (cond->count > 0) {
202         cond->count--;
203     }
204     (VOID)pthread_mutex_unlock(cond->mutex);
205 }
206 
207 
pthread_cond_broadcast(pthread_cond_t * cond)208 int pthread_cond_broadcast(pthread_cond_t *cond)
209 {
210     int ret = 0;
211 
212     if (cond == NULL) {
213         return EINVAL;
214     }
215 
216     (VOID)pthread_mutex_lock(cond->mutex);
217     if (cond->count > 0) {
218         cond->count = 0;
219         (VOID)pthread_mutex_unlock(cond->mutex);
220         (VOID)LOS_EventWrite(&(cond->event), BROADCAST_EVENT);
221         return ret;
222     }
223     (VOID)pthread_mutex_unlock(cond->mutex);
224 
225     return ret;
226 }
227 
pthread_cond_signal(pthread_cond_t * cond)228 int pthread_cond_signal(pthread_cond_t *cond)
229 {
230     int ret = 0;
231 
232     if (cond == NULL) {
233         return EINVAL;
234     }
235 
236     (VOID)pthread_mutex_lock(cond->mutex);
237     if (cond->count > 0) {
238         cond->count--;
239         (VOID)pthread_mutex_unlock(cond->mutex);
240         // This should modify to once.
241         (VOID)LOS_EventWrite(&(cond->event), BROADCAST_EVENT);
242 
243         return ret;
244     }
245     (VOID)pthread_mutex_unlock(cond->mutex);
246 
247     return ret;
248 }
249 
ProcessReturnVal(pthread_cond_t * cond,INT32 val)250 STATIC INT32 ProcessReturnVal(pthread_cond_t *cond, INT32 val)
251 {
252     INT32 ret;
253     switch (val) {
254         /* 0: event does not occur */
255         case 0:
256         case BROADCAST_EVENT:
257             ret = 0;
258             break;
259         case LOS_ERRNO_EVENT_READ_TIMEOUT:
260             PthreadCountSub(cond);
261             ret = ETIMEDOUT;
262             break;
263         default:
264             PthreadCountSub(cond);
265             ret = EINVAL;
266             break;
267     }
268 
269     return ret;
270 }
271 
pthread_cond_timedwait(pthread_cond_t * cond,pthread_mutex_t * mutex,const struct timespec * ts)272 int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
273                            const struct timespec *ts)
274 {
275     INT32 ret;
276     UINT64 absTicks;
277     LosMuxCB *muxPosted = NULL;
278     pthread_testcancel();
279     if ((cond == NULL) || (mutex == NULL) || (ts == NULL) || (mutex->magic != _MUX_MAGIC)) {
280         return EINVAL;
281     }
282 
283     muxPosted = GET_MUX(mutex->handle);
284     if ((mutex->stAttr.type == PTHREAD_MUTEX_ERRORCHECK) && (g_losTask.runTask != muxPosted->owner)) {
285         return EPERM;
286     }
287 
288     if (CondInitCheck(cond)) {
289         ret = pthread_cond_init(cond, NULL);
290         if (ret != 0) {
291             return ret;
292         }
293     }
294 
295     (VOID)pthread_mutex_lock(cond->mutex);
296     cond->count++;
297     (VOID)pthread_mutex_unlock(cond->mutex);
298 
299     ret = OsGetTickTimeFromNow(ts, cond->clock, &absTicks);
300     if (ret != 0) {
301         return ret;
302     }
303 
304     if (absTicks >= UINT32_MAX) {
305         return EINVAL;
306     }
307 
308     if (pthread_mutex_unlock(mutex) != 0) {
309         PRINT_ERR("%s: %d failed\n", __FUNCTION__, __LINE__);
310     }
311 
312     (VOID)LOS_EventClear(&(cond->event), 0);
313     ret = (INT32)LOS_EventRead(&(cond->event), 0x0f, LOS_WAITMODE_OR | LOS_WAITMODE_CLR, (UINT32)absTicks);
314 
315     if (pthread_mutex_lock(mutex) != 0) {
316         PRINT_ERR("%s: %d failed\n", __FUNCTION__, __LINE__);
317     }
318 
319     ret = ProcessReturnVal(cond, ret);
320     pthread_testcancel();
321 
322     return ret;
323 }
324 
pthread_cond_wait(pthread_cond_t * cond,pthread_mutex_t * mutex)325 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
326 {
327     int ret;
328 
329     if ((cond == NULL) || (mutex == NULL)) {
330         return EINVAL;
331     }
332 
333     if (CondInitCheck(cond)) {
334         ret = pthread_cond_init(cond, NULL);
335         if (ret != 0) {
336             return ret;
337         }
338     }
339 
340     (VOID)pthread_mutex_lock(cond->mutex);
341     cond->count++;
342     (VOID)pthread_mutex_unlock(cond->mutex);
343 
344     if (pthread_mutex_unlock(mutex) != 0) {
345         PRINT_ERR("%s: %d failed\n", __FUNCTION__, __LINE__);
346     }
347     ret = (INT32)LOS_EventRead(&(cond->event), 0x0f, LOS_WAITMODE_OR | LOS_WAITMODE_CLR, LOS_WAIT_FOREVER);
348     if (pthread_mutex_lock(mutex) != 0) {
349         PRINT_ERR("%s: %d failed\n", __FUNCTION__, __LINE__);
350     }
351 
352     switch (ret) {
353         /* 0: event does not occur */
354         case 0:
355         case BROADCAST_EVENT:
356             ret = 0;
357             break;
358         default:
359             PthreadCountSub(cond);
360             ret = EINVAL;
361             break;
362     }
363 
364     return ret;
365 }
366