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