1275970Scy/*
2275970Scy * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
3275970Scy * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4275970Scy *
5275970Scy * Redistribution and use in source and binary forms, with or without
6275970Scy * modification, are permitted provided that the following conditions
7275970Scy * are met:
8275970Scy * 1. Redistributions of source code must retain the above copyright
9275970Scy *    notice, this list of conditions and the following disclaimer.
10275970Scy * 2. Redistributions in binary form must reproduce the above copyright
11275970Scy *    notice, this list of conditions and the following disclaimer in the
12275970Scy *    documentation and/or other materials provided with the distribution.
13275970Scy * 3. The name of the author may not be used to endorse or promote products
14275970Scy *    derived from this software without specific prior written permission.
15275970Scy *
16275970Scy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17275970Scy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18275970Scy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19275970Scy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20275970Scy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21275970Scy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22275970Scy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23275970Scy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24275970Scy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25275970Scy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26275970Scy */
27275970Scy#ifndef EVENT2_EVENT_STRUCT_H_INCLUDED_
28275970Scy#define EVENT2_EVENT_STRUCT_H_INCLUDED_
29275970Scy
30275970Scy/** @file event2/event_struct.h
31275970Scy
32275970Scy  Structures used by event.h.  Using these structures directly WILL harm
33275970Scy  forward compatibility: be careful.
34275970Scy
35275970Scy  No field declared in this file should be used directly in user code.  Except
36275970Scy  for historical reasons, these fields would not be exposed at all.
37275970Scy */
38275970Scy
39275970Scy#ifdef __cplusplus
40275970Scyextern "C" {
41275970Scy#endif
42275970Scy
43275970Scy#include <event2/event-config.h>
44275970Scy#ifdef EVENT__HAVE_SYS_TYPES_H
45275970Scy#include <sys/types.h>
46275970Scy#endif
47275970Scy#ifdef EVENT__HAVE_SYS_TIME_H
48275970Scy#include <sys/time.h>
49275970Scy#endif
50275970Scy
51275970Scy/* For int types. */
52275970Scy#include <event2/util.h>
53275970Scy
54275970Scy/* For evkeyvalq */
55275970Scy#include <event2/keyvalq_struct.h>
56275970Scy
57275970Scy#define EVLIST_TIMEOUT	    0x01
58275970Scy#define EVLIST_INSERTED	    0x02
59275970Scy#define EVLIST_SIGNAL	    0x04
60275970Scy#define EVLIST_ACTIVE	    0x08
61275970Scy#define EVLIST_INTERNAL	    0x10
62275970Scy#define EVLIST_ACTIVE_LATER 0x20
63275970Scy#define EVLIST_FINALIZING   0x40
64275970Scy#define EVLIST_INIT	    0x80
65275970Scy
66275970Scy#define EVLIST_ALL          0xff
67275970Scy
68275970Scy/* Fix so that people don't have to run with <sys/queue.h> */
69275970Scy#ifndef TAILQ_ENTRY
70275970Scy#define EVENT_DEFINED_TQENTRY_
71275970Scy#define TAILQ_ENTRY(type)						\
72275970Scystruct {								\
73275970Scy	struct type *tqe_next;	/* next element */			\
74275970Scy	struct type **tqe_prev;	/* address of previous next element */	\
75275970Scy}
76275970Scy#endif /* !TAILQ_ENTRY */
77275970Scy
78275970Scy#ifndef TAILQ_HEAD
79275970Scy#define EVENT_DEFINED_TQHEAD_
80275970Scy#define TAILQ_HEAD(name, type)			\
81275970Scystruct name {					\
82275970Scy	struct type *tqh_first;			\
83275970Scy	struct type **tqh_last;			\
84275970Scy}
85275970Scy#endif
86275970Scy
87275970Scy/* Fix so that people don't have to run with <sys/queue.h> */
88275970Scy#ifndef LIST_ENTRY
89275970Scy#define EVENT_DEFINED_LISTENTRY_
90275970Scy#define LIST_ENTRY(type)						\
91275970Scystruct {								\
92275970Scy	struct type *le_next;	/* next element */			\
93275970Scy	struct type **le_prev;	/* address of previous next element */	\
94275970Scy}
95275970Scy#endif /* !LIST_ENTRY */
96275970Scy
97275970Scy#ifndef LIST_HEAD
98275970Scy#define EVENT_DEFINED_LISTHEAD_
99275970Scy#define LIST_HEAD(name, type)						\
100275970Scystruct name {								\
101275970Scy	struct type *lh_first;  /* first element */			\
102275970Scy	}
103275970Scy#endif /* !LIST_HEAD */
104275970Scy
105275970Scystruct event;
106275970Scy
107275970Scystruct event_callback {
108275970Scy	TAILQ_ENTRY(event_callback) evcb_active_next;
109275970Scy	short evcb_flags;
110275970Scy	ev_uint8_t evcb_pri;	/* smaller numbers are higher priority */
111275970Scy	ev_uint8_t evcb_closure;
112275970Scy	/* allows us to adopt for different types of events */
113275970Scy        union {
114275970Scy		void (*evcb_callback)(evutil_socket_t, short, void *);
115275970Scy		void (*evcb_selfcb)(struct event_callback *, void *);
116275970Scy		void (*evcb_evfinalize)(struct event *, void *);
117275970Scy		void (*evcb_cbfinalize)(struct event_callback *, void *);
118275970Scy	} evcb_cb_union;
119275970Scy	void *evcb_arg;
120275970Scy};
121275970Scy
122275970Scystruct event_base;
123275970Scystruct event {
124275970Scy	struct event_callback ev_evcallback;
125275970Scy
126275970Scy	/* for managing timeouts */
127275970Scy	union {
128275970Scy		TAILQ_ENTRY(event) ev_next_with_common_timeout;
129275970Scy		int min_heap_idx;
130275970Scy	} ev_timeout_pos;
131275970Scy	evutil_socket_t ev_fd;
132275970Scy
133275970Scy	struct event_base *ev_base;
134275970Scy
135275970Scy	union {
136275970Scy		/* used for io events */
137275970Scy		struct {
138275970Scy			LIST_ENTRY (event) ev_io_next;
139275970Scy			struct timeval ev_timeout;
140275970Scy		} ev_io;
141275970Scy
142275970Scy		/* used by signal events */
143275970Scy		struct {
144275970Scy			LIST_ENTRY (event) ev_signal_next;
145275970Scy			short ev_ncalls;
146275970Scy			/* Allows deletes in callback */
147275970Scy			short *ev_pncalls;
148275970Scy		} ev_signal;
149275970Scy	} ev_;
150275970Scy
151275970Scy	short ev_events;
152275970Scy	short ev_res;		/* result passed to event callback */
153275970Scy	struct timeval ev_timeout;
154275970Scy};
155275970Scy
156275970ScyTAILQ_HEAD (event_list, event);
157275970Scy
158275970Scy#ifdef EVENT_DEFINED_TQENTRY_
159275970Scy#undef TAILQ_ENTRY
160275970Scy#endif
161275970Scy
162275970Scy#ifdef EVENT_DEFINED_TQHEAD_
163275970Scy#undef TAILQ_HEAD
164275970Scy#endif
165275970Scy
166275970ScyLIST_HEAD (event_dlist, event);
167275970Scy
168275970Scy#ifdef EVENT_DEFINED_LISTENTRY_
169275970Scy#undef LIST_ENTRY
170275970Scy#endif
171275970Scy
172275970Scy#ifdef EVENT_DEFINED_LISTHEAD_
173275970Scy#undef LIST_HEAD
174275970Scy#endif
175275970Scy
176275970Scy#ifdef __cplusplus
177275970Scy}
178275970Scy#endif
179275970Scy
180275970Scy#endif /* EVENT2_EVENT_STRUCT_H_INCLUDED_ */
181