aboutsummaryrefslogtreecommitdiffstats
path: root/erts/emulator/sys/common/erl_poll.h
blob: ad8f714d6efc54bbd3769d40f60d45cabaefbfb3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 2006-2013. All Rights Reserved.
 * 
 * The contents of this file are subject to the Erlang Public License,
 * Version 1.1, (the "License"); you may not use this file except in
 * compliance with the License. You should have received a copy of the
 * Erlang Public License along with this software. If not, it can be
 * retrieved online at http://www.erlang.org/.
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * %CopyrightEnd%
 */

/*
 * Description:	Poll interface suitable for ERTS with or without
 *              SMP support.
 *
 * Author: 	Rickard Green
 */

#ifndef ERL_POLL_H__
#define ERL_POLL_H__

#include "sys.h"

#define ERTS_POLL_NO_TIMEOUT ERTS_MONOTONIC_TIME_MIN

#if 0
#define ERTS_POLL_COUNT_AVOIDED_WAKEUPS
#endif

#ifdef ERTS_ENABLE_KERNEL_POLL
#  if defined(ERTS_KERNEL_POLL_VERSION)
#    define ERTS_POLL_EXPORT(FUNC) FUNC ## _kp
#  else
#    define ERTS_POLL_EXPORT(FUNC) FUNC ## _nkp
#    undef ERTS_POLL_DISABLE_KERNEL_POLL
#    define ERTS_POLL_DISABLE_KERNEL_POLL
#  endif
#else
#    define ERTS_POLL_EXPORT(FUNC) FUNC
#    undef ERTS_POLL_DISABLE_KERNEL_POLL
#    define ERTS_POLL_DISABLE_KERNEL_POLL
#endif

#ifdef ERTS_POLL_DISABLE_KERNEL_POLL
#  undef HAVE_SYS_EPOLL_H
#  undef HAVE_SYS_EVENT_H
#  undef HAVE_SYS_DEVPOLL_H
#endif

#undef ERTS_POLL_USE_KERNEL_POLL
#define ERTS_POLL_USE_KERNEL_POLL 0

#undef ERTS_POLL_USE_KQUEUE
#define ERTS_POLL_USE_KQUEUE 0
#undef ERTS_POLL_USE_EPOLL
#define ERTS_POLL_USE_EPOLL 0
#undef ERTS_POLL_USE_DEVPOLL
#define ERTS_POLL_USE_DEVPOLL 0
#undef ERTS_POLL_USE_POLL
#define ERTS_POLL_USE_POLL 0
#undef ERTS_POLL_USE_SELECT
#define ERTS_POLL_USE_SELECT 0

#if defined(HAVE_SYS_EVENT_H)
#  undef ERTS_POLL_USE_KQUEUE
#  define ERTS_POLL_USE_KQUEUE 1
#  undef ERTS_POLL_USE_KERNEL_POLL
#  define ERTS_POLL_USE_KERNEL_POLL 1
#elif defined(HAVE_SYS_EPOLL_H)
#  undef ERTS_POLL_USE_EPOLL
#  define ERTS_POLL_USE_EPOLL 1
#  undef ERTS_POLL_USE_KERNEL_POLL
#  define ERTS_POLL_USE_KERNEL_POLL 1
#elif defined(HAVE_SYS_DEVPOLL_H)
#  undef ERTS_POLL_USE_DEVPOLL
#  define ERTS_POLL_USE_DEVPOLL 1
#  undef ERTS_POLL_USE_KERNEL_POLL
#  define ERTS_POLL_USE_KERNEL_POLL 1
#endif

#define ERTS_POLL_USE_FALLBACK (ERTS_POLL_USE_KQUEUE || ERTS_POLL_USE_EPOLL)

#if !ERTS_POLL_USE_KERNEL_POLL || ERTS_POLL_USE_FALLBACK
#  if defined(ERTS_USE_POLL)
#    undef ERTS_POLL_USE_POLL
#    define ERTS_POLL_USE_POLL 1
#  elif !defined(__WIN32__) && !defined(__OSE__)
#    undef ERTS_POLL_USE_SELECT
#    define ERTS_POLL_USE_SELECT 1
#  endif
#endif

