Kannel: Open Source WAP and SMS gateway  svn-r5335
timers.h File Reference
#include "gwlib/gwlib.h"
#include "wap_events.h"

Go to the source code of this file.

Typedefs

typedef struct Timer Timer
 

Functions

void timers_init (void)
 
void timers_shutdown (void)
 
Timergwtimer_create (List *outputlist)
 
void gwtimer_destroy (Timer *timer)
 
void gwtimer_start (Timer *timer, int interval, WAPEvent *event)
 
void gwtimer_stop (Timer *timer)
 

Typedef Documentation

◆ Timer

typedef struct Timer Timer

Definition at line 93 of file timers.h.

Function Documentation

◆ gwtimer_create()

Timer* gwtimer_create ( List outputlist)

Definition at line 224 of file timers.c.

References Timer::elapsed_event, Timer::elapses, Timer::event, gw_assert(), gwlist_add_producer(), Timer::index, initialized, and Timer::output.

225 {
226  Timer *t;
227 
229 
230  t = gw_malloc(sizeof(*t));
231  t->elapses = -1;
232  t->event = NULL;
233  t->elapsed_event = NULL;
234  t->index = -1;
235  t->output = outputlist;
236  gwlist_add_producer(outputlist);
237 
238  return t;
239 }
WAPEvent * elapsed_event
Definition: timers.c:145
gw_assert(wtls_machine->packet_to_send !=NULL)
static int initialized
Definition: timers.c:166
List * output
Definition: gw-timer.c:132
WAPEvent * event
Definition: timers.c:138
long elapses
Definition: gw-timer.c:142
long index
Definition: gw-timer.c:161
void gwlist_add_producer(List *list)
Definition: list.c:383

◆ gwtimer_destroy()

void gwtimer_destroy ( Timer timer)

Definition at line 241 of file timers.c.

References Timer::event, gw_assert(), gwlist_remove_producer(), gwtimer_stop(), initialized, Timer::output, and wap_event_destroy().

242 {
244 
245  if (timer == NULL)
246  return;
247 
248  gwtimer_stop(timer);
250  wap_event_destroy(timer->event);
251  gw_free(timer);
252 }
gw_assert(wtls_machine->packet_to_send !=NULL)
static int initialized
Definition: timers.c:166
List * output
Definition: gw-timer.c:132
void gwtimer_stop(Timer *timer)
Definition: timers.c:299
void gwlist_remove_producer(List *list)
Definition: list.c:401
WAPEvent * event
Definition: timers.c:138
void wap_event_destroy(WAPEvent *event)
Definition: wap_events.c:102

◆ gwtimer_start()

void gwtimer_start ( Timer timer,
int  interval,
WAPEvent event 
)

Definition at line 254 of file timers.c.

References abort_elapsed(), Timer::elapses, Timer::event, gw_assert(), gwthread_wakeup(), Timerset::heap, heap_adjust(), heap_insert(), Timer::index, initialized, interval, lock(), TimerHeap::tab, Timerset::thread, timers, unlock(), and wap_event_destroy().

Referenced by start_initiator_timer_R(), start_timer_A(), start_timer_R(), and start_timer_W().

255 {
256  int wakeup = 0;
257 
259  gw_assert(timer != NULL);
260  gw_assert(event != NULL || timer->event != NULL);
261 
262  lock(timers);
263 
264  /* Convert to absolute time */
265  interval += time(NULL);
266 
267  if (timer->elapses > 0) {
268  /* Resetting an existing timer. Move it to its new
269  * position in the heap. */
270  if (interval < timer->elapses && timer->index == 0)
271  wakeup = 1;
272  timer->elapses = interval;
273  gw_assert(timers->heap->tab[timer->index] == timer);
274  wakeup |= heap_adjust(timers->heap, timer->index);
275  } else {
276  /* Setting a new timer, or resetting an elapsed one.
277  * First deal with a possible elapse event that may
278  * still be on the output list. */
279  abort_elapsed(timer);
280 
281  /* Then activate the timer. */
282  timer->elapses = interval;
283  gw_assert(timer->index < 0);
284  heap_insert(timers->heap, timer);
285  wakeup = timer->index == 0; /* Do we have a new top? */
286  }
287 
288  if (event != NULL) {
289  wap_event_destroy(timer->event);
290  timer->event = event;
291  }
292 
293  unlock(timers);
294 
295  if (wakeup)
297 }
static void heap_insert(TimerHeap *heap, Timer *timer)
Definition: timers.c:403
gw_assert(wtls_machine->packet_to_send !=NULL)
static int initialized
Definition: timers.c:166
static void lock(Timerset *set)
Definition: timers.c:320
Timer ** tab
Definition: gw-timer.c:87
TimerHeap * heap
Definition: gw-timer.c:110
static Timerset * timers
Definition: timers.c:160
double interval
Definition: fakewap.c:234
long thread
Definition: gw-timer.c:115
WAPEvent * event
Definition: timers.c:138
static void unlock(Timerset *set)
Definition: timers.c:326
long elapses
Definition: gw-timer.c:142
static int heap_adjust(TimerHeap *heap, long index)
Definition: timers.c:446
void gwthread_wakeup(long thread)
long index
Definition: gw-timer.c:161
void wap_event_destroy(WAPEvent *event)
Definition: wap_events.c:102
static void abort_elapsed(Timer *timer)
Definition: timers.c:338

