Debian Codegraph @

crt/aws-c-common/tests/rw_lock_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/rw_lock.h>
17
18 #include <aws/common/thread.h>
19 #include <aws/testing/aws_test_harness.h>
20
21 static int s_test_rw_lock_acquire_release(struct aws_allocator *allocator, void *ctx) {
22 (void)allocator;
23 (void)ctx;
24
25 struct aws_rw_lock rw_lock;
26 aws_rw_lock_init(&rw_lock);
27
28 ASSERT_SUCCESS(aws_rw_lock_wlock(&rw_lock), "rw_lock acquire should have returned success.");
29 ASSERT_SUCCESS(aws_rw_lock_wunlock(&rw_lock), "rw_lock release should have returned success.");
30
31 ASSERT_SUCCESS(aws_rw_lock_rlock(&rw_lock), "rw_lock acquire should have returned success.");
32 ASSERT_SUCCESS(aws_rw_lock_runlock(&rw_lock), "rw_lock release should have returned success.");
33
34 aws_rw_lock_clean_up(&rw_lock);
35
36 return 0;
37 }
38 AWS_TEST_CASE(rw_lock_aquire_release_test, s_test_rw_lock_acquire_release)
39
40 struct thread_rw_lock_data {
41 struct aws_rw_lock rw_lock;
42 volatile int counter;
43 int max_counts;
44 volatile int thread_fn_increments;
45 };
46
47 static void s_rw_lock_thread_fn(void *rw_lock_data) {
48 struct thread_rw_lock_data *p_rw_lock = (struct thread_rw_lock_data *)rw_lock_data;
49 int finished = 0;
50 while (!finished) {
51
52 aws_rw_lock_rlock(&p_rw_lock->rw_lock);
53
54 if (p_rw_lock->counter != p_rw_lock->max_counts) {
55 int counter = p_rw_lock->counter + 1;
56
57 aws_rw_lock_runlock(&p_rw_lock->rw_lock);
58 aws_rw_lock_wlock(&p_rw_lock->rw_lock);
59
60 p_rw_lock->counter = counter;
61 p_rw_lock->thread_fn_increments += 1;
62
63 aws_rw_lock_wunlock(&p_rw_lock->rw_lock);
64 aws_rw_lock_rlock(&p_rw_lock->rw_lock);
65
66 finished = p_rw_lock->counter == p_rw_lock->max_counts;
67 } else {
68 finished = 1;
69 }
70
71 aws_rw_lock_runlock(&p_rw_lock->rw_lock);
72 }
73 }
74
75 static int s_test_rw_lock_is_actually_rw_lock(struct aws_allocator *allocator, void *ctx) {
76 (void)ctx;
77
78 struct thread_rw_lock_data rw_lock_data = {
79 .counter = 0,
80 .max_counts = 1000000,
81 .thread_fn_increments = 0,
82 };
83
84 aws_rw_lock_init(&rw_lock_data.rw_lock);
85
86 struct aws_thread thread;
87 aws_thread_init(&thread, allocator);
88 ASSERT_SUCCESS(
89 aws_thread_launch(&thread, s_rw_lock_thread_fn, &rw_lock_data, 0),
90 "thread creation failed with error %d",
91 aws_last_error());
92 int finished = 0;
93 while (!finished) {
94
95 aws_rw_lock_rlock(&rw_lock_data.rw_lock);
96
97 finished = rw_lock_data.counter == rw_lock_data.max_counts;
98
99 aws_rw_lock_runlock(&rw_lock_data.rw_lock);
100 }
101
102 ASSERT_SUCCESS(aws_thread_join(&thread), "Thread join failed with error code %d.", aws_last_error());
103 ASSERT_INT_EQUALS(
104 rw_lock_data.thread_fn_increments, rw_lock_data.max_counts, "Thread 2 should have written all data");
105 ASSERT_INT_EQUALS(
106 rw_lock_data.max_counts, rw_lock_data.counter, "Both threads should have written exactly the max counts.");
107
108 aws_thread_clean_up(&thread);
109 aws_rw_lock_clean_up(&rw_lock_data.rw_lock);
110
111 return 0;
112 }
113 AWS_TEST_CASE(rw_lock_is_actually_rw_lock_test, s_test_rw_lock_is_actually_rw_lock)
114
115 static int s_iterations = 0;
116 static void s_thread_reader_fn(void *ud) {
117
118 struct aws_rw_lock *lock = ud;
119
120 int finished = 0;
121 while (!finished) {
122
123 aws_rw_lock_rlock(lock);
124
125 finished = s_iterations == 10000;
126
127 aws_rw_lock_runlock(lock);
128 }
129 }
130
131 static int s_test_rw_lock_many_readers(struct aws_allocator *allocator, void *ctx) {
132 (void)ctx;
133
134 struct aws_rw_lock lock;
135 aws_rw_lock_init(&lock);
136
137 struct aws_thread threads[2];
138 AWS_ZERO_ARRAY(threads);
139
140 for (size_t i = 0; i < AWS_ARRAY_SIZE(threads); ++i) {
141
142 aws_thread_init(&threads[i], allocator);
143 ASSERT_SUCCESS(
144 aws_thread_launch(&threads[i], s_thread_reader_fn, &lock, 0),
145 "thread creation failed with error %d",
146 aws_last_error());
147 }
148
149 int finished = 0;
150 while (!finished) {
151
152 aws_rw_lock_wlock(&lock);
153
154 finished = ++s_iterations == 10000;
155
156 aws_rw_lock_wunlock(&lock);
157 }
158
159 for (size_t i = 0; i < AWS_ARRAY_SIZE(threads); ++i) {
160
161 ASSERT_SUCCESS(aws_thread_join(&threads[i]), "Thread join failed with error code %d.", aws_last_error());
162 aws_thread_clean_up(&threads[i]);
163 }
164
165 aws_rw_lock_clean_up(&lock);
166
167 return 0;
168 }
169 AWS_TEST_CASE(rw_lock_many_readers_test, s_test_rw_lock_many_readers)