1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "osal_sem.h"
32 #ifndef __LITEOS_M__
33 #include "los_hwi.h"
34 #endif
35 #include "los_sem.h"
36 #ifndef __LITEOS_M__
37 #include "los_typedef.h"
38 #endif
39 #include "hdf_log.h"
40
41 #define HDF_LOG_TAG osal_sem
42 #define HDF_INVALID_SEM_ID UINT32_MAX
43
OsalSemInit(struct OsalSem * sem,uint32_t value)44 int32_t OsalSemInit(struct OsalSem *sem, uint32_t value)
45 {
46 uint32_t semId;
47 uint32_t ret;
48
49 if (sem == NULL) {
50 HDF_LOGE("%s invalid param", __func__);
51 return HDF_ERR_INVALID_PARAM;
52 }
53
54 ret = LOS_SemCreate((uint16_t)value, &semId);
55 if (ret == LOS_OK) {
56 sem->realSemaphore = (void *)(uintptr_t)semId;
57 return HDF_SUCCESS;
58 } else {
59 sem->realSemaphore = (void *)(uintptr_t)HDF_INVALID_SEM_ID;
60 HDF_LOGE("%s create fail %u", __func__, ret);
61 return HDF_FAILURE;
62 }
63 }
64
OsalSemWait(struct OsalSem * sem,uint32_t ms)65 int32_t OsalSemWait(struct OsalSem *sem, uint32_t ms)
66 {
67 uint32_t ret;
68
69 if (sem == NULL || sem->realSemaphore == (void *)(uintptr_t)HDF_INVALID_SEM_ID) {
70 HDF_LOGE("%s invalid param", __func__);
71 return HDF_ERR_INVALID_PARAM;
72 }
73
74 ret = LOS_SemPend((uint32_t)(uintptr_t)sem->realSemaphore, LOS_MS2Tick(ms));
75 if (ret == LOS_OK) {
76 return HDF_SUCCESS;
77 } else {
78 if (ret == LOS_ERRNO_SEM_TIMEOUT) {
79 return HDF_ERR_TIMEOUT;
80 }
81 HDF_LOGE("%s LOS_SemPend fail %u", __func__, ret);
82 return HDF_FAILURE;
83 }
84 }
85
OsalSemPost(struct OsalSem * sem)86 int32_t OsalSemPost(struct OsalSem *sem)
87 {
88 uint32_t ret;
89
90 if (sem == NULL || sem->realSemaphore == (void *)(uintptr_t)HDF_INVALID_SEM_ID) {
91 HDF_LOGE("%s invalid param", __func__);
92 return HDF_ERR_INVALID_PARAM;
93 }
94
95 ret = LOS_SemPost((uint32_t)(uintptr_t)sem->realSemaphore);
96 if (ret == LOS_OK) {
97 return HDF_SUCCESS;
98 } else {
99 HDF_LOGE("%s LOS_SemPost fail %u", __func__, ret);
100 return HDF_FAILURE;
101 }
102 }
103
OsalSemDestroy(struct OsalSem * sem)104 int32_t OsalSemDestroy(struct OsalSem *sem)
105 {
106 uint32_t ret;
107
108 if (sem == NULL || sem->realSemaphore == (void *)(uintptr_t)HDF_INVALID_SEM_ID) {
109 HDF_LOGE("%s invalid param", __func__);
110 return HDF_ERR_INVALID_PARAM;
111 }
112
113 ret = LOS_SemDelete((uint32_t)(uintptr_t)sem->realSemaphore);
114 if (ret != LOS_OK) {
115 HDF_LOGE("%s LOS_SemDelete fail %u", __func__, ret);
116 return HDF_FAILURE;
117 }
118 sem->realSemaphore = (void *)(uintptr_t)HDF_INVALID_SEM_ID;
119 return HDF_SUCCESS;
120 }
121