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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
/*
* $Id: dlg_timer.c,v 1.1 2006/04/14 11:00:06 bogdan_iancu Exp $
*
* Copyright (C) 2006 Voice System SRL
*
* This file is part of openser, a free SIP server.
*
* openser is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* openser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* History:
* --------
* 2006-04-14 initial version (bogdan)
*/
#include "../../mem/shm_mem.h"
#include "../../timer.h"
#include "dlg_timer.h"
struct dlg_timer *d_timer = 0;
dlg_timer_handler timer_hdl = 0;
int init_dlg_timer( dlg_timer_handler hdl )
{
d_timer = (struct dlg_timer*)shm_malloc(sizeof(struct dlg_timer));
if (d_timer==0) {
LOG(L_ERR,"ERROR:dialog:init_dlg_timer: no more shm mem\n");
return -1;
}
memset( d_timer, 0, sizeof(struct dlg_timer) );
d_timer->first.next = d_timer->first.prev = &(d_timer->first);
d_timer->lock = lock_alloc();
if (d_timer->lock==0) {
LOG(L_ERR,"ERROR:dialog:init_dlg_timer: failed to alloc lock\n");
goto error0;
}
if (lock_init(d_timer->lock)==0) {
LOG(L_ERR,"ERROR:dialog:init_dlg_timer: failed to init lock\n");
goto error1;
}
timer_hdl = hdl;
return 0;
error1:
lock_dealloc(d_timer->lock);
error0:
shm_free(d_timer);
d_timer = 0;
return -1;
}
void destroy_dlg_timer()
{
if (d_timer==0)
return;
lock_destroy(d_timer->lock);
lock_dealloc(d_timer->lock);
shm_free(d_timer);
d_timer = 0;
}
static inline void insert_dlg_timer_unsafe(struct dlg_tl *tl)
{
struct dlg_tl* ptr;
for(ptr = d_timer->first.prev; ptr != &d_timer->first ; ptr = ptr->prev) {
if ( ptr->timeout <= tl->timeout )
break;
}
DBG("DEBUG:dialog:insert_tl: inserting %p for %d\n",
tl,tl->timeout);
tl->prev = ptr;
tl->next = ptr->next;
tl->prev->next = tl;
tl->next->prev = tl;
}
int insert_dlg_timer(struct dlg_tl *tl, int interval)
{
if (tl->next!=0 || tl->prev!=0) {
LOG(L_CRIT,"BUG:dialog:insert_dlg_timer: links are not nul!\n");
return -1;
}
tl->timeout = get_ticks()+interval;
lock_get( d_timer->lock);
insert_dlg_timer_unsafe( tl );
lock_release( d_timer->lock);
return 0;
}
static inline void remove_dlg_timer_unsafe(struct dlg_tl *tl)
{
tl->prev->next = tl->next;
tl->next->prev = tl->prev;
}
int remove_dlg_timer(struct dlg_tl *tl)
{
if (tl->prev==0) {
return -1;
} else if (tl->next==0) {
LOG(L_CRIT,"BUG:dialog:remove_dlg_timer: links are nul!\n");
return -1;
}
lock_get( d_timer->lock);
remove_dlg_timer_unsafe(tl);
tl->next = 0;
tl->prev = 0;
tl->timeout = 0;
lock_release( d_timer->lock);
return 0;
}
int update_dlg_timer( struct dlg_tl *tl, int timeout )
{
if (tl->next==0 || tl->prev==0) {
LOG(L_CRIT,"BUG:dialog:update_dlg_timer: links are nul "
"(not in timer)!\n");
return -1;
}
lock_get( d_timer->lock);
remove_dlg_timer_unsafe(tl);
tl->timeout = get_ticks()+timeout;
insert_dlg_timer_unsafe( tl );
lock_release( d_timer->lock);
return 0;
}
static inline struct dlg_tl* get_expired_dlgs(int time)
{
struct dlg_tl *tl , *end, *ret;
if (d_timer->first.next==&(d_timer->first)
|| d_timer->first.next->timeout > time )
return 0;
lock_get( d_timer->lock);
end = &d_timer->first;
tl = d_timer->first.next;
DBG("DEBUG:dialog:insert_tl: start with %p (%d) at %d\n",
tl,tl->timeout,time);
while( tl!=end && tl->timeout <= time) {
DBG("DEBUG:dialog:get_expired_tl: getting %p with %d\n",
tl,tl->timeout);
tl->prev = 0;
tl=tl->next;
}
if (tl==end && d_timer->first.next->prev) {
ret = 0;
} else {
ret = d_timer->first.next;
tl->prev->next = 0;
d_timer->first.next = tl;
tl->prev = &d_timer->first;
}
lock_release( d_timer->lock);
return ret;
}
void dlg_timer_routine(unsigned int ticks , void * attr)
{
struct dlg_tl *tl, *ctl;
tl = get_expired_dlgs( ticks );
while (tl) {
ctl = tl;
tl = tl->next;
ctl->next = ctl->prev = 0;
DBG("DEBUG:dialog:dlg_timer_routine: tl=%p next=%p\n", ctl, tl);
timer_hdl( ctl );
}
}
|