socket_poller.cpp 21.3 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
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

    This file is part of libzmq, the ZeroMQ core engine in C++.

    libzmq is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

30
#include "precompiled.hpp"
31 32 33 34 35
#include "socket_poller.hpp"
#include "err.hpp"

zmq::socket_poller_t::socket_poller_t () :
    tag (0xCAFEBABE),
36
    signaler (NULL),
37
    need_rebuild (true),
38 39
    use_signaler (false),
    poll_size(0)
40
#if defined ZMQ_POLL_BASED_ON_POLL
41 42
    ,
    pollfds (NULL)
43 44 45
#elif defined ZMQ_POLL_BASED_ON_SELECT
    ,
    maxfd(0)
46
#endif
47
{
48
#if defined ZMQ_POLL_BASED_ON_SELECT
49 50 51 52 53 54 55 56
#if defined ZMQ_HAVE_WINDOWS
    // On Windows fd_set contains array of SOCKETs, each 4 bytes.
    // For large fd_sets memset() could be expensive and it is unnecessary.
    // It is enough to set fd_count to 0, exactly what FD_ZERO() macro does.
    FD_ZERO (&pollset_in);
    FD_ZERO (&pollset_out);
    FD_ZERO (&pollset_err);
#else
57
    memset(&pollset_in, 0, sizeof(pollset_in));
58 59 60
    memset(&pollset_out, 0, sizeof(pollset_out));
    memset(&pollset_err, 0, sizeof(pollset_err));
#endif
61
#endif
62 63 64 65 66
}

zmq::socket_poller_t::~socket_poller_t ()
{
    //  Mark the socket_poller as dead
67
    tag = 0xdeadbeef;
68

69
    for (items_t::iterator it = items.begin(); it != items.end(); ++it) {
70
        if (it->socket && it->socket->check_tag()) {
71 72 73
            int thread_safe;
            size_t thread_safe_size = sizeof(int);

74
            if (it->socket->getsockopt (ZMQ_THREAD_SAFE, &thread_safe, &thread_safe_size) == 0 && thread_safe)
75
                it->socket->remove_signaler (signaler);
76 77 78
        }
    }

79 80 81 82 83
    if (signaler != NULL) {
        delete signaler;
        signaler = NULL;
    }

84 85 86 87
#if defined ZMQ_POLL_BASED_ON_POLL
    if (pollfds) {
        free (pollfds);
        pollfds = NULL;
88
    }
89
#endif
90 91
}

92
bool zmq::socket_poller_t::check_tag ()
93 94 95 96
{
    return tag == 0xCAFEBABE;
}

97
int zmq::socket_poller_t::add (socket_base_t *socket_, void* user_data_, short events_)
98
{
99
    for (items_t::iterator it = items.begin (); it != items.end (); ++it) {
100 101 102
        if (it->socket == socket_) {
            errno = EINVAL;
            return -1;
103
        }
104 105 106 107
    }

    int thread_safe;
    size_t thread_safe_size = sizeof(int);
108

109 110 111
    int rc =
      socket_->getsockopt (ZMQ_THREAD_SAFE, &thread_safe, &thread_safe_size);
    zmq_assert (rc == 0);
112 113

    if (thread_safe) {
114 115 116
        if (signaler == NULL)
            signaler = new signaler_t ();

117 118
        rc = socket_->add_signaler (signaler);
        zmq_assert (rc == 0);
119
    }
120

121 122 123 124 125
    item_t item = {socket_, 0, user_data_, events_
#if defined ZMQ_POLL_BASED_ON_POLL
                   ,-1
#endif
    };
126
    items.push_back (item);
127 128 129 130 131
    need_rebuild = true;

    return 0;
}

