forked from zeromq/libzmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket_poller.cpp
668 lines (570 loc) · 18 KB
/
socket_poller.cpp
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/* SPDX-License-Identifier: MPL-2.0 */
#include "precompiled.hpp"
#include "socket_poller.hpp"
#include "err.hpp"
#include "polling_util.hpp"
#include "macros.hpp"
#include <limits.h>
static bool is_thread_safe (const zmq::socket_base_t &socket_)
{
// do not use getsockopt here, since that would fail during context termination
return socket_.is_thread_safe ();
}
// compare elements to value
template <class It, class T, class Pred>
static It find_if2 (It b_, It e_, const T &value, Pred pred)
{
for (; b_ != e_; ++b_) {
if (pred (*b_, value)) {
break;
}
}
return b_;
}
zmq::socket_poller_t::socket_poller_t () :
_tag (0xCAFEBABE),
_signaler (NULL)
#if defined ZMQ_POLL_BASED_ON_POLL
,
_pollfds (NULL)
#elif defined ZMQ_POLL_BASED_ON_SELECT
,
_max_fd (0)
#endif
{
rebuild ();
}
zmq::socket_poller_t::~socket_poller_t ()
{
// Mark the socket_poller as dead
_tag = 0xdeadbeef;
for (items_t::iterator it = _items.begin (), end = _items.end (); it != end;
++it) {
// TODO shouldn't this zmq_assert (it->socket->check_tag ()) instead?
if (it->socket && it->socket->check_tag ()
&& is_thread_safe (*it->socket)) {
it->socket->remove_signaler (_signaler);
}
}
if (_signaler != NULL) {
LIBZMQ_DELETE (_signaler);
}
#if defined ZMQ_POLL_BASED_ON_POLL
if (_pollfds) {
free (_pollfds);
_pollfds = NULL;
}
#endif
}
bool zmq::socket_poller_t::check_tag () const
{
return _tag == 0xCAFEBABE;
}
int zmq::socket_poller_t::signaler_fd (fd_t *fd_) const
{
if (_signaler) {
*fd_ = _signaler->get_fd ();
return 0;
}
// Only thread-safe socket types are guaranteed to have a signaler.
errno = EINVAL;
return -1;
}
int zmq::socket_poller_t::add (socket_base_t *socket_,
void *user_data_,
short events_)
{
if (find_if2 (_items.begin (), _items.end (), socket_, &is_socket)
!= _items.end ()) {
errno = EINVAL;
return -1;
}
if (is_thread_safe (*socket_)) {
if (_signaler == NULL) {
_signaler = new (std::nothrow) signaler_t ();
if (!_signaler) {
errno = ENOMEM;
return -1;
}
if (!_signaler->valid ()) {
delete _signaler;
_signaler = NULL;
errno = EMFILE;
return -1;
}
}
socket_->add_signaler (_signaler);
}
const item_t item = {
socket_,
0,
user_data_,
events_
#if defined ZMQ_POLL_BASED_ON_POLL
,
-1
#endif
};
try {
_items.push_back (item);
}
catch (const std::bad_alloc &) {
errno = ENOMEM;
return -1;
}
_need_rebuild = true;
return 0;
}
int zmq::socket_poller_t::add_fd (fd_t fd_, void *user_data_, short events_)
{
if (find_if2 (_items.begin (), _items.end (), fd_, &is_fd)
!= _items.end ()) {
errno = EINVAL;
return -1;
}
const item_t item = {
NULL,
fd_,
user_data_,
events_
#if defined ZMQ_POLL_BASED_ON_POLL
,
-1
#endif
};
try {
_items.push_back (item);
}
catch (const std::bad_alloc &) {
errno = ENOMEM;
return -1;
}
_need_rebuild = true;
return 0;
}
int zmq::socket_poller_t::modify (const socket_base_t *socket_, short events_)
{
const items_t::iterator it =
find_if2 (_items.begin (), _items.end (), socket_, &is_socket);
if (it == _items.end ()) {
errno = EINVAL;
return -1;
}
it->events = events_;
_need_rebuild = true;
return 0;
}
int zmq::socket_poller_t::modify_fd (fd_t fd_, short events_)
{
const items_t::iterator it =
find_if2 (_items.begin (), _items.end (), fd_, &is_fd);
if (it == _items.end ()) {
errno = EINVAL;
return -1;
}
it->events = events_;
_need_rebuild = true;
return 0;
}
int zmq::socket_poller_t::remove (socket_base_t *socket_)
{
const items_t::iterator it =
find_if2 (_items.begin (), _items.end (), socket_, &is_socket);
if (it == _items.end ()) {
errno = EINVAL;
return -1;
}
_items.erase (it);
_need_rebuild = true;
if (is_thread_safe (*socket_)) {
socket_->remove_signaler (_signaler);
}
return 0;
}
int zmq::socket_poller_t::remove_fd (fd_t fd_)
{
const items_t::iterator it =
find_if2 (_items.begin (), _items.end (), fd_, &is_fd);
if (it == _items.end ()) {
errno = EINVAL;
return -1;
}
_items.erase (it);
_need_rebuild = true;
return 0;
}
int zmq::socket_poller_t::rebuild ()
{
_use_signaler = false;
_pollset_size = 0;
_need_rebuild = false;
#if defined ZMQ_POLL_BASED_ON_POLL
if (_pollfds) {
free (_pollfds);
_pollfds = NULL;
}
for (items_t::iterator it = _items.begin (), end = _items.end (); it != end;
++it) {
if (it->events) {
if (it->socket && is_thread_safe (*it->socket)) {
if (!_use_signaler) {
_use_signaler = true;
_pollset_size++;
}
} else
_pollset_size++;
}
}
if (_pollset_size == 0)
return 0;
_pollfds = static_cast<pollfd *> (malloc (_pollset_size * sizeof (pollfd)));
if (!_pollfds) {
errno = ENOMEM;
_need_rebuild = true;
return -1;
}
int item_nbr = 0;
if (_use_signaler) {
item_nbr = 1;
_pollfds[0].fd = _signaler->get_fd ();
_pollfds[0].events = POLLIN;
}
for (items_t::iterator it = _items.begin (), end = _items.end (); it != end;
++it) {
if (it->events) {
if (it->socket) {
if (!is_thread_safe (*it->socket)) {
size_t fd_size = sizeof (zmq::fd_t);
const int rc = it->socket->getsockopt (
ZMQ_FD, &_pollfds[item_nbr].fd, &fd_size);
zmq_assert (rc == 0);
_pollfds[item_nbr].events = POLLIN;
item_nbr++;
}
} else {
_pollfds[item_nbr].fd = it->fd;
_pollfds[item_nbr].events =
(it->events & ZMQ_POLLIN ? POLLIN : 0)
| (it->events & ZMQ_POLLOUT ? POLLOUT : 0)
| (it->events & ZMQ_POLLPRI ? POLLPRI : 0);
it->pollfd_index = item_nbr;
item_nbr++;
}
}
}
#elif defined ZMQ_POLL_BASED_ON_SELECT
// Ensure we do not attempt to select () on more than FD_SETSIZE
// file descriptors.
zmq_assert (_items.size () <= FD_SETSIZE);
_pollset_in.resize (_items.size ());
_pollset_out.resize (_items.size ());
_pollset_err.resize (_items.size ());
FD_ZERO (_pollset_in.get ());
FD_ZERO (_pollset_out.get ());
FD_ZERO (_pollset_err.get ());
for (items_t::iterator it = _items.begin (), end = _items.end (); it != end;
++it) {
if (it->socket && is_thread_safe (*it->socket) && it->events) {
_use_signaler = true;
FD_SET (_signaler->get_fd (), _pollset_in.get ());
_pollset_size = 1;
break;
}
}
_max_fd = 0;
// Build the fd_sets for passing to select ().
for (items_t::iterator it = _items.begin (), end = _items.end (); it != end;
++it) {
if (it->events) {
// If the poll item is a 0MQ socket we are interested in input on the
// notification file descriptor retrieved by the ZMQ_FD socket option.
if (it->socket) {
if (!is_thread_safe (*it->socket)) {
zmq::fd_t notify_fd;
size_t fd_size = sizeof (zmq::fd_t);
int rc =
it->socket->getsockopt (ZMQ_FD, ¬ify_fd, &fd_size);
zmq_assert (rc == 0);
FD_SET (notify_fd, _pollset_in.get ());
if (_max_fd < notify_fd)
_max_fd = notify_fd;
_pollset_size++;
}
}
// Else, the poll item is a raw file descriptor. Convert the poll item
// events to the appropriate fd_sets.
else {
if (it->events & ZMQ_POLLIN)
FD_SET (it->fd, _pollset_in.get ());
if (it->events & ZMQ_POLLOUT)
FD_SET (it->fd, _pollset_out.get ());
if (it->events & ZMQ_POLLERR)
FD_SET (it->fd, _pollset_err.get ());
if (_max_fd < it->fd)
_max_fd = it->fd;
_pollset_size++;
}
}
}
#endif
return 0;
}
void zmq::socket_poller_t::zero_trail_events (
zmq::socket_poller_t::event_t *events_, int n_events_, int found_)
{
for (int i = found_; i < n_events_; ++i) {
events_[i].socket = NULL;
events_[i].fd = zmq::retired_fd;
events_[i].user_data = NULL;
events_[i].events = 0;
}
}
#if defined ZMQ_POLL_BASED_ON_POLL
int zmq::socket_poller_t::check_events (zmq::socket_poller_t::event_t *events_,
int n_events_)
#elif defined ZMQ_POLL_BASED_ON_SELECT
int zmq::socket_poller_t::check_events (zmq::socket_poller_t::event_t *events_,
int n_events_,
fd_set &inset_,
fd_set &outset_,
fd_set &errset_)
#endif
{
int found = 0;
for (items_t::iterator it = _items.begin (), end = _items.end ();
it != end && found < n_events_; ++it) {
// The poll item is a 0MQ socket. Retrieve pending events
// using the ZMQ_EVENTS socket option.
if (it->socket) {
size_t events_size = sizeof (uint32_t);
uint32_t events;
if (it->socket->getsockopt (ZMQ_EVENTS, &events, &events_size)
== -1) {
return -1;
}
if (it->events & events) {
events_[found].socket = it->socket;
events_[found].fd = zmq::retired_fd;
events_[found].user_data = it->user_data;
events_[found].events = it->events & events;
++found;
}
}
// Else, the poll item is a raw file descriptor, simply convert
// the events to zmq_pollitem_t-style format.
else if (it->events) {
#if defined ZMQ_POLL_BASED_ON_POLL
zmq_assert (it->pollfd_index >= 0);
const short revents = _pollfds[it->pollfd_index].revents;
short events = 0;
if (revents & POLLIN)
events |= ZMQ_POLLIN;
if (revents & POLLOUT)
events |= ZMQ_POLLOUT;
if (revents & POLLPRI)
events |= ZMQ_POLLPRI;
if (revents & ~(POLLIN | POLLOUT | POLLPRI))
events |= ZMQ_POLLERR;
#elif defined ZMQ_POLL_BASED_ON_SELECT
short events = 0;
if (FD_ISSET (it->fd, &inset_))
events |= ZMQ_POLLIN;
if (FD_ISSET (it->fd, &outset_))
events |= ZMQ_POLLOUT;
if (FD_ISSET (it->fd, &errset_))
events |= ZMQ_POLLERR;
#endif //POLL_SELECT
if (events) {
events_[found].socket = NULL;
events_[found].fd = it->fd;
events_[found].user_data = it->user_data;
events_[found].events = events;
++found;
}
}
}
return found;
}
//Return 0 if timeout is expired otherwise 1
int zmq::socket_poller_t::adjust_timeout (zmq::clock_t &clock_,
long timeout_,
uint64_t &now_,
uint64_t &end_,
bool &first_pass_)
{
// If socket_poller_t::timeout is zero, exit immediately whether there
// are events or not.
if (timeout_ == 0)
return 0;
// At this point we are meant to wait for events but there are none.
// If timeout is infinite we can just loop until we get some events.
if (timeout_ < 0) {
if (first_pass_)
first_pass_ = false;
return 1;
}
// The timeout is finite and there are no events. In the first pass
// we get a timestamp of when the polling have begun. (We assume that
// first pass have taken negligible time). We also compute the time
// when the polling should time out.
now_ = clock_.now_ms ();
if (first_pass_) {
end_ = now_ + timeout_;
first_pass_ = false;
return 1;
}
// Find out whether timeout have expired.
if (now_ >= end_)
return 0;
return 1;
}
int zmq::socket_poller_t::wait (zmq::socket_poller_t::event_t *events_,
int n_events_,
long timeout_)
{
if (_items.empty () && timeout_ < 0) {
errno = EFAULT;
return -1;
}
if (_need_rebuild) {
const int rc = rebuild ();
if (rc == -1)
return -1;
}
if (unlikely (_pollset_size == 0)) {
if (timeout_ < 0) {
// Fail instead of trying to sleep forever
errno = EFAULT;
return -1;
}
// We'll report an error (timed out) as if the list was non-empty and
// no event occurred within the specified timeout. Otherwise the caller
// needs to check the return value AND the event to avoid using the
// nullified event data.
errno = EAGAIN;
if (timeout_ == 0)
return -1;
#if defined ZMQ_HAVE_WINDOWS
Sleep (timeout_ > 0 ? timeout_ : INFINITE);
return -1;
#elif defined ZMQ_HAVE_ANDROID
usleep (timeout_ * 1000);
return -1;
#elif defined ZMQ_HAVE_OSX
usleep (timeout_ * 1000);
errno = EAGAIN;
return -1;
#elif defined ZMQ_HAVE_VXWORKS
struct timespec ns_;
ns_.tv_sec = timeout_ / 1000;
ns_.tv_nsec = timeout_ % 1000 * 1000000;
nanosleep (&ns_, 0);
return -1;
#else
usleep (timeout_ * 1000);
return -1;
#endif
}
#if defined ZMQ_POLL_BASED_ON_POLL
zmq::clock_t clock;
uint64_t now = 0;
uint64_t end = 0;
bool first_pass = true;
while (true) {
// Compute the timeout for the subsequent poll.
int timeout;
if (first_pass)
timeout = 0;
else if (timeout_ < 0)
timeout = -1;
else
timeout =
static_cast<int> (std::min<uint64_t> (end - now, INT_MAX));
// Wait for events.
const int rc = poll (_pollfds, _pollset_size, timeout);
if (rc == -1 && errno == EINTR) {
return -1;
}
errno_assert (rc >= 0);
// Receive the signal from pollfd
if (_use_signaler && _pollfds[0].revents & POLLIN)
_signaler->recv ();
// Check for the events.
const int found = check_events (events_, n_events_);
if (found) {
if (found > 0)
zero_trail_events (events_, n_events_, found);
return found;
}
// Adjust timeout or break
if (adjust_timeout (clock, timeout_, now, end, first_pass) == 0)
break;
}
errno = EAGAIN;
return -1;
#elif defined ZMQ_POLL_BASED_ON_SELECT
zmq::clock_t clock;
uint64_t now = 0;
uint64_t end = 0;
bool first_pass = true;
optimized_fd_set_t inset (_pollset_size);
optimized_fd_set_t outset (_pollset_size);
optimized_fd_set_t errset (_pollset_size);
while (true) {
// Compute the timeout for the subsequent poll.
timeval timeout;
timeval *ptimeout;
if (first_pass) {
timeout.tv_sec = 0;
timeout.tv_usec = 0;
ptimeout = &timeout;
} else if (timeout_ < 0)
ptimeout = NULL;
else {
timeout.tv_sec = static_cast<long> ((end - now) / 1000);
timeout.tv_usec = static_cast<long> ((end - now) % 1000 * 1000);
ptimeout = &timeout;
}
// Wait for events. Ignore interrupts if there's infinite timeout.
memcpy (inset.get (), _pollset_in.get (),
valid_pollset_bytes (*_pollset_in.get ()));
memcpy (outset.get (), _pollset_out.get (),
valid_pollset_bytes (*_pollset_out.get ()));
memcpy (errset.get (), _pollset_err.get (),
valid_pollset_bytes (*_pollset_err.get ()));
const int rc = select (static_cast<int> (_max_fd + 1), inset.get (),
outset.get (), errset.get (), ptimeout);
#if defined ZMQ_HAVE_WINDOWS
if (unlikely (rc == SOCKET_ERROR)) {
errno = wsa_error_to_errno (WSAGetLastError ());
wsa_assert (errno == ENOTSOCK);
return -1;
}
#else
if (unlikely (rc == -1)) {
errno_assert (errno == EINTR || errno == EBADF);
return -1;
}
#endif
if (_use_signaler && FD_ISSET (_signaler->get_fd (), inset.get ()))
_signaler->recv ();
// Check for the events.
const int found = check_events (events_, n_events_, *inset.get (),
*outset.get (), *errset.get ());
if (found) {
if (found > 0)
zero_trail_events (events_, n_events_, found);
return found;
}
// Adjust timeout or break
if (adjust_timeout (clock, timeout_, now, end, first_pass) == 0)
break;
}
errno = EAGAIN;
return -1;
#else
// Exotic platforms that support neither poll() nor select().
errno = ENOTSUP;
return -1;
#endif
}