Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
gw-timer.h File Reference
#include "gwlib/gwlib.h"

Go to the source code of this file.

Typedefs

typedef struct Timer Timer
 
typedef struct Timerset Timerset
 

Functions

Timersetgw_timerset_create (void)
 
void gw_timerset_destroy (Timerset *set)
 
Timergw_timer_create (Timerset *set, List *outputlist, void(*callback)(void *))
 
void gw_timer_destroy (Timer *timer)
 
void gw_timer_elapsed_destroy (Timer *timer)
 
void gw_timer_start (Timer *timer, int interval, void *data)
 
void gw_timer_elapsed_start (Timer *timer, int interval, void *data)
 
void gw_timer_stop (Timer *timer)
 
void gw_timer_elapsed_stop (Timer *timer)
 
Listgw_timer_break (Timerset *set)
 
void * gw_timer_data (Timer *timer)
 

Typedef Documentation

typedef struct Timer Timer

Definition at line 92 of file gw-timer.h.

typedef struct Timerset Timerset

Definition at line 93 of file gw-timer.h.

Function Documentation

List* gw_timer_break ( Timerset set)

Definition at line 385 of file gw-timer.c.

References abort_elapsed(), Timer::elapses, gw_assert(), gwlist_append(), gwlist_create, Timerset::heap, heap_delete(), Timer::index, TimerHeap::len, lock(), TimerHeap::tab, and unlock().

386 {
387  List *ret = NULL;
388 
389  lock(set);
390 
391  if (set->heap->len == 0) {
392  unlock(set);
393  return NULL;
394  }
395 
396  ret = gwlist_create();
397 
398  /* Stop all timers. */
399  while (set->heap->len > 0) {
400  Timer *timer = set->heap->tab[0];
401 
402  gwlist_append(ret, timer);
403 
404  /*
405  * If the timer is active, make it inactive and remove it from
406  * the heap.
407  */
408  if (timer->elapses > 0) {
409  timer->elapses = -1;
410  gw_assert(set->heap->tab[timer->index] == timer);
411  heap_delete(set->heap, timer->index);
412  }
413 
414  abort_elapsed(timer);
415  }
416 
417  unlock(set);
418 
419  return ret;
420 }
void gwlist_append(List *list, void *item)
Definition: list.c:179
static void abort_elapsed(Timer *timer)
Definition: gw-timer.c:447
Timer ** tab
Definition: gw-timer.c:83
TimerHeap * heap
Definition: gw-timer.c:106
static void lock(Timerset *set)
Definition: gw-timer.c:429
gw_assert(wtls_machine->packet_to_send!=NULL)
long elapses
Definition: gw-timer.c:138
#define gwlist_create()
Definition: list.h:136
long index
Definition: gw-timer.c:157
long len
Definition: gw-timer.c:84
static void unlock(Timerset *set)
Definition: gw-timer.c:435
static void heap_delete(TimerHeap *heap, long index)
Definition: gw-timer.c:486
Definition: list.c:102
Timer* gw_timer_create ( Timerset set,
List outputlist,
void(*)(void *)  callback 
)

Definition at line 211 of file gw-timer.c.

References Timer::callback, Timer::data, Timer::elapsed_data, Timer::elapses, gwlist_add_producer(), Timer::index, Timer::output, and Timer::timerset.

Referenced by url_result_thread().

212 {
213  Timer *t;
214 
215  t = gw_malloc(sizeof(*t));
216  t->timerset = set;
217  t->elapses = -1;
218  t->data = NULL;
219  t->elapsed_data = NULL;
220  t->index = -1;
221  t->output = outputlist;
222  if (t->output != NULL)
223  gwlist_add_producer(outputlist);
224  t->callback = callback;
225 
226  return t;
227 }
List * output
Definition: gw-timer.c:128
void * data
Definition: gw-timer.c:144
void(* callback)(void *data)
Definition: gw-timer.c:132
void * elapsed_data
Definition: gw-timer.c:151
Timerset * timerset
Definition: gw-timer.c:119
long elapses
Definition: gw-timer.c:138
long index
Definition: gw-timer.c:157
void gwlist_add_producer(List *list)
Definition: list.c:383
void* gw_timer_data ( Timer timer)

