1 # Copyright (c) 2010 Nicira Networks
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:
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
21 """High-level wrapper around the "poll" system call.
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."""
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.
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.
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)
48 def __timer_wait(self, msec):
49 if self.timeout < 0 or msec < self.timeout:
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.
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."""
63 self.__timer_wait(msec)
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.
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()
78 self.__timer_wait(msec - now)
80 def immediate_wake(self):
81 """Causes the following call to self.block() to wake up immediately,
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
92 events = self.poll.poll(self.timeout)
93 self.__log_wakeup(events)
94 except select.error, e:
97 if error != errno.EINTR:
98 logging.error("poll: %s" % e[1])
102 def __log_wakeup(self, events):
104 logging.debug("%d-ms timeout" % self.timeout)
106 for fd, revents in events:
109 if revents & select.POLLIN:
111 if revents & select.POLLOUT:
113 if revents & select.POLLERR:
115 if revents & select.POLLHUP:
117 if revents & select.POLLNVAL:
119 logging.debug("%s on fd %d" % (s, fd))
122 self.poll = select.poll()