blob: 8bf091e7e7868da4b46384d7e56ca33a3750b195 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
/*
Copyright (c) 2010-2012 250bpm s.r.o.
Copyright (c) 2010-2011 Other contributors as noted in the AUTHORS file
This file is part of Crossroads project.
Crossroads is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Crossroads is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "poller_base.hpp"
#include "err.hpp"
#include "select.hpp"
#include "poll.hpp"
#include "epoll.hpp"
#include "devpoll.hpp"
#include "kqueue.hpp"
xs::poller_base_t *xs::poller_base_t::create ()
{
poller_base_t *result;
#if defined XS_HAVE_SELECT
result = new (std::nothrow) select_t;
#elif defined XS_HAVE_POLL
result = new (std::nothrow) poll_t;
#elif defined XS_HAVE_EPOLL
result = new (std::nothrow) epoll_t;
#elif defined XS_HAVE_DEVPOLL
result = new (std::nothrow) devpoll_t;
#elif defined XS_HAVE_KQUEUE
result = new (std::nothrow) kqueue_t;
#endif
alloc_assert (result);
return result;
}
xs::poller_base_t::poller_base_t ()
{
}
xs::poller_base_t::~poller_base_t ()
{
// Make sure there is no more load on the shutdown.
xs_assert (get_load () == 0);
}
int xs::poller_base_t::get_load ()
{
return load.get ();
}
void xs::poller_base_t::adjust_load (int amount_)
{
if (amount_ > 0)
load.add (amount_);
else if (amount_ < 0)
load.sub (-amount_);
}
xs::handle_t xs::poller_base_t::add_timer (int timeout_, i_poll_events *sink_)
{
uint64_t expiration = clock.now_ms () + timeout_;
timer_info_t info = {sink_, timers_t::iterator ()};
timers_t::iterator it = timers.insert (
timers_t::value_type (expiration, info));
it->second.self = it;
return (handle_t) &(it->second);
}
void xs::poller_base_t::rm_timer (handle_t handle_)
{
timer_info_t *info = (timer_info_t*) handle_;
timers.erase (info->self);
}
uint64_t xs::poller_base_t::execute_timers ()
{
// Fast track.
if (timers.empty ())
return 0;
// Get the current time.
uint64_t current = clock.now_ms ();
// Execute the timers that are already due.
timers_t::iterator it = timers.begin ();
while (it != timers.end ()) {
// If we have to wait to execute the item, same will be true about
// all the following items (multimap is sorted). Thus we can stop
// checking the subsequent timers and return the time to wait for
// the next timer (at least 1ms).
if (it->first > current)
return it->first - current;
// Trigger the timer.
it->second.sink->timer_event ((handle_t) &it->second);
// Remove it from the list of active timers.
timers_t::iterator o = it;
++it;
timers.erase (o);
}
// There are no more timers.
return 0;
}
|