1132943Sgshapiro/* 2132943Sgshapiro * event.h 3261194Sgshapiro */ 4132943Sgshapiro 5132943Sgshapiro/*- 6132943Sgshapiro * Copyright (c) 2009 Maksim Yevmenkin <m_evmenkin@yahoo.com> 7132943Sgshapiro * All rights reserved. 8132943Sgshapiro * 9132943Sgshapiro * Redistribution and use in source and binary forms, with or without 10132943Sgshapiro * modification, are permitted provided that the following conditions 11132943Sgshapiro * are met: 12132943Sgshapiro * 1. Redistributions of source code must retain the above copyright 13266527Sgshapiro * notice, this list of conditions and the following disclaimer. 14132943Sgshapiro * 2. Redistributions in binary form must reproduce the above copyright 15132943Sgshapiro * notice, this list of conditions and the following disclaimer in the 16132943Sgshapiro * documentation and/or other materials provided with the distribution. 17132943Sgshapiro * 18132943Sgshapiro * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19132943Sgshapiro * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20132943Sgshapiro * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21132943Sgshapiro * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22132943Sgshapiro * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23132943Sgshapiro * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24132943Sgshapiro * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25132943Sgshapiro * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26132943Sgshapiro * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27132943Sgshapiro * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28132943Sgshapiro * SUCH DAMAGE. 29132943Sgshapiro */ 30132943Sgshapiro 31132943Sgshapiro/* $FreeBSD$ */ 32132943Sgshapiro 33132943Sgshapiro/* 34132943Sgshapiro * Hack to provide libevent (see devel/libevent port) like API. 35132943Sgshapiro * Should be removed if FreeBSD ever decides to import libevent into base. 36132943Sgshapiro */ 37 38#ifndef _EVENT_H_ 39#define _EVENT_H_ 1 40 41#define EV_READ 0x02 42#define EV_WRITE 0x04 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! */ 47 48struct event 49{ 50 int fd; 51 short flags; 52 void (*cb)(int, short, void *); 53 void *cbarg; 54 struct timeval timeout; 55 struct timeval expire; 56 57#ifdef EVENT_DEBUG 58 char const *files[3]; 59 int lines[3]; 60#endif 61 62 TAILQ_ENTRY(event) next; 63}; 64 65void event_init (void); 66int event_dispatch (void); 67 68void __event_set (struct event *, int, short, 69 void (*)(int, short, void *), void *); 70int __event_add (struct event *, struct timeval const *); 71int __event_del (struct event *); 72 73#ifdef EVENT_DEBUG 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) 78 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) 85 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) 90 91static inline void 92_event_set(char const *file, int line, struct event *ev, int fd, short flags, 93 void (*cb)(int, short, void *), void *cbarg) 94{ 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); 97 98 ev->files[0] = file; 99 ev->lines[0] = line; 100 101 __event_set(ev, fd, flags, cb, cbarg); 102} 103 104static inline int 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, 109 ev->cbarg, timeout); 110 111 ev->files[1] = file; 112 ev->lines[1] = line; 113 114 return (__event_add(ev, timeout)); 115} 116 117static inline int 118_event_del(char const *file, int line, struct event *ev) 119{ 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); 122 123 ev->files[2] = file; 124 ev->lines[2] = line; 125 126 return (__event_del(ev)); 127} 128#else 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...) 133 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) \ 139 __event_del(ev) 140 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 */ 146 147#endif /* ndef _EVENT_H_ */ 148