summaryrefslogtreecommitdiff
path: root/cpp/include/IceUtil/Timer.h
blob: da5cb38054f9423322958b071fe2515433509657 (plain)
1
2
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
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
// **********************************************************************
//
// Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#ifndef ICEUTIL_TIMER_H
#define ICEUTIL_TIMER_H

#include <IceUtil/Shared.h>
#include <IceUtil/Thread.h>
#include <IceUtil/Monitor.h>
#include <IceUtil/Time.h>

#include <set>
#include <map>

namespace IceUtil
{

class Timer;
typedef IceUtil::Handle<Timer> TimerPtr;

//
// Extend the TimerTask class and override the run() method to execute
// code at a specific time or repeatedly.
//
class TimerTask : public IceUtil::Shared
{
public:

    virtual ~TimerTask() { }

    virtual bool operator<(const TimerTask& r) const;

    virtual void run() = 0;
};
typedef IceUtil::Handle<TimerTask> TimerTaskPtr;

//
// The timer class is used to schedule tasks for one-time execution or
// repeated execution. Tasks are executed by the dedicated timer thread 
// sequentially.
//
class Timer : public virtual IceUtil::Shared, private virtual IceUtil::Thread
{
public:

    //
    // Construct a timer and starts its execution thread.
    //
    Timer();

    //
    // Destroy the timer and join with its execution thread.
    //
    void destroy();

    //
    // Schedule a task for execution at a given time.
    //
    void schedule(const TimerTaskPtr&, const IceUtil::Time&);

    //
    // Schedule a task for repeated execution with the given delay
    // between each execution.
    //
    void scheduleRepeated(const TimerTaskPtr&, const IceUtil::Time&);

    //
    // Cancel a task. Returns true if the task has not yet run or if
    // it's a task scheduled for repeated execution. Returns false if
    // the task has already run, was already cancelled or was never
    // schedulded.
    //
    bool cancel(const TimerTaskPtr&);

private:

    struct Token
    {
        IceUtil::Time scheduledTime;
        IceUtil::Time delay;
        TimerTaskPtr task;
        
        bool operator<(const Token& r) const
        {
            if(scheduledTime < r.scheduledTime)
            {
                return true;
            }
            else if(scheduledTime > r.scheduledTime)
            {
                return false;
            }
            
            return task.get() < r.task.get();
        }
    };

    virtual void run();

    IceUtil::Monitor<IceUtil::Mutex> _monitor;
    bool _destroyed;
    std::set<Token> _tokens;
    std::map<TimerTaskPtr, IceUtil::Time> _tasks;
};
typedef IceUtil::Handle<Timer> TimerPtr;

}

#endif