Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix thread unit tests #268

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't dig into exactly why, but moving this back down is indeed required for the tests to succeed.

}

}
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