132
int zmq::socket_poller_t::add_fd (fd_t fd_, void *user_data_, short events_)
133
{
134
   for (items_t::iterator it = items.begin (); it != items.end (); ++it) {
135 136 137
        if (!it->socket && it->fd == fd_) {
            errno = EINVAL;
            return -1;
138
        }
139 140
    }

141 142 143 144 145
    item_t item = {NULL, fd_, user_data_, events_
#if defined ZMQ_POLL_BASED_ON_POLL
                   ,-1
#endif
                   };
146
    items.push_back (item);
147 148 149 150 151
    need_rebuild = true;

    return 0;
}

152
int zmq::socket_poller_t::modify (socket_base_t  *socket_, short events_)
153
{
154
    items_t::iterator it;
155

156
    for (it = items.begin (); it != items.end (); ++it) {
157 158 159 160
        if (it->socket == socket_)
            break;
    }

161
    if (it == items.end()) {
162 163 164 165 166 167 168 169 170 171 172
        errno = EINVAL;
        return -1;
    }

    it->events = events_;
    need_rebuild = true;

    return 0;
}


173
int zmq::socket_poller_t::modify_fd (fd_t fd_, short events_)
174
{
175
    items_t::iterator it;
176

177
    for (it = items.begin (); it != items.end (); ++it) {
178 179 180 181
        if (!it->socket && it->fd == fd_)
            break;
    }

182
    if (it == items.end()) {
183 184 185
        errno = EINVAL;
        return -1;
    }
186

187 188 189 190
    it->events = events_;
    need_rebuild = true;

    return 0;
191
}
192 193


194
int zmq::socket_poller_t::remove (socket_base_t *socket_)
195
{
196
    items_t::iterator it;
197

198
    for (it = items.begin (); it != items.end (); ++it) {
199 200 201 202
        if (it->socket == socket_)
            break;
    }

203
    if (it == items.end()) {
204 205 206
        errno = EINVAL;
        return -1;
    }
207

208 209 210
    items.erase(it);
    need_rebuild = true;

211 212 213
    int thread_safe;
    size_t thread_safe_size = sizeof(int);

214
    if (socket_->getsockopt (ZMQ_THREAD_SAFE, &thread_safe, &thread_safe_size) == 0 && thread_safe)
215
        socket_->remove_signaler (signaler);
216

217 218 219
    return 0;
}

220
int zmq::socket_poller_t::remove_fd (fd_t fd_)
221
{
222
    items_t::iterator it;
223

224
    for (it = items.begin (); it != items.end (); ++it) {
225 226 227 228
        if (!it->socket && it->fd == fd_)
            break;
    }

229
    if (it == items.end()) {
230 231 232
        errno = EINVAL;
        return -1;
    }
233

234
    items.erase (it);
235 236 237
    need_rebuild = true;

    return 0;
238
}
239