Definition at line 422 of file gw-timer.c.

References Timer::data, and gw_assert().

423 {
424  gw_assert(timer != NULL);
425 
426  return timer->data;
427 }
void * data
Definition: gw-timer.c:144
gw_assert(wtls_machine->packet_to_send!=NULL)
void gw_timer_destroy ( Timer timer)

Definition at line 229 of file gw-timer.c.

References gw_timer_stop(), gwlist_remove_producer(), and Timer::output.

230 {
231  if (timer == NULL)
232  return;
233 
234  gw_timer_stop(timer);
235  if (timer->output != NULL)
237  gw_free(timer);
238 }
List * output
Definition: gw-timer.c:128
void gw_timer_stop(Timer *timer)
Definition: gw-timer.c:344
void gwlist_remove_producer(List *list)
Definition: list.c:401
void gw_timer_elapsed_destroy ( Timer timer)

Definition at line 240 of file gw-timer.c.

References gw_timer_elapsed_stop(), gwlist_remove_producer(), and Timer::output.

Referenced by http_queue_thread().

241 {
242  if (timer == NULL)
243  return;
244 
245  gw_timer_elapsed_stop(timer);
246  if (timer->output != NULL)
248  gw_free(timer);
249 }
List * output
Definition: gw-timer.c:128
void gw_timer_elapsed_stop(Timer *timer)
Definition: gw-timer.c:364
void gwlist_remove_producer(List *list)
Definition: list.c:401
void gw_timer_elapsed_start ( Timer timer,
int  interval,
void *  data 
)

Definition at line 297 of file gw-timer.c.

References Timer::data, Timer::elapsed_data, Timer::elapses, gw_assert(), gwthread_wakeup(), Timerset::heap, heap_adjust(), heap_insert(), Timer::index, interval, lock(), TimerHeap::tab, Timerset::thread, Timer::timerset, and unlock().

Referenced by url_result_thread().

298 {
299  int wakeup = 0;
300 
301  gw_assert(timer != NULL);
302 
303  if (timer == NULL)
304  return;
305 
306  lock(timer->timerset);
307 
308  /* Convert to absolute time */
309  interval += time(NULL);
310 
311  if (timer->elapses > 0) {
312  /* Resetting an existing timer. Move it to its new
313  * position in the heap. */
314  if (interval < timer->elapses && timer->index == 0)
315  wakeup = 1;
316  timer->elapses = interval;
317  gw_assert(timer->index >= 0);
318  gw_assert(timer->timerset->heap->tab[timer->index] == timer);
319  wakeup |= heap_adjust(timer->timerset->heap, timer->index);
320  } else {
321  /* Setting a new timer, or resetting an elapsed one.
322  * There should be no further elapse event on the
323  * output list here. */
324  /* abort_elapsed(timer); */
325  timer->elapsed_data = NULL;
326 
327  /* Then activate the timer. */
328  timer->elapses = interval;
329  gw_assert(timer->index < 0);
330  heap_insert(timer->timerset->heap, timer);
331  wakeup = timer->index == 0; /* Do we have a new top? */
332  }
333 
334  if (data != NULL) {
335  timer->data = data;
336  }
337 
338  unlock(timer->timerset);
339 
340  if (wakeup)
342 }
Timer ** tab
Definition: gw-timer.c:83
void * data
Definition: gw-timer.c:144
TimerHeap * heap
Definition: gw-timer.c:106
static void heap_insert(TimerHeap *heap, Timer *timer)
Definition: gw-timer.c:506
static void lock(Timerset *set)
Definition: gw-timer.c:429
void * elapsed_data
Definition: gw-timer.c:151
gw_assert(wtls_machine->packet_to_send!=NULL)
Timerset * timerset
Definition: gw-timer.c:119
double interval
Definition: fakewap.c:234
long thread
Definition: gw-timer.c:111
long elapses
Definition: gw-timer.c:138
void gwthread_wakeup(long thread)
long index
Definition: gw-timer.c:157
static void unlock(Timerset *set)
Definition: gw-timer.c:435
static int heap_adjust(TimerHeap *heap, long index)
Definition: gw-timer.c:549
void gw_timer_elapsed_stop ( Timer timer)

