| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/testing/aws_test_harness.h> |
| 7 |
|
| 8 |
#include <aws/common/condition_variable.h> |
| 9 |
|
| 10 |
#include <aws/common/clock.h> |
| 11 |
#include <aws/common/thread.h> |
| 12 |
|
| 13 |
struct condition_predicate_args { |
| 14 |
int call_count; |
| 15 |
}; |
| 16 |
|
| 17 |
struct conditional_test_data { |
| 18 |
struct aws_mutex mutex; |
| 19 |
struct aws_condition_variable condition_variable_1; |
| 20 |
struct aws_condition_variable condition_variable_2; |
| 21 |
struct condition_predicate_args *predicate_args; |
| 22 |
int thread_1; |
| 23 |
int thread_2; |
| 24 |
int thread_3; |
| 25 |
}; |
| 26 |
|
| 27 |
static bool s_conditional_predicate(void *arg) { |
| 28 |
struct condition_predicate_args *condition_predicate_args = (struct condition_predicate_args *)arg; |
| 29 |
condition_predicate_args->call_count++; |
| 30 |
return condition_predicate_args->call_count % 2 == 0; |
| 31 |
} |
| 32 |
|
| 33 |
static void s_conditional_thread_2_fn(void *arg) { |
| 34 |
struct conditional_test_data *test_data = (struct conditional_test_data *)arg; |
| 35 |
|
| 36 |
aws_mutex_lock(&test_data->mutex); |
| 37 |
|
| 38 |
while (!test_data->thread_1) { |
| 39 |
aws_condition_variable_wait_pred( |
| 40 |
&test_data->condition_variable_1, &test_data->mutex, s_conditional_predicate, test_data->predicate_args); |
| 41 |
} |
| 42 |
|
| 43 |
test_data->thread_2 = 1; |
| 44 |
aws_condition_variable_notify_one(&test_data->condition_variable_2); |
| 45 |
aws_mutex_unlock(&test_data->mutex); |
| 46 |
} |
| 47 |
|
| 48 |
static void s_conditional_thread_3_fn(void *arg) { |
| 49 |
struct conditional_test_data *test_data = (struct conditional_test_data *)arg; |
| 50 |
|
| 51 |
aws_mutex_lock(&test_data->mutex); |
| 52 |
|
| 53 |
while (!test_data->thread_1) { |
| 54 |
aws_condition_variable_wait_pred( |
| 55 |
&test_data->condition_variable_1, &test_data->mutex, s_conditional_predicate, test_data->predicate_args); |
| 56 |
} |
| 57 |
|
| 58 |
test_data->thread_3 = 1; |
| 59 |
aws_condition_variable_notify_one(&test_data->condition_variable_2); |
| 60 |
aws_mutex_unlock(&test_data->mutex); |
| 61 |
} |
| 62 |
|
| 63 |
static int s_test_conditional_notify_one_fn(struct aws_allocator *allocator, void *ctx) { |
| 64 |
(void)ctx; |
| 65 |
|
| 66 |
struct condition_predicate_args predicate_args = {.call_count = 0}; |
| 67 |
|
| 68 |
struct conditional_test_data test_data = { |
| 69 |
.condition_variable_1 = AWS_CONDITION_VARIABLE_INIT, |
| 70 |
.condition_variable_2 = AWS_CONDITION_VARIABLE_INIT, |
| 71 |
.mutex = AWS_MUTEX_INIT, |
| 72 |
.predicate_args = &predicate_args, |
| 73 |
.thread_1 = 0, |
| 74 |
.thread_2 = 0, |
| 75 |
.thread_3 = 0, |
| 76 |
}; |
| 77 |
|
| 78 |
ASSERT_SUCCESS(aws_mutex_lock(&test_data.mutex)); |
| 79 |
|
| 80 |
struct aws_thread thread; |
| 81 |
ASSERT_SUCCESS(aws_thread_init(&thread, allocator)); |
| 82 |
ASSERT_SUCCESS(aws_thread_launch(&thread, s_conditional_thread_2_fn, &test_data, NULL)); |
| 83 |
|
| 84 |
test_data.thread_1 = 1; |
| 85 |
ASSERT_SUCCESS(aws_condition_variable_notify_one(&test_data.condition_variable_1)); |
| 86 |
|
| 87 |
while (!test_data.thread_2) { |
| 88 |
ASSERT_SUCCESS(aws_condition_variable_wait_pred( |
| 89 |
&test_data.condition_variable_2, &test_data.mutex, s_conditional_predicate, &predicate_args)); |
| 90 |
} |
| 91 |
|
| 92 |
ASSERT_SUCCESS(aws_mutex_unlock(&test_data.mutex)); |
| 93 |
|
| 94 |
aws_thread_join(&thread); |
| 95 |
aws_thread_clean_up(&thread); |
| 96 |
ASSERT_TRUE(predicate_args.call_count >= 2); |
| 97 |
|
| 98 |
return AWS_OP_SUCCESS; |
| 99 |
} |
| 100 |
|
| 101 |
AWS_TEST_CASE(conditional_notify_one, s_test_conditional_notify_one_fn) |
| 102 |
|
| 103 |
static int s_test_conditional_notify_all_fn(struct aws_allocator *allocator, void *ctx) { |
| 104 |
(void)ctx; |
| 105 |
|
| 106 |
struct condition_predicate_args predicate_args = {.call_count = 0}; |
| 107 |
|
| 108 |
struct conditional_test_data test_data = { |
| 109 |
.condition_variable_1 = AWS_CONDITION_VARIABLE_INIT, |
| 110 |
.condition_variable_2 = AWS_CONDITION_VARIABLE_INIT, |
| 111 |
.mutex = AWS_MUTEX_INIT, |
| 112 |
.predicate_args = &predicate_args, |
| 113 |
.thread_1 = 0, |
| 114 |
.thread_2 = 0, |
| 115 |
.thread_3 = 0, |
| 116 |
}; |
| 117 |
|
| 118 |
ASSERT_SUCCESS(aws_mutex_lock(&test_data.mutex)); |
| 119 |
|
| 120 |
struct aws_thread thread_2; |
| 121 |
ASSERT_SUCCESS(aws_thread_init(&thread_2, allocator)); |
| 122 |
ASSERT_SUCCESS(aws_thread_launch(&thread_2, s_conditional_thread_2_fn, &test_data, NULL)); |
| 123 |
|
| 124 |
struct aws_thread thread_3; |
| 125 |
ASSERT_SUCCESS(aws_thread_init(&thread_3, allocator)); |
| 126 |
ASSERT_SUCCESS(aws_thread_launch(&thread_3, s_conditional_thread_3_fn, &test_data, NULL)); |
| 127 |
|
| 128 |
test_data.thread_1 = 1; |
| 129 |
ASSERT_SUCCESS(aws_condition_variable_notify_all(&test_data.condition_variable_1)); |
| 130 |
|
| 131 |
while (!test_data.thread_2 && !test_data.thread_3) { |
| 132 |
ASSERT_SUCCESS(aws_condition_variable_wait_pred( |
| 133 |
&test_data.condition_variable_2, &test_data.mutex, s_conditional_predicate, &predicate_args)); |
| 134 |
} |
| 135 |
|
| 136 |
ASSERT_SUCCESS(aws_mutex_unlock(&test_data.mutex)); |
| 137 |
|
| 138 |
aws_thread_join(&thread_2); |
| 139 |
aws_thread_join(&thread_3); |
| 140 |
aws_thread_clean_up(&thread_2); |
| 141 |
aws_thread_clean_up(&thread_3); |
| 142 |
|
| 143 |
ASSERT_TRUE(predicate_args.call_count >= 2); |
| 144 |
|
| 145 |
return AWS_OP_SUCCESS; |
| 146 |
} |
| 147 |
|
| 148 |
AWS_TEST_CASE(conditional_notify_all, s_test_conditional_notify_all_fn) |