X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/ad8d42f83cdf7a4f26969957e94ed166a1a5710e..2190f6aa153416d1c225532bb026df98b3e273ad:/src/common/selectdispatcher.cpp?ds=sidebyside diff --git a/src/common/selectdispatcher.cpp b/src/common/selectdispatcher.cpp index 2a96b33453..990b62c21c 100644 --- a/src/common/selectdispatcher.cpp +++ b/src/common/selectdispatcher.cpp @@ -19,9 +19,9 @@ // for compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" +#if wxUSE_SELECT_DISPATCHER + #include "wx/private/selectdispatcher.h" -#include "wx/module.h" -#include "wx/timer.h" #include "wx/unix/private.h" #ifndef WX_PRECOMP @@ -30,7 +30,8 @@ #include "wx/intl.h" #endif -#ifdef HAVE_SYS_SELECT_H +#if defined(HAVE_SYS_SELECT_H) || defined(__WATCOMC__) + #include #include #endif @@ -95,14 +96,10 @@ bool wxSelectSets::SetFD(int fd, int flags) if ( flags & ms_flags[n] ) { wxFD_SET(fd, &m_fds[n]); - wxLogTrace(wxSelectDispatcher_Trace, - _T("Registered fd %d for %s events"), fd, ms_names[n]); } else if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) ) { wxFD_CLR(fd, &m_fds[n]); - wxLogTrace(wxSelectDispatcher_Trace, - _T("Unregistered fd %d from %s events"), fd, ms_names[n]); } } @@ -123,6 +120,9 @@ void wxSelectSets::Handle(int fd, wxFDIOHandler& handler) const wxLogTrace(wxSelectDispatcher_Trace, _T("Got %s event on fd %d"), ms_names[n], fd); (handler.*ms_handlers[n])(); + // callback can modify sets and destroy handler + // this forces that one event can be processed at one time + return; } } } @@ -131,33 +131,6 @@ void wxSelectSets::Handle(int fd, wxFDIOHandler& handler) const // wxSelectDispatcher // ---------------------------------------------------------------------------- -static wxSelectDispatcher *gs_selectDispatcher = NULL; - -/* static */ -wxSelectDispatcher *wxSelectDispatcher::Get() -{ - if ( !gs_selectDispatcher ) - { - // the dispatcher should be only created from one thread so it should - // be ok to use a global without any protection here - gs_selectDispatcher = new wxSelectDispatcher; - } - - return gs_selectDispatcher; -} - -/* static */ -void wxSelectDispatcher::DispatchPending() -{ - if ( gs_selectDispatcher ) - gs_selectDispatcher->RunLoop(0); -} - -wxSelectDispatcher::wxSelectDispatcher() -{ - m_maxFD = -1; -} - bool wxSelectDispatcher::RegisterFD(int fd, wxFDIOHandler *handler, int flags) { if ( !wxMappedFDIODispatcher::RegisterFD(fd, handler, flags) ) @@ -169,6 +142,8 @@ bool wxSelectDispatcher::RegisterFD(int fd, wxFDIOHandler *handler, int flags) if ( fd > m_maxFD ) m_maxFD = fd; + wxLogTrace(wxSelectDispatcher_Trace, + _T("Registered fd %d: input:%d, output:%d, exceptional:%d"), fd, (flags & wxFDIO_INPUT) == wxFDIO_INPUT, (flags & wxFDIO_OUTPUT), (flags & wxFDIO_EXCEPTION) == wxFDIO_EXCEPTION); return true; } @@ -179,12 +154,17 @@ bool wxSelectDispatcher::ModifyFD(int fd, wxFDIOHandler *handler, int flags) wxASSERT_MSG( fd <= m_maxFD, _T("logic error: registered fd > m_maxFD?") ); + wxLogTrace(wxSelectDispatcher_Trace, + _T("Modified fd %d: input:%d, output:%d, exceptional:%d"), fd, (flags & wxFDIO_INPUT) == wxFDIO_INPUT, (flags & wxFDIO_OUTPUT) == wxFDIO_OUTPUT, (flags & wxFDIO_EXCEPTION) == wxFDIO_EXCEPTION); return m_sets.SetFD(fd, flags); } -bool wxSelectDispatcher::UnregisterFD(int fd, int flags) +bool wxSelectDispatcher::UnregisterFD(int fd) { - m_sets.ClearFD(fd, flags); + m_sets.ClearFD(fd); + + if ( !wxMappedFDIODispatcher::UnregisterFD(fd) ) + return false; // remove the handler if we don't need it any more if ( !m_sets.HasFD(fd) ) @@ -198,16 +178,21 @@ bool wxSelectDispatcher::UnregisterFD(int fd, int flags) ++it ) { if ( it->first > m_maxFD ) + { m_maxFD = it->first; + } } } } + wxLogTrace(wxSelectDispatcher_Trace, + _T("Removed fd %d, current max: %d"), fd, m_maxFD); return true; } -void wxSelectDispatcher::ProcessSets(const wxSelectSets& sets) +bool wxSelectDispatcher::ProcessSets(const wxSelectSets& sets) { + bool gotEvent = false; for ( int fd = 0; fd <= m_maxFD; fd++ ) { if ( !sets.HasFD(fd) ) @@ -220,73 +205,52 @@ void wxSelectDispatcher::ProcessSets(const wxSelectSets& sets) continue; } + gotEvent = true; + sets.Handle(fd, *handler); } + + return gotEvent; } -void wxSelectDispatcher::RunLoop(int timeout) +bool wxSelectDispatcher::Dispatch(int timeout) { struct timeval tv, - *ptv = NULL; + *ptv; if ( timeout != TIMEOUT_INFINITE ) { ptv = &tv; tv.tv_sec = 0; tv.tv_usec = timeout*1000; } - - for ( ;; ) + else // no timeout { - wxSelectSets sets = m_sets; - - wxStopWatch sw; - if ( ptv && timeout ) - sw.Start(ptv->tv_usec/10); - - const int ret = sets.Select(m_maxFD + 1, ptv); - switch ( ret ) - { - case -1: - // continue if we were interrupted by a signal, else bail out - if ( errno != EINTR ) - { - wxLogSysError(_("Failed to monitor I/O channels")); - return; - } - break; + ptv = NULL; + } - case 0: - // timeout expired without anything happening - return; + wxSelectSets sets = m_sets; - default: - ProcessSets(sets); - } + const int ret = sets.Select(m_maxFD + 1, ptv); + switch ( ret ) + { + case -1: + if ( errno != EINTR ) + { + wxLogSysError(_("Failed to monitor I/O channels")); + } + break; - if ( ptv ) - { - timeout -= sw.Time(); - if ( timeout <= 0 ) - break; + case 0: + // timeout expired without anything happening + break; - ptv->tv_usec = timeout*1000; - } + default: + if ( ProcessSets(sets) ) + return true; } -} - -// ---------------------------------------------------------------------------- -// wxSelectDispatcherModule -// ---------------------------------------------------------------------------- -class wxSelectDispatcherModule : public wxModule -{ -public: - virtual bool OnInit() { return true; } - virtual void OnExit() { wxDELETE(gs_selectDispatcher); } - -private: - DECLARE_DYNAMIC_CLASS(wxSelectDispatcherModule) -}; - -IMPLEMENT_DYNAMIC_CLASS(wxSelectDispatcherModule, wxModule) + // nothing happened + return false; +} +#endif // wxUSE_SELECT_DISPATCHER