Definition at line 364 of file gw-timer.c.

References Timer::elapsed_data, Timer::elapses, gw_assert(), Timerset::heap, heap_delete(), Timer::index, lock(), TimerHeap::tab, Timer::timerset, and unlock().

Referenced by gw_timer_elapsed_destroy().

365 {
366  gw_assert(timer != NULL);
367  lock(timer->timerset);
368 
369  /*
370  * If the timer is active, make it inactive and remove it from
371  * the heap.
372  */
373  if (timer->elapses > 0) {
374  timer->elapses = -1;
375  gw_assert(timer->timerset->heap->tab[timer->index] == timer);
376  heap_delete(timer->timerset->heap, timer->index);
377  }
378 
379  /* abort_elapsed(timer); */
380  timer->elapsed_data = NULL;
381 
382  unlock(timer->timerset);
383 }
Timer ** tab
Definition: gw-timer.c:83
TimerHeap * heap
Definition: gw-timer.c:106
static void lock(Timerset *set)
Definition: gw-timer.c:429
void * elapsed_data
Definition: gw-timer.c:151
gw_assert(wtls_machine->packet_to_send!=NULL)
Timerset * timerset
Definition: gw-timer.c:119
long elapses
Definition: gw-timer.c:138
long index
Definition: gw-timer.c:157
static void unlock(Timerset *set)
Definition: gw-timer.c:435
static void heap_delete(TimerHeap *heap, long index)
Definition: gw-timer.c:486
void gw_timer_start ( Timer timer,
int  interval,
void *  data 
)

Definition at line 251 of file gw-timer.c.

References abort_elapsed(), Timer::data, Timer::elapses, gw_assert(), gwthread_wakeup(), Timerset::heap, heap_adjust(), heap_insert(), Timer::index, interval, lock(), TimerHeap::tab, Timerset::thread, Timer::timerset, and unlock().

252 {
253  int wakeup = 0;
254 
255  gw_assert(timer != NULL);
256 
257  if (timer == NULL)
258  return;
259 
260  lock(timer->timerset);
261 
262  /* Convert to absolute time */
263  interval += time(NULL);
264 
265  if (timer->elapses > 0) {
266  /* Resetting an existing timer. Move it to its new
267  * position in the heap. */
268  if (interval < timer->elapses && timer->index == 0)
269  wakeup = 1;
270  timer->elapses = interval;
271  gw_assert(timer->index >= 0);
272  gw_assert(timer->timerset->heap->tab[timer->index] == timer);
273  wakeup |= heap_adjust(timer->timerset->heap, timer->index);
274  } else {
275  /* Setting a new timer, or resetting an elapsed one.
276  * First deal with a possible elapse event that may
277  * still be on the output list. */
278  abort_elapsed(timer);
279 
280  /* Then activate the timer. */
281  timer->elapses = interval;
282  gw_assert(timer->index < 0);
283  heap_insert(timer->timerset->heap, timer);
284  wakeup = timer->index == 0; /* Do we have a new top? */
285  }
286 
287  if (data != NULL) {
288  timer->data = data;
289  }
290 
291  unlock(timer->timerset);
292 
293  if (wakeup)
295 }
static void abort_elapsed(Timer *timer)
Definition: gw-timer.c:447
Timer ** tab
Definition: gw-timer.c:83
void * data
Definition: gw-timer.c:144
TimerHeap * heap
Definition: gw-timer.c:106
static void heap_insert(TimerHeap *heap, Timer *timer)
Definition: gw-timer.c:506
static void lock(Timerset *set)
Definition: gw-timer.c:429
gw_assert(wtls_machine->packet_to_send!=NULL)
Timerset * timerset
Definition: gw-timer.c:119
double interval
Definition: fakewap.c:234
long thread
Definition: gw-timer.c:111
long elapses
Definition: gw-timer.c:138
void gwthread_wakeup(long thread)
long index
Definition: gw-timer.c:157
static void unlock(Timerset *set)
Definition: gw-timer.c:435
static int heap_adjust(TimerHeap *heap, long index)
Definition: gw-timer.c:549
void gw_timer_stop ( Timer timer)

