eventlib_p.h revision 165258
172445Sassar/*
255682Smarkm * Copyright (c) 2005 by Internet Systems Consortium, Inc. ("ISC")
355682Smarkm * Copyright (c) 1995-1999 by Internet Software Consortium
455682Smarkm *
555682Smarkm * Permission to use, copy, modify, and distribute this software for any
655682Smarkm * purpose with or without fee is hereby granted, provided that the above
755682Smarkm * copyright notice and this permission notice appear in all copies.
855682Smarkm *
955682Smarkm * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
1055682Smarkm * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1155682Smarkm * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
1255682Smarkm * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1355682Smarkm * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1455682Smarkm * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
1555682Smarkm * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1655682Smarkm */
1755682Smarkm
1855682Smarkm/* eventlib_p.h - private interfaces for eventlib
1955682Smarkm * vix 09sep95 [initial]
2055682Smarkm *
2155682Smarkm * $Id: eventlib_p.h,v 1.3.2.1.4.4 2006/03/10 00:17:21 marka Exp $
2255682Smarkm * $FreeBSD: head/lib/libc/isc/eventlib_p.h 165258 2006-12-15 20:59:55Z ume $
2355682Smarkm */
2455682Smarkm
2555682Smarkm#ifndef _EVENTLIB_P_H
2655682Smarkm#define _EVENTLIB_P_H
2755682Smarkm
2855682Smarkm#include <sys/param.h>
2955682Smarkm#include <sys/types.h>
3055682Smarkm#include <sys/socket.h>
3172445Sassar#include <netinet/in.h>
3272445Sassar#include <sys/un.h>
3372445Sassar
3472445Sassar#define EVENTLIB_DEBUG 1
3555682Smarkm
3655682Smarkm#include <errno.h>
3755682Smarkm#include <fcntl.h>
3855682Smarkm#include <stdio.h>
3955682Smarkm#include <stdlib.h>
4055682Smarkm#include <string.h>
4155682Smarkm
4255682Smarkm#ifndef _LIBC
4355682Smarkm#include <isc/list.h>
4455682Smarkm#include <isc/heap.h>
4555682Smarkm#include <isc/memcluster.h>
4655682Smarkm#endif
4755682Smarkm
4855682Smarkm#define	EV_MASK_ALL	(EV_READ | EV_WRITE | EV_EXCEPT)
4955682Smarkm#define EV_ERR(e)		return (errno = (e), -1)
5055682Smarkm#define OK(x)		if ((x) < 0) EV_ERR(errno); else (void)NULL
5155682Smarkm#define OKFREE(x, y)	if ((x) < 0) { FREE((y)); EV_ERR(errno); } \
5255682Smarkm			else (void)NULL
5355682Smarkm
5455682Smarkm#define	NEW(p)		if (((p) = memget(sizeof *(p))) != NULL) \
5555682Smarkm				FILL(p); \
5655682Smarkm			else \
5755682Smarkm				(void)NULL;
5855682Smarkm#define OKNEW(p)	if (!((p) = memget(sizeof *(p)))) { \
5955682Smarkm				errno = ENOMEM; \
6055682Smarkm				return (-1); \
6155682Smarkm			} else \
62				FILL(p)
63#define FREE(p)		memput((p), sizeof *(p))
64
65#if EVENTLIB_DEBUG
66#define FILL(p)		memset((p), 0xF5, sizeof *(p))
67#else
68#define FILL(p)
69#endif
70
71#ifdef USE_POLL
72#ifdef HAVE_STROPTS_H
73#include <stropts.h>
74#endif
75#include <poll.h>
76#endif /* USE_POLL */
77
78typedef struct evConn {
79	evConnFunc	func;
80	void *		uap;
81	int		fd;
82	int		flags;
83#define EV_CONN_LISTEN		0x0001		/* Connection is a listener. */
84#define EV_CONN_SELECTED	0x0002		/* evSelectFD(conn->file). */
85#define EV_CONN_BLOCK		0x0004		/* Listener fd was blocking. */
86	evFileID	file;
87	struct evConn *	prev;
88	struct evConn *	next;
89} evConn;
90
91#ifndef _LIBC
92typedef struct evAccept {
93	int		fd;
94	union {
95		struct sockaddr		sa;
96		struct sockaddr_in	in;
97#ifndef NO_SOCKADDR_UN
98		struct sockaddr_un	un;
99#endif
100	}		la;
101	ISC_SOCKLEN_T	lalen;
102	union {
103		struct sockaddr		sa;
104		struct sockaddr_in	in;
105#ifndef NO_SOCKADDR_UN
106		struct sockaddr_un	un;
107#endif
108	}		ra;
109	ISC_SOCKLEN_T	ralen;
110	int		ioErrno;
111	evConn *	conn;
112	LINK(struct evAccept) link;
113} evAccept;
114
115typedef struct evFile {
116	evFileFunc	func;
117	void *		uap;
118	int		fd;
119	int		eventmask;
120	int		preemptive;
121	struct evFile *	prev;
122	struct evFile *	next;
123	struct evFile *	fdprev;
124	struct evFile *	fdnext;
125} evFile;
126
127typedef struct evStream {
128	evStreamFunc	func;
129	void *		uap;
130	evFileID	file;
131	evTimerID	timer;
132	int		flags;
133#define EV_STR_TIMEROK	0x0001	/* IFF timer valid. */
134	int		fd;
135	struct iovec *	iovOrig;
136	int		iovOrigCount;
137	struct iovec *	iovCur;
138	int		iovCurCount;
139	int		ioTotal;
140	int		ioDone;
141	int		ioErrno;
142	struct evStream	*prevDone, *nextDone;
143	struct evStream	*prev, *next;
144} evStream;
145
146typedef struct evTimer {
147	evTimerFunc	func;
148	void *		uap;
149	struct timespec	due, inter;
150	int		index;
151	int		mode;
152#define EV_TMR_RATE	1
153} evTimer;
154
155typedef struct evWait {
156	evWaitFunc	func;
157	void *		uap;
158	const void *	tag;
159	struct evWait *	next;
160} evWait;
161
162typedef struct evWaitList {
163	evWait *		first;
164	evWait *		last;
165	struct evWaitList *	prev;
166	struct evWaitList *	next;
167} evWaitList;
168
169typedef struct evEvent_p {
170	enum {  Accept, File, Stream, Timer, Wait, Free, Null  } type;
171	union {
172		struct {  evAccept *this;  }			accept;
173		struct {  evFile *this; int eventmask;  }	file;
174		struct {  evStream *this;  }			stream;
175		struct {  evTimer *this;  }			timer;
176		struct {  evWait *this;  }			wait;
177		struct {  struct evEvent_p *next;  }		free;
178		struct {  const void *placeholder;  }		null;
179	} u;
180} evEvent_p;
181#endif
182
183#ifdef USE_POLL
184typedef struct {
185	void		*ctx;	/* pointer to the evContext_p   */
186	uint32_t	type;	/* READ, WRITE, EXCEPT, nonblk  */
187	uint32_t	result;	/* 1 => revents, 0 => events    */
188} __evEmulMask;
189
190#define emulMaskInit(ctx, field, ev, lastnext) \
191	ctx->field.ctx = ctx; \
192	ctx->field.type = ev; \
193	ctx->field.result = lastnext;
194
195extern short	*__fd_eventfield(int fd, __evEmulMask *maskp);
196extern short	__poll_event(__evEmulMask *maskp);
197extern void		__fd_clr(int fd, __evEmulMask *maskp);
198extern void		__fd_set(int fd, __evEmulMask *maskp);
199
200#undef  FD_ZERO
201#define FD_ZERO(maskp)
202
203#undef  FD_SET
204#define FD_SET(fd, maskp) \
205	__fd_set(fd, maskp)
206
207#undef  FD_CLR
208#define FD_CLR(fd, maskp) \
209	__fd_clr(fd, maskp)
210
211#undef  FD_ISSET
212#define FD_ISSET(fd, maskp) \
213	((*__fd_eventfield(fd, maskp) & __poll_event(maskp)) != 0)
214
215#endif /* USE_POLL */
216
217#ifndef _LIBC
218typedef struct {
219	/* Global. */
220	const evEvent_p	*cur;
221	/* Debugging. */
222	int		debug;
223	FILE		*output;
224	/* Connections. */
225	evConn		*conns;
226	LIST(evAccept)	accepts;
227	/* Files. */
228	evFile		*files, *fdNext;
229#ifndef USE_POLL
230	fd_set		rdLast, rdNext;
231	fd_set		wrLast, wrNext;
232	fd_set		exLast, exNext;
233	fd_set		nonblockBefore;
234	int		fdMax, fdCount, highestFD;
235	evFile		*fdTable[FD_SETSIZE];
236#else
237	struct pollfd	*pollfds;	/* Allocated as needed  */
238	evFile		**fdTable;	/* Ditto                */
239	int		maxnfds;	/* # elements in above  */
240	int		firstfd;	/* First active fd      */
241	int		fdMax;		/* Last active fd       */
242	int		fdCount;	/* # fd:s with I/O      */
243	int		highestFD;	/* max fd allowed by OS */
244	__evEmulMask	rdLast, rdNext;
245	__evEmulMask	wrLast, wrNext;
246	__evEmulMask	exLast, exNext;
247	__evEmulMask	nonblockBefore;
248#endif /* USE_POLL */
249#ifdef EVENTLIB_TIME_CHECKS
250	struct timespec	lastSelectTime;
251	int		lastFdCount;
252#endif
253	/* Streams. */
254	evStream	*streams;
255	evStream	*strDone, *strLast;
256	/* Timers. */
257	struct timespec	lastEventTime;
258	heap_context	timers;
259	/* Waits. */
260	evWaitList	*waitLists;
261	evWaitList	waitDone;
262} evContext_p;
263
264/* eventlib.c */
265#define evPrintf __evPrintf
266void evPrintf(const evContext_p *ctx, int level, const char *fmt, ...)
267     ISC_FORMAT_PRINTF(3, 4);
268
269#ifdef USE_POLL
270extern int evPollfdRealloc(evContext_p *ctx, int pollfd_chunk_size, int fd);
271#endif /* USE_POLL */
272
273/* ev_timers.c */
274#define evCreateTimers __evCreateTimers
275heap_context evCreateTimers(const evContext_p *);
276#define evDestroyTimers __evDestroyTimers
277void evDestroyTimers(const evContext_p *);
278
279/* ev_waits.c */
280#define evFreeWait __evFreeWait
281evWait *evFreeWait(evContext_p *ctx, evWait *old);
282#endif
283
284/* Global options */
285#ifndef _LIBC
286extern int	__evOptMonoTime;
287#endif
288
289#endif /*_EVENTLIB_P_H*/
290