#define ERTS_POLL_USE_TIMERFD 0

typedef Uint32 ErtsPollEvents;
#undef ERTS_POLL_EV_E2N

#if defined(__WIN32__) || defined(__OSE__)	/* --- win32 or ose -------- */

#define ERTS_POLL_EV_IN   1
#define ERTS_POLL_EV_OUT  2
#define ERTS_POLL_EV_ERR  4
#define ERTS_POLL_EV_NVAL 8

#ifdef __OSE__

typedef struct ErtsPollOseMsgList_ {
  struct ErtsPollOseMsgList_ *next;
  union SIGNAL *data;
} ErtsPollOseMsgList;

struct erts_sys_fd_type {
    SIGSELECT signo;
    ErlDrvOseEventId id;
    ErtsPollOseMsgList *msgs;
    ErlDrvOseEventId (*resolve_signal)(union SIGNAL *sig);
    ethr_mutex mtx;
    void *extra;
};

#endif

#elif ERTS_POLL_USE_EPOLL	/* --- epoll ------------------------------- */

#include <sys/epoll.h>

#ifdef HAVE_SYS_TIMERFD_H
#include <sys/timerfd.h>
#undef ERTS_POLL_USE_TIMERFD
#define ERTS_POLL_USE_TIMERFD 1
#endif

#define ERTS_POLL_EV_E2N(EV) \
  ((__uint32_t) (EV))
#define ERTS_POLL_EV_N2E(EV) \
  ((ErtsPollEvents) (EV))

#define ERTS_POLL_EV_IN			ERTS_POLL_EV_N2E(EPOLLIN)
#define ERTS_POLL_EV_OUT		ERTS_POLL_EV_N2E(EPOLLOUT)
#define ERTS_POLL_EV_NVAL		ERTS_POLL_EV_N2E(EPOLLET)
#define ERTS_POLL_EV_ERR		ERTS_POLL_EV_N2E(EPOLLERR|EPOLLHUP)

#elif ERTS_POLL_USE_DEVPOLL	/* --- devpoll ----------------------------- */

#include <sys/devpoll.h>

#define ERTS_POLL_EV_E2N(EV) \
  ((short) ((EV) & ~((~((ErtsPollEvents) 0)) << 8*SIZEOF_SHORT)))
#define ERTS_POLL_EV_N2E(EV) \
  ((ErtsPollEvents) ((unsigned short) (EV)))

#define ERTS_POLL_EV_IN			ERTS_POLL_EV_N2E(POLLIN)
#define ERTS_POLL_EV_OUT		ERTS_POLL_EV_N2E(POLLOUT)
#define ERTS_POLL_EV_NVAL		ERTS_POLL_EV_N2E(POLLNVAL)
#define ERTS_POLL_EV_ERR		ERTS_POLL_EV_N2E(POLLERR|POLLHUP)

#elif ERTS_POLL_USE_KQUEUE	/* --- kqueue ------------------------------ */
/* Kqueue use fallback defines (poll() or select()) */
#endif

#if ERTS_POLL_USE_POLL	/* --- poll -------------------------------- */

#include <poll.h>

#define ERTS_POLL_EV_NKP_E2N(EV) \
  ((short) ((EV) & ~((~((ErtsPollEvents) 0)) << 8*SIZEOF_SHORT)))
#define ERTS_POLL_EV_NKP_N2E(EV) \
  ((ErtsPollEvents) ((unsigned short) (EV)))

/* At least on FreeBSD, we need POLLRDNORM for normal files, not POLLIN. */
/* Whether this is a bug in FreeBSD, I don't know. */
#ifdef POLLRDNORM
#define ERTS_POLL_EV_NKP_IN		ERTS_POLL_EV_N2E(POLLIN|POLLRDNORM)
#else
#define ERTS_POLL_EV_NKP_IN		ERTS_POLL_EV_N2E(POLLIN)
#endif
#define ERTS_POLL_EV_NKP_OUT		ERTS_POLL_EV_N2E(POLLOUT)
#define ERTS_POLL_EV_NKP_NVAL		ERTS_POLL_EV_N2E(POLLNVAL)
#define ERTS_POLL_EV_NKP_ERR		ERTS_POLL_EV_N2E(POLLERR|POLLHUP)

