X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/d2a91066c99aebac0e651b09ffd699b17f156f94..9b5ac6ff2ef1b71e7ec53c756cad37b1844b9d1e:/man/sel.3 diff --git a/man/sel.3 b/man/sel.3 index 1452347..de62ed1 100644 --- a/man/sel.3 +++ b/man/sel.3 @@ -1,13 +1,17 @@ .\" -*-nroff-*- -.TH sel 3 "22 May 1999" mLib +.TH sel 3 "22 May 1999" "Straylight/Edgeware" "mLib utilities library" .SH NAME sel \- low level interface for waiting for I/O .\" @sel_init .\" @sel_initfile .\" @sel_addfile +.\" @sel_force .\" @sel_rmfile .\" @sel_addtimer .\" @sel_rmtimer +.\" @sel_addhook +.\" @sel_rmhook +.\" @sel_fdmerge .\" @sel_select .SH SYNOPSIS .nf @@ -20,13 +24,22 @@ sel \- low level interface for waiting for I/O .BI " void (*" func ")(int " fd ", unsigned " mode ", void *" p ), .BI " void *" p ); .BI "void sel_addfile(sel_file *" f ); +.BI "void sel_force(sel_file *" f ); .BI "void sel_rmfile(sel_file *" f ); .BI "void sel_addtimer(sel_state *" s ", sel_timer *" t , .BI " struct timeval *" tv , .BI " void (*" func ")(struct timeval *" tv ", void *" p ), +.BI " void *" p ); .BI "void sel_rmtimer(sel_timer *" t ); +.BI "void sel_addhook(sel_state *" s ", sel_hook *" h , +.BI " sel_hookfn " before ", sel_hookfn " after , +.BI " void *" p ); +.BI "void sel_rmhook(sel_hook *" h ); + +.BI "int sel_fdmerge(fd_set *" dest ", fd_set *" fd ", int " maxfd ); + .BI "int sel_select(sel_state *" s ); .fi .SH "OVERVIEW" @@ -100,6 +113,7 @@ interface. For examples, see and .BR conn (3). .SH "PROGRAMMING INTERFACE" +.SS "Multiplexors" A multiplexor is represented using the type .B sel_state defined in the @@ -119,34 +133,34 @@ initialization, addition to multiplexor, and removal from a multiplexor. It's convenient to separate addition and removal from initialization because file selectors often get added and removed many times over during their lifetimes. -.PP +.SS "File selectors" A file selector is initialized by the .B sel_initfile function. This requires a large number of arguments: .TP -.I s +.BI "sel_state *" s A pointer to the multiplexor with which the file selector will be associated. This is stored in the selector so that the multiplexor argument can be omitted from later calls. .TP -.I f +.BI "sel_file *" f Pointer to the file selector object to be initialized. .TP -.I fd +.BI "int " fd The file descriptor which the selector is meant to watch. .TP -.I mode +.BI "unsigned " mode A constant describing which condition the selector is interested in. This must be one of the .B SEL_ constants described below. .TP -.I func +.BI "void (*" func ")(int " fd ", unsigned " mode ", void *" p ); The handler function which is called when the appropriate condition occurs on the file. This function's interface is described in more detail below. .TP -.I p +.BI "void *" p An arbitrary pointer argument passed to .I func when it's called. Beyond this, no meaning is attached to the value of @@ -182,6 +196,16 @@ the file descriptor for the file, a mode argument which describes the file's new condition, and the pointer argument set up at initialization time. .PP +The function +.B sel_force +will sometimes be useful while a +.B sel_select +call (see below) is in progress. It marks a file selector as being +ready even if it's not really. This is most useful when dynamically +adding a write selector: it's likely that the write will succeed +immediately, so it's worth trying. This will only work properly if +the write is non-blocking. +.PP The member .B fd of the @@ -189,7 +213,7 @@ of the structure is exported. It contains the file descriptor in which the selector is interested. You may not modify this value, but it's useful to be able to read it out \- it saves having to keep a copy. -.PP +.SS "Timer selectors" Timer selectors are simpler. There are only two operations provided on timer selectors: addition and removal. Initialization is performed as part of the addition operation. @@ -201,16 +225,14 @@ The function .B sel_addtimer requires lots of arguments: .TP -.I s +.BI "sel_state *" s Pointer to the multiplexor to which the selector is to be added. .TP -.I t +.BI "sel_timer *" t Pointer to the timer selector object being initialized and added. .TP -.I tv -A -.B "struct timeval" -object describing when the selector should raise its event. This is an +.BI "struct timeval " tv +When the selector should raise its event. This is an .I absolute time, not a relative time as required by the traditional .BR select (2) @@ -218,7 +240,7 @@ and .BR poll (2) system calls. .TP -.I func +.BI "void (*" func ")(struct timeval *" tv ", void *" p ) A handler function to be called when the event occurs. The function is passed the .I current @@ -228,7 +250,7 @@ as the .I p argument. .TP -.I p +.BI "void *" p A pointer passed to .I func when the timer event occurs. Beyond this, the value of the pointer is @@ -242,16 +264,116 @@ Note that timer events are a one-shot thing. Once they've happened, the timer selector is removed and the event can't happen again. This is normally what you want. Removing a timer is only useful (or safe!) before the timer event has been sent. -.PP +.SS "Performing I/O" Finally, the function .B sel_select is passed a multiplexor object. It waits for something interesting to happen, informs the appropriate selector handlers, and returns. If everything went according to plan, .B sel_select -returns zero. Otherwise it returns -1, and the global variable +returns zero. Otherwise it returns \-1, and the global variable .B errno is set appropriately. +.SS "Hook functions" +In order to interface other I/O multiplexing systems to this one, it's +possible to register +.I hook +functions which are called before and after each +.BR select (2) +system call. +.PP +The function +.B sel_addhook +registers a pair of hook functions. It is passed the pointer to the +multiplexor which is being hooked, the address of a +.B sel_hook +structure which will be used to record the hook information, the two +hook functions (either of which may be a null pointer, signifying no +action to be taken), and a pointer argument to be passed to the hook +functions. +.PP +The function +.B sel_rmhook +removes a pair of hooks given the address of the +.B sel_hook +structure which recorded their registration. +.PP +A +.I "hook function" +is passed three arguments: +.TP +.BI "sel_state *" s +A pointer to the multiplexor block. This probably isn't very useful, +actually. +.TP +.BI "sel_args *" a +A pointer to a block containing proposed arguments for, or results from, +.BR select (2). +The format of this block is described below. +.TP +.BI "void *" p +A pointer argument set up in the call to +.B sel_addhook +to provide the hook function with some context. +.PP +The argument block contains the following members: +.TP +.B "int maxfd" +One greater than the highest-numbered file descriptor to be examined. +This may need to be modified if the file descriptor sets are altered. +.TP +.B "fd_set fd[SEL_MODES]" +A file descriptor set for each of +.BR SEL_READ , +.B SEL_WRITE +and +.BR SEL_EXC . +Before the +.B select +call, these may be modified to register an interest in other file +descriptors. Afterwards, they may be examined to decide which file +descriptors are active. +.TP +.B "struct timeval tv, *tvp" +Before the +.B select +call, these specify the time after which to return even if no files are +active. If +.B tvp +is null, there is no timeout, and +.B select +should wait forever if necessary. Otherwise +.B tvp +should contain the address of +.BR tv , +and +.B tv +should contain the timeout. After the +.B select +call, the contents of +.B tv +are undefined. +.TP +.B "struct timeval now" +Before the +.B select +call, contains the current time. After the call, this will have been +updated to reflect the new current time only if there was a timeout +set before the call. +.PP +Hook functions may find the call +.B sel_fdmerge +useful. Given two file descriptor sets +.I dest +and +.IR fd , +and a possibly overestimated highest file descriptor in +.IR fd , +the function sets in +.I dest +all of the descriptors set in +.I fd +and returns an accurate file descriptor count as its result. .SH "OTHER NOTES" Although the naming seems to suggest that this is all based around the BSD-ish @@ -259,11 +381,14 @@ based around the BSD-ish system call (and indeed it is), the interface is actually a good deal more general than that. An implementation which worked off System V-ish .BR poll (2) -instead would be fairly trivial to make, and would look just the same -from the outside. +instead would be possible to make, and would look just the same from the +outside. Some work would be needed to make the hook functions work, +though. .SH "SEE ALSO" .BR select (2), .BR poll (2), +.BR conn (3), +.BR selbuf (3), .BR mLib (3). .SH AUTHOR -Mark Wooding, +Mark Wooding,