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 #ifdef __cplusplus 19 extern "C" { 20 #endif 21 HcMutexLock(HcMutex * mutex)22static int HcMutexLock(HcMutex *mutex) 23 { 24 if (mutex == NULL) { 25 return -1; 26 } 27 return -pthread_mutex_lock(&mutex->mutex); 28 } 29 HcMutexUnlock(HcMutex * mutex)30static void HcMutexUnlock(HcMutex *mutex) 31 { 32 if (mutex == NULL) { 33 return; 34 } 35 pthread_mutex_unlock(&mutex->mutex); 36 } 37 InitHcMutex(struct HcMutexT * mutex)38int32_t InitHcMutex(struct HcMutexT *mutex) 39 { 40 if (mutex == NULL) { 41 return -1; 42 } 43 int res = pthread_mutex_init(&mutex->mutex, NULL); 44 if (res != 0) { 45 return res; 46 } 47 mutex->lock = HcMutexLock; 48 mutex->unlock = HcMutexUnlock; 49 return 0; 50 } 51 DestroyHcMutex(struct HcMutexT * mutex)52void DestroyHcMutex(struct HcMutexT *mutex) 53 { 54 if (mutex == NULL) { 55 return; 56 } 57 pthread_mutex_destroy(&mutex->mutex); 58 } 59 60 #ifdef __cplusplus 61 } 62 #endif