File: TimedEventService.cpp

package info (click to toggle)
eris 1.3.10-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,592 kB
  • ctags: 1,516
  • sloc: cpp: 9,850; sh: 8,288; perl: 287; ansic: 165; makefile: 162
file content (68 lines) | stat: -rw-r--r-- 1,401 bytes parent folder | download | duplicates (2)
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
#include <Eris/TimedEventService.h>
#include <Eris/Poll.h>

using WFMath::TimeStamp;
using WFMath::TimeDiff;

namespace Eris
{

static TimeDiff TD_ZERO(0);

TimedEventService* TimedEventService::static_instance = NULL;

TimedEventService::TimedEventService()
{
}

TimedEventService* TimedEventService::instance()
{
    if (!static_instance)
    {
        static_instance = new TimedEventService;
    }
    
    return static_instance;
}

unsigned long TimedEventService::tick()
{
    TimeStamp n(TimeStamp::now());
    TimedEventsByDue::iterator it = m_events.begin();
    
    unsigned long waitMsecs = 0xffff; // arbitrary big number
    while (it != m_events.end())
    {
        TimeDiff d = (*it)->due() - n;
        if (d <= TD_ZERO)
        {
            // expired
            TimedEvent* e = *it;
            m_events.erase(it++);
            
            // must not use e after calling expired(), it may delete self
            e->expired();
        } else {
            // all later events can wait too
            return d.milliseconds();
        }
    }
    
    return waitMsecs;
}

void TimedEventService::registerEvent(TimedEvent* te)
{
    assert(te);
    m_events.insert(te); // STL rocks, sometimes
    
    Poll::newTimedEvent(); // this could die?
}

void TimedEventService::unregisterEvent(TimedEvent* te)
{
    assert(te);    
    m_events.erase(te);
}

} // of namespace Eris