timers.cpp 4.71 KB
Newer Older
somdoron's avatar
somdoron committed
1
/*
2
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
somdoron's avatar
somdoron committed
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"
somdoron's avatar
somdoron committed
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
#include "timers.hpp"
#include "err.hpp"

zmq::timers_t::timers_t () :
tag (0xCAFEDADA),
next_timer_id (0)
{

}

zmq::timers_t::~timers_t ()
{
    //  Mark the timers as dead
    tag = 0xdeadbeef;
}

bool zmq::timers_t::check_tag ()
{
    return tag == 0xCAFEDADA;
}

int zmq::timers_t::add (size_t interval_, timers_timer_fn handler_, void* arg_)
{
    uint64_t when = clock.now_ms() + interval_;
    timer_t timer = {++next_timer_id, interval_, handler_, arg_};
    timers.insert (timersmap_t::value_type (when, timer));

    return timer.timer_id;
}

int zmq::timers_t::cancel (int timer_id_)
{
    cancelled_timers_t::iterator it = cancelled_timers.find (timer_id_);

    if (it != cancelled_timers.end ()) {
        errno = EINVAL;
        return -1;
    }

    cancelled_timers.insert (timer_id_);

    return 0;
}

int zmq::timers_t::set_interval (int timer_id_, size_t interval_)
{
    for (timersmap_t::iterator it = timers.begin (); it != timers.end (); ++it) {
        if (it->second.timer_id == timer_id_) {
            timer_t timer = it->second;
            timer.interval = interval_;
            uint64_t when = clock.now_ms() + interval_;
            timers.erase (it);
                timers.insert (timersmap_t::value_type (when, timer));

            return 0;
        }
    }

    errno = EINVAL;
    return -1;
}

int zmq::timers_t::reset (int timer_id_) {
    for (timersmap_t::iterator it = timers.begin (); it != timers.end (); ++it) {
        if (it->second.timer_id == timer_id_) {
            timer_t timer = it->second;
            uint64_t when = clock.now_ms() + timer.interval;
            timers.erase (it);
            timers.insert (timersmap_t::value_type (when, timer));

            return 0;
        }
    }

    errno = EINVAL;
    return -1;
}

long zmq::timers_t::timeout ()
{
    timersmap_t::iterator it = timers.begin ();

    uint64_t now = clock.now_ms();

    while (it != timers.end ()) {
        cancelled_timers_t::iterator cancelled_it = cancelled_timers.find (it->second.timer_id);

        //  Live timer, lets return the timeout
        if (cancelled_it == cancelled_timers.end ()) {
            if (it->first > now)
121
                return (long) (it->first - now);
somdoron's avatar
somdoron committed
122
            else
123
                return 0;
somdoron's avatar
somdoron committed
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
        }

        // Let's remove it from the begining of the list
        timersmap_t::iterator old = it;
        ++it;
        timers.erase (old);
        cancelled_timers.erase (cancelled_it);
    }

    //  Wait forever as no timers are alive
    return -1;
}

int zmq::timers_t::execute ()
{
    timersmap_t::iterator it = timers.begin ();

    uint64_t now = clock.now_ms();

    while (it != timers.end ()) {
        cancelled_timers_t::iterator cancelled_it = cancelled_timers.find (it->second.timer_id);

        //  Dead timer, lets remove it and continue
        if (cancelled_it != cancelled_timers.end ()) {
            timersmap_t::iterator old = it;
            ++it;
            timers.erase (old);
            cancelled_timers.erase (cancelled_it);
            continue;
        }

        //  Map is ordered, if we have to wait for current timer we can stop.
        if (it->first > now)
        break;

        timer_t timer = it->second;

        timer.handler (timer.timer_id, timer.arg);

        timersmap_t::iterator old = it;
        ++it;
        timers.erase (old);
        timers.insert (timersmap_t::value_type (now + timer.interval, timer));
    }

    return 0;
}