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
|
/*
* This file is open source software, licensed to you under the terms
* of the Apache License, Version 2.0 (the "License"). See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. You may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright (C) 2015 Cloudius Systems, Ltd.
*/
#include <seastar/core/thread.hh>
#include <seastar/core/do_with.hh>
#include <seastar/testing/test_case.hh>
#include <seastar/core/sstring.hh>
#include <seastar/core/reactor.hh>
#include <seastar/core/semaphore.hh>
#include <seastar/core/do_with.hh>
#include <seastar/core/future-util.hh>
#include <seastar/core/sleep.hh>
using namespace seastar;
using namespace std::chrono_literals;
SEASTAR_TEST_CASE(test_thread_1) {
return do_with(sstring(), [] (sstring& x) {
auto t1 = new thread([&x] {
x = "abc";
});
return t1->join().then([&x, t1] {
BOOST_REQUIRE_EQUAL(x, "abc");
delete t1;
});
});
}
SEASTAR_TEST_CASE(test_thread_2) {
struct tmp {
std::vector<thread> threads;
semaphore sem1{0};
semaphore sem2{0};
int counter = 0;
void thread_fn() {
sem1.wait(1).get();
++counter;
sem2.signal(1);
}
};
return do_with(tmp(), [] (tmp& x) {
auto n = 10;
for (int i = 0; i < n; ++i) {
x.threads.emplace_back(std::bind(&tmp::thread_fn, &x));
}
BOOST_REQUIRE_EQUAL(x.counter, 0);
x.sem1.signal(n);
return x.sem2.wait(n).then([&x, n] {
BOOST_REQUIRE_EQUAL(x.counter, n);
return parallel_for_each(x.threads.begin(), x.threads.end(), std::mem_fn(&thread::join));
});
});
}
SEASTAR_TEST_CASE(test_thread_async) {
sstring x = "x";
sstring y = "y";
auto concat = [] (sstring x, sstring y) {
sleep(10ms).get();
return x + y;
};
return async(concat, x, y).then([] (sstring xy) {
BOOST_REQUIRE_EQUAL(xy, "xy");
});
}
SEASTAR_TEST_CASE(test_thread_async_immed) {
return async([] { return 3; }).then([] (int three) {
BOOST_REQUIRE_EQUAL(three, 3);
});
}
SEASTAR_TEST_CASE(test_thread_async_nested) {
return async([] {
return async([] {
return 3;
}).get0();
}).then([] (int three) {
BOOST_REQUIRE_EQUAL(three, 3);
});
}
void compute(float& result, bool& done, uint64_t& ctr) {
while (!done) {
for (int n = 0; n < 10000; ++n) {
result += 1 / (result + 1);
++ctr;
}
thread::yield();
}
}
SEASTAR_TEST_CASE(test_thread_sched_group) {
return async([] {
float metered_ratio = 0.2;
thread_scheduling_group sched_group(1ms, metered_ratio);
float metered_result = 0;
float unmetered_result = 0;
bool done = false;
uint64_t u_ctr = 0, m_ctr = 0;
thread unmetered([&] { compute(unmetered_result, done, u_ctr); });
thread_attributes metered_thread_attributes;
metered_thread_attributes.scheduling_group = &sched_group;
thread metered(metered_thread_attributes, [&] { compute(metered_result, done, m_ctr); });
sleep(500ms).get();
done = true;
when_all(metered.join(), unmetered.join()).discard_result().get();
auto ratio = float(m_ctr) / (u_ctr + m_ctr);
#ifndef DEBUG
BOOST_REQUIRE(ratio > metered_ratio - 0.05);
BOOST_REQUIRE(ratio < metered_ratio + 0.05);
#else
// debug mode is too slow to test this accurately
(void)ratio;
#endif
});
}
#if defined(SEASTAR_ASAN_ENABLED) && defined(SEASTAR_HAVE_ASAN_FIBER_SUPPORT)
volatile int force_write;
volatile void* shut_up_gcc;
[[gnu::noinline]]
void throw_exception() {
volatile char buf[1024];
shut_up_gcc = &buf;
for (int i = 0; i < 1024; i++) {
buf[i] = force_write;
}
throw 1;
}
[[gnu::noinline]]
void use_stack() {
volatile char buf[2 * 1024];
shut_up_gcc = &buf;
for (int i = 0; i < 2 * 1024; i++) {
buf[i] = force_write;
}
}
SEASTAR_TEST_CASE(test_asan_false_positive) {
return async([] {
try {
throw_exception();
} catch (...) {
use_stack();
}
});
}
#endif
|