From 830f6acbeffffcba412ff0d7aee8e5043d55838b Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Thu, 1 Feb 2018 10:10:46 -0500 Subject: [PATCH] fix windows build and refactor last addition to EventQueue --- src/lib/base/EventQueue.cpp | 12 +++++++++--- src/lib/base/EventQueue.h | 1 + src/lib/base/NonBlockingStream.cpp | 6 +++++- src/lib/base/NonBlockingStream.h | 17 ++++++++++++++++- 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/lib/base/EventQueue.cpp b/src/lib/base/EventQueue.cpp index 7ccb99fe..b17e35bf 100644 --- a/src/lib/base/EventQueue.cpp +++ b/src/lib/base/EventQueue.cpp @@ -201,14 +201,20 @@ EventQueue::adoptBuffer(IEventQueueBuffer* buffer) } } +bool +EventQueue::parent_requests_shutdown() const +{ + char ch; + return m_parentStream.try_read_char(ch) && ch == ShutdownCh; +} + bool EventQueue::getEvent(Event& event, double timeout) { Stopwatch timer(true); retry: - // check to see if parent wants us to shutdown now - char ch; - if (m_parentStream.try_read_char(ch) && ch == ShutdownCh) { + // before handling any events make sure we don't need to shutdown + if (parent_requests_shutdown()) { event = Event(Event::kQuit); return false; } diff --git a/src/lib/base/EventQueue.h b/src/lib/base/EventQueue.h index bbfcfcdf..97e7fba7 100644 --- a/src/lib/base/EventQueue.h +++ b/src/lib/base/EventQueue.h @@ -73,6 +73,7 @@ private: bool hasTimerExpired(Event& event); double getNextTimerTimeout() const; void addEventToBuffer(const Event& event); + bool parent_requests_shutdown() const; private: class Timer { diff --git a/src/lib/base/NonBlockingStream.cpp b/src/lib/base/NonBlockingStream.cpp index da20cad3..d44add15 100644 --- a/src/lib/base/NonBlockingStream.cpp +++ b/src/lib/base/NonBlockingStream.cpp @@ -15,6 +15,8 @@ * along with this program. If not, see . */ +#if !defined(_WIN32) + #include "base/NonBlockingStream.h" #include // tcgetattr/tcsetattr, read @@ -46,7 +48,7 @@ NonBlockingStream::~NonBlockingStream() delete _p_ta_previous; } -bool NonBlockingStream::try_read_char(char &ch) +bool NonBlockingStream::try_read_char(char &ch) const { int result = read(_fd, &ch, 1); if (result == 1) @@ -54,3 +56,5 @@ bool NonBlockingStream::try_read_char(char &ch) assert(result == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)); return false; } + +#endif // !defined(_WIN32) diff --git a/src/lib/base/NonBlockingStream.h b/src/lib/base/NonBlockingStream.h index 61faf3d0..4c27762a 100644 --- a/src/lib/base/NonBlockingStream.h +++ b/src/lib/base/NonBlockingStream.h @@ -17,6 +17,19 @@ #pragma once +// windows doesn't have a unistd.h so this class won't work as-written. +// at the moment barrier doesn't need this functionality on windows so +// it's left as a stub to be optimized out +#if defined(_WIN32) + +class NonBlockingStream +{ +public: + bool try_read_char(char &ch) const { return false; }; +}; + +#else // non-windows platforms + struct termios; class NonBlockingStream @@ -25,10 +38,12 @@ public: explicit NonBlockingStream(int fd = 0); ~NonBlockingStream(); - bool try_read_char(char &ch); + bool try_read_char(char &ch) const; private: int _fd; termios * _p_ta_previous; int _cntl_previous; }; + +#endif