1 |
/* fdwatch.h - header file for fdwatch package |
2 |
** |
3 |
** This package abstracts the use of the select() and/or poll() system |
4 |
** calls. The basic function of these calls is to watch a set of |
5 |
** file descriptors for activity. select() originated in the BSD world, |
6 |
** while poll() came from SysV land, and their interfaces are somewhat |
7 |
** different. fdwatch lets you write your code to a single interface, |
8 |
** with the portability differences hidden inside the package. |
9 |
** |
10 |
** Furthermore, if your system implements both select() and poll(), |
11 |
** then fdwatch will use whichever call is most advantageous. It may |
12 |
** actually switch back and forth at runtime, as the workload changes. |
13 |
** |
14 |
** Usage is fairly simple. Call fdwatch_get_nfiles() to initialize |
15 |
** the package and find out how many fine descriptors are available. |
16 |
** Then each time through your main loop, call fdwatch_clear(), then |
17 |
** fdwatch_add_fd() for each of the descriptors you want to watch, |
18 |
** then call fdwatch() to actually perform the watch. After it returns |
19 |
** you can check which descriptors are ready via fdwatch_check_fd(). |
20 |
** |
21 |
** If your descriptor set hasn't changed from the last time through |
22 |
** the loop, you can skip calling fdwatch_clear() and fdwatch_add_fd() |
23 |
** to save a little CPU time. |
24 |
** |
25 |
** |
26 |
** Copyright © 1999 by Jef Poskanzer <jef@acme.com>. |
27 |
** All rights reserved. |
28 |
** |
29 |
** Redistribution and use in source and binary forms, with or without |
30 |
** modification, are permitted provided that the following conditions |
31 |
** are met: |
32 |
** 1. Redistributions of source code must retain the above copyright |
33 |
** notice, this list of conditions and the following disclaimer. |
34 |
** 2. Redistributions in binary form must reproduce the above copyright |
35 |
** notice, this list of conditions and the following disclaimer in the |
36 |
** documentation and/or other materials provided with the distribution. |
37 |
** |
38 |
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
39 |
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
40 |
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
41 |
** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
42 |
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
43 |
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
44 |
** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
45 |
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
46 |
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
47 |
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
48 |
** SUCH DAMAGE. |
49 |
*/ |
50 |
|
51 |
#ifndef _FDWATCH_H_ |
52 |
#define _FDWATCH_H_ |
53 |
|
54 |
#define FDW_READ 0 |
55 |
#define FDW_WRITE 1 |
56 |
|
57 |
#ifndef INFTIM |
58 |
#define INFTIM -1 |
59 |
#endif /* INFTIM */ |
60 |
|
61 |
/* Figure out how many file descriptors the system allows, and |
62 |
** initialize the fdwatch data structures. Returns -1 on failure. |
63 |
*/ |
64 |
extern int fdwatch_get_nfiles( void ); |
65 |
|
66 |
/* Add a descriptor to the watch list. rw is either FDW_READ or FDW_WRITE. */ |
67 |
extern void fdwatch_add_fd( int fd, void* client_data, int rw ); |
68 |
|
69 |
/* Delete a descriptor from the watch list. */ |
70 |
extern void fdwatch_del_fd( int fd ); |
71 |
|
72 |
/* Do the watch. Return value is the number of descriptors that are ready, |
73 |
** or 0 if the timeout expired, or -1 on errors. A timeout of INFTIM means |
74 |
** wait indefinitely. |
75 |
*/ |
76 |
extern int fdwatch( long timeout_msecs ); |
77 |
|
78 |
/* Check if a descriptor was ready. */ |
79 |
extern int fdwatch_check_fd( int fd ); |
80 |
|
81 |
/* Get the client data for an event. The argument is an index into the |
82 |
** set of ready descriptors returned by fdwatch(). |
83 |
*/ |
84 |
extern void* fdwatch_get_client_data( int ridx ); |
85 |
|
86 |
/* Generate debugging statistics syslog message. */ |
87 |
extern void fdwatch_logstats( long secs ); |
88 |
|
89 |
#endif /* _FDWATCH_H_ */ |