• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "osal_sem.h"
10 #include <errno.h>
11 #include <semaphore.h>
12 #include <time.h>
13 #include "securec.h"
14 #include "hdf_log.h"
15 #include "osal_mem.h"
16 
17 const int32_t SHARE = 0;
18 #define HDF_LOG_TAG osal_sem
19 #define HDF_NANO_UNITS 1000000000
20 
OsalSemInit(struct OsalSem * sem,uint32_t value)21 int32_t OsalSemInit(struct OsalSem *sem, uint32_t value)
22 {
23     sem_t *semTmp = NULL;
24 
25     if (sem == NULL) {
26         HDF_LOGE("%s invalid param", __func__);
27         return HDF_ERR_INVALID_PARAM;
28     }
29 
30     sem->realSemaphore = NULL;
31 
32     semTmp = (sem_t *)OsalMemCalloc(sizeof(sem_t));
33     if (semTmp == NULL) {
34         HDF_LOGE("malloc fail");
35         return HDF_ERR_MALLOC_FAIL;
36     }
37 
38     if (sem_init(semTmp, SHARE, value) != 0) {
39         HDF_LOGE("sem_init fail errno:%d", errno);
40         OsalMemFree(semTmp);
41         return HDF_FAILURE;
42     }
43     sem->realSemaphore = (void *)semTmp;
44 
45     return HDF_SUCCESS;
46 }
47 
OsalSemWait(struct OsalSem * sem,uint32_t ms)48 int32_t OsalSemWait(struct OsalSem *sem, uint32_t ms)
49 {
50     if (sem == NULL || sem->realSemaphore == NULL) {
51         HDF_LOGE("%s invalid param", __func__);
52         return HDF_ERR_INVALID_PARAM;
53     }
54 
55     if (ms == HDF_WAIT_FOREVER) {
56         if (sem_wait((sem_t *)sem->realSemaphore) != 0) {
57             HDF_LOGE("sem_wait fail errno:%d", errno);
58             return HDF_FAILURE;
59         }
60     } else {
61         struct timespec time;
62         (void)memset_s(&time, sizeof(time), 0, sizeof(time));
63         clock_gettime(CLOCK_REALTIME, &time);
64         time.tv_sec += (time_t)ms / HDF_KILO_UNIT;
65         time.tv_nsec += (time_t)(ms % HDF_KILO_UNIT) * HDF_KILO_UNIT * HDF_KILO_UNIT;
66         if (time.tv_nsec >= HDF_NANO_UNITS) {
67             time.tv_nsec -= HDF_NANO_UNITS;
68             time.tv_sec += 1;
69         }
70         int32_t ret = sem_timedwait((sem_t *)sem->realSemaphore, &time);
71         if (ret != 0) {
72             if (errno == ETIMEDOUT) {
73                 return HDF_ERR_TIMEOUT;
74             } else {
75                 HDF_LOGE("%s time_out time:%d ret:%d,errno:%d", __func__, ms, ret, errno);
76                 return HDF_FAILURE;
77             }
78         }
79     }
80 
81     return HDF_SUCCESS;
82 }
83 
OsalSemPost(struct OsalSem * sem)84 int32_t OsalSemPost(struct OsalSem *sem)
85 {
86     if (sem == NULL || sem->realSemaphore == NULL) {
87         HDF_LOGE("%s invalid param", __func__);
88         return HDF_ERR_INVALID_PARAM;
89     }
90 
91     if (sem_post((sem_t *)sem->realSemaphore) != 0) {
92         HDF_LOGE("sem_post fail errno:%d", errno);
93         return HDF_FAILURE;
94     }
95 
96     return HDF_SUCCESS;
97 }
98 
OsalSemDestroy(struct OsalSem * sem)99 int32_t OsalSemDestroy(struct OsalSem *sem)
100 {
101     if (sem == NULL || sem->realSemaphore == NULL) {
102         HDF_LOGE("%s invalid param", __func__);
103         return HDF_ERR_INVALID_PARAM;
104     }
105 
106     int32_t ret = sem_destroy((sem_t *)sem->realSemaphore);
107     if (ret != 0) {
108         HDF_LOGE("sem_destroy fail errno:%d", errno);
109         return HDF_FAILURE;
110     }
111     OsalMemFree(sem->realSemaphore);
112     sem->realSemaphore = NULL;
113 
114     return HDF_SUCCESS;
115 }
116 
117