Definition at line 344 of file gw-timer.c.

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

Referenced by gw_timer_destroy(), and gw_timerset_destroy().

345 {
346  gw_assert(timer != NULL);
347  lock(timer->timerset);
348 
349  /*
350  * If the timer is active, make it inactive and remove it from
351  * the heap.
352  */
353  if (timer->elapses > 0) {
354  timer->elapses = -1;
355  gw_assert(timer->timerset->heap->tab[timer->index] == timer);
356  heap_delete(timer->timerset->heap, timer->index);
357  }
358 
359  abort_elapsed(timer);
360 
361  unlock(timer->timerset);
362 }
static void abort_elapsed(Timer *timer)
Definition: gw-timer.c:447
Timer ** tab
Definition: gw-timer.c:83
TimerHeap * heap
Definition: gw-timer.c:106
static void lock(Timerset *set)
Definition: gw-timer.c:429
gw_assert(wtls_machine->packet_to_send!=NULL)
Timerset * timerset
Definition: gw-timer.c:119
long elapses
Definition: gw-timer.c:138
long index
Definition: gw-timer.c:157
static void unlock(Timerset *set)
Definition: gw-timer.c:435
static void heap_delete(TimerHeap *heap, long index)
Definition: gw-timer.c:486
Timerset* gw_timerset_create ( void  )

Definition at line 177 of file gw-timer.c.

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

Referenced by main().

178 {
179  Timerset *set;
180 
181  set = gw_malloc(sizeof(Timerset));
182  set->mutex = mutex_create();
183  set->heap = heap_create();
184  set->stopping = 0;
185  set->thread = gwthread_create(watch_timers, set);
186 
187  return set;
188 }
volatile sig_atomic_t stopping
Definition: gw-timer.c:94
#define mutex_create()
Definition: thread.h:96
TimerHeap * heap
Definition: gw-timer.c:106
static void watch_timers(void *arg)
Definition: gw-timer.c:636
#define gwthread_create(func, arg)
Definition: gwthread.h:90
long thread
Definition: gw-timer.c:111
static TimerHeap * heap_create(void)
Definition: gw-timer.c:460
Mutex * mutex
Definition: gw-timer.c:101
void gw_timerset_destroy ( Timerset set)

Definition at line 190 of file gw-timer.c.

References gw_timer_stop(), gwthread_join(), gwthread_wakeup(), Timerset::heap, heap_destroy(), TimerHeap::len, Timerset::mutex, mutex_destroy(), Timerset::stopping, TimerHeap::tab, and Timerset::thread.

Referenced by main().

191 {
192  if (set == NULL)
193  return;
194 
195  /* Stop all timers. */
196  while (set->heap->len > 0)
197  gw_timer_stop(set->heap->tab[0]);
198 
199  /* Kill timer thread */
200  set->stopping = 1;
201  gwthread_wakeup(set->thread);
202  gwthread_join(set->thread);
203 
204  /* Free resources */
205  heap_destroy(set->heap);
206  mutex_destroy(set->mutex);
207  gw_free(set);
208 }
volatile sig_atomic_t stopping
Definition: gw-timer.c:94
void gwthread_join(long thread)
static void heap_destroy(TimerHeap *heap)
Definition: gw-timer.c:472
Timer ** tab
Definition: gw-timer.c:83
TimerHeap * heap
Definition: gw-timer.c:106
void gw_timer_stop(Timer *timer)
Definition: gw-timer.c:344
void mutex_destroy(Mutex *mutex)
Definition: thread.c:97
long thread
Definition: gw-timer.c:111
void gwthread_wakeup(long thread)
long len
Definition: gw-timer.c:84
Mutex * mutex
Definition: gw-timer.c:101
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.