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