Skip to content

Commit

Permalink
Only send one byte and ignore any following calls to poke.
Browse files Browse the repository at this point in the history
  • Loading branch information
rakshasa committed Apr 3, 2013
1 parent de08a5f commit 2ba037b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
22 changes: 17 additions & 5 deletions src/torrent/utils/thread_interrupt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,14 @@

namespace torrent {

thread_interrupt::thread_interrupt(int fd) :
m_poking(false) {
m_fileDesc = fd;
get_fd().set_nonblock();
}

thread_interrupt::~thread_interrupt() {
if (m_fileDesc != -1)
if (m_fileDesc == -1)
return;

::close(m_fileDesc);
Expand All @@ -55,6 +61,11 @@ thread_interrupt::~thread_interrupt() {

bool
thread_interrupt::poke() {
if (is_poking())
return true;

__sync_bool_compare_and_swap(&m_other->m_poking, false, true);

int result = ::send(m_fileDesc, "a", 1, 0);

if (result == 0 ||
Expand All @@ -66,17 +77,16 @@ thread_interrupt::poke() {

thread_interrupt::pair_type
thread_interrupt::create_pair() {
int fd1;
int fd2;
int fd1, fd2;

if (!SocketFd::open_socket_pair(fd1, fd2))
throw internal_error("Could not create socket pair for thread_interrupt: " + std::string(rak::error_number::current().c_str()) + ".");

thread_interrupt* t1 = new thread_interrupt(fd1);
thread_interrupt* t2 = new thread_interrupt(fd2);

t1->get_fd().set_nonblock();
t2->get_fd().set_nonblock();
t1->m_other = t2;
t2->m_other = t1;

return pair_type(t1, t2);
}
Expand All @@ -89,6 +99,8 @@ thread_interrupt::event_read() {
if (result == 0 ||
(result == -1 && !rak::error_number::current().is_blocked_momentary()))
throw internal_error("Invalid result reading from thread_interrupt socket.");

__sync_bool_compare_and_swap(&m_poking, true, false);
}

}
13 changes: 12 additions & 1 deletion src/torrent/utils/thread_interrupt.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,29 @@ class LIBTORRENT_EXPORT lt_cacheline_aligned thread_interrupt : public Event {

static pair_type create_pair();

bool is_poking() const;

bool poke();

void event_read();
void event_write() {}
void event_error() {}

private:
thread_interrupt(int fd);

SocketFd& get_fd() { return *reinterpret_cast<SocketFd*>(&m_fileDesc); }

thread_interrupt(int fd) { m_fileDesc = fd; }
thread_interrupt* m_other;
bool m_poking lt_cacheline_aligned;
};

inline bool
thread_interrupt::is_poking() const {
__sync_synchronize();
return m_poking;
}

}

#endif

0 comments on commit 2ba037b

Please sign in to comment.