fix windows build and refactor last addition to EventQueue

This commit is contained in:
walker0643 2018-02-01 10:10:46 -05:00
parent d9530bf7aa
commit 830f6acbef
4 changed files with 31 additions and 5 deletions

View File

@ -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;
}

View File

@ -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 {

View File

@ -15,6 +15,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#if !defined(_WIN32)
#include "base/NonBlockingStream.h"
#include <unistd.h> // 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)

View File

@ -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