]> git.saurik.com Git - wxWidgets.git/blame - src/common/selectdispatcher.cpp
use wxString in wxDateTime methods (only partially done)
[wxWidgets.git] / src / common / selectdispatcher.cpp
CommitLineData
30c45bdd 1///////////////////////////////////////////////////////////////////////////////
b46b1d59 2// Name: src/common/selectdispatcher.cpp
30c45bdd 3// Purpose: implements dispatcher for select() call
b46b1d59 4// Author: Lukasz Michalski and Vadim Zeitlin
30c45bdd
VZ
5// Created: December 2006
6// RCS-ID: $Id$
7// Copyright: (c) 2006 Lukasz Michalski
8// License: wxWindows licence
9///////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
19// for compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#include "wx/private/selectdispatcher.h"
23#include "wx/module.h"
24#include "wx/timer.h"
25#include "wx/unix/private.h"
30c45bdd
VZ
26
27#ifndef WX_PRECOMP
28 #include "wx/hash.h"
b5ef33b2
VS
29 #include "wx/log.h"
30 #include "wx/intl.h"
30c45bdd
VZ
31#endif
32
30c45bdd 33#ifdef HAVE_SYS_SELECT_H
b46b1d59 34 #include <sys/select.h>
30c45bdd
VZ
35#endif
36
b46b1d59
VZ
37#include <errno.h>
38
30c45bdd
VZ
39#define wxSelectDispatcher_Trace wxT("selectdispatcher")
40
41// ============================================================================
42// implementation
43// ============================================================================
44
45// ----------------------------------------------------------------------------
b46b1d59 46// wxSelectSets
30c45bdd
VZ
47// ----------------------------------------------------------------------------
48
b46b1d59
VZ
49int wxSelectSets::ms_flags[wxSelectSets::Max] =
50{
51 wxFDIO_INPUT,
52 wxFDIO_OUTPUT,
53 wxFDIO_EXCEPTION,
54};
30c45bdd 55
b46b1d59 56const char *wxSelectSets::ms_names[wxSelectSets::Max] =
30c45bdd 57{
b46b1d59
VZ
58 "input",
59 "output",
60 "exceptional",
61};
30c45bdd 62
b46b1d59 63wxSelectSets::Callback wxSelectSets::ms_handlers[wxSelectSets::Max] =
30c45bdd 64{
b46b1d59
VZ
65 &wxFDIOHandler::OnReadWaiting,
66 &wxFDIOHandler::OnWriteWaiting,
67 &wxFDIOHandler::OnExceptionWaiting,
68};
30c45bdd 69
b46b1d59
VZ
70wxSelectSets::wxSelectSets()
71{
72 for ( int n = 0; n < Max; n++ )
30c45bdd 73 {
b46b1d59 74 wxFD_ZERO(&m_fds[n]);
30c45bdd 75 }
b46b1d59 76}
30c45bdd 77
b46b1d59
VZ
78bool wxSelectSets::HasFD(int fd) const
79{
80 for ( int n = 0; n < Max; n++ )
30c45bdd 81 {
fa15eac7 82 if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) )
b46b1d59
VZ
83 return true;
84 }
30c45bdd 85
b46b1d59 86 return false;
30c45bdd
VZ
87}
88
b46b1d59 89bool wxSelectSets::SetFD(int fd, int flags)
30c45bdd 90{
b46b1d59 91 wxCHECK_MSG( fd >= 0, false, _T("invalid descriptor") );
30c45bdd 92
b46b1d59 93 for ( int n = 0; n < Max; n++ )
30c45bdd 94 {
b46b1d59
VZ
95 if ( flags & ms_flags[n] )
96 {
97 wxFD_SET(fd, &m_fds[n]);
98 wxLogTrace(wxSelectDispatcher_Trace,
99 _T("Registered fd %d for %s events"), fd, ms_names[n]);
100 }
fa15eac7 101 else if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) )
b46b1d59
VZ
102 {
103 wxFD_CLR(fd, &m_fds[n]);
104 wxLogTrace(wxSelectDispatcher_Trace,
105 _T("Unregistered fd %d from %s events"), fd, ms_names[n]);
106 }
30c45bdd
VZ
107 }
108
b46b1d59
VZ
109 return true;
110}
30c45bdd 111
b46b1d59
VZ
112int wxSelectSets::Select(int nfds, struct timeval *tv)
113{
114 return select(nfds, &m_fds[Read], &m_fds[Write], &m_fds[Except], tv);
115}
30c45bdd 116
b46b1d59
VZ
117void wxSelectSets::Handle(int fd, wxFDIOHandler& handler) const
118{
119 for ( int n = 0; n < Max; n++ )
30c45bdd 120 {
fa15eac7 121 if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) )
30c45bdd 122 {
b46b1d59
VZ
123 wxLogTrace(wxSelectDispatcher_Trace,
124 _T("Got %s event on fd %d"), ms_names[n], fd);
125 (handler.*ms_handlers[n])();
126 }
127 }
30c45bdd
VZ
128}
129
b46b1d59
VZ
130// ----------------------------------------------------------------------------
131// wxSelectDispatcher
132// ----------------------------------------------------------------------------
133
134static wxSelectDispatcher *gs_selectDispatcher = NULL;
135
136/* static */
137wxSelectDispatcher *wxSelectDispatcher::Get()
30c45bdd 138{
b46b1d59 139 if ( !gs_selectDispatcher )
30c45bdd 140 {
b46b1d59
VZ
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;
144 }
30c45bdd 145
b46b1d59
VZ
146 return gs_selectDispatcher;
147}
148
149/* static */
150void wxSelectDispatcher::DispatchPending()
151{
152 if ( gs_selectDispatcher )
153 gs_selectDispatcher->RunLoop(0);
154}
30c45bdd 155
b46b1d59
VZ
156wxSelectDispatcher::wxSelectDispatcher()
157{
158 m_maxFD = -1;
159}
160
161bool wxSelectDispatcher::RegisterFD(int fd, wxFDIOHandler *handler, int flags)
162{
ad8d42f8 163 if ( !wxMappedFDIODispatcher::RegisterFD(fd, handler, flags) )
b46b1d59
VZ
164 return false;
165
166 if ( !m_sets.SetFD(fd, flags) )
167 return false;
168
169 if ( fd > m_maxFD )
170 m_maxFD = fd;
171
172 return true;
173}
174
175bool wxSelectDispatcher::ModifyFD(int fd, wxFDIOHandler *handler, int flags)
176{
ad8d42f8 177 if ( !wxMappedFDIODispatcher::ModifyFD(fd, handler, flags) )
b46b1d59
VZ
178 return false;
179
180 wxASSERT_MSG( fd <= m_maxFD, _T("logic error: registered fd > m_maxFD?") );
181
182 return m_sets.SetFD(fd, flags);
183}
184
ad8d42f8 185bool wxSelectDispatcher::UnregisterFD(int fd, int flags)
b46b1d59 186{
b46b1d59
VZ
187 m_sets.ClearFD(fd, flags);
188
189 // remove the handler if we don't need it any more
190 if ( !m_sets.HasFD(fd) )
191 {
192 if ( fd == m_maxFD )
30c45bdd 193 {
b46b1d59
VZ
194 // need to find new max fd
195 m_maxFD = -1;
196 for ( wxFDIOHandlerMap::const_iterator it = m_handlers.begin();
197 it != m_handlers.end();
198 ++it )
30c45bdd 199 {
b46b1d59
VZ
200 if ( it->first > m_maxFD )
201 m_maxFD = it->first;
202 }
203 }
204 }
205
ad8d42f8 206 return true;
30c45bdd
VZ
207}
208
b46b1d59 209void wxSelectDispatcher::ProcessSets(const wxSelectSets& sets)
30c45bdd 210{
b46b1d59
VZ
211 for ( int fd = 0; fd <= m_maxFD; fd++ )
212 {
213 if ( !sets.HasFD(fd) )
214 continue;
215
216 wxFDIOHandler * const handler = FindHandler(fd);
217 if ( !handler )
218 {
219 wxFAIL_MSG( _T("NULL handler in wxSelectDispatcher?") );
220 continue;
221 }
222
223 sets.Handle(fd, *handler);
224 }
225}
30c45bdd
VZ
226
227void wxSelectDispatcher::RunLoop(int timeout)
228{
b46b1d59
VZ
229 struct timeval tv,
230 *ptv = NULL;
231 if ( timeout != TIMEOUT_INFINITE )
30c45bdd
VZ
232 {
233 ptv = &tv;
234 tv.tv_sec = 0;
b46b1d59
VZ
235 tv.tv_usec = timeout*1000;
236 }
30c45bdd 237
b46b1d59 238 for ( ;; )
30c45bdd 239 {
b46b1d59
VZ
240 wxSelectSets sets = m_sets;
241
30c45bdd 242 wxStopWatch sw;
f7b3c5ec 243 if ( ptv && timeout )
30c45bdd 244 sw.Start(ptv->tv_usec/10);
b46b1d59
VZ
245
246 const int ret = sets.Select(m_maxFD + 1, ptv);
30c45bdd
VZ
247 switch ( ret )
248 {
30c45bdd 249 case -1:
b46b1d59
VZ
250 // continue if we were interrupted by a signal, else bail out
251 if ( errno != EINTR )
f7b3c5ec 252 {
b5ef33b2 253 wxLogSysError(_("Failed to monitor I/O channels"));
f7b3c5ec
VZ
254 return;
255 }
30c45bdd
VZ
256 break;
257
30c45bdd 258 case 0:
b46b1d59
VZ
259 // timeout expired without anything happening
260 return;
30c45bdd
VZ
261
262 default:
b46b1d59
VZ
263 ProcessSets(sets);
264 }
265
266 if ( ptv )
267 {
268 timeout -= sw.Time();
269 if ( timeout <= 0 )
270 break;
271
272 ptv->tv_usec = timeout*1000;
273 }
274 }
30c45bdd
VZ
275}
276
277// ----------------------------------------------------------------------------
278// wxSelectDispatcherModule
279// ----------------------------------------------------------------------------
280
b46b1d59 281class wxSelectDispatcherModule : public wxModule
30c45bdd
VZ
282{
283public:
b46b1d59
VZ
284 virtual bool OnInit() { return true; }
285 virtual void OnExit() { wxDELETE(gs_selectDispatcher); }
30c45bdd
VZ
286
287private:
288 DECLARE_DYNAMIC_CLASS(wxSelectDispatcherModule)
289};
290
291IMPLEMENT_DYNAMIC_CLASS(wxSelectDispatcherModule, wxModule)
292