1/*
2 * event.h
3 */
4
5/*-
6 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
7 *
8 * Copyright (c) 2009 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/* $FreeBSD$ */
34
35/*
36 * Hack to provide libevent (see devel/libevent port) like API.
37 * Should be removed if FreeBSD ever decides to import libevent into base.
38 */
39
40#ifndef	_EVENT_H_
41#define	_EVENT_H_	1
42
43#define	EV_READ         0x02
44#define	EV_WRITE        0x04
45#define	EV_PERSIST      0x10		/* Persistent event */
46#define	EV_PENDING	(1 << 13)	/* internal use only! */
47#define	EV_HAS_TIMEOUT	(1 << 14)	/* internal use only! */
48#define	EV_CURRENT	(1 << 15)	/* internal use only! */
49
50struct event
51{
52	int			fd;
53	short			flags;
54	void			(*cb)(int, short, void *);
55	void			*cbarg;
56	struct timeval		timeout;
57	struct timeval		expire;
58
59#ifdef	EVENT_DEBUG
60	char const		*files[3];
61	int			lines[3];
62#endif
63
64	TAILQ_ENTRY(event)	next;
65};
66
67void	event_init	(void);
68int	event_dispatch	(void);
69
70void	__event_set	(struct event *, int, short,
71			 void (*)(int, short, void *), void *);
72int	__event_add	(struct event *, struct timeval const *);
73int	__event_del	(struct event *);
74
75#ifdef	EVENT_DEBUG
76#define event_log_err(fmt, args...)	syslog(LOG_ERR, fmt, ##args)
77#define event_log_info(fmt, args...)	syslog(LOG_INFO, fmt, ##args)
78#define event_log_notice(fmt, args...)	syslog(LOG_NOTICE, fmt, ##args)
79#define event_log_debug(fmt, args...)	syslog(LOG_DEBUG, fmt, ##args)
80
81#define	event_set(ev, fd, flags, cb, cbarg) \
82	_event_set(__FILE__, __LINE__, ev, fd, flags, cb, cbarg)
83#define	event_add(ev, timeout) \
84	_event_add(__FILE__, __LINE__, ev, timeout)
85#define	event_del(ev) \
86	_event_del(__FILE__, __LINE__, ev)
87
88#define	evtimer_set(ev, cb, cbarg) \
89	_event_set(__FILE__, __LINE__, ev, -1, 0, cb, cbarg)
90#define	evtimer_add(ev, timeout) \
91	_event_add(__FILE__, __LINE__, ev, timeout)
92
93static inline void
94_event_set(char const *file, int line, struct event *ev, int fd, short flags,
95		void (*cb)(int, short, void *), void *cbarg)
96{
97	event_log_debug("set %s:%d ev=%p, fd=%d, flags=%#x, cb=%p, cbarg=%p",
98		file, line, ev, fd, flags, cb, cbarg);
99
100	ev->files[0] = file;
101	ev->lines[0] = line;
102
103	__event_set(ev, fd, flags, cb, cbarg);
104}
105
106static inline int
107_event_add(char const *file, int line, struct event *ev,
108		struct timeval const *timeout) {
109	event_log_debug("add %s:%d ev=%p, fd=%d, flags=%#x, cb=%p, cbarg=%p, " \
110		"timeout=%p", file, line, ev, ev->fd, ev->flags, ev->cb,
111		ev->cbarg, timeout);
112
113	ev->files[1] = file;
114	ev->lines[1] = line;
115
116	return (__event_add(ev, timeout));
117}
118
119static inline int
120_event_del(char const *file, int line, struct event *ev)
121{
122	event_log_debug("del %s:%d ev=%p, fd=%d, flags=%#x, cb=%p, cbarg=%p",
123		file, line, ev, ev->fd, ev->flags, ev->cb, ev->cbarg);
124
125	ev->files[2] = file;
126	ev->lines[2] = line;
127
128	return (__event_del(ev));
129}
130#else
131#define event_log_err(fmt, args...)
132#define event_log_info(fmt, args...)
133#define event_log_notice(fmt, args...)
134#define event_log_debug(fmt, args...)
135
136#define	event_set(ev, fd, flags, cb, cbarg) \
137	__event_set(ev, fd, flags, cb, cbarg)
138#define	event_add(ev, timeout) \
139	__event_add(ev, timeout)
140#define	event_del(ev) \
141	__event_del(ev)
142
143#define	evtimer_set(ev, cb, cbarg) \
144	__event_set(ev, -1, 0, cb, cbarg)
145#define	evtimer_add(ev, timeout) \
146	__event_add(ev, timeout)
147#endif	/* EVENT_DEBUG */
148
149#endif	/* ndef _EVENT_H_ */
150