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
InitHcMutex(HcMutex *mutex)24 int32_t InitHcMutex(HcMutex *mutex)
25 {
26 if (mutex == NULL) {
27 return -1;
28 }
29 int res = pthread_mutex_init(mutex, NULL);
30 if (res != 0) {
31 LOGE("[OS]: pthread_mutex_init fail. [Res]: %d", res);
32 }
33 return res;
34 }
35
DestroyHcMutex(HcMutex *mutex)36 void DestroyHcMutex(HcMutex *mutex)
37 {
38 if (mutex == NULL) {
39 return;
40 }
41 int res = pthread_mutex_destroy(mutex);
42 if (res != 0) {
43 LOGW("[OS]: pthread_mutex_destroy fail. [Res]: %d", res);
44 }
45 }
46
LockHcMutex(HcMutex* mutex)47 int LockHcMutex(HcMutex* mutex)
48 {
49 if (mutex == NULL) {
50 LOGE("[OS]: mutex is null pointer!");
51 return -1;
52 }
53 int res = pthread_mutex_lock(mutex);
54 if (res != 0) {
55 LOGE("[OS]: pthread_mutex_lock fail. [Res]: %d", res);
56 }
57 return res;
58 }
59
UnlockHcMutex(HcMutex* mutex)60 void UnlockHcMutex(HcMutex* mutex)
61 {
62 if (mutex == NULL) {
63 LOGE("[OS]: mutex is null pointer!");
64 return;
65 }
66 int res = pthread_mutex_unlock(mutex);
67 if (res != 0) {
68 LOGW("[OS]: pthread_mutex_unlock fail. [Res]: %d", res);
69 }
70 }
71
72 #ifdef __cplusplus
73 }
74 #endif