xref: /openbsd/usr.bin/dig/lib/isc/include/isc/event.h (revision 1a012e20)
1*5185a700Sflorian /*
2*5185a700Sflorian  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3*5185a700Sflorian  *
4*5185a700Sflorian  * Permission to use, copy, modify, and/or distribute this software for any
5*5185a700Sflorian  * purpose with or without fee is hereby granted, provided that the above
6*5185a700Sflorian  * copyright notice and this permission notice appear in all copies.
7*5185a700Sflorian  *
8*5185a700Sflorian  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9*5185a700Sflorian  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10*5185a700Sflorian  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11*5185a700Sflorian  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12*5185a700Sflorian  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13*5185a700Sflorian  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14*5185a700Sflorian  * PERFORMANCE OF THIS SOFTWARE.
15*5185a700Sflorian  */
16*5185a700Sflorian 
17*5185a700Sflorian #ifndef ISC_EVENT_H
18*5185a700Sflorian #define ISC_EVENT_H 1
19*5185a700Sflorian 
20*5185a700Sflorian /*! \file isc/event.h */
21*5185a700Sflorian 
22*5185a700Sflorian #include <isc/types.h>
23*5185a700Sflorian 
24*5185a700Sflorian /*****
25*5185a700Sflorian  ***** Events.
26*5185a700Sflorian  *****/
27*5185a700Sflorian 
28*5185a700Sflorian typedef void (*isc_eventdestructor_t)(isc_event_t *);
29*5185a700Sflorian 
30*5185a700Sflorian #define ISC_EVENT_COMMON(ltype)		\
31*5185a700Sflorian 	size_t				ev_size; \
32*5185a700Sflorian 	unsigned int			ev_attributes; \
33*5185a700Sflorian 	void *				ev_tag; \
34*5185a700Sflorian 	isc_eventtype_t			ev_type; \
35*5185a700Sflorian 	isc_taskaction_t		ev_action; \
36*5185a700Sflorian 	void *				ev_arg; \
37*5185a700Sflorian 	void *				ev_sender; \
38*5185a700Sflorian 	isc_eventdestructor_t		ev_destroy; \
39*5185a700Sflorian 	void *				ev_destroy_arg; \
40*5185a700Sflorian 	ISC_LINK(ltype)			ev_link; \
41*5185a700Sflorian 	ISC_LINK(ltype)			ev_ratelink
42*5185a700Sflorian 
43*5185a700Sflorian /*%
44*5185a700Sflorian  * Attributes matching a mask of 0x000000ff are reserved for the task library's
45*5185a700Sflorian  * definition.  Attributes of 0xffffff00 may be used by the application
46*5185a700Sflorian  * or non-ISC libraries.
47*5185a700Sflorian  */
48*5185a700Sflorian #define ISC_EVENTATTR_NOPURGE		0x00000001
49*5185a700Sflorian 
50*5185a700Sflorian /*%
51*5185a700Sflorian  * The ISC_EVENTATTR_CANCELED attribute is intended to indicate
52*5185a700Sflorian  * that an event is delivered as a result of a canceled operation
53*5185a700Sflorian  * rather than successful completion, by mutual agreement
54*5185a700Sflorian  * between the sender and receiver.  It is not set or used by
55*5185a700Sflorian  * the task system.
56*5185a700Sflorian  */
57*5185a700Sflorian #define ISC_EVENTATTR_CANCELED		0x00000002
58*5185a700Sflorian 
59*5185a700Sflorian #define ISC_EVENT_INIT(event, sz, at, ta, ty, ac, ar, sn, df) \
60*5185a700Sflorian do { \
61*5185a700Sflorian 	(event)->ev_size = (sz); \
62*5185a700Sflorian 	(event)->ev_attributes = (at); \
63*5185a700Sflorian 	(event)->ev_tag = (ta); \
64*5185a700Sflorian 	(event)->ev_type = (ty); \
65*5185a700Sflorian 	(event)->ev_action = (ac); \
66*5185a700Sflorian 	(event)->ev_arg = (ar); \
67*5185a700Sflorian 	(event)->ev_sender = (sn); \
68*5185a700Sflorian 	(event)->ev_destroy = (df); \
69*5185a700Sflorian 	ISC_LINK_INIT((event), ev_link); \
70*5185a700Sflorian 	ISC_LINK_INIT((event), ev_ratelink); \
71*5185a700Sflorian } while (0)
72*5185a700Sflorian 
73*5185a700Sflorian /*%
74*5185a700Sflorian  * This structure is public because "subclassing" it may be useful when
75*5185a700Sflorian  * defining new event types.
76*5185a700Sflorian  */
77*5185a700Sflorian struct isc_event {
78*5185a700Sflorian 	ISC_EVENT_COMMON(struct isc_event);
79*5185a700Sflorian };
80*5185a700Sflorian 
81*5185a700Sflorian #define ISC_EVENTTYPE_FIRSTEVENT	0x00000000
82*5185a700Sflorian #define ISC_EVENTTYPE_LASTEVENT		0xffffffff
83*5185a700Sflorian 
84*5185a700Sflorian #define ISC_EVENT_PTR(p) ((isc_event_t **)(void *)(p))
85*5185a700Sflorian 
86*5185a700Sflorian isc_event_t *
87*5185a700Sflorian isc_event_allocate(void *sender, isc_eventtype_t type,
88*5185a700Sflorian 		   isc_taskaction_t action, void *arg, size_t size);
89*5185a700Sflorian /*%<
90*5185a700Sflorian  * Allocate an event structure.
91*5185a700Sflorian  *
92*5185a700Sflorian  * Allocate and initialize in a structure with initial elements
93*5185a700Sflorian  * defined by:
94*5185a700Sflorian  *
95*5185a700Sflorian  * \code
96*5185a700Sflorian  *	struct {
97*5185a700Sflorian  *		ISC_EVENT_COMMON(struct isc_event);
98*5185a700Sflorian  *		...
99*5185a700Sflorian  *	};
100*5185a700Sflorian  * \endcode
101*5185a700Sflorian  *
102*5185a700Sflorian  * Requires:
103*5185a700Sflorian  *\li	'size' >= sizeof(struct isc_event)
104*5185a700Sflorian  *\li	'action' to be non NULL
105*5185a700Sflorian  *
106*5185a700Sflorian  * Returns:
107*5185a700Sflorian  *\li	a pointer to a initialized structure of the requested size.
108*5185a700Sflorian  *\li	NULL if unable to allocate memory.
109*5185a700Sflorian  */
110*5185a700Sflorian 
111*5185a700Sflorian void
112*5185a700Sflorian isc_event_free(isc_event_t **);
113*5185a700Sflorian 
114*5185a700Sflorian #endif /* ISC_EVENT_H */
115