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