| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/clock.h> |
| 7 |
#include <aws/common/private/thread_shared.h> |
| 8 |
#include <aws/common/thread.h> |
| 9 |
|
| 10 |
#include <aws/testing/aws_test_harness.h> |
| 11 |
|
| 12 |
struct thread_test_data { |
| 13 |
struct aws_allocator *allocator; |
| 14 |
aws_thread_id_t thread_id; |
| 15 |
struct aws_string *thread_name; |
| 16 |
int get_thread_name_error; |
| 17 |
}; |
| 18 |
|
| 19 |
static void s_thread_fn(void *arg) { |
| 20 |
struct thread_test_data *test_data = (struct thread_test_data *)arg; |
| 21 |
test_data->thread_id = aws_thread_current_thread_id(); |
| 22 |
test_data->get_thread_name_error = AWS_OP_SUCCESS; |
| 23 |
if (aws_thread_name(test_data->allocator, test_data->thread_id, &test_data->thread_name)) { |
| 24 |
test_data->get_thread_name_error = aws_last_error(); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
static int s_test_thread_creation_join_fn(struct aws_allocator *allocator, void *ctx) { |
| 29 |
(void)ctx; |
| 30 |
aws_common_library_init(allocator); |
| 31 |
struct thread_test_data test_data = {.allocator = allocator}; |
| 32 |
|
| 33 |
struct aws_thread thread; |
| 34 |
aws_thread_init(&thread, allocator); |
| 35 |
|
| 36 |
struct aws_thread_options thread_options = *aws_default_thread_options(); |
| 37 |
|
| 38 |
|
| 39 |
thread_options.cpu_id = 0; |
| 40 |
|
| 41 |
|
| 42 |
thread_options.name = aws_byte_cursor_from_c_str("MyThreadName"); |
| 43 |
|
| 44 |
ASSERT_SUCCESS( |
| 45 |
aws_thread_launch(&thread, s_thread_fn, (void *)&test_data, &thread_options), "thread creation failed"); |
| 46 |
ASSERT_INT_EQUALS( |
| 47 |
AWS_THREAD_JOINABLE, aws_thread_get_detach_state(&thread), "thread state should have returned JOINABLE"); |
| 48 |
ASSERT_SUCCESS(aws_thread_join(&thread), "thread join failed"); |
| 49 |
ASSERT_TRUE( |
| 50 |
aws_thread_thread_id_equal(test_data.thread_id, aws_thread_get_id(&thread)), |
| 51 |
"get_thread_id should have returned the same id as the thread calling current_thread_id"); |
| 52 |
ASSERT_INT_EQUALS( |
| 53 |
AWS_THREAD_JOIN_COMPLETED, |
| 54 |
aws_thread_get_detach_state(&thread), |
| 55 |
"thread state should have returned JOIN_COMPLETED"); |
| 56 |
|
| 57 |
if (AWS_OP_SUCCESS == test_data.get_thread_name_error) { |
| 58 |
ASSERT_CURSOR_VALUE_STRING_EQUALS( |
| 59 |
aws_byte_cursor_from_c_str("MyThreadName"), test_data.thread_name, "thread name equals"); |
| 60 |
} else { |
| 61 |
ASSERT_INT_EQUALS(test_data.get_thread_name_error, AWS_ERROR_PLATFORM_NOT_SUPPORTED); |
| 62 |
} |
| 63 |
|
| 64 |
aws_string_destroy(test_data.thread_name); |
| 65 |
aws_thread_clean_up(&thread); |
| 66 |
aws_common_library_clean_up(); |
| 67 |
|
| 68 |
return 0; |
| 69 |
} |
| 70 |
|
| 71 |
AWS_TEST_CASE(thread_creation_join_test, s_test_thread_creation_join_fn) |
| 72 |
|
| 73 |
static int s_test_thread_creation_join_invalid_cpu_id_fn(struct aws_allocator *allocator, void *ctx) { |
| 74 |
(void)ctx; |
| 75 |
aws_common_library_init(allocator); |
| 76 |
struct thread_test_data test_data = {.allocator = allocator}; |
| 77 |
|
| 78 |
struct aws_thread thread; |
| 79 |
aws_thread_init(&thread, allocator); |
| 80 |
|
| 81 |
struct aws_thread_options thread_options = *aws_default_thread_options(); |
| 82 |
|
| 83 |
thread_options.cpu_id = 512; |
| 84 |
|
| 85 |
ASSERT_SUCCESS( |
| 86 |
aws_thread_launch(&thread, s_thread_fn, (void *)&test_data, &thread_options), "thread creation failed"); |
| 87 |
ASSERT_INT_EQUALS( |
| 88 |
AWS_THREAD_JOINABLE, aws_thread_get_detach_state(&thread), "thread state should have returned JOINABLE"); |
| 89 |
ASSERT_SUCCESS(aws_thread_join(&thread), "thread join failed"); |
| 90 |
ASSERT_TRUE( |
| 91 |
aws_thread_thread_id_equal(test_data.thread_id, aws_thread_get_id(&thread)), |
| 92 |
"get_thread_id should have returned the same id as the thread calling current_thread_id"); |
| 93 |
ASSERT_INT_EQUALS( |
| 94 |
AWS_THREAD_JOIN_COMPLETED, |
| 95 |
aws_thread_get_detach_state(&thread), |
| 96 |
"thread state should have returned JOIN_COMPLETED"); |
| 97 |
|
| 98 |
aws_string_destroy(test_data.thread_name); |
| 99 |
aws_thread_clean_up(&thread); |
| 100 |
aws_common_library_clean_up(); |
| 101 |
|
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
AWS_TEST_CASE(thread_creation_join_invalid_cpu_id_test, s_test_thread_creation_join_invalid_cpu_id_fn) |
| 106 |
|
| 107 |
static uint32_t s_atexit_call_count = 0; |
| 108 |
static void s_thread_atexit_fn(void *user_data) { |
| 109 |
(void)user_data; |
| 110 |
AWS_FATAL_ASSERT(s_atexit_call_count == 0); |
| 111 |
s_atexit_call_count = 1; |
| 112 |
} |
| 113 |
|
| 114 |
static void s_thread_atexit_fn2(void *user_data) { |
| 115 |
(void)user_data; |
| 116 |
AWS_FATAL_ASSERT(s_atexit_call_count == 1); |
| 117 |
s_atexit_call_count = 2; |
| 118 |
} |
| 119 |
|
| 120 |
static void s_thread_worker_with_atexit(void *arg) { |
| 121 |
(void)arg; |
| 122 |
AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_thread_current_at_exit(s_thread_atexit_fn2, NULL)); |
| 123 |
AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_thread_current_at_exit(s_thread_atexit_fn, NULL)); |
| 124 |
} |
| 125 |
|
| 126 |
static int s_test_thread_atexit(struct aws_allocator *allocator, void *ctx) { |
| 127 |
(void)ctx; |
| 128 |
struct aws_thread thread; |
| 129 |
ASSERT_SUCCESS(aws_thread_init(&thread, allocator)); |
| 130 |
|
| 131 |
ASSERT_SUCCESS(aws_thread_launch(&thread, s_thread_worker_with_atexit, NULL, 0), "thread creation failed"); |
| 132 |
ASSERT_SUCCESS(aws_thread_join(&thread), "thread join failed"); |
| 133 |
|
| 134 |
ASSERT_INT_EQUALS(2, s_atexit_call_count); |
| 135 |
|
| 136 |
aws_thread_clean_up(&thread); |
| 137 |
|
| 138 |
return 0; |
| 139 |
} |
| 140 |
|
| 141 |
AWS_TEST_CASE(thread_atexit_test, s_test_thread_atexit) |
| 142 |
|
| 143 |
struct managed_thread_test_data { |
| 144 |
uint64_t sleep_time_in_ns; |
| 145 |
}; |
| 146 |
|
| 147 |
static void s_managed_thread_fn(void *arg) { |
| 148 |
struct managed_thread_test_data *test_data = (struct managed_thread_test_data *)arg; |
| 149 |
|
| 150 |
aws_thread_current_sleep(test_data->sleep_time_in_ns); |
| 151 |
} |
| 152 |
|
| 153 |
#define MAX_MANAGED_THREAD_TEST_QUANTITY 16 |
| 154 |
|
| 155 |
static int s_do_managed_thread_join_test(struct aws_allocator *allocator, size_t thread_count) { |
| 156 |
|
| 157 |
struct aws_thread threads[MAX_MANAGED_THREAD_TEST_QUANTITY]; |
| 158 |
struct managed_thread_test_data thread_data[MAX_MANAGED_THREAD_TEST_QUANTITY]; |
| 159 |
|
| 160 |
AWS_FATAL_ASSERT(thread_count <= MAX_MANAGED_THREAD_TEST_QUANTITY); |
| 161 |
|
| 162 |
for (size_t i = 0; i < thread_count; ++i) { |
| 163 |
thread_data[i].sleep_time_in_ns = |
| 164 |
aws_timestamp_convert(100 * (i / 2), AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL); |
| 165 |
aws_thread_init(&threads[i], allocator); |
| 166 |
} |
| 167 |
|
| 168 |
struct aws_thread_options thread_options = *aws_default_thread_options(); |
| 169 |
thread_options.join_strategy = AWS_TJS_MANAGED; |
| 170 |
|
| 171 |
for (size_t i = 0; i < thread_count; ++i) { |
| 172 |
ASSERT_SUCCESS( |
| 173 |
aws_thread_launch(&threads[i], s_managed_thread_fn, (void *)&thread_data[i], &thread_options), |
| 174 |
"thread creation failed"); |
| 175 |
|
| 176 |
ASSERT_INT_EQUALS( |
| 177 |
AWS_THREAD_MANAGED, aws_thread_get_detach_state(&threads[i]), "thread state should have returned JOINABLE"); |
| 178 |
} |
| 179 |
|
| 180 |
aws_thread_join_all_managed(); |
| 181 |
|
| 182 |
return AWS_OP_SUCCESS; |
| 183 |
} |
| 184 |
|
| 185 |
static int s_test_managed_thread_join(struct aws_allocator *allocator, void *ctx) { |
| 186 |
(void)ctx; |
| 187 |
aws_common_library_init(allocator); |
| 188 |
|
| 189 |
for (size_t i = 1; i <= MAX_MANAGED_THREAD_TEST_QUANTITY; ++i) { |
| 190 |
ASSERT_SUCCESS(s_do_managed_thread_join_test(allocator, i)); |
| 191 |
} |
| 192 |
|
| 193 |
aws_common_library_clean_up(); |
| 194 |
|
| 195 |
return 0; |
| 196 |
} |
| 197 |
|
| 198 |
AWS_TEST_CASE(test_managed_thread_join, s_test_managed_thread_join) |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
static int s_test_managed_thread_join_timeout(struct aws_allocator *allocator, void *ctx) { |
| 205 |
(void)ctx; |
| 206 |
aws_common_library_init(allocator); |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
aws_thread_set_managed_join_timeout_ns(aws_timestamp_convert(1, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL)); |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
struct managed_thread_test_data thread_data; |
| 217 |
AWS_ZERO_STRUCT(thread_data); |
| 218 |
thread_data.sleep_time_in_ns = aws_timestamp_convert(3, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL); |
| 219 |
|
| 220 |
struct aws_thread thread; |
| 221 |
AWS_ZERO_STRUCT(thread); |
| 222 |
aws_thread_init(&thread, allocator); |
| 223 |
|
| 224 |
struct aws_thread_options thread_options = *aws_default_thread_options(); |
| 225 |
thread_options.join_strategy = AWS_TJS_MANAGED; |
| 226 |
|
| 227 |
ASSERT_SUCCESS( |
| 228 |
aws_thread_launch(&thread, s_managed_thread_fn, (void *)&thread_data, &thread_options), |
| 229 |
"thread creation failed"); |
| 230 |
|
| 231 |
ASSERT_TRUE(aws_thread_get_managed_thread_count() == 1); |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
aws_thread_join_all_managed(); |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
ASSERT_TRUE(aws_thread_get_managed_thread_count() == 1); |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
aws_thread_set_managed_join_timeout_ns(aws_timestamp_convert(10, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL)); |
| 247 |
|
| 248 |
aws_common_library_clean_up(); |
| 249 |
|
| 250 |
ASSERT_TRUE(aws_thread_get_managed_thread_count() == 0); |
| 251 |
|
| 252 |
return 0; |
| 253 |
} |
| 254 |
|
| 255 |
AWS_TEST_CASE(test_managed_thread_join_timeout, s_test_managed_thread_join_timeout) |