1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#ifndef AWS_COMMON_CONDITION_VARIABLE_H
#define AWS_COMMON_CONDITION_VARIABLE_H
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/common/common.h>
#ifndef _WIN32
# include <pthread.h>
#endif
struct aws_mutex;
struct aws_condition_variable;
typedef bool(aws_condition_predicate_fn)(void *);
struct aws_condition_variable {
#ifdef _WIN32
void *condition_handle;
#else
pthread_cond_t condition_handle;
#endif
bool initialized;
};
/**
* Static initializer for condition variable.
* You can do something like struct aws_condition_variable var =
* AWS_CONDITION_VARIABLE_INIT;
*
* If on Windows and you get an error about AWS_CONDITION_VARIABLE_INIT being undefined, please include Windows.h to get
* CONDITION_VARIABLE_INIT.
*/
#ifdef _WIN32
# define AWS_CONDITION_VARIABLE_INIT \
{ .condition_handle = NULL, .initialized = true }
#else
# define AWS_CONDITION_VARIABLE_INIT \
{ .condition_handle = PTHREAD_COND_INITIALIZER, .initialized = true }
#endif
AWS_EXTERN_C_BEGIN
/**
* Initializes a condition variable.
*/
AWS_COMMON_API
int aws_condition_variable_init(struct aws_condition_variable *condition_variable);
/**
* Cleans up a condition variable.
*/
AWS_COMMON_API
void aws_condition_variable_clean_up(struct aws_condition_variable *condition_variable);
/**
* Notifies/Wakes one waiting thread
*/
AWS_COMMON_API
int aws_condition_variable_notify_one(struct aws_condition_variable *condition_variable);
/**
* Notifies/Wakes all waiting threads.
*/
AWS_COMMON_API
int aws_condition_variable_notify_all(struct aws_condition_variable *condition_variable);
/**
* Waits the calling thread on a notification from another thread.
*/
AWS_COMMON_API
int aws_condition_variable_wait(struct aws_condition_variable *condition_variable, struct aws_mutex *mutex);
/**
* Waits the calling thread on a notification from another thread. If predicate returns false, the wait is reentered,
* otherwise control returns to the caller.
*/
AWS_COMMON_API
int aws_condition_variable_wait_pred(
struct aws_condition_variable *condition_variable,
struct aws_mutex *mutex,
aws_condition_predicate_fn *pred,
void *pred_ctx);
/**
* Waits the calling thread on a notification from another thread. Times out after time_to_wait. time_to_wait is in
* nanoseconds.
*/
AWS_COMMON_API
int aws_condition_variable_wait_for(
struct aws_condition_variable *condition_variable,
struct aws_mutex *mutex,
int64_t time_to_wait);
/**
* Waits the calling thread on a notification from another thread. Times out after time_to_wait. time_to_wait is in
* nanoseconds. If predicate returns false, the wait is reentered, otherwise control returns to the caller.
*/
AWS_COMMON_API
int aws_condition_variable_wait_for_pred(
struct aws_condition_variable *condition_variable,
struct aws_mutex *mutex,
int64_t time_to_wait,
aws_condition_predicate_fn *pred,
void *pred_ctx);
AWS_EXTERN_C_END
#endif /* AWS_COMMON_CONDITION_VARIABLE_H */