xref: /freebsd/contrib/ntp/sntp/libevent/changelist-internal.h (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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_INTERNAL_H_INCLUDED_
27 #define CHANGELIST_INTERNAL_H_INCLUDED_
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 	ev_uint8_t close_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