2 .TH sel 3 "22 May 1999" "Straylight/Edgeware" "mLib utilities library"
4 sel \- low level interface for waiting for I/O
18 .B "#include <mLib/sel.h>"
23 .B " SEL_WRITE = ...,"
28 .B "typedef struct { ...\& } sel_state;"
29 .B "typedef struct { ...\& } sel_timer;"
30 .B "typedef struct { ...\& } sel_hook;"
39 .B " fd_set fd[SEL_MODES];"
40 .B " struct timeval tv, *tvp;"
41 .B " struct timeval now;"
44 .BI "typedef void (*sel_hookfn)(sel_state *" s ", sel_args *" a ", void *" p );
46 .BI "void sel_init(sel_state *" s );
48 .ta \w'\fBvoid sel_initfile('u
49 .BI "void sel_initfile(sel_state *" s ", sel_file *" f ,
50 .BI " int " fd ", unsigned " mode ,
51 .BI " void (*" func ")(int " fd ", unsigned " mode ", void *" p ),
53 .BI "void sel_addfile(sel_file *" f );
54 .BI "void sel_force(sel_file *" f );
55 .BI "void sel_rmfile(sel_file *" f );
57 .ta \w'\fBvoid sel_addtimer('u
58 .BI "void sel_addtimer(sel_state *" s ", sel_timer *" t ,
59 .BI " struct timeval *" tv ,
60 .BI " void (*" func ")(struct timeval *" tv ", void *" p ),
62 .BI "void sel_rmtimer(sel_timer *" t );
64 .ta \w'\fBvoid sel_addhook('u
65 .BI "void sel_addtimer(sel_state *" s ", sel_hook *" h ,
66 .BI " sel_hookfn " before ", sel_hookfn " after ,
68 .BI "void sel_rmhook(sel_hook *" h );
70 .BI "int sel_fdmerge(fd_set *" dest ", fd_set *" fd ", int " maxfd );
72 .BI "int sel_select(sel_state *" s );
77 subsystem provides a structured way of handling I/O in a non-blocking
78 event-driven sort of a way, for single-threaded programs. (Although
79 there's no reason at all why multithreaded programs shouldn't use
81 it's much less useful.)
85 subsystem does no memory allocation, and has no static state. All
86 of its data is stored in structures allocated by the caller. I'll
87 explain how this fits in nicely with typical calling sequences below.
89 Although all the data structures are exposed in the header file, you
92 data structures to be opaque except where described here, and not fiddle
93 around inside them. Some things may become more sophisticated later.
94 .SH "IMPORTANT CONCEPTS"
95 The system is based around two concepts:
102 is interested in some sort of I/O event, which might be something like
103 `my socket has become readable', or `the time is now half past three on
104 the third of June 2013'. It has a handler function attached to it,
105 which is called when the appropriate event occurs. Some events happen
106 once only ever; some events happen over and over again. For example, a
107 socket might become readable many times, but it's only half-past three
108 on the third of June 2013 once.
110 When a selector is initialized, the caller describes the event the
111 selector is interested in, and specifies which function should handle
112 the event. Also, it must specify an arbitrary pointer which is passed
113 to the handler function when the event occurs. This is typically some
114 sort of pointer to instance data of some kind, providing more
115 information about the event (`it's
117 socket that's become readable'), or what to do about it.
119 A multiplexor gathers information about who's interested in what. It
120 maintains lists of selectors. Selectors must be added to a
121 mulitplexor before the events they're interested in are actually watched
122 for. Selectors can be removed again when their events aren't
123 interesting any more. Apart from adding and removing selectors, you can
125 on a multiplexor. This waits for something interesting to happen and
126 then fires off all the selectors which have events waiting for them.
128 You can have lots of multiplexors in your program if you like. You can
129 only ask for events from one of them at a time, though.
131 There are currently two types of selector understood by the low-level
133 system: file selectors and timer selectors. These two types of
134 selectors react to corresponding different types of events. A file
135 event indicates that a file is now ready for reading or writing. A
136 timer event indicates that a particular time has now passed (useful for
137 implementing timeouts). More sophisticated selectors can be constructed
140 interface. For examples, see
144 .SH "PROGRAMMING INTERFACE"
146 A multiplexor is represented using the type
150 header file. Before use, a
152 must be initialized, by passing it to the
154 function. The header file talks about `state blocks' a lot \- that's
155 because it was written before I thought the word `multiplexor' was
158 File selectors are represented by the type
160 The interface provides three operations on file selectors:
161 initialization, addition to multiplexor, and removal from a
162 multiplexor. It's convenient to separate addition and removal from
163 initialization because file selectors often get added and removed many
164 times over during their lifetimes.
166 A file selector is initialized by the
168 function. This requires a large number of arguments:
171 A pointer to the multiplexor with which the file selector will be
172 associated. This is stored in the selector so that the multiplexor
173 argument can be omitted from later calls.
176 Pointer to the file selector object to be initialized.
179 The file descriptor which the selector is meant to watch.
182 A constant describing which condition the selector is interested in.
183 This must be one of the
185 constants described below.
187 .BI "void (*" func ")(int " fd ", unsigned " mode ", void *" p );
188 The handler function which is called when the appropriate condition
189 occurs on the file. This function's interface is described in more
193 An arbitrary pointer argument passed to
195 when it's called. Beyond this, no meaning is attached to the value of
196 the pointer. If you don't care about it, just leave it as null.
198 The mode argument is one of the following constants:
201 Raise an event when the file is ready to be read from.
204 Raise an event when the file is ready to be written to.
207 Raise an event when the file has an `exceptional condition'.
211 contains the number of possible file modes. This is useful internally
212 for allocating arrays of the right size.
218 perform the addition and removal operations on file selectors. They are
219 passed only the actual selector object, since the selector already knows
220 which multiplexor it's associated with. A newly initialized file
221 selector is not added to its multiplexor: this must be done explicitly.
223 The handler function for a file multiplexor is passed three arguments:
224 the file descriptor for the file, a mode argument which describes the
225 file's new condition, and the pointer argument set up at initialization
230 will sometimes be useful while a
232 call (see below) is in progress. It marks a file selector as being
233 ready even if it's not really. This is most useful when dynamically
234 adding a write selector: it's likely that the write will succeed
235 immediately, so it's worth trying. This will only work properly if
236 the write is non-blocking.
242 structure is exported. It contains the file descriptor in which the
243 selector is interested. You may not modify this value, but it's useful
244 to be able to read it out \- it saves having to keep a copy.
245 .SS "Timer selectors"
246 Timer selectors are simpler. There are only two operations provided on
247 timer selectors: addition and removal. Initialization is performed as
248 part of the addition operation.
250 A timer selector is represented by an object of time
255 requires lots of arguments:
258 Pointer to the multiplexor to which the selector is to be added.
261 Pointer to the timer selector object being initialized and added.
263 .BI "struct timeval " tv
264 When the selector should raise its event. This is an
266 time, not a relative time as required by the traditional
272 .BI "void (*" func ")(struct timeval *" tv ", void *" p )
273 A handler function to be called when the event occurs. The function is
276 time, and the arbitrary pointer passed to
285 when the timer event occurs. Beyond this, the value of the pointer is
290 removes a timer selector. It is passed only the selector object.
292 Note that timer events are a one-shot thing. Once they've happened, the
293 timer selector is removed and the event can't happen again. This is
294 normally what you want. Removing a timer is only useful (or safe!)
295 before the timer event has been sent.
297 Finally, the function
299 is passed a multiplexor object. It waits for something interesting to
300 happen, informs the appropriate selector handlers, and returns. If
301 everything went according to plan,
303 returns zero. Otherwise it returns \-1, and the global variable
305 is set appropriately.
307 In order to interface other I/O multiplexing systems to this one, it's
310 functions which are called before and after each
316 registers a pair of hook functions. It is passed the pointer to the
317 multiplexor which is being hooked, the address of a
319 structure which will be used to record the hook information, the two
320 hook functions (either of which may be a null pointer, signifying no
321 action to be taken), and a pointer argument to be passed to the hook
326 removes a pair of hooks given the address of the
328 structure which recorded their registration.
332 is passed three arguments:
335 A pointer to the multiplexor block. This probably isn't very useful,
339 A pointer to a block containing proposed arguments for, or results from,
341 The format of this block is described below.
344 A pointer argument set up in the call to
346 to provide the hook function with some context.
348 The argument block contains the following members:
351 One greater than the highest-numbered file descriptor to be examined.
352 This may need to be modified if the file descriptor sets are altered.
354 .B "fd_set fd[SEL_MODES]"
355 A file descriptor set for each of
362 call, these may be modified to register an interest in other file
363 descriptors. Afterwards, they may be examined to decide which file
364 descriptors are active.
366 .B "struct timeval tv, *tvp"
369 call, these specify the time after which to return even if no files are
372 is null, there is no timeout, and
374 should wait forever if necessary. Otherwise
376 should contain the address of
380 should contain the timeout. After the
382 call, the contents of
386 .B "struct timeval now"
389 call, contains the current time. After the call, this will have been
390 updated to reflect the new current time only if there was a timeout
393 Hook functions may find the call
395 useful. Given two file descriptor sets
399 and a possibly overestimated highest file descriptor in
403 all of the descriptors set in
405 and returns an accurate file descriptor count as its result.
407 Although the naming seems to suggest that this is all
408 based around the BSD-ish
410 system call (and indeed it is), the interface is actually a good deal
411 more general than that. An implementation which worked off System V-ish
413 instead would be possible to make, and would look just the same from the
414 outside. Some work would be needed to make the hook functions work,
423 Mark Wooding, <mdw@distorted.org.uk>