python: timer_wait_until calculated current time incorrectly
[sliver-openvswitch.git] / python / ovs / poller.py
1 # Copyright (c) 2010 Nicira Networks
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import errno
16 import logging
17 import select
18 import ovs.timeval
19
20 class Poller(object):
21     """High-level wrapper around the "poll" system call.
22
23     Intended usage is for the program's main loop to go about its business
24     servicing whatever events it needs to.  Then, when it runs out of immediate
25     tasks, it calls each subordinate module or object's "wait" function, which
26     in turn calls one (or more) of the functions Poller.fd_wait(),
27     Poller.immediate_wake(), and Poller.timer_wait() to register to be awakened
28     when the appropriate event occurs.  Then the main loop calls
29     Poller.block(), which blocks until one of the registered events happens."""
30
31     def __init__(self):
32         self.__reset()
33
34     def fd_wait(self, fd, events):
35         """Registers 'fd' as waiting for the specified 'events' (which should
36         be select.POLLIN or select.POLLOUT or their bitwise-OR).  The following
37         call to self.block() will wake up when 'fd' becomes ready for one or
38         more of the requested events.
39         
40         The event registration is one-shot: only the following call to
41         self.block() is affected.  The event will need to be re-registered
42         after self.block() is called if it is to persist.
43
44         'fd' may be an integer file descriptor or an object with a fileno()
45         method that returns an integer file descriptor."""
46         self.poll.register(fd, events)
47
48     def __timer_wait(self, msec):
49         if self.timeout < 0 or msec < self.timeout:
50             self.timeout = msec
51
52     def timer_wait(self, msec):
53         """Causes the following call to self.block() to block for no more than
54         'msec' milliseconds.  If 'msec' is nonpositive, the following call to
55         self.block() will not block at all.
56
57         The timer registration is one-shot: only the following call to
58         self.block() is affected.  The timer will need to be re-registered
59         after self.block() is called if it is to persist."""
60         if msec <= 0:
61             self.immediate_wake()
62         else:
63             self.__timer_wait(msec)
64
65     def timer_wait_until(self, msec):
66         """Causes the following call to self.block() to wake up when the current
67         time, as returned by ovs.timeval.msec(), reaches 'msec' or later.  If
68         'msec' is earlier than the current time, the following call to
69         self.block() will not block at all.
70
71         The timer registration is one-shot: only the following call to
72         self.block() is affected.  The timer will need to be re-registered
73         after self.block() is called if it is to persist."""
74         now = ovs.timeval.msec()
75         if msec <= now:
76             self.immediate_wake()
77         else:
78             self.__timer_wait(msec - now)
79
80     def immediate_wake(self):
81         """Causes the following call to self.block() to wake up immediately,
82         without blocking."""
83         self.timeout = 0
84
85     def block(self):
86         """Blocks until one or more of the events registered with
87         self.fd_wait() occurs, or until the minimum duration registered with
88         self.timer_wait() elapses, or not at all if self.immediate_wake() has
89         been called."""
90         try:
91             try:
92                 events = self.poll.poll(self.timeout)
93                 self.__log_wakeup(events)
94             except select.error, e:
95                 # XXX rate-limit
96                 error, msg = e
97                 if error != errno.EINTR:
98                     logging.error("poll: %s" % e[1])
99         finally:
100             self.__reset()
101
102     def __log_wakeup(self, events):
103         if not events:
104             logging.debug("%d-ms timeout" % self.timeout)
105         else:
106             for fd, revents in events:
107                 if revents != 0:
108                     s = ""
109                     if revents & select.POLLIN:
110                         s += "[POLLIN]"
111                     if revents & select.POLLOUT:
112                         s += "[POLLOUT]"
113                     if revents & select.POLLERR:
114                         s += "[POLLERR]"
115                     if revents & select.POLLHUP:
116                         s += "[POLLHUP]"
117                     if revents & select.POLLNVAL:
118                         s += "[POLLNVAL]"
119                     logging.debug("%s on fd %d" % (s, fd))
120
121     def __reset(self):
122         self.poll = select.poll()
123         self.timeout = -1            
124