• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "hks_mutex.h"
17 
18 #include <pthread.h>
19 
20 #include "hks_mem.h"
21 
22 struct HksMutex {
23     pthread_mutex_t mutex;
24 };
25 
HksMutexCreate(void)26 HksMutex *HksMutexCreate(void)
27 {
28     HksMutex *mutex = (HksMutex *)HksMalloc(sizeof(HksMutex));
29     if (mutex != NULL) {
30         int result = pthread_mutex_init(&mutex->mutex, NULL);
31         if (result != 0) {
32             HksFree(mutex);
33             mutex = NULL;
34         }
35     }
36     return mutex;
37 }
38 
HksMutexLock(HksMutex * mutex)39 int32_t HksMutexLock(HksMutex *mutex)
40 {
41     if (mutex == NULL) {
42         return -1;
43     }
44 
45     return pthread_mutex_lock(&mutex->mutex);
46 }
47 
HksMutexUnlock(HksMutex * mutex)48 int32_t HksMutexUnlock(HksMutex *mutex)
49 {
50     if (mutex == NULL) {
51         return -1;
52     }
53 
54     return pthread_mutex_unlock(&mutex->mutex);
55 }
56 
HksMutexClose(HksMutex * mutex)57 void HksMutexClose(HksMutex *mutex)
58 {
59     if (mutex == NULL) {
60         return;
61     }
62 
63     pthread_mutex_destroy(&mutex->mutex);
64     HksFree(mutex);
65 }
66