xref: /openbsd/lib/libevent/event.h (revision db3296cf)
1 /*	$OpenBSD: event.h,v 1.4 2002/07/12 18:50:48 provos Exp $	*/
2 
3 /*
4  * Copyright 2000-2002 Niels Provos <provos@citi.umich.edu>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Niels Provos.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 #ifndef _EVENT_H_
33 #define _EVENT_H_
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 #define EVLIST_TIMEOUT	0x01
40 #define EVLIST_INSERTED	0x02
41 #define EVLIST_SIGNAL	0x04
42 #define EVLIST_ACTIVE	0x08
43 #define EVLIST_INIT	0x80
44 
45 /* EVLIST_X_ Private space: 0x1000-0xf000 */
46 #define EVLIST_ALL	(0xf000 | 0x8f)
47 
48 #define EV_TIMEOUT	0x01
49 #define EV_READ		0x02
50 #define EV_WRITE	0x04
51 #define EV_SIGNAL	0x08
52 #define EV_PERSIST	0x10	/* Persistant event */
53 
54 /* Fix so that ppl dont have to run with <sys/queue.h> */
55 #ifndef TAILQ_ENTRY
56 #define _EVENT_DEFINED_TQENTRY
57 #define TAILQ_ENTRY(type)						\
58 struct {								\
59 	struct type *tqe_next;	/* next element */			\
60 	struct type **tqe_prev;	/* address of previous next element */	\
61 }
62 #endif /* !TAILQ_ENTRY */
63 #ifndef RB_ENTRY
64 #define _EVENT_DEFINED_RBENTRY
65 #define RB_ENTRY(type)							\
66 struct {								\
67 	struct type *rbe_left;		/* left element */		\
68 	struct type *rbe_right;		/* right element */		\
69 	struct type *rbe_parent;	/* parent element */		\
70 	int rbe_color;			/* node color */		\
71 }
72 #endif /* !RB_ENTRY */
73 
74 struct event {
75 	TAILQ_ENTRY (event) ev_next;
76 	TAILQ_ENTRY (event) ev_active_next;
77 	TAILQ_ENTRY (event) ev_signal_next;
78 	RB_ENTRY (event) ev_timeout_node;
79 
80 	int ev_fd;
81 	short ev_events;
82 	short ev_ncalls;
83 	short *ev_pncalls;	/* Allows deletes in callback */
84 
85 	struct timeval ev_timeout;
86 
87 	void (*ev_callback)(int, short, void *arg);
88 	void *ev_arg;
89 
90 	int ev_res;		/* result passed to event callback */
91 	int ev_flags;
92 };
93 
94 #define EVENT_SIGNAL(ev)	ev->ev_fd
95 #define EVENT_FD(ev)		ev->ev_fd
96 
97 #ifdef _EVENT_DEFINED_TQENTRY
98 #undef TAILQ_ENTRY
99 #undef _EVENT_DEFINED_TQENTRY
100 #else
101 TAILQ_HEAD (event_list, event);
102 #endif /* _EVENT_DEFINED_TQENTRY */
103 #ifdef _EVENT_DEFINED_RBENTRY
104 #undef RB_ENTRY
105 #undef _EVENT_DEFINED_RBENTRY
106 #endif /* _EVENT_DEFINED_RBENTRY */
107 
108 struct eventop {
109 	char *name;
110 	void *(*init)(void);
111 	int (*add)(void *, struct event *);
112 	int (*del)(void *, struct event *);
113 	int (*recalc)(void *, int);
114 	int (*dispatch)(void *, struct timeval *);
115 };
116 
117 #define TIMEOUT_DEFAULT	{5, 0}
118 
119 void event_init(void);
120 int event_dispatch(void);
121 
122 #define EVLOOP_ONCE	0x01
123 #define EVLOOP_NONBLOCK	0x02
124 int event_loop(int);
125 
126 int timeout_next(struct timeval *);
127 void timeout_correct(struct timeval *);
128 void timeout_process(void);
129 
130 #define evtimer_add(ev, tv)		event_add(ev, tv)
131 #define evtimer_set(ev, cb, arg)	event_set(ev, -1, 0, cb, arg)
132 #define evtimer_del(ev)			event_del(ev)
133 #define evtimer_pending(ev, tv)		event_pending(ev, EV_TIMEOUT, tv)
134 #define evtimer_initialized(ev)		((ev)->ev_flags & EVLIST_INIT)
135 
136 #define signal_add(ev, tv)		event_add(ev, tv)
137 #define signal_set(ev, x, cb, arg)	\
138 	event_set(ev, x, EV_SIGNAL|EV_PERSIST, cb, arg)
139 #define signal_del(ev)			event_del(ev)
140 #define signal_pending(ev, tv)		event_pending(ev, EV_SIGNAL, tv)
141 #define signal_initialized(ev)		((ev)->ev_flags & EVLIST_INIT)
142 
143 void event_set(struct event *, int, short, void (*)(int, short, void *), void *);
144 int event_add(struct event *, struct timeval *);
145 int event_del(struct event *);
146 void event_active(struct event *, int, short);
147 
148 int event_pending(struct event *, short, struct timeval *);
149 
150 #define event_initialized(ev)		((ev)->ev_flags & EVLIST_INIT)
151 
152 #ifdef __cplusplus
153 }
154 #endif
155 
156 #endif /* _EVENT_H_ */
157