◆ gwtimer_stop()

void gwtimer_stop ( Timer timer)

Definition at line 299 of file timers.c.

References abort_elapsed(), Timer::elapses, gw_assert(), Timerset::heap, heap_delete(), Timer::index, initialized, lock(), TimerHeap::tab, timers, and unlock().

Referenced by gwtimer_destroy(), stop_initiator_timer(), and timers_shutdown().

300 {
302  gw_assert(timer != NULL);
303  lock(timers);
304 
305  /*
306  * If the timer is active, make it inactive and remove it from
307  * the heap.
308  */
309  if (timer->elapses > 0) {
310  timer->elapses = -1;
311  gw_assert(timers->heap->tab[timer->index] == timer);
312  heap_delete(timers->heap, timer->index);
313  }
314 
315  abort_elapsed(timer);
316 
317  unlock(timers);
318 }
gw_assert(wtls_machine->packet_to_send !=NULL)
static int initialized
Definition: timers.c:166
static void heap_delete(TimerHeap *heap, long index)
Definition: timers.c:383
static void lock(Timerset *set)
Definition: timers.c:320
Timer ** tab
Definition: gw-timer.c:87
TimerHeap * heap
Definition: gw-timer.c:110
static Timerset * timers
Definition: timers.c:160
static void unlock(Timerset *set)
Definition: timers.c:326
long elapses
Definition: gw-timer.c:142
long index
Definition: gw-timer.c:161
static void abort_elapsed(Timer *timer)
Definition: timers.c:338

◆ timers_init()

void timers_init ( void  )

Definition at line 184 of file timers.c.

References gwthread_create, Timerset::heap, heap_create(), initialized, Timerset::mutex, mutex_create, Timerset::stopping, Timerset::thread, timers, and watch_timers().

Referenced by wtp_initiator_init(), and wtp_resp_init().

185 {
186  if (initialized == 0) {
187  timers = gw_malloc(sizeof(*timers));
189  timers->heap = heap_create();
190  timers->stopping = 0;
192  }
193  initialized++;
194 }
static int initialized
Definition: timers.c:166
static TimerHeap * heap_create(void)
Definition: timers.c:357
volatile sig_atomic_t stopping
Definition: gw-timer.c:98
#define mutex_create()
Definition: thread.h:96
TimerHeap * heap
Definition: gw-timer.c:110
static Timerset * timers
Definition: timers.c:160
#define gwthread_create(func, arg)
Definition: gwthread.h:90
long thread
Definition: gw-timer.c:115
static void watch_timers(void *arg)
Definition: timers.c:532
Mutex * mutex
Definition: gw-timer.c:105

◆ timers_shutdown()

void timers_shutdown ( void  )

Definition at line 196 of file timers.c.

References gwthread_join(), gwthread_wakeup(), gwtimer_stop(), Timerset::heap, heap_destroy(), initialized, TimerHeap::len, Timerset::mutex, mutex_destroy(), Timerset::stopping, TimerHeap::tab, Timerset::thread, timers, and warning().

Referenced by wtp_initiator_shutdown(), and wtp_resp_shutdown().

197 {
198  if (initialized > 1) {
199  initialized--;
200  return;
201  }
202 
203  /* Stop all timers. */
204  if (timers->heap->len > 0)
205  warning(0, "Timers shutting down with %ld active timers.",
206  timers->heap->len);
207  while (timers->heap->len > 0)
208  gwtimer_stop(timers->heap->tab[0]);
209 
210  /* Kill timer thread */
211  timers->stopping = 1;
214 
215  initialized = 0;
216 
217  /* Free resources */
220  gw_free(timers);
221 }
static int initialized
Definition: timers.c:166
volatile sig_atomic_t stopping
Definition: gw-timer.c:98
void gwthread_join(long thread)
Timer ** tab
Definition: gw-timer.c:87
void gwtimer_stop(Timer *timer)
Definition: timers.c:299
static void heap_destroy(TimerHeap *heap)
Definition: timers.c:369
TimerHeap * heap
Definition: gw-timer.c:110
void warning(int err, const char *fmt,...)
Definition: log.c:660
static Timerset * timers
Definition: timers.c:160
void mutex_destroy(Mutex *mutex)
Definition: thread.c:97
long thread
Definition: gw-timer.c:115
void gwthread_wakeup(long thread)
long len
Definition: gw-timer.c:88
Mutex * mutex
Definition: gw-timer.c:105
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.