240
void zmq::socket_poller_t::rebuild ()
241
{
242
#if defined ZMQ_POLL_BASED_ON_POLL
243

244 245 246 247
    if (pollfds) {
        free (pollfds);
        pollfds = NULL;
    }
248

249 250 251 252 253 254 255 256 257 258
    use_signaler = false;

    poll_size = 0;

    for (items_t::iterator it = items.begin (); it != items.end (); ++it) {
        if (it->events) {
            if (it->socket) {
                int thread_safe;
                size_t thread_safe_size = sizeof(int);

259 260 261
                int rc = it->socket->getsockopt (ZMQ_THREAD_SAFE, &thread_safe,
                                                 &thread_safe_size);
                zmq_assert (rc == 0);
262 263 264 265 266

                if (thread_safe) {
                    if (!use_signaler) {
                        use_signaler = true;
                        poll_size++;
267
                    }
268 269 270 271
                }
                else
                    poll_size++;
            }
272 273
            else
                poll_size++;
274
        }
275
    }
276

277
    if (poll_size == 0)
278
        return;
279

280 281 282 283 284 285 286
    pollfds = (pollfd*) malloc (poll_size * sizeof (pollfd));
    alloc_assert (pollfds);

    int item_nbr = 0;

    if (use_signaler) {
        item_nbr = 1;
287
        pollfds[0].fd = signaler->get_fd();
288
        pollfds[0].events = POLLIN;
289 290
    }

291
    for (items_t::iterator it = items.begin (); it != items.end (); ++it) {
292 293 294 295 296
        if (it->events) {
            if (it->socket) {
                int thread_safe;
                size_t thread_safe_size = sizeof(int);

297 298 299
                int rc = it->socket->getsockopt (ZMQ_THREAD_SAFE, &thread_safe,
                                                 &thread_safe_size);
                zmq_assert (rc == 0);
300

301
                if (!thread_safe) {
302
                    size_t fd_size = sizeof (zmq::fd_t);
303 304
                    rc = it->socket->getsockopt (ZMQ_FD, &pollfds [item_nbr].fd, &fd_size);
                    zmq_assert (rc == 0);
305

306 307 308 309 310 311 312 313 314 315 316
                    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;
317
                item_nbr++;
318 319 320
            }
        }
    }
321

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
 #elif defined ZMQ_POLL_BASED_ON_SELECT

    FD_ZERO (&pollset_in);
    FD_ZERO (&pollset_out);
    FD_ZERO (&pollset_err);

    //  Ensure we do not attempt to select () on more than FD_SETSIZE
    //  file descriptors.
    zmq_assert (items.size () <= FD_SETSIZE);

    poll_size = 0;

    use_signaler = false;

    for (items_t::iterator it = items.begin (); it != items.end (); ++it) {
        if (it->socket) {
            int thread_safe;
            size_t thread_safe_size = sizeof(int);

341 342 343
            int rc = it->socket->getsockopt (ZMQ_THREAD_SAFE, &thread_safe,
                                             &thread_safe_size);
            zmq_assert (rc == 0);
344 345 346

            if (thread_safe && it->events) {
                use_signaler = true;
347
                FD_SET (signaler->get_fd (), &pollset_in);
348 349 350
                poll_size = 1;
                break;
            }
351 352
        }
    }
353 354 355 356 357 358 359 360 361 362 363 364

    maxfd = 0;

    //  Build the fd_sets for passing to select ().
    for (items_t::iterator it = items.begin (); it != items.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) {
                int thread_safe;
                size_t thread_safe_size = sizeof(int);

365 366 367
                int rc = it->socket->getsockopt (ZMQ_THREAD_SAFE, &thread_safe,
                                                 &thread_safe_size);
                zmq_assert (rc == 0);
368 369 370 371

                if (!thread_safe) {
                    zmq::fd_t notify_fd;
                    size_t fd_size = sizeof (zmq::fd_t);
372 373
                    rc = it->socket->getsockopt (ZMQ_FD, &notify_fd, &fd_size);
                    zmq_assert (rc == 0);
374 375 376

                    FD_SET (notify_fd, &pollset_in);
                    if (maxfd < notify_fd)
377
                        maxfd = notify_fd;
378

379
                    poll_size++;
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
                }
            }
            //  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);
                if (it->events & ZMQ_POLLOUT)
                    FD_SET (it->fd, &pollset_out);
                if (it->events & ZMQ_POLLERR)
                    FD_SET (it->fd, &pollset_err);
                if (maxfd < it->fd)
                    maxfd = it->fd;

                poll_size++;
            }
        }
    }

#endif

    need_rebuild = false;
402 403
}

