changelist-internal.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. A "changelist" is a list of all the fd status changes that should be made
  30. between calls to the backend's dispatch function. There are a few reasons
  31. that a backend would want to queue changes like this rather than processing
  32. them immediately.
  33. 1) Sometimes applications will add and delete the same event more than
  34. once between calls to dispatch. Processing these changes immediately
  35. is needless, and potentially expensive (especially if we're on a system
  36. that makes one syscall per changed event).
  37. 2) Sometimes we can coalesce multiple changes on the same fd into a single
  38. syscall if we know about them in advance. For example, epoll can do an
  39. add and a delete at the same time, but only if we have found out about
  40. both of them before we tell epoll.
  41. 3) Sometimes adding an event that we immediately delete can cause
  42. unintended consequences: in kqueue, this makes pending events get
  43. reported spuriously.
  44. */
  45. #include "event2/util.h"
  46. /** Represents a */
  47. struct event_change {
  48. /** The fd or signal whose events are to be changed */
  49. evutil_socket_t fd;
  50. /* The events that were enabled on the fd before any of these changes
  51. were made. May include EV_READ or EV_WRITE. */
  52. short old_events;
  53. /* The changes that we want to make in reading and writing on this fd.
  54. * If this is a signal, then read_change has EV_CHANGE_SIGNAL set,
  55. * and write_change is unused. */
  56. ev_uint8_t read_change;
  57. ev_uint8_t write_change;
  58. ev_uint8_t close_change;
  59. };
  60. /* Flags for read_change and write_change. */
  61. /* If set, add the event. */
  62. #define EV_CHANGE_ADD 0x01
  63. /* If set, delete the event. Exclusive with EV_CHANGE_ADD */
  64. #define EV_CHANGE_DEL 0x02
  65. /* If set, this event refers a signal, not an fd. */
  66. #define EV_CHANGE_SIGNAL EV_SIGNAL
  67. /* Set for persistent events. Currently not used. */
  68. #define EV_CHANGE_PERSIST EV_PERSIST
  69. /* Set for adding edge-triggered events. */
  70. #define EV_CHANGE_ET EV_ET
  71. /* The value of fdinfo_size that a backend should use if it is letting
  72. * changelist handle its add and delete functions. */
  73. #define EVENT_CHANGELIST_FDINFO_SIZE sizeof(int)
  74. /** Set up the data fields in a changelist. */
  75. void event_changelist_init_(struct event_changelist *changelist);
  76. /** Remove every change in the changelist, and make corresponding changes
  77. * in the event maps in the base. This function is generally used right
  78. * after making all the changes in the changelist. */
  79. void event_changelist_remove_all_(struct event_changelist *changelist,
  80. struct event_base *base);
  81. /** Free all memory held in a changelist. */
  82. void event_changelist_freemem_(struct event_changelist *changelist);
  83. /** Implementation of eventop_add that queues the event in a changelist. */
  84. int event_changelist_add_(struct event_base *base, evutil_socket_t fd, short old, short events,
  85. void *p);
  86. /** Implementation of eventop_del that queues the event in a changelist. */
  87. int event_changelist_del_(struct event_base *base, evutil_socket_t fd, short old, short events,
  88. void *p);
  89. #endif