mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-21 20:01:46 +00:00
Fix a lost wake() in PollPoller
wait() cleared the pending flag before reading the wake byte. A wake() landing in between sent a second byte, the read took both, and the flag stayed set with nothing left to read, so every later wake() was skipped and the waiting thread slept on. Clear the flag after the read instead, as libuv does for uv_async_send(): a wake() that lands in between finds the flag still set and sends nothing, which loses nothing because the caller looks at its shared state after wait() returns.
This commit is contained in:
1 parent
c51417f458
commit
1b914e1597
2 files changed
+34
-2
No files matched your search
@@ -7961,10 +7961,12 @@ inline bool PollPoller::wait(int timeout_ms, std::vector<Token> &ready) {
|
|||||||
auto woken = fds_[0].revents != 0;
|
auto woken = fds_[0].revents != 0;
|
||||||
if (woken) {
|
if (woken) {
|
||||||
n--;
|
n--;
|
||||||
// Clear the flag first: a wake() from here on sends another byte.
|
|
||||||
wake_pending_ = false;
|
|
||||||
char buf[64];
|
char buf[64];
|
||||||
read_socket(wake_read_, buf, sizeof(buf), CPPHTTPLIB_RECV_FLAGS);
|
read_socket(wake_read_, buf, sizeof(buf), CPPHTTPLIB_RECV_FLAGS);
|
||||||
|
// Only after the read, which could take the byte of a later wake() too.
|
||||||
|
// A wake() in between finds the flag still set and sends nothing, which
|
||||||
|
// is fine: the caller looks at what it was woken for after this returns.
|
||||||
|
wake_pending_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Walk backwards: remove_at() moves the last entry, already seen, into the
|
// Walk backwards: remove_at() moves the last entry, already seen, into the
|
||||||
|
|||||||
@@ -34107,6 +34107,36 @@ TEST_P(PollerTest, WakesCoalesceAndAreConsumed) {
|
|||||||
EXPECT_TRUE(poller_->wait(0, ready));
|
EXPECT_TRUE(poller_->wait(0, ready));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_P(PollerTest, ConcurrentWakesAreNeverLost) {
|
||||||
|
// Two threads publish changes and wake() after each, as a reactor's workers
|
||||||
|
// do. Every change must be seen without a wait() timing out. A lost wake()
|
||||||
|
// needs an unlucky interleaving, hence the rounds.
|
||||||
|
for (int round = 0; round < 5; round++) {
|
||||||
|
SCOPED_TRACE(round);
|
||||||
|
const int per_thread = 20000;
|
||||||
|
std::atomic<int> posted{0};
|
||||||
|
auto post = [&] {
|
||||||
|
for (int i = 0; i < per_thread; i++) {
|
||||||
|
posted++;
|
||||||
|
poller_->wake();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
std::thread t1(post), t2(post);
|
||||||
|
|
||||||
|
auto lost = false;
|
||||||
|
Tokens ready;
|
||||||
|
for (auto seen = 0; seen < 2 * per_thread; seen = posted) {
|
||||||
|
if (!poller_->wait(1000, ready)) {
|
||||||
|
lost = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t1.join();
|
||||||
|
t2.join();
|
||||||
|
ASSERT_FALSE(lost);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST(PollerFactoryTest, MakePollerWorks) {
|
TEST(PollerFactoryTest, MakePollerWorks) {
|
||||||
auto poller = detail::make_poller();
|
auto poller = detail::make_poller();
|
||||||
ASSERT_TRUE(poller != nullptr);
|
ASSERT_TRUE(poller != nullptr);
|
||||||
|
|||||||
Reference in new issue
Block a user