#elif ERTS_POLL_USE_SELECT	/* --- select ------------------------------ */

#define ERTS_POLL_EV_NKP_E2N(EV) (EV)
#define ERTS_POLL_EV_NKP_N2E(EV) (EV)

#define ERTS_POLL_EV_NKP_IN		(((ErtsPollEvents) 1) << 0)
#define ERTS_POLL_EV_NKP_OUT		(((ErtsPollEvents) 1) << 1)
#define ERTS_POLL_EV_NKP_NVAL		(((ErtsPollEvents) 1) << 2)
#define ERTS_POLL_EV_NKP_ERR		(((ErtsPollEvents) 1) << 3)

#endif				/* ----------------------------------------- */


#if !defined(ERTS_POLL_EV_E2N) && defined(ERTS_POLL_EV_NKP_E2N)
/* poll(), select(), and kqueue() */

#define ERTS_POLL_EV_E2N(EV) 		ERTS_POLL_EV_NKP_E2N((EV))
#define ERTS_POLL_EV_N2E(EV) 		ERTS_POLL_EV_NKP_N2E((EV))

#define ERTS_POLL_EV_IN			ERTS_POLL_EV_NKP_IN
#define ERTS_POLL_EV_OUT		ERTS_POLL_EV_NKP_OUT
#define ERTS_POLL_EV_NVAL		ERTS_POLL_EV_NKP_NVAL
#define ERTS_POLL_EV_ERR		ERTS_POLL_EV_NKP_ERR

#endif

typedef struct ErtsPollSet_ *ErtsPollSet;

typedef struct {
    ErtsSysFdType fd;
    ErtsPollEvents events;
    int on;
} ErtsPollControlEntry;

typedef struct {
    ErtsSysFdType fd;
    ErtsPollEvents events;
} ErtsPollResFd;

typedef struct {
    char *primary;
    char *fallback;
    char *kernel_poll;
    Uint memory_size;
    int poll_set_size;
    int fallback_poll_set_size;
    int lazy_updates;
    int pending_updates;
    int batch_updates;
    int concurrent_updates;
    int max_fds;
#ifdef ERTS_POLL_COUNT_AVOIDED_WAKEUPS
    long no_avoided_wakeups;
    long no_avoided_interrupts;
    long no_interrupt_timed;
#endif
} ErtsPollInfo;

#ifdef ERTS_POLL_NEED_ASYNC_INTERRUPT_SUPPORT
void		ERTS_POLL_EXPORT(erts_poll_async_sig_interrupt)(ErtsPollSet);
#endif
void		ERTS_POLL_EXPORT(erts_poll_interrupt)(ErtsPollSet,
						      int);
void		ERTS_POLL_EXPORT(erts_poll_interrupt_timed)(ErtsPollSet,
							    int,
							    ErtsMonotonicTime);
ErtsPollEvents	ERTS_POLL_EXPORT(erts_poll_control)(ErtsPollSet,
						    ErtsSysFdType,
						    ErtsPollEvents,
						    int on,
						    int* wake_poller
						    );
void		ERTS_POLL_EXPORT(erts_poll_controlv)(ErtsPollSet,
						     ErtsPollControlEntry [],
						     int on);
int		ERTS_POLL_EXPORT(erts_poll_wait)(ErtsPollSet,
						 ErtsPollResFd [],
						 int *,
						 ErtsMonotonicTime);
int		ERTS_POLL_EXPORT(erts_poll_max_fds)(void);
void		ERTS_POLL_EXPORT(erts_poll_info)(ErtsPollSet,
						 ErtsPollInfo *);
ErtsPollSet	ERTS_POLL_EXPORT(erts_poll_create_pollset)(void);
void		ERTS_POLL_EXPORT(erts_poll_destroy_pollset)(ErtsPollSet);
void		ERTS_POLL_EXPORT(erts_poll_init)(void);
void		ERTS_POLL_EXPORT(erts_poll_get_selected_events)(ErtsPollSet,
								ErtsPollEvents [],
								int);

int erts_poll_new_table_len(int old_len, int need_len);

#endif /* #ifndef ERL_POLL_H__ */