Debian Codegraph @

crt/aws-c-common/include/aws/common/condition_variable.h

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