1/*
2Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3
4This file is part of libzmq, the ZeroMQ core engine in C++.
5
6libzmq is free software; you can redistribute it and/or modify it under
7the terms of the GNU Lesser General Public License (LGPL) as published
8by the Free Software Foundation; either version 3 of the License, or
9(at your option) any later version.
10
11As a special exception, the Contributors give you permission to link
12this library with independent modules to produce an executable,
13regardless of the license terms of these independent modules, and to
14copy and distribute the resulting executable under terms of your choice,
15provided that you also meet, for each linked independent module, the
16terms and conditions of the license of that module. An independent
17module is a module which is not derived from or based on this library.
18If you modify this library, you must extend this exception to your
19version of the library.
20
21libzmq is distributed in the hope that it will be useful, but WITHOUT
22ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24License for more details.
25
26You should have received a copy of the GNU Lesser General Public License
27along with this program. If not, see <http://www.gnu.org/licenses/>.
28*/
29
30#ifndef __ZMQ_TIMERS_HPP_INCLUDED__
31#define __ZMQ_TIMERS_HPP_INCLUDED__
32
33#include <stddef.h>
34#include <map>
35#include <set>
36
37#include "clock.hpp"
38
39namespace zmq
40{
41typedef void(timers_timer_fn) (int timer_id_, void *arg_);
42
43class timers_t
44{
45 public:
46 timers_t ();
47 ~timers_t ();
48
49 // Add timer to the set, timer repeats forever, or until cancel is called.
50 // Returns a timer_id that is used to cancel the timer.
51 // Returns -1 if there was an error.
52 int add (size_t interval_, timers_timer_fn handler_, void *arg_);
53
54 // Set the interval of the timer.
55 // This method is slow, cancelling exsting and adding a new timer yield better performance.
56 // Returns 0 on success and -1 on error.
57 int set_interval (int timer_id_, size_t interval_);
58
59 // Reset the timer.
60 // This method is slow, cancelling exsting and adding a new timer yield better performance.
61 // Returns 0 on success and -1 on error.
62 int reset (int timer_id_);
63
64 // Cancel a timer.
65 // Returns 0 on success and -1 on error.
66 int cancel (int timer_id_);
67
68 // Returns the time in millisecond until the next timer.
69 // Returns -1 if no timer is due.
70 long timeout ();
71
72 // Execute timers.
73 // Return 0 if all succeed and -1 if error.
74 int execute ();
75
76 // Return false if object is not a timers class.
77 bool check_tag ();
78
79 private:
80 // Used to check whether the object is a timers class.
81 uint32_t _tag;
82
83 int _next_timer_id;
84
85 // Clock instance.
86 clock_t _clock;
87
88 typedef struct timer_t
89 {
90 int timer_id;
91 size_t interval;
92 timers_timer_fn *handler;
93 void *arg;
94 } timer_t;
95
96 typedef std::multimap<uint64_t, timer_t> timersmap_t;
97 timersmap_t _timers;
98
99 typedef std::set<int> cancelled_timers_t;
100 cancelled_timers_t _cancelled_timers;
101
102 struct match_by_id;
103
104 ZMQ_NON_COPYABLE_NOR_MOVABLE (timers_t)
105};
106}
107
108#endif
109