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