e67225805f3964a970f6e6d8fb93f82cb616d376
[libs/core.git] / source / io / windows / poll.cpp
1 #include <msp/core/systemerror.h>
2 #include "eventobject.h"
3 #include "handle.h"
4 #include "handle_private.h"
5 #include "poll.h"
6 #include "poll_platform.h"
7
8 namespace Msp {
9 namespace IO {
10
11 void Poller::rebuild_array()
12 {
13         priv->handles.clear();
14
15         for(EventMap::iterator i=objects.begin(); i!=objects.end(); ++i)
16                 priv->handles.push_back(*i->first->get_event_handle());
17
18         objs_changed = false;
19 }
20
21 void Poller::platform_poll(int timeout)
22 {
23         if(timeout<0)
24                 timeout = INFINITE;
25
26         DWORD ret = WaitForMultipleObjects(priv->handles.size(), &priv->handles.front(), false, timeout);
27         if(/*ret>=WAIT_OBJECT_0 &&*/ ret<WAIT_OBJECT_0+priv->handles.size())
28         {
29                 EventMap::iterator i = objects.begin();
30                 advance(i, ret-WAIT_OBJECT_0);
31                 poll_result.push_back(Slot(i->first, i->second));
32         }
33         else if(ret==WAIT_FAILED)
34                 throw system_error("WaitForMultipleObjects");
35 }
36
37
38 PollEvent platform_poll(EventObject &obj, PollEvent pe, int timeout)
39 {
40         if(timeout<0)
41                 timeout = INFINITE;
42
43         DWORD ret = WaitForSingleObject(*obj.get_event_handle(), timeout);
44         if(ret==WAIT_OBJECT_0)
45                 return pe;
46         else if(ret==WAIT_FAILED)
47                 throw system_error("WaitForSingleObject");
48
49         return P_NONE;
50 }
51
52 } // namespace IO
53 } // namespace Msp