1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "hc_mutex.h"
17
18 #include "hc_log.h"
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
HcMutexLock(HcMutex * mutex)24 static int HcMutexLock(HcMutex *mutex)
25 {
26 if (mutex == NULL) {
27 return -1;
28 }
29 int res = pthread_mutex_lock(&mutex->mutex);
30 if (res != 0) {
31 LOGW("[OS]: pthread_mutex_lock fail. [Res]: %d", res);
32 }
33 return res;
34 }
35
HcMutexUnlock(HcMutex * mutex)36 static void HcMutexUnlock(HcMutex *mutex)
37 {
38 if (mutex == NULL) {
39 return;
40 }
41 int res = pthread_mutex_unlock(&mutex->mutex);
42 if (res != 0) {
43 LOGW("[OS]: pthread_mutex_unlock fail. [Res]: %d", res);
44 }
45 }
46
InitHcMutex(struct HcMutexT * mutex)47 int32_t InitHcMutex(struct HcMutexT *mutex)
48 {
49 if (mutex == NULL) {
50 return -1;
51 }
52 int res = pthread_mutex_init(&mutex->mutex, NULL);
53 if (res != 0) {
54 LOGE("[OS]: pthread_mutex_init fail. [Res]: %d", res);
55 return res;
56 }
57 mutex->lock = HcMutexLock;
58 mutex->unlock = HcMutexUnlock;
59 return 0;
60 }
61
DestroyHcMutex(struct HcMutexT * mutex)62 void DestroyHcMutex(struct HcMutexT *mutex)
63 {
64 if (mutex == NULL) {
65 return;
66 }
67 int res = pthread_mutex_destroy(&mutex->mutex);
68 if (res != 0) {
69 LOGW("[OS]: pthread_mutex_destroy fail. [Res]: %d", res);
70 }
71 }
72
73 #ifdef __cplusplus
74 }
75 #endif