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