1#ifndef AWS_COMMON_CONDITION_VARIABLE_H
2#define AWS_COMMON_CONDITION_VARIABLE_H
3
4/*
5 * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License").
8 * You may not use this file except in compliance with the License.
9 * A copy of the License is located at
10 *
11 * http://aws.amazon.com/apache2.0
12 *
13 * or in the "license" file accompanying this file. This file is distributed
14 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
15 * express or implied. See the License for the specific language governing
16 * permissions and limitations under the License.
17 */
18
19#include <aws/common/common.h>
20#ifndef _WIN32
21# include <pthread.h>
22#endif
23
24struct aws_mutex;
25
26struct aws_condition_variable;
27
28typedef bool(aws_condition_predicate_fn)(void *);
29
30struct aws_condition_variable {
31#ifdef _WIN32
32 void *condition_handle;
33#else
34 pthread_cond_t condition_handle;
35#endif
36};
37
38/**
39 * Static initializer for condition variable.
40 * You can do something like struct aws_condition_variable var =
41 * AWS_CONDITION_VARIABLE_INIT;
42 *
43 * If on Windows and you get an error about AWS_CONDITION_VARIABLE_INIT being undefined, please include Windows.h to get
44 * CONDITION_VARIABLE_INIT.
45 */
46#ifdef _WIN32
47# define AWS_CONDITION_VARIABLE_INIT \
48 { .condition_handle = NULL }
49#else
50# define AWS_CONDITION_VARIABLE_INIT \
51 { .condition_handle = PTHREAD_COND_INITIALIZER }
52#endif
53
54AWS_EXTERN_C_BEGIN
55
56/**
57 * Initializes a condition variable.
58 */
59AWS_COMMON_API
60int aws_condition_variable_init(struct aws_condition_variable *condition_variable);
61
62/**
63 * Cleans up a condition variable.
64 */
65AWS_COMMON_API
66void aws_condition_variable_clean_up(struct aws_condition_variable *condition_variable);
67
68/**
69 * Notifies/Wakes one waiting thread
70 */
71AWS_COMMON_API
72int aws_condition_variable_notify_one(struct aws_condition_variable *condition_variable);
73
74/**
75 * Notifies/Wakes all waiting threads.
76 */
77AWS_COMMON_API
78int aws_condition_variable_notify_all(struct aws_condition_variable *condition_variable);
79
80/**
81 * Waits the calling thread on a notification from another thread.
82 */
83AWS_COMMON_API
84int aws_condition_variable_wait(struct aws_condition_variable *condition_variable, struct aws_mutex *mutex);
85
86/**
87 * Waits the calling thread on a notification from another thread. If predicate returns false, the wait is reentered,
88 * otherwise control returns to the caller.
89 */
90AWS_COMMON_API
91int aws_condition_variable_wait_pred(
92 struct aws_condition_variable *condition_variable,
93 struct aws_mutex *mutex,
94 aws_condition_predicate_fn *pred,
95 void *pred_ctx);
96
97/**
98 * Waits the calling thread on a notification from another thread. Times out after time_to_wait. time_to_wait is in
99 * nanoseconds.
100 */
101AWS_COMMON_API
102int aws_condition_variable_wait_for(
103 struct aws_condition_variable *condition_variable,
104 struct aws_mutex *mutex,
105 int64_t time_to_wait);
106
107/**
108 * Waits the calling thread on a notification from another thread. Times out after time_to_wait. time_to_wait is in
109 * nanoseconds. If predicate returns false, the wait is reentered, otherwise control returns to the caller.
110 */
111AWS_COMMON_API
112int aws_condition_variable_wait_for_pred(
113 struct aws_condition_variable *condition_variable,
114 struct aws_mutex *mutex,
115 int64_t time_to_wait,
116 aws_condition_predicate_fn *pred,
117 void *pred_ctx);
118
119AWS_EXTERN_C_END
120#endif /* AWS_COMMON_CONDITION_VARIABLE_H */
121