added wxFDIODispatcher::HasPending() and implemented correctly wxConsoleEventLoop::Pending() using it to fix the pending events processing in console event loop based programs; also changed wxFDIODispatcher::Dispatch() return type/value to be able to indicate the errors
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@57804 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -67,10 +67,13 @@ public:
|
|||||||
// unregister descriptor previously registered with RegisterFD()
|
// unregister descriptor previously registered with RegisterFD()
|
||||||
virtual bool UnregisterFD(int fd) = 0;
|
virtual bool UnregisterFD(int fd) = 0;
|
||||||
|
|
||||||
|
// check if any events are currently available without dispatching them
|
||||||
|
virtual bool HasPending() const = 0;
|
||||||
|
|
||||||
// wait for an event for at most timeout milliseconds and process it;
|
// wait for an event for at most timeout milliseconds and process it;
|
||||||
// return true if we processed any events or false if timeout expired
|
// return the number of events processed (possibly 0 if timeout expired) or
|
||||||
// without anything happening
|
// -1 if an error occurred
|
||||||
virtual bool Dispatch(int timeout = TIMEOUT_INFINITE) = 0;
|
virtual int Dispatch(int timeout = TIMEOUT_INFINITE) = 0;
|
||||||
|
|
||||||
virtual ~wxFDIODispatcher() { }
|
virtual ~wxFDIODispatcher() { }
|
||||||
};
|
};
|
||||||
|
@@ -56,8 +56,9 @@ public:
|
|||||||
// select() itself
|
// select() itself
|
||||||
int Select(int nfds, struct timeval *tv);
|
int Select(int nfds, struct timeval *tv);
|
||||||
|
|
||||||
// call the handler methods corresponding to the sets having this fd
|
// call the handler methods corresponding to the sets having this fd if it
|
||||||
void Handle(int fd, wxFDIOHandler& handler) const;
|
// is present in any set and return true if it is
|
||||||
|
bool Handle(int fd, wxFDIOHandler& handler) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
typedef void (wxFDIOHandler::*Callback)();
|
typedef void (wxFDIOHandler::*Callback)();
|
||||||
@@ -91,20 +92,25 @@ public:
|
|||||||
virtual bool RegisterFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL);
|
virtual bool RegisterFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL);
|
||||||
virtual bool ModifyFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL);
|
virtual bool ModifyFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL);
|
||||||
virtual bool UnregisterFD(int fd);
|
virtual bool UnregisterFD(int fd);
|
||||||
virtual bool Dispatch(int timeout = TIMEOUT_INFINITE);
|
virtual bool HasPending() const;
|
||||||
|
virtual int Dispatch(int timeout = TIMEOUT_INFINITE);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// common part of RegisterFD() and ModifyFD()
|
// common part of RegisterFD() and ModifyFD()
|
||||||
bool DoUpdateFDAndHandler(int fd, wxFDIOHandler *handler, int flags);
|
bool DoUpdateFDAndHandler(int fd, wxFDIOHandler *handler, int flags);
|
||||||
|
|
||||||
// call the handlers for the fds present in the given sets, return true if
|
// call the handlers for the fds present in the given sets, return the
|
||||||
// we called any handlers
|
// number of handlers we called
|
||||||
bool ProcessSets(const wxSelectSets& sets);
|
int ProcessSets(const wxSelectSets& sets);
|
||||||
|
|
||||||
// helper of ProcessSets(): call the handler if its fd is in the set
|
// helper of ProcessSets(): call the handler if its fd is in the set
|
||||||
void DoProcessFD(int fd, const fd_set& fds, wxFDIOHandler *handler,
|
void DoProcessFD(int fd, const fd_set& fds, wxFDIOHandler *handler,
|
||||||
const char *name);
|
const char *name);
|
||||||
|
|
||||||
|
// common part of HasPending() and Dispatch(): calls select() with the
|
||||||
|
// specified timeout
|
||||||
|
int DoSelect(wxSelectSets& sets, int timeout) const;
|
||||||
|
|
||||||
|
|
||||||
// the select sets containing all the registered fds
|
// the select sets containing all the registered fds
|
||||||
wxSelectSets m_sets;
|
wxSelectSets m_sets;
|
||||||
|
@@ -17,6 +17,8 @@
|
|||||||
|
|
||||||
#include "wx/private/fdiodispatcher.h"
|
#include "wx/private/fdiodispatcher.h"
|
||||||
|
|
||||||
|
struct epoll_event;
|
||||||
|
|
||||||
class WXDLLIMPEXP_CORE wxEpollDispatcher : public wxFDIODispatcher
|
class WXDLLIMPEXP_CORE wxEpollDispatcher : public wxFDIODispatcher
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -32,12 +34,18 @@ public:
|
|||||||
virtual bool RegisterFD(int fd, wxFDIOHandler* handler, int flags = wxFDIO_ALL);
|
virtual bool RegisterFD(int fd, wxFDIOHandler* handler, int flags = wxFDIO_ALL);
|
||||||
virtual bool ModifyFD(int fd, wxFDIOHandler* handler, int flags = wxFDIO_ALL);
|
virtual bool ModifyFD(int fd, wxFDIOHandler* handler, int flags = wxFDIO_ALL);
|
||||||
virtual bool UnregisterFD(int fd);
|
virtual bool UnregisterFD(int fd);
|
||||||
virtual bool Dispatch(int timeout = TIMEOUT_INFINITE);
|
virtual bool HasPending() const;
|
||||||
|
virtual int Dispatch(int timeout = TIMEOUT_INFINITE);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// ctor is private, use Create()
|
// ctor is private, use Create()
|
||||||
wxEpollDispatcher(int epollDescriptor);
|
wxEpollDispatcher(int epollDescriptor);
|
||||||
|
|
||||||
|
// common part of HasPending() and Dispatch(): calls epoll_wait() with the
|
||||||
|
// given timeout
|
||||||
|
int DoPoll(epoll_event *events, int numEvents, int timeout) const;
|
||||||
|
|
||||||
|
|
||||||
int m_epollDescriptor;
|
int m_epollDescriptor;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -111,7 +111,7 @@ int wxSelectSets::Select(int nfds, struct timeval *tv)
|
|||||||
return select(nfds, &m_fds[Read], &m_fds[Write], &m_fds[Except], tv);
|
return select(nfds, &m_fds[Read], &m_fds[Write], &m_fds[Except], tv);
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxSelectSets::Handle(int fd, wxFDIOHandler& handler) const
|
bool wxSelectSets::Handle(int fd, wxFDIOHandler& handler) const
|
||||||
{
|
{
|
||||||
for ( int n = 0; n < Max; n++ )
|
for ( int n = 0; n < Max; n++ )
|
||||||
{
|
{
|
||||||
@@ -122,9 +122,11 @@ void wxSelectSets::Handle(int fd, wxFDIOHandler& handler) const
|
|||||||
(handler.*ms_handlers[n])();
|
(handler.*ms_handlers[n])();
|
||||||
// callback can modify sets and destroy handler
|
// callback can modify sets and destroy handler
|
||||||
// this forces that one event can be processed at one time
|
// this forces that one event can be processed at one time
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
@@ -190,9 +192,9 @@ bool wxSelectDispatcher::UnregisterFD(int fd)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxSelectDispatcher::ProcessSets(const wxSelectSets& sets)
|
int wxSelectDispatcher::ProcessSets(const wxSelectSets& sets)
|
||||||
{
|
{
|
||||||
bool gotEvent = false;
|
int numEvents = 0;
|
||||||
for ( int fd = 0; fd <= m_maxFD; fd++ )
|
for ( int fd = 0; fd <= m_maxFD; fd++ )
|
||||||
{
|
{
|
||||||
if ( !sets.HasFD(fd) )
|
if ( !sets.HasFD(fd) )
|
||||||
@@ -205,15 +207,14 @@ bool wxSelectDispatcher::ProcessSets(const wxSelectSets& sets)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
gotEvent = true;
|
if ( sets.Handle(fd, *handler) )
|
||||||
|
numEvents++;
|
||||||
sets.Handle(fd, *handler);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return gotEvent;
|
return numEvents;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxSelectDispatcher::Dispatch(int timeout)
|
int wxSelectDispatcher::DoSelect(wxSelectSets& sets, int timeout) const
|
||||||
{
|
{
|
||||||
struct timeval tv,
|
struct timeval tv,
|
||||||
*ptv;
|
*ptv;
|
||||||
@@ -228,29 +229,38 @@ bool wxSelectDispatcher::Dispatch(int timeout)
|
|||||||
ptv = NULL;
|
ptv = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
wxSelectSets sets = m_sets;
|
int ret = sets.Select(m_maxFD + 1, ptv);
|
||||||
|
|
||||||
const int ret = sets.Select(m_maxFD + 1, ptv);
|
// TODO: we need to restart select() in this case but for now just return
|
||||||
switch ( ret )
|
// as if timeout expired
|
||||||
|
if ( ret == -1 && errno == EINTR )
|
||||||
|
ret = 0;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool wxSelectDispatcher::HasPending() const
|
||||||
|
{
|
||||||
|
wxSelectSets sets(m_sets);
|
||||||
|
return DoSelect(sets, 0) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int wxSelectDispatcher::Dispatch(int timeout)
|
||||||
|
{
|
||||||
|
wxSelectSets sets(m_sets);
|
||||||
|
switch ( DoSelect(sets, timeout) )
|
||||||
{
|
{
|
||||||
case -1:
|
case -1:
|
||||||
if ( errno != EINTR )
|
|
||||||
{
|
|
||||||
wxLogSysError(_("Failed to monitor I/O channels"));
|
wxLogSysError(_("Failed to monitor I/O channels"));
|
||||||
}
|
return -1;
|
||||||
break;
|
|
||||||
|
|
||||||
case 0:
|
case 0:
|
||||||
// timeout expired without anything happening
|
// timeout expired without anything happening
|
||||||
break;
|
return 0;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if ( ProcessSets(sets) )
|
return ProcessSets(sets);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// nothing happened
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // wxUSE_SELECT_DISPATCHER
|
#endif // wxUSE_SELECT_DISPATCHER
|
||||||
|
@@ -159,10 +159,9 @@ bool wxEpollDispatcher::UnregisterFD(int fd)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxEpollDispatcher::Dispatch(int timeout)
|
int
|
||||||
|
wxEpollDispatcher::DoPoll(epoll_event *events, int numEvents, int timeout) const
|
||||||
{
|
{
|
||||||
epoll_event events[16];
|
|
||||||
|
|
||||||
// the code below relies on TIMEOUT_INFINITE being -1 so that we can pass
|
// the code below relies on TIMEOUT_INFINITE being -1 so that we can pass
|
||||||
// timeout value directly to epoll_wait() which interprets -1 as meaning to
|
// timeout value directly to epoll_wait() which interprets -1 as meaning to
|
||||||
// wait forever and would need to be changed if the value of
|
// wait forever and would need to be changed if the value of
|
||||||
@@ -170,33 +169,48 @@ bool wxEpollDispatcher::Dispatch(int timeout)
|
|||||||
wxCOMPILE_TIME_ASSERT( TIMEOUT_INFINITE == -1, UpdateThisCode );
|
wxCOMPILE_TIME_ASSERT( TIMEOUT_INFINITE == -1, UpdateThisCode );
|
||||||
|
|
||||||
wxMilliClock_t timeEnd;
|
wxMilliClock_t timeEnd;
|
||||||
if ( timeout != -1 )
|
if ( timeout > 0 )
|
||||||
timeEnd = wxGetLocalTimeMillis();
|
timeEnd = wxGetLocalTimeMillis();
|
||||||
|
|
||||||
int rc;
|
int rc;
|
||||||
for ( ;; )
|
for ( ;; )
|
||||||
{
|
{
|
||||||
rc = epoll_wait(m_epollDescriptor, events, WXSIZEOF(events), timeout);
|
rc = epoll_wait(m_epollDescriptor, events, numEvents, timeout);
|
||||||
if ( rc != -1 || errno != EINTR )
|
if ( rc != -1 || errno != EINTR )
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// we got interrupted, update the timeout and restart
|
// we got interrupted, update the timeout and restart
|
||||||
if ( timeout == -1 )
|
if ( timeout > 0 )
|
||||||
continue;
|
{
|
||||||
|
|
||||||
timeout = wxMilliClockToLong(timeEnd - wxGetLocalTimeMillis());
|
timeout = wxMilliClockToLong(timeEnd - wxGetLocalTimeMillis());
|
||||||
if ( timeout < 0 )
|
if ( timeout < 0 )
|
||||||
return false;
|
return 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool wxEpollDispatcher::HasPending() const
|
||||||
|
{
|
||||||
|
epoll_event event;
|
||||||
|
return DoPoll(&event, 1, 0) == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int wxEpollDispatcher::Dispatch(int timeout)
|
||||||
|
{
|
||||||
|
epoll_event events[16];
|
||||||
|
|
||||||
|
const int rc = DoPoll(events, WXSIZEOF(events), timeout);
|
||||||
|
|
||||||
if ( rc == -1 )
|
if ( rc == -1 )
|
||||||
{
|
{
|
||||||
wxLogSysError(_("Waiting for IO on epoll descriptor %d failed"),
|
wxLogSysError(_("Waiting for IO on epoll descriptor %d failed"),
|
||||||
m_epollDescriptor);
|
m_epollDescriptor);
|
||||||
return false;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool gotEvents = false;
|
int numEvents = 0;
|
||||||
for ( epoll_event *p = events; p < events + rc; p++ )
|
for ( epoll_event *p = events; p < events + rc; p++ )
|
||||||
{
|
{
|
||||||
wxFDIOHandler * const handler = (wxFDIOHandler *)(p->data.ptr);
|
wxFDIOHandler * const handler = (wxFDIOHandler *)(p->data.ptr);
|
||||||
@@ -219,10 +233,10 @@ bool wxEpollDispatcher::Dispatch(int timeout)
|
|||||||
else
|
else
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
gotEvents = true;
|
numEvents++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return gotEvents;
|
return numEvents;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // wxUSE_EPOLL_DISPATCHER
|
#endif // wxUSE_EPOLL_DISPATCHER
|
||||||
|
@@ -85,7 +85,7 @@ void wxConsoleEventLoop::PipeIOHandler::WakeUp()
|
|||||||
void wxConsoleEventLoop::PipeIOHandler::OnReadWaiting()
|
void wxConsoleEventLoop::PipeIOHandler::OnReadWaiting()
|
||||||
{
|
{
|
||||||
// got wakeup from child thread: read all data available in pipe just to
|
// got wakeup from child thread: read all data available in pipe just to
|
||||||
// make it empty (evevn though we write one byte at a time from WakeUp(),
|
// make it empty (even though we write one byte at a time from WakeUp(),
|
||||||
// it could have been called several times)
|
// it could have been called several times)
|
||||||
char buf[4];
|
char buf[4];
|
||||||
for ( ;; )
|
for ( ;; )
|
||||||
@@ -107,7 +107,9 @@ void wxConsoleEventLoop::PipeIOHandler::OnReadWaiting()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
wxTheApp->ProcessPendingEvents();
|
// writing to the wake up pipe will make wxConsoleEventLoop return from
|
||||||
|
// wxFDIODispatcher::Dispatch() it might be currently blocking in, nothing
|
||||||
|
// else needs to be done
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===========================================================================
|
// ===========================================================================
|
||||||
@@ -144,7 +146,17 @@ wxConsoleEventLoop::wxConsoleEventLoop()
|
|||||||
|
|
||||||
bool wxConsoleEventLoop::Pending() const
|
bool wxConsoleEventLoop::Pending() const
|
||||||
{
|
{
|
||||||
return wxTheApp->HasPendingEvents();
|
if ( m_dispatcher->HasPending() )
|
||||||
|
return true;
|
||||||
|
|
||||||
|
#if wxUSE_TIMER
|
||||||
|
wxUsecClock_t nextTimer;
|
||||||
|
if ( wxTimerScheduler::Get().GetNext(&nextTimer) &&
|
||||||
|
!wxMilliClockToLong(nextTimer) )
|
||||||
|
return true;
|
||||||
|
#endif // wxUSE_TIMER
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxConsoleEventLoop::Dispatch()
|
bool wxConsoleEventLoop::Dispatch()
|
||||||
@@ -167,7 +179,7 @@ int wxConsoleEventLoop::DispatchTimeout(unsigned long timeout)
|
|||||||
}
|
}
|
||||||
#endif // wxUSE_TIMER
|
#endif // wxUSE_TIMER
|
||||||
|
|
||||||
bool hadEvent = m_dispatcher->Dispatch(timeout);
|
bool hadEvent = m_dispatcher->Dispatch(timeout) > 0;
|
||||||
|
|
||||||
#if wxUSE_TIMER
|
#if wxUSE_TIMER
|
||||||
if ( wxTimerScheduler::Get().NotifyExpired() )
|
if ( wxTimerScheduler::Get().NotifyExpired() )
|
||||||
|
Reference in New Issue
Block a user