404
int zmq::socket_poller_t::wait (zmq::socket_poller_t::event_t *events_, int n_events_, long timeout_)
405
{
406 407 408 409 410
    if (items.empty () && timeout_ < 0) {
        errno = EFAULT;
        return -1;
    }

411
    if (need_rebuild)
412
        rebuild ();
413 414 415

#if defined ZMQ_POLL_BASED_ON_POLL
    if (unlikely (poll_size == 0)) {
416
        // We'll report an error (timed out) as if the list was non-empty and
417
        // no event occurred within the specified timeout. Otherwise the caller
418 419
        // needs to check the return value AND the event to avoid using the
        // nullified event data.
420
        errno = EAGAIN;
421
        if (timeout_ == 0)
422
            return -1;
423 424
#if defined ZMQ_HAVE_WINDOWS
        Sleep (timeout_ > 0 ? timeout_ : INFINITE);
425
        return -1;
426 427
#elif defined ZMQ_HAVE_ANDROID
        usleep (timeout_ * 1000);
428
        return -1;
429
#else
430 431
        usleep (timeout_ * 1000);
        return -1;
432
#endif
433 434
    }

435 436 437
    zmq::clock_t clock;
    uint64_t now = 0;
    uint64_t end = 0;
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
    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 = end - now;

        //  Wait for events.
        while (true) {
            int rc = poll (pollfds, poll_size, timeout);
            if (rc == -1 && errno == EINTR) {
                return -1;
            }
            errno_assert (rc >= 0);
            break;
        }

        //  Receive the signal from pollfd
        if (use_signaler && pollfds[0].revents & POLLIN)
464
            signaler->recv ();
465 466

        //  Check for the events.
467 468
        int found = 0;
        for (items_t::iterator it = items.begin (); it != items.end () && found < n_events_; ++it) {
469

470 471 472 473
            events_[found].socket = NULL;
            events_[found].fd = 0;
            events_[found].user_data = NULL;
            events_[found].events = 0;
474 475 476 477 478 479 480 481 482 483 484

            //  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) {
485 486 487 488
                    events_[found].socket = it->socket;
                    events_[found].user_data = it->user_data;
                    events_[found].events = it->events & events;
                    ++found;
489
                }
490 491 492 493 494
            }
            //  Else, the poll item is a raw file descriptor, simply convert
            //  the events to zmq_pollitem_t-style format.
            else {
                short revents = pollfds [it->pollfd_index].revents;
495 496
                short events = 0;

497 498 499 500 501 502 503 504 505 506
                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;

                if (events) {
507 508 509 510 511
                    events_[found].socket = NULL;
                    events_[found].user_data = it->user_data;
                    events_[found].fd = it->fd;
                    events_[found].events = events;
                    ++found;
512 513 514
                }
            }
        }
515
        if (found) {
516 517 518 519 520 521 522
            for (int i = found; i < n_events_; ++i) {
                events_[i].socket = NULL;
                events_[i].fd = 0;
                events_[i].user_data = NULL;
                events_[i].events = 0;
            }
            return found;
523
        }
524 525 526 527 528 529 530 531 532 533 534 535 536

        //  If timeout is zero, exit immediately whether there are events or not.
        if (timeout_ == 0)
            break;

        //  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;
            continue;
        }

537 538 539 540 541
        //  The timeout is finite but non-zero 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 ();
542 543 544 545 546 547 548 549 550
        if (first_pass) {
            end = now + timeout_;
            first_pass = false;
            continue;
        }

        //  Find out whether timeout have expired.
        if (now >= end)
            break;
551
    }
552
    errno = EAGAIN;
553 554 555 556 557
    return -1;

#elif defined ZMQ_POLL_BASED_ON_SELECT

    if (unlikely (poll_size == 0)) {
558 559 560 561
        // We'll report an error (timed out) as if the list was non-empty and
        // no event occured within the specified timeout. Otherwise the caller
        // needs to check the return value AND the event to avoid using the
        // nullified event data.
562
        errno = EAGAIN;
563
        if (timeout_ == 0)
564
            return -1;
565 566
#if defined ZMQ_HAVE_WINDOWS
        Sleep (timeout_ > 0 ? timeout_ : INFINITE);
567
        return -1;
568
#else
569 570
        usleep (timeout_ * 1000);
        return -1;
571 572 573 574 575 576
#endif
    }
    zmq::clock_t clock;
    uint64_t now = 0;
    uint64_t end = 0;

