6 * Copyright (c) 2009 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Hack to provide libevent (see devel/libevent port) like API.
35 * Should be removed if FreeBSD ever decides to import libevent into base.
43 #define EV_PERSIST 0x10 /* Persistent event */
44 #define EV_PENDING (1 << 13) /* internal use only! */
45 #define EV_HAS_TIMEOUT (1 << 14) /* internal use only! */
46 #define EV_CURRENT (1 << 15) /* internal use only! */
52 void (*cb)(int, short, void *);
54 struct timeval timeout;
55 struct timeval expire;
62 TAILQ_ENTRY(event) next;
65 void event_init (void);
66 int event_dispatch (void);
68 void __event_set (struct event *, int, short,
69 void (*)(int, short, void *), void *);
70 int __event_add (struct event *, struct timeval const *);
71 int __event_del (struct event *);
74 #define event_log_err(fmt, args...) syslog(LOG_ERR, fmt, ##args)
75 #define event_log_info(fmt, args...) syslog(LOG_INFO, fmt, ##args)
76 #define event_log_notice(fmt, args...) syslog(LOG_NOTICE, fmt, ##args)
77 #define event_log_debug(fmt, args...) syslog(LOG_DEBUG, fmt, ##args)
79 #define event_set(ev, fd, flags, cb, cbarg) \
80 _event_set(__FILE__, __LINE__, ev, fd, flags, cb, cbarg)
81 #define event_add(ev, timeout) \
82 _event_add(__FILE__, __LINE__, ev, timeout)
83 #define event_del(ev) \
84 _event_del(__FILE__, __LINE__, ev)
86 #define evtimer_set(ev, cb, cbarg) \
87 _event_set(__FILE__, __LINE__, ev, -1, 0, cb, cbarg)
88 #define evtimer_add(ev, timeout) \
89 _event_add(__FILE__, __LINE__, ev, timeout)
92 _event_set(char const *file, int line, struct event *ev, int fd, short flags,
93 void (*cb)(int, short, void *), void *cbarg)
95 event_log_debug("set %s:%d ev=%p, fd=%d, flags=%#x, cb=%p, cbarg=%p",
96 file, line, ev, fd, flags, cb, cbarg);
101 __event_set(ev, fd, flags, cb, cbarg);
105 _event_add(char const *file, int line, struct event *ev,
106 struct timeval const *timeout) {
107 event_log_debug("add %s:%d ev=%p, fd=%d, flags=%#x, cb=%p, cbarg=%p, " \
108 "timeout=%p", file, line, ev, ev->fd, ev->flags, ev->cb,
114 return (__event_add(ev, timeout));
118 _event_del(char const *file, int line, struct event *ev)
120 event_log_debug("del %s:%d ev=%p, fd=%d, flags=%#x, cb=%p, cbarg=%p",
121 file, line, ev, ev->fd, ev->flags, ev->cb, ev->cbarg);
126 return (__event_del(ev));
129 #define event_log_err(fmt, args...)
130 #define event_log_info(fmt, args...)
131 #define event_log_notice(fmt, args...)
132 #define event_log_debug(fmt, args...)
134 #define event_set(ev, fd, flags, cb, cbarg) \
135 __event_set(ev, fd, flags, cb, cbarg)
136 #define event_add(ev, timeout) \
137 __event_add(ev, timeout)
138 #define event_del(ev) \
141 #define evtimer_set(ev, cb, cbarg) \
142 __event_set(ev, -1, 0, cb, cbarg)
143 #define evtimer_add(ev, timeout) \
144 __event_add(ev, timeout)
145 #endif /* EVENT_DEBUG */
147 #endif /* ndef _EVENT_H_ */