Skip to content

Commit

Permalink
Fix thread unit tests
Browse files Browse the repository at this point in the history
Convert m_poking back to a bool due to test_and_set having different
return values than compare_exchange

Also minor typo fix
  • Loading branch information
kannibalox authored and rakshasa committed Dec 7, 2024
1 parent 5f4e9ba commit d1d0445
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
9 changes: 4 additions & 5 deletions src/torrent/utils/thread_interrupt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ thread_interrupt::~thread_interrupt() {

bool
thread_interrupt::poke() {
if (m_poking.test_and_set())
bool expected = false;
if (!m_other->m_poking.compare_exchange_strong(expected, true))
return true;

int result = ::send(m_fileDesc, "a", 1, 0);
Expand Down Expand Up @@ -61,10 +62,6 @@ thread_interrupt::create_pair() {

void
thread_interrupt::event_read() {
// This has a race condition where if poked again while processing the polled events it might be
// missed.
m_poking.clear();

char buffer[256];
int result = ::recv(m_fileDesc, buffer, 256, 0);

Expand All @@ -73,6 +70,8 @@ thread_interrupt::event_read() {
throw internal_error("Invalid result reading from thread_interrupt socket.");

instrumentation_update(INSTRUMENTATION_POLLING_INTERRUPT_READ_EVENT, 1);

m_poking = false;
}

}
2 changes: 1 addition & 1 deletion src/torrent/utils/thread_interrupt.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class LIBTORRENT_EXPORT thread_interrupt : public Event {
SocketFd& get_fd() { return *reinterpret_cast<SocketFd*>(&m_fileDesc); }

thread_interrupt* m_other;
std::atomic_flag m_poking;
std::atomic_bool m_poking;
};

}
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/test_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class test_thread : public torrent::thread_base {

void init_thread();

void set_pre_stop() { m_test_state |= test_flag_pre_stop; }
void set_pre_stop() { m_test_flags |= test_flag_pre_stop; }
void set_acquire_global() { m_test_flags |= test_flag_acquire_global; }
void set_test_flag(int flags) { m_test_flags |= flags; }

Expand Down

0 comments on commit d1d0445

Please sign in to comment.