Debian Codegraph @

crt/aws-c-common/tests/mutex_test.c

raw symbols SCIP
1 /*
2 * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include <aws/common/mutex.h>
17
18 #include <aws/common/thread.h>
19 #include <aws/testing/aws_test_harness.h>
20
21 static int s_test_mutex_acquire_release(struct aws_allocator *allocator, void *ctx) {
22 (void)allocator;
23 (void)ctx;
24
25 struct aws_mutex mutex;
26 aws_mutex_init(&mutex);
27
28 ASSERT_SUCCESS(aws_mutex_lock(&mutex), "Mutex acquire should have returned success.");
29 ASSERT_SUCCESS(aws_mutex_unlock(&mutex), "Mutex release should have returned success.");
30
31 aws_mutex_clean_up(&mutex);
32
33 return 0;
34 }
35
36 struct thread_mutex_data {
37 struct aws_mutex mutex;
38 int counter;
39 int max_counts;
40
41 /* To ensure both threads are in their loops at the same time, fighting over the mutex,
42 * the main thread will wait for the spawned thread to tick the counter from 0->1
43 * and the spawned thread will wait for the main thread to tick the counter from 1->2.
44 * Without this, it's possible for either thread to do all the work before
45 * the other one enters its loop. */
46 int thread_fn_increments;
47 int main_fn_increments;
48 };
49
50 static void s_mutex_thread_fn(void *mutex_data) {
51 struct thread_mutex_data *p_mutex = (struct thread_mutex_data *)mutex_data;
52 int finished = 0;
53 while (!finished) {
54 aws_mutex_lock(&p_mutex->mutex);
55 if (p_mutex->counter != p_mutex->max_counts) {
56 if (p_mutex->counter == 1) {
57 /* wait for the main thread to tick the counter from 1->2. (see notes on thread_mutex_data)*/
58 } else {
59 int counter = p_mutex->counter + 1;
60 p_mutex->counter = counter;
61 p_mutex->thread_fn_increments += 1;
62 finished = p_mutex->counter == p_mutex->max_counts;
63 }
64 } else {
65 finished = 1;
66 }
67 aws_mutex_unlock(&p_mutex->mutex);
68 }
69 }
70
71 static int s_test_mutex_is_actually_mutex(struct aws_allocator *allocator, void *ctx) {
72 (void)ctx;
73
74 struct thread_mutex_data mutex_data = {
75 .counter = 0,
76 .max_counts = 1000000,
77 .thread_fn_increments = 0,
78 .main_fn_increments = 0,
79 };
80
81 aws_mutex_init(&mutex_data.mutex);
82
83 struct aws_thread thread;
84 aws_thread_init(&thread, allocator);
85 ASSERT_SUCCESS(
86 aws_thread_launch(&thread, s_mutex_thread_fn, &mutex_data, 0),
87 "thread creation failed with error %d",
88 aws_last_error());
89 int finished = 0;
90 while (!finished) {
91 aws_mutex_lock(&mutex_data.mutex);
92 /* wait for the spawned thread to tick the counter from 0->1. (see notes on thread_mutex_data)*/
93 if (!mutex_data.thread_fn_increments) {
94 aws_mutex_unlock(&mutex_data.mutex);
95 continue;
96 }
97
98 if (mutex_data.counter != mutex_data.max_counts) {
99 mutex_data.main_fn_increments += 1;
100 int counter = mutex_data.counter + 1;
101 mutex_data.counter = counter;
102 finished = mutex_data.counter == mutex_data.max_counts;
103 } else {
104 finished = 1;
105 }
106 aws_mutex_unlock(&mutex_data.mutex);
107 }
108
109 ASSERT_SUCCESS(aws_thread_join(&thread), "Thread join failed with error code %d.", aws_last_error());
110 ASSERT_TRUE(mutex_data.thread_fn_increments > 0, "Thread 2 should have written some");
111 ASSERT_TRUE(mutex_data.main_fn_increments > 0, "Main thread should have written some");
112 ASSERT_INT_EQUALS(
113 mutex_data.max_counts, mutex_data.counter, "Both threads should have written exactly the max counts.");
114 ASSERT_INT_EQUALS(
115 mutex_data.counter,
116 mutex_data.thread_fn_increments + mutex_data.main_fn_increments,
117 "Both threads should have written up to the max count");
118
119 aws_thread_clean_up(&thread);
120 aws_mutex_clean_up(&mutex_data.mutex);
121
122 return 0;
123 }
124
125 AWS_TEST_CASE(mutex_aquire_release_test, s_test_mutex_acquire_release)
126 AWS_TEST_CASE(mutex_is_actually_mutex_test, s_test_mutex_is_actually_mutex)
127
128 static int s_test_mutex_try_lock_is_correct(struct aws_allocator *allocator, void *ctx) {
129 (void)allocator;
130 (void)ctx;
131
132 struct aws_mutex lock;
133 ASSERT_SUCCESS(aws_mutex_init(&lock));
134
135 ASSERT_SUCCESS(aws_mutex_lock(&lock));
136 ASSERT_FAILS(aws_mutex_try_lock(&lock));
137 ASSERT_SUCCESS(aws_mutex_unlock(&lock));
138
139 ASSERT_SUCCESS(aws_mutex_try_lock(&lock));
140 ASSERT_FAILS(aws_mutex_try_lock(&lock));
141 ASSERT_SUCCESS(aws_mutex_unlock(&lock));
142
143 aws_mutex_clean_up(&lock);
144
145 return 0;
146 }
147
148 AWS_TEST_CASE(mutex_try_lock_is_correct_test, s_test_mutex_try_lock_is_correct)