]>
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
, (fd_set
*) &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
, (fd_set
*) &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
, (fd_set
*) &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 ( !wxMappedFDIODispatcher::RegisterFD(fd
, handler
, flags
) )
166 if ( !m_sets
.SetFD(fd
, flags
) )
175 bool wxSelectDispatcher::ModifyFD(int fd
, wxFDIOHandler
*handler
, int flags
)
177 if ( !wxMappedFDIODispatcher::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 bool wxSelectDispatcher::UnregisterFD(int fd
, int flags
)
187 m_sets
.ClearFD(fd
, flags
);
189 // remove the handler if we don't need it any more
190 if ( !m_sets
.HasFD(fd
) )
194 // need to find new max fd
196 for ( wxFDIOHandlerMap::const_iterator it
= m_handlers
.begin();
197 it
!= m_handlers
.end();
200 if ( it
->first
> m_maxFD
)
209 void wxSelectDispatcher::ProcessSets(const wxSelectSets
& sets
)
211 for ( int fd
= 0; fd
<= m_maxFD
; fd
++ )
213 if ( !sets
.HasFD(fd
) )
216 wxFDIOHandler
* const handler
= FindHandler(fd
);
219 wxFAIL_MSG( _T("NULL handler in wxSelectDispatcher?") );
223 sets
.Handle(fd
, *handler
);
227 void wxSelectDispatcher::RunLoop(int timeout
)
231 if ( timeout
!= TIMEOUT_INFINITE
)
235 tv
.tv_usec
= timeout
*1000;
240 wxSelectSets sets
= m_sets
;
243 if ( ptv
&& timeout
)
244 sw
.Start(ptv
->tv_usec
/10);
246 const int ret
= sets
.Select(m_maxFD
+ 1, ptv
);
250 // continue if we were interrupted by a signal, else bail out
251 if ( errno
!= EINTR
)
253 wxLogSysError(_("Failed to monitor I/O channels"));
259 // timeout expired without anything happening
268 timeout
-= sw
.Time();
272 ptv
->tv_usec
= timeout
*1000;
277 // ----------------------------------------------------------------------------
278 // wxSelectDispatcherModule
279 // ----------------------------------------------------------------------------
281 class wxSelectDispatcherModule
: public wxModule
284 virtual bool OnInit() { return true; }
285 virtual void OnExit() { wxDELETE(gs_selectDispatcher
); }
288 DECLARE_DYNAMIC_CLASS(wxSelectDispatcherModule
)
291 IMPLEMENT_DYNAMIC_CLASS(wxSelectDispatcherModule
, wxModule
)