3 * \brief Waitset and low-level event handling mechanism
7 * Copyright (c) 2009, 2010, 2012, ETH Zurich.
10 * This file is distributed under the terms in the attached LICENSE file.
11 * If you do not find this file, copies can be found by writing to:
12 * ETH Zurich D-INFK, CAB F.78, Universitaetstr. 6, CH-8092 Zurich,
13 * Attn: Systems Group.
16 #ifndef BARRELFISH_WAITSET_H
17 #define BARRELFISH_WAITSET_H
19 #include <barrelfish/types.h>
20 #include <errors/errno.h>
21 #include <sys/cdefs.h>
22 #include <barrelfish/dispatch.h>
26 #include <errors/errno.h>
29 #include <barrelfish/types.h>
34 extern cycles_t waitset_poll_cycles;
36 struct event_closure {
37 void (*handler)(void *arg);
41 #define MKCLOSURE(h,a) (struct event_closure){ /*handler*/ (h), /*arg*/ (a) }
42 #define NOP_CLOSURE MKCLOSURE(NULL, NULL)
48 * This is used for debugging and to determine the function to call when polling.
54 CHANTYPE_DEFERRED, ///< Timer events
63 /// Current state of a channel on a specific waitset
65 CHAN_UNREGISTERED, ///< Initialised, but not yet registered on a waitset
66 CHAN_IDLE, ///< Has a registered event handler, but the event has not fired
67 CHAN_POLLED, ///< Idle and polled. Channel implementation must be called to check for pending events
68 CHAN_PENDING, ///< Has a pending event waiting to be delivered
69 CHAN_WAITING ///< There's no registered event handler (for now)
73 * \brief Per-channel state belonging to waitset
75 * This data is logically private to the waitset, but is allocated and stored
76 * inside the channels themselves.
78 struct waitset_chanstate {
79 struct waitset_chanstate *next, *prev; ///< Next/prev channel in queue
80 struct waitset *waitset; ///< Waitset in which this channel is registered
81 struct event_closure closure; ///< Event closure to run when channel is ready
82 enum ws_chantype chantype; ///< Channel type
83 enum ws_chanstate state; ///< Channel event state
85 uint32_t token; ///< Token of an event
86 bool persistent; ///< Channel should be always registered
87 struct waitset_chanstate *polled_next, *polled_prev; ///< Dispatcher's polled queue
88 struct thread *wait_for; ///< Thread waiting for this event
95 * This data is private to the waitset (a waitset is an opaque type),
96 * but defined in the header for allocation purposes.
99 struct waitset_chanstate *pending, ///< Channels with pending events
100 *polled, ///< Channels that need to be polled
101 *idle, ///< All other channels on this waitset
102 *waiting; ///< Channels waiting for an event handler registration
104 /// Queue of threads blocked on this waitset (when no events are pending)
105 struct thread *waiting_threads;
108 void poll_channels_disabled(dispatcher_handle_t handle);
110 void waitset_init(struct waitset *ws);
111 errval_t waitset_destroy(struct waitset *ws);
113 errval_t get_next_event(struct waitset *ws, struct event_closure *retclosure);
114 errval_t get_next_event_disabled(struct waitset *ws, struct waitset_chanstate **retchan,
115 struct event_closure *retclosure, struct waitset_chanstate *waitfor, dispatcher_handle_t handle, bool debug);
116 errval_t check_for_event(struct waitset *ws);
117 errval_t event_dispatch(struct waitset *ws);
118 errval_t wait_for_channel(struct waitset *ws, struct waitset_chanstate *channel, errval_t *error_var);
119 errval_t event_dispatch_disabled(struct waitset *ws, dispatcher_handle_t handle);
120 errval_t event_dispatch_debug(struct waitset *ws);
121 errval_t event_dispatch_non_block(struct waitset *ws);
125 #endif // BARRELFISH_WAITSET_H