]>
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"
26 #include "wx/module.h"
33 #include <sys/epoll.h>
36 #define wxEpollDispatcher_Trace wxT("epolldispatcher")
38 static wxEpollDispatcher
*gs_epollDispatcher
= NULL
;
40 // ============================================================================
42 // ============================================================================
44 // helper: return EPOLLxxx mask corresponding to the given flags (and also log
45 // debugging messages about it)
46 static uint32_t GetEpollMask(int flags
, int fd
)
50 if ( flags
& wxFDIO_INPUT
)
53 wxLogTrace(wxEpollDispatcher_Trace
,
54 _T("Registered fd %d for input events"), fd
);
57 if ( flags
& wxFDIO_OUTPUT
)
60 wxLogTrace(wxEpollDispatcher_Trace
,
61 _T("Registered fd %d for output events"), fd
);
64 if ( flags
& wxFDIO_EXCEPTION
)
66 ep
|= EPOLLERR
| EPOLLHUP
;
67 wxLogTrace(wxEpollDispatcher_Trace
,
68 _T("Registered fd %d for exceptional events"), fd
);
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 wxEpollDispatcher::wxEpollDispatcher()
80 m_epollDescriptor
= epoll_create(1024);
81 if ( m_epollDescriptor
== -1 )
83 wxLogSysError(_("Failed to create epoll descriptor"));
87 bool wxEpollDispatcher::RegisterFD(int fd
, wxFDIOHandler
* handler
, int flags
)
90 ev
.events
= GetEpollMask(flags
, fd
);
91 ev
.data
.ptr
= handler
;
93 const int ret
= epoll_ctl(m_epollDescriptor
, EPOLL_CTL_ADD
, fd
, &ev
);
96 wxLogSysError(_("Failed to add descriptor %d to epoll descriptor %d"),
97 fd
, m_epollDescriptor
);
105 bool wxEpollDispatcher::ModifyFD(int fd
, wxFDIOHandler
* handler
, int flags
)
108 ev
.events
= GetEpollMask(flags
, fd
);
109 ev
.data
.ptr
= handler
;
111 const int ret
= epoll_ctl(m_epollDescriptor
, EPOLL_CTL_MOD
, fd
, &ev
);
114 wxLogSysError(_("Failed to modify descriptor %d in epoll descriptor %d"),
115 fd
, m_epollDescriptor
);
123 bool wxEpollDispatcher::UnregisterFD(int fd
, int flags
)
129 if ( epoll_ctl(m_epollDescriptor
, EPOLL_CTL_DEL
, fd
, &ev
) != 0 )
131 wxLogSysError(_("Failed to unregister descriptor %d from epoll descriptor %d"),
132 fd
, m_epollDescriptor
);
138 void wxEpollDispatcher::RunLoop(int timeout
)
140 epoll_event events
[16];
142 const int e_num
= epoll_wait
147 timeout
== TIMEOUT_INFINITE
? -1 : timeout
152 if ( errno
!= EINTR
)
154 wxLogSysError(_("Waiting for IO on epoll descriptor %d failed"),
160 for ( epoll_event
*p
= events
; p
< events
+ e_num
; p
++ )
162 wxFDIOHandler
* const handler
= (wxFDIOHandler
*)(p
->data
.ptr
);
165 wxFAIL_MSG( _T("NULL handler in epoll_event?") );
169 if ( p
->events
& EPOLLIN
)
170 handler
->OnReadWaiting();
172 if ( p
->events
& EPOLLOUT
)
173 handler
->OnWriteWaiting();
175 if ( p
->events
& (EPOLLERR
| EPOLLHUP
) )
176 handler
->OnExceptionWaiting();
181 wxEpollDispatcher
*wxEpollDispatcher::Get()
183 if ( !gs_epollDispatcher
)
185 gs_epollDispatcher
= new wxEpollDispatcher
;
186 if ( !gs_epollDispatcher
->IsOk() )
188 delete gs_epollDispatcher
;
189 gs_epollDispatcher
= NULL
;
193 return gs_epollDispatcher
;
196 // ----------------------------------------------------------------------------
197 // wxEpollDispatcherModule
198 // ----------------------------------------------------------------------------
200 class wxEpollDispatcherModule
: public wxModule
203 wxEpollDispatcherModule() { }
205 virtual bool OnInit() { return true; }
206 virtual void OnExit() { wxDELETE(gs_epollDispatcher
); }
208 DECLARE_DYNAMIC_CLASS(wxEpollDispatcherModule
)
211 IMPLEMENT_DYNAMIC_CLASS(wxEpollDispatcherModule
, wxModule
)
213 #endif // HAVE_SYS_EPOLL_H