1 /*
2  * wepoll - epoll for Windows
3  * https://github.com/piscisaureus/wepoll
4  *
5  * Copyright 2012-2018, Bert Belder <bertbelder@gmail.com>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *
15  *   * Redistributions in binary form must reproduce the above copyright
16  *     notice, this list of conditions and the following disclaimer in the
17  *     documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef WEPOLL_H_
33 #define WEPOLL_H_
34 
35 #ifndef WEPOLL_EXPORT
36 #define WEPOLL_EXPORT
37 #endif
38 
39 #include <stdint.h>
40 
41 /* clang-format off */
42 
43 enum EPOLL_EVENTS {
44   EPOLLIN      = (int) (1U <<  0),
45   EPOLLPRI     = (int) (1U <<  1),
46   EPOLLOUT     = (int) (1U <<  2),
47   EPOLLERR     = (int) (1U <<  3),
48   EPOLLHUP     = (int) (1U <<  4),
49   EPOLLRDNORM  = (int) (1U <<  6),
50   EPOLLRDBAND  = (int) (1U <<  7),
51   EPOLLWRNORM  = (int) (1U <<  8),
52   EPOLLWRBAND  = (int) (1U <<  9),
53   EPOLLMSG     = (int) (1U << 10), /* Never reported. */
54   EPOLLRDHUP   = (int) (1U << 13),
55   EPOLLONESHOT = (int) (1U << 31)
56 };
57 
58 #define EPOLLIN      (1U <<  0)
59 #define EPOLLPRI     (1U <<  1)
60 #define EPOLLOUT     (1U <<  2)
61 #define EPOLLERR     (1U <<  3)
62 #define EPOLLHUP     (1U <<  4)
63 #define EPOLLRDNORM  (1U <<  6)
64 #define EPOLLRDBAND  (1U <<  7)
65 #define EPOLLWRNORM  (1U <<  8)
66 #define EPOLLWRBAND  (1U <<  9)
67 #define EPOLLMSG     (1U << 10)
68 #define EPOLLRDHUP   (1U << 13)
69 #define EPOLLONESHOT (1U << 31)
70 
71 #define EPOLL_CTL_ADD 1
72 #define EPOLL_CTL_MOD 2
73 #define EPOLL_CTL_DEL 3
74 
75 /* clang-format on */
76 
77 typedef void* HANDLE;
78 typedef uintptr_t SOCKET;
79 
80 typedef union epoll_data {
81   void* ptr;
82   int fd;
83   uint32_t u32;
84   uint64_t u64;
85   SOCKET sock; /* Windows specific */
86   HANDLE hnd;  /* Windows specific */
87 } epoll_data_t;
88 
89 struct epoll_event {
90   uint32_t events;   /* Epoll events and flags */
91   epoll_data_t data; /* User data variable */
92 };
93 
94 #ifdef __cplusplus
95 extern "C" {
96 #endif
97 
98 WEPOLL_EXPORT HANDLE epoll_create(int size);
99 WEPOLL_EXPORT HANDLE epoll_create1(int flags);
100 
101 WEPOLL_EXPORT int epoll_close(HANDLE ephnd);
102 
103 WEPOLL_EXPORT int epoll_ctl(HANDLE ephnd,
104                             int op,
105                             SOCKET sock,
106                             struct epoll_event* event);
107 
108 WEPOLL_EXPORT int epoll_wait(HANDLE ephnd,
109                              struct epoll_event* events,
110                              int maxevents,
111                              int timeout);
112 
113 #ifdef __cplusplus
114 } /* extern "C" */
115 #endif
116 
117 #endif /* WEPOLL_H_ */
118