Ice 3.7 C++11 API Reference
Loading...
Searching...
No Matches
Timer.h
Go to the documentation of this file.
1//
2// Copyright (c) ZeroC, Inc. All rights reserved.
3//
4
5#ifndef ICE_UTIL_TIMER_H
6#define ICE_UTIL_TIMER_H
7
8#include <IceUtil/Shared.h>
9#include <IceUtil/Thread.h>
10#include <IceUtil/Monitor.h>
11#include <IceUtil/Time.h>
12
13#include <set>
14#include <map>
15#include <functional>
16
17namespace IceUtil
18{
19
20class Timer;
22
23//
24// Extend the TimerTask class and override the runTimerTask() method to execute
25// code at a specific time or repeatedly.
26//
28#ifndef ICE_CPP11_MAPPING
29 : public virtual IceUtil::Shared
30#endif
31{
32public:
33
34 virtual ~TimerTask();
35
36 virtual void runTimerTask() = 0;
37};
39
40//
41// The timer class is used to schedule tasks for one-time execution or
42// repeated execution. Tasks are executed by the dedicated timer thread
43// sequentially.
44//
45class ICE_API Timer : public virtual IceUtil::Shared, private IceUtil::Thread
46{
47public:
48
49 //
50 // Construct a timer and starts its execution thread.
51 //
53
54 //
55 // Construct a timer and starts its execution thread with the priority.
56 //
57 Timer(int priority);
58
59 //
60 // Destroy the timer and detach its execution thread if the calling thread
61 // is the timer thread, join the timer execution thread otherwise.
62 //
63 void destroy();
64
65 //
66 // Schedule a task for execution after a given delay.
67 //
68 void schedule(const TimerTaskPtr&, const IceUtil::Time&);
69
70 //
71 // Schedule a task for repeated execution with the given delay
72 // between each execution.
73 //
75
76 //
77 // Cancel a task. Returns true if the task has not yet run or if
78 // it's a task scheduled for repeated execution. Returns false if
79 // the task has already run, was already cancelled or was never
80 // schedulded.
81 //
82 bool cancel(const TimerTaskPtr&);
83
84protected:
85
86 virtual void run();
87 virtual void runTimerTask(const TimerTaskPtr&);
88
89 struct Token
90 {
94
95 inline Token(const IceUtil::Time&, const IceUtil::Time&, const TimerTaskPtr&);
96 inline bool operator<(const Token& r) const;
97 };
98
101 std::set<Token> _tokens;
102
103#ifdef __clang__
104# pragma clang diagnostic push
105# pragma clang diagnostic ignored "-Wdeprecated-declarations" // for std::binary_function below
106#endif
107
108#if (ICE_CPLUSPLUS >= 201703L)
109 class TimerTaskCompare
110#else
111 class TimerTaskCompare : public std::binary_function<TimerTaskPtr, TimerTaskPtr, bool>
112#endif
113 {
114 public:
115
116 bool operator()(const TimerTaskPtr& lhs, const TimerTaskPtr& rhs) const
117 {
118 return lhs.get() < rhs.get();
119 }
120 };
121 std::map<TimerTaskPtr, IceUtil::Time, TimerTaskCompare> _tasks;
123};
125
126#ifdef __clang__
127# pragma clang diagnostic pop
128#endif
129
130inline
132 scheduledTime(st), delay(d), task(t)
133{
134}
135
136inline bool
138{
140 {
141 return true;
142 }
143 else if(scheduledTime > r.scheduledTime)
144 {
145 return false;
146 }
147
148 return task.get() < r.task.get();
149}
150
151}
152
153#endif
#define ICE_DEFINE_PTR(TPtr, T)
Definition Config.h:359
#define ICE_API
Definition Config.h:197
Definition Handle.h:143
Definition Monitor.h:22
Definition Shared.h:78
Definition Thread.h:99
Definition Time.h:18
Definition Timer.h:31
virtual ~TimerTask()
virtual void runTimerTask()=0
Definition Timer.h:113
bool operator()(const TimerTaskPtr &lhs, const TimerTaskPtr &rhs) const
Definition Timer.h:116
Definition Timer.h:46
std::map< TimerTaskPtr, IceUtil::Time, TimerTaskCompare > _tasks
Definition Timer.h:121
bool cancel(const TimerTaskPtr &)
void scheduleRepeated(const TimerTaskPtr &, const IceUtil::Time &)
Timer(int priority)
std::set< Token > _tokens
Definition Timer.h:101
IceUtil::Monitor< IceUtil::Mutex > _monitor
Definition Timer.h:99
void schedule(const TimerTaskPtr &, const IceUtil::Time &)
virtual void run()
virtual void runTimerTask(const TimerTaskPtr &)
IceUtil::Time _wakeUpTime
Definition Timer.h:122
bool _destroyed
Definition Timer.h:100
Definition Optional.h:1095
IceUtil::Handle< Timer > TimerPtr
Definition Timer.h:21
::std::shared_ptr< TimerTask > TimerTaskPtr
Definition Timer.h:38
Definition Timer.h:90
IceUtil::Time delay
Definition Timer.h:92
Token(const IceUtil::Time &, const IceUtil::Time &, const TimerTaskPtr &)
Definition Timer.h:131
bool operator<(const Token &r) const
Definition Timer.h:137
IceUtil::Time scheduledTime
Definition Timer.h:91
TimerTaskPtr task
Definition Timer.h:93