1
/* $Id: timer.hpp 2394 2008-12-23 17:27:53Z bennylp $ */
3
* Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#ifndef __PJPP_TIMER_HPP__
21
#define __PJPP_TIMER_HPP__
24
#include <pj++/types.hpp>
25
#include <pj/assert.h>
26
#include <pj++/lock.hpp>
30
//////////////////////////////////////////////////////////////////////////////
34
// Derive class from Pj_Timer_Entry and override on_timeout().
35
// Scheduler timer in Pj_Timer_Heap.
37
class Pj_Timer_Entry : public Pj_Object
39
friend class Pj_Timer_Heap;
43
// Default constructor.
47
entry_.user_data = this;
48
entry_.cb = &timer_heap_callback;
52
// Destructor, do nothing.
59
// Override this to get the timeout notification.
61
virtual void on_timeout(int id) = 0;
64
pj_timer_entry entry_;
66
static void timer_heap_callback(pj_timer_heap_t*, pj_timer_entry *e)
68
Pj_Timer_Entry *entry = (Pj_Timer_Entry*) e->user_data;
69
entry->on_timeout(e->id);
74
//////////////////////////////////////////////////////////////////////////////
77
class Pj_Timer_Heap : public Pj_Object
81
// Default constructor.
89
// Construct timer heap.
91
Pj_Timer_Heap(Pj_Pool *pool, pj_size_t initial_count)
94
create(pool, initial_count);
108
pj_status_t create(Pj_Pool *pool, pj_size_t initial_count)
111
return pj_timer_heap_create(pool->pool_(), initial_count, &ht_);
120
pj_timer_heap_destroy(ht_);
126
// Get pjlib compatible timer heap object.
128
pj_timer_heap_t *get_timer_heap()
134
// Set the lock object.
136
void set_lock( Pj_Lock *lock, bool auto_delete )
138
pj_timer_heap_set_lock( ht_, lock->pj_lock_t_(), auto_delete);
142
// Set maximum number of timed out entries to be processed per poll.
144
unsigned set_max_timed_out_per_poll(unsigned count)
146
return pj_timer_heap_set_max_timed_out_per_poll(ht_, count);
152
bool schedule( Pj_Timer_Entry *ent, const Pj_Time_Val &delay,
156
return pj_timer_heap_schedule(ht_, &ent->entry_, &delay) == 0;
162
bool cancel(Pj_Timer_Entry *ent)
164
return pj_timer_heap_cancel(ht_, &ent->entry_) == 1;
168
// Get current number of timers
172
return pj_timer_heap_count(ht_);
176
// Get the earliest time.
177
// Return false if no timer is found.
179
bool earliest_time(Pj_Time_Val *t)
181
return pj_timer_heap_earliest_time(ht_, t) == PJ_SUCCESS;
186
// Return number of timed out entries has been called.
188
unsigned poll(Pj_Time_Val *next_delay = NULL)
190
return pj_timer_heap_poll(ht_, next_delay);
194
pj_timer_heap_t *ht_;
197
#endif /* __PJPP_TIMER_HPP__ */