-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdaw_atomic_wait_test.cpp
More file actions
216 lines (195 loc) · 6.36 KB
/
Copy pathdaw_atomic_wait_test.cpp
File metadata and controls
216 lines (195 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) Darrell Wright
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/beached/header_libraries
//
#include <daw/daw_atomic_wait.h>
#include <daw/daw_attributes.h>
#include <daw/daw_ensure.h>
#include <atomic>
#include <chrono>
#include <thread>
using namespace std::chrono_literals;
DAW_ATTRIB_NOINLINE void test_atomic_wait_value_until_timeout( ) {
auto value = std::atomic<int>{ };
auto result = daw::atomic_wait_value_equal_until(
&value, 42, std::chrono::system_clock::now( ) + 30ms );
daw_ensure( result == daw::wait_status::timeout );
daw_ensure( value == 0 );
}
DAW_ATTRIB_NOINLINE void test_atomic_wait_value_until_no_timeout( ) {
auto value = std::atomic<int>{ };
auto th = std::thread( [&value] {
std::this_thread::sleep_for( 500ms );
value = 42;
std::atomic_notify_one( &value );
} );
auto const result = daw::atomic_wait_value_equal_until(
&value, 42, std::chrono::system_clock::now( ) + 1min );
th.join( );
daw_ensure( result == daw::wait_status::found );
daw_ensure( value == 42 );
}
DAW_ATTRIB_NOINLINE void test_atomic_wait_value_for_timeout( ) {
auto value = std::atomic<int>{ };
auto result = daw::atomic_wait_value_equal_for( &value, 42, 30ms );
daw_ensure( result == daw::wait_status::timeout );
daw_ensure( value == 0 );
}
DAW_ATTRIB_NOINLINE void test_atomic_wait_value_for_no_timeout( ) {
auto value = std::atomic<int>{ };
auto th = std::thread( [&value] {
std::this_thread::sleep_for( 500ms );
value = 42;
std::atomic_notify_one( &value );
} );
auto const result = daw::atomic_wait_value_equal_for( &value, 42, 1min );
th.join( );
daw_ensure( result == daw::wait_status::found );
daw_ensure( value == 42 );
}
DAW_ATTRIB_NOINLINE void test_atomic_wait_value_already_set( ) {
auto value = std::atomic<int>{ 42 };
daw::atomic_wait_value_equal( &value, 42 );
daw_ensure( value == 42 );
}
DAW_ATTRIB_NOINLINE void test_atomic_wait_value( ) {
auto value = std::atomic<int>{ 4 };
auto const fn = [&] {
std::this_thread::sleep_for( 50ms );
--value;
std::atomic_notify_one( &value );
};
auto ths = std::array<std::thread, 4>{ std::thread( fn ), std::thread( fn ),
std::thread( fn ), std::thread( fn ) };
daw::atomic_wait_value_equal( &value, 0 );
for( auto &th : ths ) {
th.join( );
}
daw_ensure( value == 0 );
}
DAW_ATTRIB_NOINLINE void test_atomic_wait_if_until_timeout( ) {
auto value = std::atomic<int>{ };
static constexpr auto pred = []( auto const &v ) {
return v == 42;
};
auto result = daw::atomic_wait_if_until(
&value, pred, std::chrono::system_clock::now( ) + 30ms );
daw_ensure( result == daw::wait_status::timeout );
daw_ensure( value == 0 );
}
DAW_ATTRIB_NOINLINE void test_atomic_wait_if_until_no_timeout( ) {
auto value = std::atomic<int>{ };
auto th = std::thread( [&value] {
std::this_thread::sleep_for( 500ms );
value = 42;
std::atomic_notify_one( &value );
} );
static constexpr auto pred = []( auto const &v ) {
return v == 42;
};
auto const result = daw::atomic_wait_if_until(
&value, pred, std::chrono::system_clock::now( ) + 1min );
th.join( );
daw_ensure( result == daw::wait_status::found );
daw_ensure( value == 42 );
}
DAW_ATTRIB_NOINLINE void test_atomic_wait_if_for_timeout( ) {
auto value = std::atomic<int>{ };
static constexpr auto pred = []( auto const &v ) {
return v == 42;
};
auto result = daw::atomic_wait_if_for( &value, pred, 30ms );
daw_ensure( result == daw::wait_status::timeout );
daw_ensure( value == 0 );
}
DAW_ATTRIB_NOINLINE void test_atomic_wait_if_for_no_timeout( ) {
auto value = std::atomic<int>{ };
auto th = std::thread( [&value] {
std::this_thread::sleep_for( 500ms );
value = 42;
std::atomic_notify_one( &value );
} );
static constexpr auto pred = []( auto const &v ) {
return v == 42;
};
auto const result = daw::atomic_wait_if_for( &value, pred, 1min );
th.join( );
daw_ensure( result == daw::wait_status::found );
daw_ensure( value == 42 );
}
DAW_ATTRIB_NOINLINE void test_atomic_wait_if( ) {
auto value = std::atomic<int>{ 4 };
auto const fn = [&] {
std::this_thread::sleep_for( 50ms );
--value;
std::atomic_notify_one( &value );
};
auto ths = std::array<std::thread, 4>{ std::thread( fn ), std::thread( fn ),
std::thread( fn ), std::thread( fn ) };
static constexpr auto pred = []( auto const &v ) {
return v == 0;
};
daw::atomic_wait_if( &value, pred );
for( auto &th : ths ) {
th.join( );
}
daw_ensure( value == 0 );
}
DAW_ATTRIB_NOINLINE void test_atomic_wait_until_timeout( ) {
auto value = std::atomic<int>{ };
auto result = daw::atomic_wait_until(
&value, 0, std::chrono::system_clock::now( ) + 30ms );
daw_ensure( result == daw::wait_status::timeout );
daw_ensure( value == 0 );
}
DAW_ATTRIB_NOINLINE void test_atomic_wait_until( ) {
auto value = std::atomic<int>{ };
auto th = std::thread( [&] {
std::this_thread::sleep_for( 100ms );
++value;
std::atomic_notify_one( &value );
} );
auto result = daw::atomic_wait_until(
&value, 0, std::chrono::system_clock::now( ) + 1min );
th.join( );
daw_ensure( result == daw::wait_status::found );
daw_ensure( value == 1 );
}
DAW_ATTRIB_NOINLINE void test_atomic_wait_for_timeout( ) {
auto value = std::atomic<int>{ };
auto result = daw::atomic_wait_for( &value, 0, 30ms );
daw_ensure( result == daw::wait_status::timeout );
daw_ensure( value == 0 );
}
DAW_ATTRIB_NOINLINE void test_atomic_wait_for( ) {
auto value = std::atomic<int>{ };
auto th = std::thread( [&] {
std::this_thread::sleep_for( 100ms );
++value;
std::atomic_notify_one( &value );
} );
auto result = daw::atomic_wait_for( &value, 0, 1min );
th.join( );
daw_ensure( result == daw::wait_status::found );
daw_ensure( value == 1 );
}
int main( ) {
test_atomic_wait_value_until_timeout( );
test_atomic_wait_value_until_no_timeout( );
test_atomic_wait_value_for_timeout( );
test_atomic_wait_value_for_no_timeout( );
test_atomic_wait_value_already_set( );
test_atomic_wait_value( );
test_atomic_wait_if_until_timeout( );
test_atomic_wait_if_until_no_timeout( );
test_atomic_wait_if_for_timeout( );
test_atomic_wait_if_for_no_timeout( );
test_atomic_wait_if( );
test_atomic_wait_until_timeout( );
test_atomic_wait_until( );
test_atomic_wait_for_timeout( );
test_atomic_wait_for( );
}