1 /*
2 * osal_mutex.c
3 *
4 * osal driver
5 *
6 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19 #include "osal_mutex.h"
20 #include <linux/export.h>
21 #include <linux/mutex.h>
22 #include "hdf_log.h"
23 #include "osal_mem.h"
24
25 #define HDF_LOG_TAG osal_mutex
26
OsalMutexInit(struct OsalMutex * mutex)27 int32_t OsalMutexInit(struct OsalMutex *mutex)
28 {
29 struct mutex *mutex_tmp = NULL;
30
31 if (mutex == NULL) {
32 HDF_LOGE("%s invalid param", __func__);
33 return HDF_ERR_INVALID_PARAM;
34 }
35
36 mutex_tmp = (struct mutex *)OsalMemCalloc(sizeof(*mutex_tmp));
37 if (mutex_tmp == NULL) {
38 HDF_LOGE("malloc fail");
39 return HDF_ERR_MALLOC_FAIL;
40 }
41 mutex_init(mutex_tmp);
42 mutex->realMutex = (void *)mutex_tmp;
43
44 return HDF_SUCCESS;
45 }
46 EXPORT_SYMBOL(OsalMutexInit);
47
OsalMutexDestroy(struct OsalMutex * mutex)48 int32_t OsalMutexDestroy(struct OsalMutex *mutex)
49 {
50 if (mutex == NULL || mutex->realMutex == NULL) {
51 HDF_LOGE("%s invalid param", __func__);
52 return HDF_ERR_INVALID_PARAM;
53 }
54
55 mutex_destroy((struct mutex *)mutex->realMutex);
56 OsalMemFree(mutex->realMutex);
57 mutex->realMutex = NULL;
58
59 return HDF_SUCCESS;
60 }
61 EXPORT_SYMBOL(OsalMutexDestroy);
62
OsalMutexLock(struct OsalMutex * mutex)63 int32_t OsalMutexLock(struct OsalMutex *mutex)
64 {
65 if (mutex == NULL || mutex->realMutex == NULL) {
66 HDF_LOGE("%s invalid param", __func__);
67 return HDF_ERR_INVALID_PARAM;
68 }
69
70 mutex_lock((struct mutex *)mutex->realMutex);
71
72 return HDF_SUCCESS;
73 }
74 EXPORT_SYMBOL(OsalMutexLock);
75
OsalMutexTimedLock(struct OsalMutex * mutex,uint32_t mSec)76 int32_t OsalMutexTimedLock(struct OsalMutex *mutex, uint32_t mSec)
77 {
78 if (mutex == NULL || mutex->realMutex == NULL) {
79 HDF_LOGE("%s invalid param", __func__);
80 return HDF_ERR_INVALID_PARAM;
81 }
82
83 (void)mSec;
84 mutex_lock((struct mutex *)mutex->realMutex);
85
86 return HDF_SUCCESS;
87 }
88 EXPORT_SYMBOL(OsalMutexTimedLock);
89
OsalMutexUnlock(struct OsalMutex * mutex)90 int32_t OsalMutexUnlock(struct OsalMutex *mutex)
91 {
92 if (mutex == NULL || mutex->realMutex == NULL) {
93 HDF_LOGE("%s invalid param", __func__);
94 return HDF_ERR_INVALID_PARAM;
95 }
96
97 mutex_unlock((struct mutex *)mutex->realMutex);
98
99 return HDF_SUCCESS;
100 }
101 EXPORT_SYMBOL(OsalMutexUnlock);
102
103