1 /*
2 * Copyright (c) 2020-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 #include "los_hwi.h"
33 #include "los_sem.h"
34 #include "los_typedef.h"
35 #include "hdf_log.h"
36
37 #define HDF_LOG_TAG osal_sem
38 #define HDF_INVALID_SEM_ID UINT32_MAX
39
OsalSemInit(struct OsalSem * sem,uint32_t value)40 int32_t OsalSemInit(struct OsalSem *sem, uint32_t value)
41 {
42 uint32_t semId;
43 uint32_t ret;
44
45 if (sem == NULL) {
46 HDF_LOGE("%s invalid param", __func__);
47 return HDF_ERR_INVALID_PARAM;
48 }
49
50 ret = LOS_SemCreate((uint16_t)value, &semId);
51 if (ret == LOS_OK) {
52 sem->realSemaphore = (void *)(uintptr_t)semId;
53 return HDF_SUCCESS;
54 } else {
55 sem->realSemaphore = (void *)(uintptr_t)HDF_INVALID_SEM_ID;
56 HDF_LOGE("%s create fail %d", __func__, ret);
57 return HDF_FAILURE;
58 }
59 }
60
OsalSemWait(struct OsalSem * sem,uint32_t ms)61 int32_t OsalSemWait(struct OsalSem *sem, uint32_t ms)
62 {
63 uint32_t ret;
64
65 if (sem == NULL || sem->realSemaphore == (void *)(uintptr_t)HDF_INVALID_SEM_ID) {
66 HDF_LOGE("%s invalid param", __func__);
67 return HDF_ERR_INVALID_PARAM;
68 }
69
70 ret = LOS_SemPend((uint32_t)(uintptr_t)sem->realSemaphore, LOS_MS2Tick(ms));
71 if (ret == LOS_OK) {
72 return HDF_SUCCESS;
73 } else {
74 if (ret == LOS_ERRNO_SEM_TIMEOUT) {
75 return HDF_ERR_TIMEOUT;
76 }
77 HDF_LOGE("%s LOS_SemPend fail %d", __func__, ret);
78 return HDF_FAILURE;
79 }
80 }
81
OsalSemPost(struct OsalSem * sem)82 int32_t OsalSemPost(struct OsalSem *sem)
83 {
84 uint32_t ret;
85
86 if (sem == NULL || sem->realSemaphore == (void *)(uintptr_t)HDF_INVALID_SEM_ID) {
87 HDF_LOGE("%s invalid param", __func__);
88 return HDF_ERR_INVALID_PARAM;
89 }
90
91 ret = LOS_SemPost((uint32_t)(uintptr_t)sem->realSemaphore);
92 if (ret == LOS_OK) {
93 return HDF_SUCCESS;
94 } else {
95 HDF_LOGE("%s LOS_SemPost fail %d", __func__, ret);
96 return HDF_FAILURE;
97 }
98 }
99
OsalSemDestroy(struct OsalSem * sem)100 int32_t OsalSemDestroy(struct OsalSem *sem)
101 {
102 uint32_t ret;
103
104 if (sem == NULL || sem->realSemaphore == (void *)(uintptr_t)HDF_INVALID_SEM_ID) {
105 HDF_LOGE("%s invalid param", __func__);
106 return HDF_ERR_INVALID_PARAM;
107 }
108
109 ret = LOS_SemDelete((uint32_t)(uintptr_t)sem->realSemaphore);
110 if (ret != LOS_OK) {
111 HDF_LOGE("%s LOS_SemDelete fail %d", __func__, ret);
112 return HDF_FAILURE;
113 }
114 sem->realSemaphore = (void *)(uintptr_t)HDF_INVALID_SEM_ID;
115 return HDF_SUCCESS;
116 }
117
118