]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/epolldispatcher.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/epolldispatcher.cpp
3 // Purpose: implements dispatcher for epoll_wait() call
4 // Author: Lukasz Michalski
7 // Copyright: (c) 2007 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 #ifdef HAVE_SYS_EPOLL_H
24 #include "wx/unix/private/epolldispatcher.h"
25 #include "wx/unix/private.h"
27 #include "wx/module.h"
29 #include <sys/epoll.h>
32 #define wxEpollDispatcher_Trace wxT("epolldispatcher")
34 static wxEpollDispatcher
*gs_epollDispatcher
= NULL
;
36 // ============================================================================
38 // ============================================================================
40 // helper: return EPOLLxxx mask corresponding to the given flags (and also log
41 // debugging messages about it)
42 static uint32_t GetEpollMask(int flags
, int fd
)
46 if ( flags
& wxFDIO_INPUT
)
49 wxLogTrace(wxEpollDispatcher_Trace
,
50 _T("Registered fd %d for input events"), fd
);
53 if ( flags
& wxFDIO_OUTPUT
)
56 wxLogTrace(wxEpollDispatcher_Trace
,
57 _T("Registered fd %d for output events"), fd
);
60 if ( flags
& wxFDIO_EXCEPTION
)
62 ep
|= EPOLLERR
| EPOLLHUP
;
63 wxLogTrace(wxEpollDispatcher_Trace
,
64 _T("Registered fd %d for exceptional events"), fd
);
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 wxEpollDispatcher::wxEpollDispatcher()
76 m_epollDescriptor
= epoll_create(1024);
77 if ( m_epollDescriptor
== -1 )
79 wxLogSysError(_("Failed to create epoll descriptor"));
83 bool wxEpollDispatcher::RegisterFD(int fd
, wxFDIOHandler
* handler
, int flags
)
85 if ( !wxFDIODispatcher::RegisterFD(fd
, handler
, flags
) )
89 ev
.events
= GetEpollMask(flags
, fd
);
90 ev
.data
.ptr
= handler
;
92 const int ret
= epoll_ctl(m_epollDescriptor
, EPOLL_CTL_ADD
, fd
, &ev
);
95 wxLogSysError(_("Failed to add descriptor %d to epoll descriptor %d"),
96 fd
, m_epollDescriptor
);
104 bool wxEpollDispatcher::ModifyFD(int fd
, wxFDIOHandler
* handler
, int flags
)
106 if ( !wxFDIODispatcher::ModifyFD(fd
, handler
, flags
) )
110 ev
.events
= GetEpollMask(flags
, fd
);
111 ev
.data
.ptr
= handler
;
113 const int ret
= epoll_ctl(m_epollDescriptor
, EPOLL_CTL_MOD
, fd
, &ev
);
116 wxLogSysError(_("Failed to modify descriptor %d in epoll descriptor %d"),
117 fd
, m_epollDescriptor
);
125 wxFDIOHandler
*wxEpollDispatcher::UnregisterFD(int fd
, int flags
)
127 wxFDIOHandler
* const handler
= wxFDIODispatcher::UnregisterFD(fd
, flags
);
135 if ( epoll_ctl(m_epollDescriptor
, EPOLL_CTL_DEL
, fd
, &ev
) != 0 )
137 wxLogSysError(_("Failed to unregister descriptor %d from epoll descriptor %d"),
138 fd
, m_epollDescriptor
);
144 void wxEpollDispatcher::RunLoop(int timeout
)
146 epoll_event events
[16];
148 const int e_num
= epoll_wait
153 timeout
== TIMEOUT_INFINITE
? -1 : timeout
158 if ( errno
!= EINTR
)
160 wxLogSysError(_("Waiting for IO on epoll descriptor %d failed"),
166 for ( epoll_event
*p
= events
; p
< events
+ e_num
; p
++ )
168 wxFDIOHandler
* const handler
= (wxFDIOHandler
*)(p
->data
.ptr
);
171 wxFAIL_MSG( _T("NULL handler in epoll_event?") );
175 if ( p
->events
& EPOLLIN
)
176 handler
->OnReadWaiting();
178 if ( p
->events
& EPOLLOUT
)
179 handler
->OnWriteWaiting();
181 if ( p
->events
& (EPOLLERR
| EPOLLHUP
) )
182 handler
->OnExceptionWaiting();
187 wxEpollDispatcher
*wxEpollDispatcher::Get()
189 if ( !gs_epollDispatcher
)
191 gs_epollDispatcher
= new wxEpollDispatcher
;
192 if ( !gs_epollDispatcher
->IsOk() )
194 delete gs_epollDispatcher
;
195 gs_epollDispatcher
= NULL
;
199 return gs_epollDispatcher
;
202 // ----------------------------------------------------------------------------
203 // wxEpollDispatcherModule
204 // ----------------------------------------------------------------------------
206 class wxEpollDispatcherModule
: public wxModule
209 wxEpollDispatcherModule() { }
211 virtual bool OnInit() { return true; }
212 virtual void OnExit() { wxDELETE(gs_epollDispatcher
); }
214 DECLARE_DYNAMIC_CLASS(wxEpollDispatcherModule
)
217 IMPLEMENT_DYNAMIC_CLASS(wxEpollDispatcherModule
, wxModule
)
219 #endif // HAVE_SYS_EPOLL_H