]> git.tdb.fi Git - libs/core.git/blob - source/io/poll.cpp
Deal with the activity being recreated during process lifetime
[libs/core.git] / source / io / poll.cpp
1 #include <stdexcept>
2 #include "eventobject.h"
3 #include "poll.h"
4 #include "poll_platform.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace IO {
10
11 Poller::Poller():
12         priv(new Private),
13         objs_changed(false)
14 { }
15
16 Poller::~Poller()
17 {
18         delete priv;
19 }
20
21 void Poller::set_object(EventObject &obj, PollEvent ev)
22 {
23         // Verify that the object has an event handle
24         if(ev)
25                 obj.get_event_handle();
26
27         EventMap::iterator i = objects.find(&obj);
28         if(i!=objects.end())
29         {
30                 if(ev)
31                         i->second = ev;
32                 else
33                         objects.erase(i);
34
35                 objs_changed = true;
36         }
37         else if(ev)
38         {
39 #ifdef WIN32
40                 if(objects.size()>=MAXIMUM_WAIT_OBJECTS)
41                         throw logic_error("Maximum number of wait objects reached");
42 #endif
43                 objects.insert(EventMap::value_type(&obj, ev));
44
45                 objs_changed = true;
46         }
47 }
48
49 int Poller::poll()
50 {
51         return do_poll(-1);
52 }
53
54 int Poller::poll(const Time::TimeDelta &timeout)
55 {
56         if(timeout<Time::zero)
57                 throw invalid_argument("Poller::poll");
58
59         return do_poll(static_cast<int>(timeout/Time::msec));
60 }
61
62 int Poller::do_poll(int timeout)
63 {
64         if(objs_changed)
65                 rebuild_array();
66
67         poll_result.clear();
68
69         platform_poll(timeout);
70
71         return poll_result.size();
72 }
73
74
75
76 PollEvent platform_poll(EventObject &, PollEvent, int);
77
78 PollEvent poll(EventObject &obj, PollEvent pe)
79 {
80         return platform_poll(obj, pe, -1);
81 }
82
83 PollEvent poll(EventObject &obj, PollEvent pe, const Time::TimeDelta &timeout)
84 {
85         if(timeout<Time::zero)
86                 throw invalid_argument("poll");
87
88         return platform_poll(obj, pe, static_cast<int>(timeout/Time::msec));
89 }
90
91 } // namespace IO
92 } // namespace Msp