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