]> git.saurik.com Git - wxWidgets.git/blob - src/common/selectdispatcher.cpp
fixed crash when setting locale
[wxWidgets.git] / 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
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"
26
27 #ifndef WX_PRECOMP
28 #include "wx/hash.h"
29 #include "wx/log.h"
30 #include "wx/intl.h"
31 #endif
32
33 #ifdef HAVE_SYS_SELECT_H
34 #include <sys/select.h>
35 #endif
36
37 #include <errno.h>
38
39 #define wxSelectDispatcher_Trace wxT("selectdispatcher")
40
41 // ============================================================================
42 // implementation
43 // ============================================================================
44
45 // ----------------------------------------------------------------------------
46 // wxSelectSets
47 // ----------------------------------------------------------------------------
48
49 int wxSelectSets::ms_flags[wxSelectSets::Max] =
50 {
51 wxFDIO_INPUT,
52 wxFDIO_OUTPUT,
53 wxFDIO_EXCEPTION,
54 };
55
56 const char *wxSelectSets::ms_names[wxSelectSets::Max] =
57 {
58 "input",
59 "output",
60 "exceptional",
61 };
62
63 wxSelectSets::Callback wxSelectSets::ms_handlers[wxSelectSets::Max] =
64 {
65 &wxFDIOHandler::OnReadWaiting,
66 &wxFDIOHandler::OnWriteWaiting,
67 &wxFDIOHandler::OnExceptionWaiting,
68 };
69
70 wxSelectSets::wxSelectSets()
71 {
72 for ( int n = 0; n < Max; n++ )
73 {
74 wxFD_ZERO(&m_fds[n]);
75 }
76 }
77
78 bool wxSelectSets::HasFD(int fd) const
79 {
80 for ( int n = 0; n < Max; n++ )
81 {
82 if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) )
83 return true;
84 }
85
86 return false;
87 }
88
89 bool wxSelectSets::SetFD(int fd, int flags)
90 {
91 wxCHECK_MSG( fd >= 0, false, _T("invalid descriptor") );
92
93 for ( int n = 0; n < Max; n++ )
94 {
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 }
101 else if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) )
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 }
107 }
108
109 return true;
110 }
111
112 int wxSelectSets::Select(int nfds, struct timeval *tv)
113 {
114 return select(nfds, &m_fds[Read], &m_fds[Write], &m_fds[Except], tv);
115 }
116
117 void wxSelectSets::Handle(int fd, wxFDIOHandler& handler) const
118 {
119 for ( int n = 0; n < Max; n++ )
120 {
121 if ( wxFD_ISSET(fd, (fd_set*) &m_fds[n]) )
122 {
123 wxLogTrace(wxSelectDispatcher_Trace,
124 _T("Got %s event on fd %d"), ms_names[n], fd);
125 (handler.*ms_handlers[n])();
126 }
127 }
128 }
129
130 // ----------------------------------------------------------------------------
131 // wxSelectDispatcher
132 // ----------------------------------------------------------------------------
133
134 static wxSelectDispatcher *gs_selectDispatcher = NULL;
135
136 /* static */
137 wxSelectDispatcher *wxSelectDispatcher::Get()
138 {
139 if ( !gs_selectDispatcher )
140 {
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 }
145
146 return gs_selectDispatcher;
147 }
148
149 /* static */
150 void wxSelectDispatcher::DispatchPending()
151 {
152 if ( gs_selectDispatcher )
153 gs_selectDispatcher->RunLoop(0);
154 }
155
156 wxSelectDispatcher::wxSelectDispatcher()
157 {
158 m_maxFD = -1;
159 }
160
161 bool wxSelectDispatcher::RegisterFD(int fd, wxFDIOHandler *handler, int flags)
162 {
163 if ( !wxMappedFDIODispatcher::RegisterFD(fd, handler, flags) )
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
175 bool wxSelectDispatcher::ModifyFD(int fd, wxFDIOHandler *handler, int flags)
176 {
177 if ( !wxMappedFDIODispatcher::ModifyFD(fd, handler, flags) )
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
185 bool wxSelectDispatcher::UnregisterFD(int fd, int flags)
186 {
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 )
193 {
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 )
199 {
200 if ( it->first > m_maxFD )
201 m_maxFD = it->first;
202 }
203 }
204 }
205
206 return true;
207 }
208
209 void wxSelectDispatcher::ProcessSets(const wxSelectSets& sets)
210 {
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 }
226
227 void wxSelectDispatcher::RunLoop(int timeout)
228 {
229 struct timeval tv,
230 *ptv = NULL;
231 if ( timeout != TIMEOUT_INFINITE )
232 {
233 ptv = &tv;
234 tv.tv_sec = 0;
235 tv.tv_usec = timeout*1000;
236 }
237
238 for ( ;; )
239 {
240 wxSelectSets sets = m_sets;
241
242 wxStopWatch sw;
243 if ( ptv && timeout )
244 sw.Start(ptv->tv_usec/10);
245
246 const int ret = sets.Select(m_maxFD + 1, ptv);
247 switch ( ret )
248 {
249 case -1:
250 // continue if we were interrupted by a signal, else bail out
251 if ( errno != EINTR )
252 {
253 wxLogSysError(_("Failed to monitor I/O channels"));
254 return;
255 }
256 break;
257
258 case 0:
259 // timeout expired without anything happening
260 return;
261
262 default:
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 }
275 }
276
277 // ----------------------------------------------------------------------------
278 // wxSelectDispatcherModule
279 // ----------------------------------------------------------------------------
280
281 class wxSelectDispatcherModule : public wxModule
282 {
283 public:
284 virtual bool OnInit() { return true; }
285 virtual void OnExit() { wxDELETE(gs_selectDispatcher); }
286
287 private:
288 DECLARE_DYNAMIC_CLASS(wxSelectDispatcherModule)
289 };
290
291 IMPLEMENT_DYNAMIC_CLASS(wxSelectDispatcherModule, wxModule)
292