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 = 0;
30 if (mutex->isReentrant) {
31 pthread_t currThread = pthread_self();
32 if (mutex->owner == currThread) {
33 mutex->count++;
34 return 0;
35 }
36 res = pthread_mutex_lock(&mutex->mutex);
37 mutex->owner = currThread;
38 mutex->count = 1;
39 } else {
40 res = pthread_mutex_lock(&mutex->mutex);
41 }
42 if (res != 0) {
43 LOGW("[OS]: pthread_mutex_lock fail. [Res]: %" LOG_PUB "d", res);
44 }
45 return res;
46 }
47
HcMutexUnlock(HcMutex * mutex)48 static void HcMutexUnlock(HcMutex *mutex)
49 {
50 if (mutex == NULL) {
51 return;
52 }
53 int res = 0;
54 if (mutex->isReentrant) {
55 pthread_t currThread = pthread_self();
56 if (mutex->owner != currThread) {
57 LOGE("[OS]: HcMutexUnlock fail. Not owner.");
58 return;
59 }
60 mutex->count--;
61 if (mutex->count == 0) {
62 mutex->owner = 0;
63 res = pthread_mutex_unlock(&mutex->mutex);
64 }
65 } else {
66 res = pthread_mutex_unlock(&mutex->mutex);
67 }
68 if (res != 0) {
69 LOGW("[OS]: pthread_mutex_unlock fail. [Res]: %" LOG_PUB "d", res);
70 }
71 }
72
InitHcMutex(struct HcMutexT * mutex,bool isReentrant)73 int32_t InitHcMutex(struct HcMutexT *mutex, bool isReentrant)
74 {
75 if (mutex == NULL) {
76 return -1;
77 }
78 int res = pthread_mutex_init(&mutex->mutex, NULL);
79 if (res != 0) {
80 LOGE("[OS]: pthread_mutex_init fail. [Res]: %" LOG_PUB "d", res);
81 return res;
82 }
83 mutex->lock = HcMutexLock;
84 mutex->unlock = HcMutexUnlock;
85 mutex->owner = 0;
86 mutex->count = 0;
87 mutex->isReentrant = isReentrant;
88 return 0;
89 }
90
DestroyHcMutex(struct HcMutexT * mutex)91 void DestroyHcMutex(struct HcMutexT *mutex)
92 {
93 if (mutex == NULL) {
94 return;
95 }
96 mutex->owner = 0;
97 mutex->count = 0;
98 mutex->isReentrant = false;
99 int res = pthread_mutex_destroy(&mutex->mutex);
100 if (res != 0) {
101 LOGW("[OS]: pthread_mutex_destroy fail. [Res]: %" LOG_PUB "d", res);
102 }
103 }
104
LockHcMutex(HcMutex * mutex)105 int LockHcMutex(HcMutex* mutex)
106 {
107 if (mutex == NULL || mutex->lock == NULL) {
108 LOGE("[OS]: mutex is null pointer!");
109 return -1;
110 }
111 return mutex->lock(mutex);
112 }
113
UnlockHcMutex(HcMutex * mutex)114 void UnlockHcMutex(HcMutex* mutex)
115 {
116 if (mutex == NULL || mutex->unlock == NULL) {
117 LOGE("[OS]: mutex is null pointer!");
118 return;
119 }
120 mutex->unlock(mutex);
121 }
122
123 #ifdef __cplusplus
124 }
125 #endif