6 #include <msp/core/systemerror.h>
7 #include <msp/strings/format.h>
8 #include <msp/time/units.h>
9 #include "eventobject.h"
11 #include "handle_private.h"
19 using namespace Msp::IO;
21 inline short int sys_poll_event(PollEvent event)
25 if(event&~(P_INPUT|P_PRIO|P_OUTPUT))
26 throw invalid_argument("sys_poll_event");
40 inline PollEvent poll_event_from_sys(int event)
42 PollEvent result = P_NONE;
48 result = result|P_INPUT;
50 result = result|P_PRIO;
52 result = result|P_OUTPUT;
54 result = result|P_ERROR;
60 inline PollEvent do_poll(EventObject &obj, PollEvent pe, int timeout)
66 DWORD ret = WaitForSingleObject(*obj.get_event_handle(), timeout);
67 if(ret==WAIT_OBJECT_0)
69 else if(ret==WAIT_FAILED)
70 throw system_error("WaitForSingleObject");
74 pollfd pfd = { *obj.get_event_handle(), sys_poll_event(pe), 0 };
76 int ret = ::poll(&pfd, 1, timeout);
82 throw system_error("poll");
85 return poll_event_from_sys(pfd.revents);
95 struct Poller::Private
98 vector<HANDLE> handles;
115 void Poller::set_object(EventObject &obj, PollEvent ev)
117 // Verify that the object has an event handle
119 obj.get_event_handle();
121 EventMap::iterator i = objects.find(&obj);
134 if(objects.size()>=MAXIMUM_WAIT_OBJECTS)
135 throw logic_error("Maximum number of wait objects reached");
137 objects.insert(EventMap::value_type(&obj, ev));
148 int Poller::poll(const Time::TimeDelta &timeout)
150 if(timeout<Time::zero)
151 throw invalid_argument("Poller::poll");
153 return do_poll(static_cast<int>(timeout/Time::msec));
156 void Poller::rebuild_array()
159 priv->handles.clear();
161 for(EventMap::iterator i=objects.begin(); i!=objects.end(); ++i)
162 priv->handles.push_back(*i->first->get_event_handle());
166 for(EventMap::iterator i=objects.begin(); i!=objects.end(); ++i)
169 p.fd = *i->first->get_event_handle();
170 p.events = sys_poll_event(i->second);
171 priv->pfd.push_back(p);
175 objs_changed = false;
178 int Poller::do_poll(int timeout)
189 DWORD ret = WaitForMultipleObjects(priv->handles.size(), &priv->handles.front(), false, timeout);
190 if(/*ret>=WAIT_OBJECT_0 &&*/ ret<WAIT_OBJECT_0+priv->handles.size())
192 EventMap::iterator i = objects.begin();
193 advance(i, ret-WAIT_OBJECT_0);
194 poll_result.push_back(Slot(i->first, i->second));
198 else if(ret==WAIT_FAILED)
199 throw system_error("WaitForMultipleObjects");
203 int ret = ::poll(&priv->pfd.front(), priv->pfd.size(), timeout);
209 throw system_error("poll");
213 EventMap::iterator j = objects.begin();
214 for(vector<pollfd>::iterator i=priv->pfd.begin(); (i!=priv->pfd.end() && n>0); ++i, ++j)
217 poll_result.push_back(Slot(j->first, poll_event_from_sys(i->revents)));
226 PollEvent poll(EventObject &obj, PollEvent pe)
228 return do_poll(obj, pe, -1);
231 PollEvent poll(EventObject &obj, PollEvent pe, const Time::TimeDelta &timeout)
233 if(timeout<Time::zero)
234 throw invalid_argument("poll");
236 return do_poll(obj, pe, static_cast<int>(timeout/Time::msec));