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_condition.h"
17
18 #include "hc_log.h"
19
HcCondWait(struct HcConditionT * hcCond)20 int HcCondWait(struct HcConditionT* hcCond)
21 {
22 if (hcCond == NULL) {
23 return -1;
24 }
25
26 int res = sem_wait(&hcCond->sem);
27 if (res != 0) {
28 LOGE("[OS]: sem_wait fail. [Res]: %" LOG_PUB "d", res);
29 }
30 return res;
31 }
32
HcCondNotify(struct HcConditionT * hcCond)33 void HcCondNotify(struct HcConditionT* hcCond)
34 {
35 if (hcCond == NULL) {
36 return;
37 }
38
39 int res = sem_post(&hcCond->sem);
40 if (res != 0) {
41 LOGW("[OS]: sem_post fail. [Res]: %" LOG_PUB "d", res);
42 }
43 }
44
InitHcCond(HcCondition * hcCond,HcMutex * mutex)45 int32_t InitHcCond(HcCondition* hcCond, HcMutex* mutex)
46 {
47 (void)mutex;
48 if (hcCond == NULL) {
49 return -1;
50 }
51 hcCond->wait = HcCondWait;
52 hcCond->notify = HcCondNotify;
53 hcCond->waitWithoutLock = HcCondWait;
54 hcCond->notifyWithoutLock = HcCondNotify;
55
56 // init the signal value to zero
57 int res = sem_init(&hcCond->sem, 0, 0);
58 if (res != 0) {
59 LOGE("[OS]: sem_init fail. [Res]: %" LOG_PUB "d", res);
60 }
61 return res;
62 }
63
DestroyHcCond(HcCondition * hcCond)64 void DestroyHcCond(HcCondition* hcCond)
65 {
66 if (hcCond == NULL) {
67 return;
68 }
69
70 int res = sem_destroy(&hcCond->sem);
71 if (res != 0) {
72 LOGW("[OS]: sem_destroy fail. [Res]: %" LOG_PUB "d", res);
73 }
74 }