]> git.tdb.fi Git - libs/core.git/blob - source/io/windows/poll.cpp
Close duplicated handles after starting a new process
[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 using namespace std;
9
10 namespace Msp {
11 namespace IO {
12
13 void Poller::rebuild_array()
14 {
15         if(!objs_changed)
16                 return;
17
18         priv->handles.clear();
19         priv->handles.reserve(objects.size());
20
21         for(vector<PolledObject>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
22                 priv->handles.push_back(*i->object->get_event_handle());
23 }
24
25 void Poller::platform_poll(int timeout)
26 {
27         if(timeout<0)
28                 timeout = INFINITE;
29
30         DWORD ret = WaitForMultipleObjects(priv->handles.size(), &priv->handles.front(), false, timeout);
31         if(/*ret>=WAIT_OBJECT_0 &&*/ ret<WAIT_OBJECT_0+priv->handles.size())
32         {
33                 const PolledObject &obj = objects[ret-WAIT_OBJECT_0];
34                 poll_result.push_back(obj);
35         }
36         else if(ret==WAIT_FAILED)
37                 throw system_error("WaitForMultipleObjects");
38 }
39
40
41 PollEvent platform_poll(EventObject &obj, PollEvent pe, int timeout)
42 {
43         if(timeout<0)
44                 timeout = INFINITE;
45
46         DWORD ret = WaitForSingleObject(*obj.get_event_handle(), timeout);
47         if(ret==WAIT_OBJECT_0)
48                 return pe;
49         else if(ret==WAIT_FAILED)
50                 throw system_error("WaitForSingleObject");
51
52         return P_NONE;
53 }
54
55 } // namespace IO
56 } // namespace Msp