timers.hpp 3.21 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 30 31 32 33 34 35 36 37 38 39 40

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/>.
*/

#ifndef __ZMQ_TIMERS_HPP_INCLUDED__
#define __ZMQ_TIMERS_HPP_INCLUDED__

#include <stddef.h>
#include <map>
#include <set>

#include "clock.hpp"

namespace zmq
{
41
typedef void(timers_timer_fn) (int timer_id_, void *arg_);
somdoron's avatar
somdoron committed
42

43 44 45 46 47
class timers_t
{
  public:
    timers_t ();
    ~timers_t ();
somdoron's avatar
somdoron committed
48

49 50 51
    //  Add timer to the set, timer repeats forever, or until cancel is called.
    //  Returns a timer_id that is used to cancel the timer.
    //  Returns -1 if there was an error.
52
    int add (size_t interval_, timers_timer_fn handler_, void *arg_);
somdoron's avatar
somdoron committed
53

54 55 56
    //  Set the interval of the timer.
    //  This method is slow, cancelling exsting and adding a new timer yield better performance.
    //  Returns 0 on success and -1 on error.
57
    int set_interval (int timer_id_, size_t interval_);
somdoron's avatar
somdoron committed
58

59 60 61
    //  Reset the timer.
    //  This method is slow, cancelling exsting and adding a new timer yield better performance.
    //  Returns 0 on success and -1 on error.
62
    int reset (int timer_id_);
somdoron's avatar
somdoron committed
63

64 65
    //  Cancel a timer.
    //  Returns 0 on success and -1 on error.
66
    int cancel (int timer_id_);
somdoron's avatar
somdoron committed
67

68 69 70
    //  Returns the time in millisecond until the next timer.
    //  Returns -1 if no timer is due.
    long timeout ();
somdoron's avatar
somdoron committed
71

72 73 74
    //  Execute timers.
    //  Return 0 if all succeed and -1 if error.
    int execute ();
somdoron's avatar
somdoron committed
75

76
    //  Return false if object is not a timers class.
77
    bool check_tag () const;
somdoron's avatar
somdoron committed
78

79 80
  private:
    //  Used to check whether the object is a timers class.
81
    uint32_t _tag;
somdoron's avatar
somdoron committed
82

83
    int _next_timer_id;
somdoron's avatar
somdoron committed
84

85
    //  Clock instance.
86
    clock_t _clock;
somdoron's avatar
somdoron committed
87

88 89 90 91 92 93 94
    typedef struct timer_t
    {
        int timer_id;
        size_t interval;
        timers_timer_fn *handler;
        void *arg;
    } timer_t;
somdoron's avatar
somdoron committed
95

96
    typedef std::multimap<uint64_t, timer_t> timersmap_t;
97
    timersmap_t _timers;
somdoron's avatar
somdoron committed
98

99
    typedef std::set<int> cancelled_timers_t;
100
    cancelled_timers_t _cancelled_timers;
somdoron's avatar
somdoron committed
101

102
    struct match_by_id;
103 104

    ZMQ_NON_COPYABLE_NOR_MOVABLE (timers_t)
105 106
};
}
somdoron's avatar
somdoron committed
107

108
#endif