]>
git.saurik.com Git - wxWidgets.git/blob - src/common/selectdispatcher.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/selectdispatcher.cpp
3 // Purpose: implements dispatcher for select() call
4 // Author: Lukasz Michalski and Vadim Zeitlin
5 // Created: December 2006
7 // Copyright: (c) 2006 Lukasz Michalski
8 // License: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
22 #include "wx/private/selectdispatcher.h"
23 #include "wx/module.h"
25 #include "wx/unix/private.h"
33 #ifdef HAVE_SYS_SELECT_H
34 #include <sys/select.h>
39 #define wxSelectDispatcher_Trace wxT("selectdispatcher")
41 // ============================================================================
43 // ============================================================================
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 int wxSelectSets::ms_flags
[wxSelectSets::Max
] =
56 const char *wxSelectSets::ms_names
[wxSelectSets::Max
] =
63 wxSelectSets::Callback
wxSelectSets::ms_handlers
[wxSelectSets::Max
] =
65 &wxFDIOHandler::OnReadWaiting
,
66 &wxFDIOHandler::OnWriteWaiting
,
67 &wxFDIOHandler::OnExceptionWaiting
,
70 wxSelectSets::wxSelectSets()
72 for ( int n
= 0; n
< Max
; n
++ )
78 bool wxSelectSets::HasFD(int fd
) const
80 for ( int n
= 0; n
< Max
; n
++ )
82 if ( wxFD_ISSET(fd
, &m_fds
[n
]) )
89 bool wxSelectSets::SetFD(int fd
, int flags
)
91 wxCHECK_MSG( fd
>= 0, false, _T("invalid descriptor") );
93 for ( int n
= 0; n
< Max
; n
++ )
95 if ( flags
& ms_flags
[n
] )
97 wxFD_SET(fd
, &m_fds
[n
]);
98 wxLogTrace(wxSelectDispatcher_Trace
,
99 _T("Registered fd %d for %s events"), fd
, ms_names
[n
]);
101 else if ( wxFD_ISSET(fd
, &m_fds
[n
]) )
103 wxFD_CLR(fd
, &m_fds
[n
]);
104 wxLogTrace(wxSelectDispatcher_Trace
,
105 _T("Unregistered fd %d from %s events"), fd
, ms_names
[n
]);
112 int wxSelectSets::Select(int nfds
, struct timeval
*tv
)
114 return select(nfds
, &m_fds
[Read
], &m_fds
[Write
], &m_fds
[Except
], tv
);
117 void wxSelectSets::Handle(int fd
, wxFDIOHandler
& handler
) const
119 for ( int n
= 0; n
< Max
; n
++ )
121 if ( wxFD_ISSET(fd
, &m_fds
[n
]) )
123 wxLogTrace(wxSelectDispatcher_Trace
,
124 _T("Got %s event on fd %d"), ms_names
[n
], fd
);
125 (handler
.*ms_handlers
[n
])();
130 // ----------------------------------------------------------------------------
131 // wxSelectDispatcher
132 // ----------------------------------------------------------------------------
134 static wxSelectDispatcher
*gs_selectDispatcher
= NULL
;
137 wxSelectDispatcher
*wxSelectDispatcher::Get()
139 if ( !gs_selectDispatcher
)
141 // the dispatcher should be only created from one thread so it should
142 // be ok to use a global without any protection here
143 gs_selectDispatcher
= new wxSelectDispatcher
;
146 return gs_selectDispatcher
;
150 void wxSelectDispatcher::DispatchPending()
152 if ( gs_selectDispatcher
)
153 gs_selectDispatcher
->RunLoop(0);
156 wxSelectDispatcher::wxSelectDispatcher()
161 bool wxSelectDispatcher::RegisterFD(int fd
, wxFDIOHandler
*handler
, int flags
)
163 if ( !wxFDIODispatcher::RegisterFD(fd
, handler
, flags
) )
166 if ( !m_sets
.SetFD(fd
, flags
) )
175 bool wxSelectDispatcher::ModifyFD(int fd
, wxFDIOHandler
*handler
, int flags
)
177 if ( !wxFDIODispatcher::ModifyFD(fd
, handler
, flags
) )
180 wxASSERT_MSG( fd
<= m_maxFD
, _T("logic error: registered fd > m_maxFD?") );
182 return m_sets
.SetFD(fd
, flags
);
185 wxFDIOHandler
*wxSelectDispatcher::UnregisterFD(int fd
, int flags
)
187 wxFDIOHandler
* const handler
= wxFDIODispatcher::UnregisterFD(fd
, flags
);
189 m_sets
.ClearFD(fd
, flags
);
191 // remove the handler if we don't need it any more
192 if ( !m_sets
.HasFD(fd
) )
196 // need to find new max fd
198 for ( wxFDIOHandlerMap::const_iterator it
= m_handlers
.begin();
199 it
!= m_handlers
.end();
202 if ( it
->first
> m_maxFD
)
211 void wxSelectDispatcher::ProcessSets(const wxSelectSets
& sets
)
213 for ( int fd
= 0; fd
<= m_maxFD
; fd
++ )
215 if ( !sets
.HasFD(fd
) )
218 wxFDIOHandler
* const handler
= FindHandler(fd
);
221 wxFAIL_MSG( _T("NULL handler in wxSelectDispatcher?") );
225 sets
.Handle(fd
, *handler
);
229 void wxSelectDispatcher::RunLoop(int timeout
)
233 if ( timeout
!= TIMEOUT_INFINITE
)
237 tv
.tv_usec
= timeout
*1000;
242 wxSelectSets sets
= m_sets
;
245 if ( ptv
&& timeout
)
246 sw
.Start(ptv
->tv_usec
/10);
248 const int ret
= sets
.Select(m_maxFD
+ 1, ptv
);
252 // continue if we were interrupted by a signal, else bail out
253 if ( errno
!= EINTR
)
255 wxLogSysError(_("Failed to monitor I/O channels"));
261 // timeout expired without anything happening
270 timeout
-= sw
.Time();
274 ptv
->tv_usec
= timeout
*1000;
279 // ----------------------------------------------------------------------------
280 // wxSelectDispatcherModule
281 // ----------------------------------------------------------------------------
283 class wxSelectDispatcherModule
: public wxModule
286 virtual bool OnInit() { return true; }
287 virtual void OnExit() { wxDELETE(gs_selectDispatcher
); }
290 DECLARE_DYNAMIC_CLASS(wxSelectDispatcherModule
)
293 IMPLEMENT_DYNAMIC_CLASS(wxSelectDispatcherModule
, wxModule
)