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