1/*	$NetBSD: changelist-internal.h,v 1.1.1.2 2017/01/31 21:14:52 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_INTERNAL_H_INCLUDED_
28#define CHANGELIST_INTERNAL_H_INCLUDED_
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 */
54struct 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	ev_uint8_t close_change;
67};
68
69/* Flags for read_change and write_change. */
70
71/* If set, add the event. */
72#define EV_CHANGE_ADD     0x01
73/* If set, delete the event.  Exclusive with EV_CHANGE_ADD */
74#define EV_CHANGE_DEL     0x02
75/* If set, this event refers a signal, not an fd. */
76#define EV_CHANGE_SIGNAL  EV_SIGNAL
77/* Set for persistent events.  Currently not used. */
78#define EV_CHANGE_PERSIST EV_PERSIST
79/* Set for adding edge-triggered events. */
80#define EV_CHANGE_ET      EV_ET
81
82/* The value of fdinfo_size that a backend should use if it is letting
83 * changelist handle its add and delete functions. */
84#define EVENT_CHANGELIST_FDINFO_SIZE sizeof(int)
85
86/** Set up the data fields in a changelist. */
87void event_changelist_init_(struct event_changelist *changelist);
88/** Remove every change in the changelist, and make corresponding changes
89 * in the event maps in the base.  This function is generally used right
90 * after making all the changes in the changelist. */
91void event_changelist_remove_all_(struct event_changelist *changelist,
92    struct event_base *base);
93/** Free all memory held in a changelist. */
94void event_changelist_freemem_(struct event_changelist *changelist);
95
96/** Implementation of eventop_add that queues the event in a changelist. */
97int event_changelist_add_(struct event_base *base, evutil_socket_t fd, short old, short events,
98    void *p);
99/** Implementation of eventop_del that queues the event in a changelist. */
100int event_changelist_del_(struct event_base *base, evutil_socket_t fd, short old, short events,
101    void *p);
102
103#endif
104