File: TimerQueue.cpp

package info (click to toggle)
libassa 3.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,268 kB
  • sloc: cpp: 15,703; sh: 12,083; makefile: 379; perl: 51
file content (164 lines) | stat: -rw-r--r-- 3,992 bytes parent folder | download | duplicates (9)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// -*- c++ -*-
//------------------------------------------------------------------------------
//                            TimerQueue.cpp
//------------------------------------------------------------------------------
//  Copyright (c) 2000,2005 by Vladislav Grinchenko
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Library General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//------------------------------------------------------------------------------
//  Created: 07/28/99
//------------------------------------------------------------------------------

#include "assa/TimerQueue.h"
using namespace ASSA;

TimerQueue::
~TimerQueue () 
{ 
    trace_with_mask("TimerQueue::~TimerQueue",REACTTRACE);

    while (m_queue.size ()) {
		delete m_queue.pop ();
    }
}

int
TimerQueue::
remove (EventHandler* eh_)
{ 
    // Like STL iterators, after deletion of an element,
    // queue structure and indexing might drastically change
    // and there is no guarantee that elements we haven't seen
    // yet will not be moved past the iterator. Therefore,
    // we must start scanning from the beginning after each deletion :-(

    trace_with_mask("TimerQueue::remove(eh_)",REACTTRACE);

    register size_t i;
    int cnt = 0;
    bool f = true;		// changed flag
    register Timer* tmr;

    DL((REACT,"Searching for Timer: 0x%x\n", dynamic_cast<void*> (eh_)));

    while (f) {
		f = false;
		DL((REACT,"Queue size: %d\n", m_queue.size()));
		for (i = 0; i < m_queue.size (); i++) {
			if (m_queue[i]->getHandler() == eh_) {
				DL((REACT,"Found Timer: 0x%x in slot: %d\n", 
					dynamic_cast<void*>(eh_), i));
				tmr = m_queue[i];
				m_queue.remove (tmr);
				delete tmr;
				cnt++;
				f = true;
			}
		}
    }
    return cnt;
}

bool
TimerQueue::
remove (TimerId tid_)
{ 
    trace_with_mask("TimerQueue::remove(tid)",REACTTRACE);
    register size_t i;

    DL((REACTTRACE,"Queue size before remove: %d\n", m_queue.size()));

    for (i = 0; i < m_queue.size (); i++) {
		if (m_queue[i] == (Timer*) tid_) {
			Timer* tmr = m_queue[i];
			int ret = m_queue.remove (tmr);
			delete tmr;
			DL((REACTTRACE,"Queue size after remove: %d\n", m_queue.size()));
			return ret;
		}
    }
    return false;
}

int
TimerQueue::
expire (const TimeVal& tv_)
{ 
    trace_with_mask("TimerQueue::expire",REACTTRACE);

    register Timer* tp = (Timer*) NULL;
    register int cnt = 0;

    while (m_queue.size () && (tp = m_queue.top ()) != (Timer*) NULL) {
		if (tp->getExpirationTime () > tv_) {
			DL((REACT,"Top timer:\n"));
			tp->dump ();
			break;
		}
		/* First, pop item from the queue. Then call an appropriate
		   EventHandler. If done in reverse, EventHandler might
		   remove item first and then pop () will fail
		   (This needs more investigation!).
		*/
		m_queue.pop ();

		DL((REACT,"Expired %s [t=%s] timer!\n",
			tp->get_id ().c_str (),
			tp->getExpirationTime ().fmtString ().c_str ()));

		int ret = tp->getHandler ()->handle_timeout ((TimerId) tp);

		/** Reschedule without deleting the Timer object so that
			application-level code can still hold to the valid TimerId.
		*/
		if (ret == 1) {			
			tp->rescheduleExpirationTime ();
			m_queue.insert (tp);
		}
		else {
			delete tp;
			tp = (Timer*)NULL;
		}
		cnt++;
    }

    if (cnt) {
		DL((TRACE,"Expired total of %d timer(s).\n",cnt));
    }

    return cnt;
}

TimerId
TimerQueue::
insert (EventHandler*      eh_, 
		const TimeVal&     tv_, 
		const TimeVal&     delta_,
		const std::string& name_) 
{ 
	trace("TimerQueue::insert");

	Timer* t = new Timer (eh_, tv_, delta_, name_);
	m_queue.insert (t);
	return (TimerId) t;
}

void
TimerQueue::
dump (void)
{
	trace("TimerQueue::dump");

	if (m_queue.size() == 0) {
		DL((REACT,"Queue is empty\n"));
	}
	else {
		for (size_t i = 0; i < m_queue.size (); ) {
			m_queue[i++]->dump();
		}
	}
}