• 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_condition.h"
17 
18 #include "hks_mem.h"
19 #include "hks_template.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 struct HksCondition {
26     bool notified;
27     bool waited;
28     pthread_mutex_t mutex;
29     pthread_cond_t cond;
30 };
31 
HksConditionWait(HksCondition * condition)32 int32_t HksConditionWait(HksCondition *condition)
33 {
34     HKS_IF_NULL_RETURN(condition, -1)
35 
36     int32_t ret = pthread_mutex_lock(&condition->mutex);
37     if (ret != 0) {
38         return ret;
39     }
40     if (condition->notified) {
41         condition->notified = false;
42         pthread_mutex_unlock(&condition->mutex);
43         return 0;
44     } else {
45         condition->waited = true;
46         ret = pthread_cond_wait(&condition->cond, &condition->mutex);
47         condition->waited = false;
48         condition->notified = false;
49         pthread_mutex_unlock(&condition->mutex);
50         return ret;
51     }
52 }
53 
HksConditionNotify(HksCondition * condition)54 int32_t HksConditionNotify(HksCondition *condition)
55 {
56     HKS_IF_NULL_RETURN(condition, -1)
57 
58     int32_t ret = pthread_mutex_lock(&condition->mutex);
59     if (ret != 0) {
60         return ret;
61     }
62 
63     if (!condition->waited) {
64         condition->notified = true;
65     } else {
66         condition->notified = false;
67     }
68     ret = pthread_cond_signal(&condition->cond);
69     pthread_mutex_unlock(&condition->mutex);
70     return ret;
71 }
72 
HksConditionCreate(void)73 HksCondition *HksConditionCreate(void)
74 {
75     HksCondition *condition = (HksCondition *)HksMalloc(sizeof(HksCondition));
76     HKS_IF_NULL_RETURN(condition, NULL)
77     condition->notified = false;
78     condition->waited = false;
79     int32_t ret = pthread_mutex_init(&condition->mutex, NULL);
80     if (ret != 0) {
81         HksFree(condition);
82         return NULL;
83     }
84 
85     pthread_condattr_t attr;
86     pthread_condattr_init(&attr);
87     ret = pthread_cond_init(&condition->cond, &attr);
88     pthread_condattr_destroy(&attr);
89     if (ret != 0) {
90         pthread_mutex_destroy(&condition->mutex);
91         HksFree(condition);
92         return NULL;
93     }
94     return condition;
95 }
96 
HksConditionDestroy(HksCondition * condition)97 void HksConditionDestroy(HksCondition* condition)
98 {
99     if (condition == NULL) {
100         return;
101     }
102     pthread_mutex_destroy(&condition->mutex);
103     pthread_cond_destroy(&condition->cond);
104     HksFree(condition);
105 }
106 
107 #ifdef __cplusplus
108 }
109 #endif