577
    bool first_pass = true;
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
    fd_set inset, outset, errset;

    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 = (long) ((end - now) / 1000);
            timeout.tv_usec = (long) ((end - now) % 1000 * 1000);
            ptimeout = &timeout;
        }

        //  Wait for events. Ignore interrupts if there's infinite timeout.
        while (true) {
#if defined ZMQ_HAVE_WINDOWS
602 603 604 605
            // On Windows we don't need to copy the whole fd_set.
            // SOCKETS are continuous from the beginning of fd_array in fd_set.
            // We just need to copy fd_count elements of fd_array.
            // We gain huge memcpy() improvement if number of used SOCKETs is much lower than FD_SETSIZE.
606 607 608
            memcpy (&inset,  &pollset_in,  (char *) (pollset_in.fd_array  + pollset_in.fd_count ) - (char *) &pollset_in );
            memcpy (&outset, &pollset_out, (char *) (pollset_out.fd_array + pollset_out.fd_count) - (char *) &pollset_out);
            memcpy (&errset, &pollset_err, (char *) (pollset_err.fd_array + pollset_err.fd_count) - (char *) &pollset_err);
609 610 611 612 613 614 615
            int rc = select (0, &inset, &outset, &errset, ptimeout);
            if (unlikely (rc == SOCKET_ERROR)) {
                errno = zmq::wsa_error_to_errno (WSAGetLastError ());
                wsa_assert (errno == ENOTSOCK);
                return -1;
            }
#else
616 617 618
            memcpy (&inset, &pollset_in, sizeof (fd_set));
            memcpy (&outset, &pollset_out, sizeof (fd_set));
            memcpy (&errset, &pollset_err, sizeof (fd_set));
619 620 621 622 623 624 625 626 627
            int rc = select (maxfd + 1, &inset, &outset, &errset, ptimeout);
            if (unlikely (rc == -1)) {
                errno_assert (errno == EINTR || errno == EBADF);
                return -1;
            }
#endif
            break;
        }

628 629
        if (use_signaler && FD_ISSET (signaler->get_fd (), &inset))
            signaler->recv ();
630 631

        //  Check for the events.
632 633
        int found = 0;
        for (items_t::iterator it = items.begin (); it != items.end () && found < n_events_; ++it) {
634 635 636 637 638 639 640 641 642 643

            //  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) {
644 645 646 647
                    events_[found].socket = it->socket;
                    events_[found].user_data = it->user_data;
                    events_[found].events = it->events & events;
                    ++found;
648
                }
649 650 651 652 653 654 655 656 657 658 659 660
            }
            //  Else, the poll item is a raw file descriptor, simply convert
            //  the events to zmq_pollitem_t-style format.
            else {
                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;
661

662
                if (events) {
663 664 665 666 667
                    events_[found].socket = NULL;
                    events_[found].user_data = it->user_data;
                    events_[found].fd = it->fd;
                    events_[found].events = events;
                    ++found;
668 669 670
                }
            }
        }
671
        if (found) {
672 673 674 675 676 677 678 679
            // zero-out remaining events
            for (int i = found; i < n_events_; ++i) {
                events_[i].socket = NULL;
                events_[i].fd = 0;
                events_[i].user_data = NULL;
                events_[i].events = 0;
            }
            return found;
680
        }
681

682 683 684
        //  If timeout is zero, exit immediately whether there are events or not.
        if (timeout_ == 0)
            break;
685

686 687 688 689 690 691 692
        //  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;
            continue;
        }
693

694 695 696 697
        //  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.
698
        now = clock.now_ms ();
699 700 701 702 703
        if (first_pass) {
            end = now + timeout_;
            first_pass = false;
            continue;
        }
704

705 706 707
        //  Find out whether timeout have expired.
        if (now >= end)
            break;
708 709
    }

710
    errno = EAGAIN;
711 712 713 714 715 716 717
    return -1;

#else
    //  Exotic platforms that support neither poll() nor select().
    errno = ENOTSUP;
    return -1;
#endif
718
}