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.
20 """High-level wrapper around the "poll" system call.
22 Intended usage is for the program's main loop to go about its business
23 servicing whatever events it needs to. Then, when it runs out of immediate
24 tasks, it calls each subordinate module or object's "wait" function, which
25 in turn calls one (or more) of the functions Poller.fd_wait(),
26 Poller.immediate_wake(), and Poller.timer_wait() to register to be awakened
27 when the appropriate event occurs. Then the main loop calls
28 Poller.block(), which blocks until one of the registered events happens."""
33 def fd_wait(self, fd, events):
34 """Registers 'fd' as waiting for the specified 'events' (which should
35 be select.POLLIN or select.POLLOUT or their bitwise-OR). The following
36 call to self.block() will wake up when 'fd' becomes ready for one or
37 more of the requested events.
39 The event registration is one-shot: only the following call to
40 self.block() is affected. The event will need to be re-registered
41 after self.block() is called if it is to persist.
43 'fd' may be an integer file descriptor or an object with a fileno()
44 method that returns an integer file descriptor."""
45 self.poll.register(fd, events)
47 def __timer_wait(self, msec):
48 if self.timeout < 0 or msec < self.timeout:
51 def timer_wait(self, msec):
52 """Causes the following call to self.block() to block for no more than
53 'msec' milliseconds. If 'msec' is nonpositive, the following call to
54 self.block() will not block at all.
56 The timer registration is one-shot: only the following call to
57 self.block() is affected. The timer will need to be re-registered
58 after self.block() is called if it is to persist."""
62 self.__timer_wait(msec)
64 def timer_wait_until(self, msec):
65 """Causes the following call to self.block() to wake up when the
66 current time, as returned by Time.msec(), reaches 'msec' or later. If
67 'msec' is earlier than the current time, the following call to
68 self.block() will not block at all.
70 The timer registration is one-shot: only the following call to
71 self.block() is affected. The timer will need to be re-registered
72 after self.block() is called if it is to persist."""
77 self.__timer_wait(msec - now)
79 def immediate_wake(self):
80 """Causes the following call to self.block() to wake up immediately,
85 """Blocks until one or more of the events registered with
86 self.fd_wait() occurs, or until the minimum duration registered with
87 self.timer_wait() elapses, or not at all if self.immediate_wake() has
91 events = self.poll.poll(self.timeout)
92 self.__log_wakeup(events)
93 except select.error, e:
96 if error != errno.EINTR:
97 logging.error("poll: %s" % e[1])
101 def __log_wakeup(self, events):
103 logging.debug("%d-ms timeout" % self.timeout)
105 for fd, revents in events:
108 if revents & select.POLLIN:
110 if revents & select.POLLOUT:
112 if revents & select.POLLERR:
114 if revents & select.POLLHUP:
116 if revents & select.POLLNVAL:
118 logging.debug("%s on fd %d" % (s, fd))
121 self.poll = select.poll()