1*2b15cb3dSCy Schubert /* 2*2b15cb3dSCy Schubert * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson 3*2b15cb3dSCy Schubert * 4*2b15cb3dSCy Schubert * Redistribution and use in source and binary forms, with or without 5*2b15cb3dSCy Schubert * modification, are permitted provided that the following conditions 6*2b15cb3dSCy Schubert * are met: 7*2b15cb3dSCy Schubert * 1. Redistributions of source code must retain the above copyright 8*2b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer. 9*2b15cb3dSCy Schubert * 2. Redistributions in binary form must reproduce the above copyright 10*2b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer in the 11*2b15cb3dSCy Schubert * documentation and/or other materials provided with the distribution. 12*2b15cb3dSCy Schubert * 3. The name of the author may not be used to endorse or promote products 13*2b15cb3dSCy Schubert * derived from this software without specific prior written permission. 14*2b15cb3dSCy Schubert * 15*2b15cb3dSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16*2b15cb3dSCy Schubert * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17*2b15cb3dSCy Schubert * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18*2b15cb3dSCy Schubert * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19*2b15cb3dSCy Schubert * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20*2b15cb3dSCy Schubert * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21*2b15cb3dSCy Schubert * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22*2b15cb3dSCy Schubert * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23*2b15cb3dSCy Schubert * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24*2b15cb3dSCy Schubert * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25*2b15cb3dSCy Schubert */ 26*2b15cb3dSCy Schubert #ifndef CHANGELIST_INTERNAL_H_INCLUDED_ 27*2b15cb3dSCy Schubert #define CHANGELIST_INTERNAL_H_INCLUDED_ 28*2b15cb3dSCy Schubert 29*2b15cb3dSCy Schubert /* 30*2b15cb3dSCy Schubert A "changelist" is a list of all the fd status changes that should be made 31*2b15cb3dSCy Schubert between calls to the backend's dispatch function. There are a few reasons 32*2b15cb3dSCy Schubert that a backend would want to queue changes like this rather than processing 33*2b15cb3dSCy Schubert them immediately. 34*2b15cb3dSCy Schubert 35*2b15cb3dSCy Schubert 1) Sometimes applications will add and delete the same event more than 36*2b15cb3dSCy Schubert once between calls to dispatch. Processing these changes immediately 37*2b15cb3dSCy Schubert is needless, and potentially expensive (especially if we're on a system 38*2b15cb3dSCy Schubert that makes one syscall per changed event). 39*2b15cb3dSCy Schubert 40*2b15cb3dSCy Schubert 2) Sometimes we can coalesce multiple changes on the same fd into a single 41*2b15cb3dSCy Schubert syscall if we know about them in advance. For example, epoll can do an 42*2b15cb3dSCy Schubert add and a delete at the same time, but only if we have found out about 43*2b15cb3dSCy Schubert both of them before we tell epoll. 44*2b15cb3dSCy Schubert 45*2b15cb3dSCy Schubert 3) Sometimes adding an event that we immediately delete can cause 46*2b15cb3dSCy Schubert unintended consequences: in kqueue, this makes pending events get 47*2b15cb3dSCy Schubert reported spuriously. 48*2b15cb3dSCy Schubert */ 49*2b15cb3dSCy Schubert 50*2b15cb3dSCy Schubert #include "event2/util.h" 51*2b15cb3dSCy Schubert 52*2b15cb3dSCy Schubert /** Represents a */ 53*2b15cb3dSCy Schubert struct event_change { 54*2b15cb3dSCy Schubert /** The fd or signal whose events are to be changed */ 55*2b15cb3dSCy Schubert evutil_socket_t fd; 56*2b15cb3dSCy Schubert /* The events that were enabled on the fd before any of these changes 57*2b15cb3dSCy Schubert were made. May include EV_READ or EV_WRITE. */ 58*2b15cb3dSCy Schubert short old_events; 59*2b15cb3dSCy Schubert 60*2b15cb3dSCy Schubert /* The changes that we want to make in reading and writing on this fd. 61*2b15cb3dSCy Schubert * If this is a signal, then read_change has EV_CHANGE_SIGNAL set, 62*2b15cb3dSCy Schubert * and write_change is unused. */ 63*2b15cb3dSCy Schubert ev_uint8_t read_change; 64*2b15cb3dSCy Schubert ev_uint8_t write_change; 65*2b15cb3dSCy Schubert ev_uint8_t close_change; 66*2b15cb3dSCy Schubert }; 67*2b15cb3dSCy Schubert 68*2b15cb3dSCy Schubert /* Flags for read_change and write_change. */ 69*2b15cb3dSCy Schubert 70*2b15cb3dSCy Schubert /* If set, add the event. */ 71*2b15cb3dSCy Schubert #define EV_CHANGE_ADD 0x01 72*2b15cb3dSCy Schubert /* If set, delete the event. Exclusive with EV_CHANGE_ADD */ 73*2b15cb3dSCy Schubert #define EV_CHANGE_DEL 0x02 74*2b15cb3dSCy Schubert /* If set, this event refers a signal, not an fd. */ 75*2b15cb3dSCy Schubert #define EV_CHANGE_SIGNAL EV_SIGNAL 76*2b15cb3dSCy Schubert /* Set for persistent events. Currently not used. */ 77*2b15cb3dSCy Schubert #define EV_CHANGE_PERSIST EV_PERSIST 78*2b15cb3dSCy Schubert /* Set for adding edge-triggered events. */ 79*2b15cb3dSCy Schubert #define EV_CHANGE_ET EV_ET 80*2b15cb3dSCy Schubert 81*2b15cb3dSCy Schubert /* The value of fdinfo_size that a backend should use if it is letting 82*2b15cb3dSCy Schubert * changelist handle its add and delete functions. */ 83*2b15cb3dSCy Schubert #define EVENT_CHANGELIST_FDINFO_SIZE sizeof(int) 84*2b15cb3dSCy Schubert 85*2b15cb3dSCy Schubert /** Set up the data fields in a changelist. */ 86*2b15cb3dSCy Schubert void event_changelist_init_(struct event_changelist *changelist); 87*2b15cb3dSCy Schubert /** Remove every change in the changelist, and make corresponding changes 88*2b15cb3dSCy Schubert * in the event maps in the base. This function is generally used right 89*2b15cb3dSCy Schubert * after making all the changes in the changelist. */ 90*2b15cb3dSCy Schubert void event_changelist_remove_all_(struct event_changelist *changelist, 91*2b15cb3dSCy Schubert struct event_base *base); 92*2b15cb3dSCy Schubert /** Free all memory held in a changelist. */ 93*2b15cb3dSCy Schubert void event_changelist_freemem_(struct event_changelist *changelist); 94*2b15cb3dSCy Schubert 95*2b15cb3dSCy Schubert /** Implementation of eventop_add that queues the event in a changelist. */ 96*2b15cb3dSCy Schubert int event_changelist_add_(struct event_base *base, evutil_socket_t fd, short old, short events, 97*2b15cb3dSCy Schubert void *p); 98*2b15cb3dSCy Schubert /** Implementation of eventop_del that queues the event in a changelist. */ 99*2b15cb3dSCy Schubert int event_changelist_del_(struct event_base *base, evutil_socket_t fd, short old, short events, 100*2b15cb3dSCy Schubert void *p); 101*2b15cb3dSCy Schubert 102*2b15cb3dSCy Schubert #endif 103