#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
// 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()
+wxSelectDispatcher *wxSelectDispatcher::Create()
{
- if ( gs_selectDispatcher )
- gs_selectDispatcher->RunLoop(0);
+ return new wxSelectDispatcher;
}
wxSelectDispatcher::wxSelectDispatcher()
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) )
}
}
-void wxSelectDispatcher::RunLoop(int timeout)
+void 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;
+ ptv = NULL;
+ }
- wxStopWatch sw;
- if ( ptv && timeout )
- sw.Start(ptv->tv_usec/10);
+ wxSelectSets sets = m_sets;
- 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;
-
- case 0:
- // timeout expired without anything happening
- return;
-
- 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:
+ ProcessSets(sets);
}
}
-// ----------------------------------------------------------------------------
-// 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)
-
#endif // wxUSE_SELECT_DISPATCHER