Merge branch 'mainstream'
[sliver-openvswitch.git] / lib / poll-loop.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2013 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /* High-level wrapper around the "poll" system call.
18  *
19  * The intended usage is for each thread's main loop to go about its business
20  * servicing whatever events it needs to.  Then, when it runs out of immediate
21  * tasks, it calls each subordinate module's "wait" function, which in turn
22  * calls one (or more) of the functions poll_fd_wait(), poll_immediate_wake(),
23  * and poll_timer_wait() to register to be awakened when the appropriate event
24  * occurs.  Then the main loop calls poll_block(), which blocks until one of
25  * the registered events happens.
26  *
27  *
28  * Thread-safety
29  * =============
30  *
31  * The poll set is per-thread, so all functions in this module are thread-safe.
32  */
33 #ifndef POLL_LOOP_H
34 #define POLL_LOOP_H 1
35
36 #include <poll.h>
37 #include "util.h"
38
39 #ifdef  __cplusplus
40 extern "C" {
41 #endif
42
43
44 /* Schedule events to wake up the following poll_block().
45  *
46  * The poll_loop logs the 'where' argument to each function at "debug" level
47  * when an event causes a wakeup.  Ordinarily, it is automatically filled in
48  * with the location in the source of the call, and the caller should therefore
49  * omit it.  But, if the function you are implementing is very generic, so that
50  * its location in the source would not be very helpful for debugging, you can
51  * avoid the macro expansion and pass a different argument, e.g.:
52  *      (poll_fd_wait)(fd, events, where);
53  * See timer_wait() for an example.
54  */
55 void poll_fd_wait(int fd, short int events, const char *where);
56 #define poll_fd_wait(fd, events) poll_fd_wait(fd, events, SOURCE_LOCATOR)
57
58 void poll_timer_wait(long long int msec, const char *where);
59 #define poll_timer_wait(msec) poll_timer_wait(msec, SOURCE_LOCATOR)
60
61 void poll_timer_wait_until(long long int msec, const char *where);
62 #define poll_timer_wait_until(msec) poll_timer_wait_until(msec, SOURCE_LOCATOR)
63
64 void poll_immediate_wake(const char *where);
65 #define poll_immediate_wake() poll_immediate_wake(SOURCE_LOCATOR)
66
67 /* Wait until an event occurs. */
68 void poll_block(void);
69
70 #ifdef  __cplusplus
71 }
72 #endif
73
74 #endif /* poll-loop.h */