Kannel: Open Source WAP and SMS gateway  svn-r5335
gw-timer.c File Reference
#include <signal.h>
#include "gwlib/gwlib.h"
#include "gw-timer.h"

Go to the source code of this file.

Data Structures

struct  TimerHeap
 
struct  Timerset
 
struct  Timer
 

Typedefs

typedef struct TimerHeap TimerHeap
 

Functions

static void abort_elapsed (Timer *timer)
 
static TimerHeapheap_create (void)
 
static void heap_destroy (TimerHeap *heap)
 
static void heap_delete (TimerHeap *heap, long index)
 
static int heap_adjust (TimerHeap *heap, long index)
 
static void heap_insert (TimerHeap *heap, Timer *timer)
 
static void heap_swap (TimerHeap *heap, long index1, long index2)
 
static void watch_timers (void *arg)
 
static void elapse_timer (Timer *timer)
 
static void lock (Timerset *set)
 
static void unlock (Timerset *set)
 
Timersetgw_timerset_create (void)
 
void gw_timerset_destroy (Timerset *set)
 
void gw_timerset_elapsed_destroy (Timerset *set)
 
long gw_timerset_count (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_elapsed_destroy_cb (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_elapsed_start_cb (Timer *timer, int interval, void *data)
 
void gw_timer_stop (Timer *timer)
 
void gw_timer_elapsed_stop (Timer *timer)
 
void gw_timer_elapsed_stop_cb (Timer *timer)
 
Listgw_timer_break (Timerset *set)
 
void * gw_timer_data (Timer *timer)
 

Typedef Documentation

◆ TimerHeap

typedef struct TimerHeap TimerHeap

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

Function Documentation

◆ abort_elapsed()

static void abort_elapsed ( Timer timer)
static

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

References Timer::elapsed_data, gwlist_delete_equal(), and Timer::output.

Referenced by gw_timer_break(), gw_timer_start(), and gw_timer_stop().

584 {
585  if (timer->elapsed_data == NULL)
586  return;
587 
588  if (timer->output != NULL)
589  gwlist_delete_equal(timer->output, timer->elapsed_data);
590  timer->elapsed_data = NULL;
591 }
List * output
Definition: gw-timer.c:132
void * elapsed_data
Definition: gw-timer.c:155
long gwlist_delete_equal(List *list, void *item)
Definition: list.c:266

◆ elapse_timer()

static void elapse_timer ( Timer timer)
static

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

References Timer::callback, Timer::data, Timer::elapsed_data, Timer::elapses, gw_assert(), gwlist_produce(), Timer::output, and Timer::timerset.

Referenced by watch_timers().

754 {
755  gw_assert(timer != NULL);
756  gw_assert(timer->timerset != NULL);
757  /* This must be true because abort_elapsed is always called
758  * before a timer is activated. */
759  gw_assert(timer->elapsed_data == NULL);
760 
761  timer->elapsed_data = timer->data;
762  timer->elapses = -1;
763  if (timer->output != NULL)
764  gwlist_produce(timer->output, timer->elapsed_data);
765  if (timer->callback != NULL)
766  timer->callback(timer->elapsed_data);
767 }
gw_assert(wtls_machine->packet_to_send !=NULL)
void gwlist_produce(List *list, void *item)
Definition: list.c:411
List * output
Definition: gw-timer.c:132
void * data
Definition: gw-timer.c:148
void(* callback)(void *data)
Definition: gw-timer.c:136
void * elapsed_data
Definition: gw-timer.c:155
Timerset * timerset
Definition: gw-timer.c:123
long elapses
Definition: gw-timer.c:142

◆ gw_timer_break()

List* gw_timer_break ( Timerset set)

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

References abort_elapsed(), Timer::elapses, gw_assert(), gwlist_append(), gwlist_create, heap_delete(), Timer::index, lock(), and unlock().

502 {
503  List *ret = NULL;
504 
505  lock(set);
506 
507  if (set->heap->len == 0) {
508  unlock(set);
509  return NULL;
510  }
511 
512  ret = gwlist_create();
513 
514  /* Stop all timers. */
515  while (set->heap->len > 0) {
516  Timer *timer = set->heap->tab[0];
517 
518  gwlist_append(ret, timer);
519 
520  /*
521  * If the timer is active, make it inactive and remove it from
522  * the heap.
523  */
524  if (timer->elapses > 0) {
525  timer->elapses = -1;
526  gw_assert(set->heap->tab[timer->index] == timer);
527  heap_delete(set->heap, timer->index);
528  }
529 
530  abort_elapsed(timer);
531  }
532 
533  unlock(set);
534 
535  return ret;
536 }
gw_assert(wtls_machine->packet_to_send !=NULL)
void gwlist_append(List *list, void *item)
Definition: list.c:179
static void abort_elapsed(Timer *timer)
Definition: gw-timer.c:583
Timer ** tab
Definition: gw-timer.c:87
TimerHeap * heap
Definition: gw-timer.c:110
static void lock(Timerset *set)
Definition: gw-timer.c:563
long elapses
Definition: gw-timer.c:142
#define gwlist_create()
Definition: list.h:136
long index
Definition: gw-timer.c:161
long len
Definition: gw-timer.c:88
static void unlock(Timerset *set)
Definition: gw-timer.c:569
static void heap_delete(TimerHeap *heap, long index)
Definition: gw-timer.c:622
Definition: list.c:102

◆ gw_timer_create()

Timer* gw_timer_create ( Timerset set,
List outputlist,
void(*)(void *)  callback 
)

Definition at line 255 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 main(), retry_consumer_thread(), and url_result_thread().

256 {
257  Timer *t;
258 
259  t = gw_malloc(sizeof(*t));
260  t->timerset = set;
261  t->elapses = -1;
262  t->data = NULL;
263  t->elapsed_data = NULL;
264  t->index = -1;
265  t->output = outputlist;
266  if (t->output != NULL)
267  gwlist_add_producer(outputlist);
268  t->callback = callback;
269 
270  return t;
271 }
List * output
Definition: gw-timer.c:132
void * data
Definition: gw-timer.c:148
void(* callback)(void *data)
Definition: gw-timer.c:136
void * elapsed_data
Definition: gw-timer.c:155
Timerset * timerset
Definition: gw-timer.c:123
long elapses
Definition: gw-timer.c:142
long index
Definition: gw-timer.c:161
void gwlist_add_producer(List *list)
Definition: list.c:383

◆ gw_timer_data()

void* gw_timer_data ( Timer timer)

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

References Timer::data, and gw_assert().

539 {
540  gw_assert(timer != NULL);
541 
542  return timer->data;
543 }
gw_assert(wtls_machine->packet_to_send !=NULL)
void * data
Definition: gw-timer.c:148

◆ gw_timer_destroy()

void gw_timer_destroy ( Timer timer)

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

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

274 {
275  if (timer == NULL)
276  return;
277 
278  gw_timer_stop(timer);
279  if (timer->output != NULL)
281  gw_free(timer);
282 }
List * output
Definition: gw-timer.c:132
void gw_timer_stop(Timer *timer)
Definition: gw-timer.c:442
void gwlist_remove_producer(List *list)
Definition: list.c:401

◆ gw_timer_elapsed_destroy()

void gw_timer_elapsed_destroy ( Timer timer)

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

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

Referenced by gw_timerset_elapsed_destroy(), http_queue_thread(), and timer_consumer_thread().

285 {
286  if (timer == NULL)
287  return;
288 
289  gw_timer_elapsed_stop(timer);
290  if (timer->output != NULL)
292  gw_free(timer);
293 }
List * output
Definition: gw-timer.c:132
void gw_timer_elapsed_stop(Timer *timer)
Definition: gw-timer.c:462
void gwlist_remove_producer(List *list)
Definition: list.c:401

◆ gw_timer_elapsed_destroy_cb()

void gw_timer_elapsed_destroy_cb ( Timer timer)

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

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

Referenced by timer_retry_cb().

296 {
297  if (timer == NULL)
298  return;
299 
301  if (timer->output != NULL)
303  gw_free(timer);
304 }
List * output
Definition: gw-timer.c:132
void gw_timer_elapsed_stop_cb(Timer *timer)
Definition: gw-timer.c:483
void gwlist_remove_producer(List *list)
Definition: list.c:401

◆ gw_timer_elapsed_start()

void gw_timer_elapsed_start ( Timer timer,
int  interval,
void *  data 
)

Definition at line 352 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 main(), retry_consumer_thread(), timer_consumer_thread(), and url_result_thread().

353 {
354  int wakeup = 0;
355 
356  gw_assert(timer != NULL);
357 
358  if (timer == NULL)
359  return;
360 
361  lock(timer->timerset);
362 
363  /* Convert to absolute time */
364  interval += time(NULL);
365 
366  if (timer->elapses > 0) {
367  /* Resetting an existing timer. Move it to its new
368  * position in the heap. */
369  if (interval < timer->elapses && timer->index == 0)
370  wakeup = 1;
371  timer->elapses = interval;
372  gw_assert(timer->index >= 0);
373  gw_assert(timer->timerset->heap->tab[timer->index] == timer);
374  wakeup |= heap_adjust(timer->timerset->heap, timer->index);
375  } else {
376  /* Setting a new timer, or resetting an elapsed one.
377  * There should be no further elapse event on the
378  * output list here. */
379  /* abort_elapsed(timer); */
380  timer->elapsed_data = NULL;
381 
382  /* Then activate the timer. */
383  timer->elapses = interval;
384  gw_assert(timer->index < 0);
385  heap_insert(timer->timerset->heap, timer);
386  wakeup = timer->index == 0; /* Do we have a new top? */
387  }
388 
389  if (data != NULL) {
390  timer->data = data;
391  }
392 
393  unlock(timer->timerset);
394 
395  if (wakeup)
397 }
gw_assert(wtls_machine->packet_to_send !=NULL)
Timer ** tab
Definition: gw-timer.c:87
void * data
Definition: gw-timer.c:148
TimerHeap * heap
Definition: gw-timer.c:110
static void heap_insert(TimerHeap *heap, Timer *timer)
Definition: gw-timer.c:642
static void lock(Timerset *set)
Definition: gw-timer.c:563
void * elapsed_data
Definition: gw-timer.c:155
Timerset * timerset
Definition: gw-timer.c:123
double interval
Definition: fakewap.c:234
long thread
Definition: gw-timer.c:115
long elapses
Definition: gw-timer.c:142
void gwthread_wakeup(long thread)
long index
Definition: gw-timer.c:161
static void unlock(Timerset *set)
Definition: gw-timer.c:569
static int heap_adjust(TimerHeap *heap, long index)
Definition: gw-timer.c:685

◆ gw_timer_elapsed_start_cb()

void gw_timer_elapsed_start_cb ( Timer timer,
int  interval,
void *  data 
)

Definition at line 399 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, TimerHeap::tab, Timerset::thread, and Timer::timerset.

400 {
401  int wakeup = 0;
402 
403  gw_assert(timer != NULL);
404 
405  if (timer == NULL)
406  return;
407 
408  /* Convert to absolute time */
409  interval += time(NULL);
410 
411  if (timer->elapses > 0) {
412  /* Resetting an existing timer. Move it to its new
413  * position in the heap. */
414  if (interval < timer->elapses && timer->index == 0)
415  wakeup = 1;
416  timer->elapses = interval;
417  gw_assert(timer->index >= 0);
418  gw_assert(timer->timerset->heap->tab[timer->index] == timer);
419  wakeup |= heap_adjust(timer->timerset->heap, timer->index);
420  } else {
421  /* Setting a new timer, or resetting an elapsed one.
422  * There should be no further elapse event on the
423  * output list here. */
424  /* abort_elapsed(timer); */
425  timer->elapsed_data = NULL;
426 
427  /* Then activate the timer. */
428  timer->elapses = interval;
429  gw_assert(timer->index < 0);
430  heap_insert(timer->timerset->heap, timer);
431  wakeup = timer->index == 0; /* Do we have a new top? */
432  }
433 
434  if (data != NULL) {
435  timer->data = data;
436  }
437 
438  if (wakeup)
440 }
gw_assert(wtls_machine->packet_to_send !=NULL)
Timer ** tab
Definition: gw-timer.c:87
void * data
Definition: gw-timer.c:148
TimerHeap * heap
Definition: gw-timer.c:110
static void heap_insert(TimerHeap *heap, Timer *timer)
Definition: gw-timer.c:642
void * elapsed_data
Definition: gw-timer.c:155
Timerset * timerset
Definition: gw-timer.c:123
double interval
Definition: fakewap.c:234
long thread
Definition: gw-timer.c:115
long elapses
Definition: gw-timer.c:142
void gwthread_wakeup(long thread)
long index
Definition: gw-timer.c:161
static int heap_adjust(TimerHeap *heap, long index)
Definition: gw-timer.c:685

◆ gw_timer_elapsed_stop()

void gw_timer_elapsed_stop ( Timer timer)

Definition at line 462 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().

463 {
464  gw_assert(timer != NULL);
465  lock(timer->timerset);
466 
467  /*
468  * If the timer is active, make it inactive and remove it from
469  * the heap.
470  */
471  if (timer->elapses > 0) {
472  timer->elapses = -1;
473  gw_assert(timer->timerset->heap->tab[timer->index] == timer);
474  heap_delete(timer->timerset->heap, timer->index);
475  }
476 
477  /* abort_elapsed(timer); */
478  timer->elapsed_data = NULL;
479 
480  unlock(timer->timerset);
481 }
gw_assert(wtls_machine->packet_to_send !=NULL)
Timer ** tab
Definition: gw-timer.c:87
TimerHeap * heap
Definition: gw-timer.c:110
static void lock(Timerset *set)
Definition: gw-timer.c:563
void * elapsed_data
Definition: gw-timer.c:155
Timerset * timerset
Definition: gw-timer.c:123
long elapses
Definition: gw-timer.c:142
long index
Definition: gw-timer.c:161
static void unlock(Timerset *set)
Definition: gw-timer.c:569
static void heap_delete(TimerHeap *heap, long index)
Definition: gw-timer.c:622

◆ gw_timer_elapsed_stop_cb()

void gw_timer_elapsed_stop_cb ( Timer timer)

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

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

Referenced by gw_timer_elapsed_destroy_cb().

484 {
485  gw_assert(timer != NULL);
486 
487  /*
488  * If the timer is active, make it inactive and remove it from
489  * the heap.
490  */
491  if (timer->elapses > 0) {
492  timer->elapses = -1;
493  gw_assert(timer->timerset->heap->tab[timer->index] == timer);
494  heap_delete(timer->timerset->heap, timer->index);
495  }
496 
497  /* abort_elapsed(timer); */
498  timer->elapsed_data = NULL;
499 }
gw_assert(wtls_machine->packet_to_send !=NULL)
Timer ** tab
Definition: gw-timer.c:87
TimerHeap * heap
Definition: gw-timer.c:110
void * elapsed_data
Definition: gw-timer.c:155
Timerset * timerset
Definition: gw-timer.c:123
long elapses
Definition: gw-timer.c:142
long index
Definition: gw-timer.c:161
static void heap_delete(TimerHeap *heap, long index)
Definition: gw-timer.c:622

◆ gw_timer_start()

void gw_timer_start ( Timer timer,
int  interval,
void *  data 
)

Definition at line 306 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().

307 {
308  int wakeup = 0;
309 
310  gw_assert(timer != NULL);
311 
312  if (timer == NULL)
313  return;
314 
315  lock(timer->timerset);
316 
317  /* Convert to absolute time */
318  interval += time(NULL);
319 
320  if (timer->elapses > 0) {
321  /* Resetting an existing timer. Move it to its new
322  * position in the heap. */
323  if (interval < timer->elapses && timer->index == 0)
324  wakeup = 1;
325  timer->elapses = interval;
326  gw_assert(timer->index >= 0);
327  gw_assert(timer->timerset->heap->tab[timer->index] == timer);
328  wakeup |= heap_adjust(timer->timerset->heap, timer->index);
329  } else {
330  /* Setting a new timer, or resetting an elapsed one.
331  * First deal with a possible elapse event that may
332  * still be on the output list. */
333  abort_elapsed(timer);
334 
335  /* Then activate the timer. */
336  timer->elapses = interval;
337  gw_assert(timer->index < 0);
338  heap_insert(timer->timerset->heap, timer);
339  wakeup = timer->index == 0; /* Do we have a new top? */
340  }
341 
342  if (data != NULL) {
343  timer->data = data;
344  }
345 
346  unlock(timer->timerset);
347 
348  if (wakeup)
350 }
gw_assert(wtls_machine->packet_to_send !=NULL)
static void abort_elapsed(Timer *timer)
Definition: gw-timer.c:583
Timer ** tab
Definition: gw-timer.c:87
void * data
Definition: gw-timer.c:148
TimerHeap * heap
Definition: gw-timer.c:110
static void heap_insert(TimerHeap *heap, Timer *timer)
Definition: gw-timer.c:642
static void lock(Timerset *set)
Definition: gw-timer.c:563
Timerset * timerset
Definition: gw-timer.c:123
double interval
Definition: fakewap.c:234
long thread
Definition: gw-timer.c:115
long elapses
Definition: gw-timer.c:142
void gwthread_wakeup(long thread)
long index
Definition: gw-timer.c:161
static void unlock(Timerset *set)
Definition: gw-timer.c:569
static int heap_adjust(TimerHeap *heap, long index)
Definition: gw-timer.c:685

◆ gw_timer_stop()

void gw_timer_stop ( Timer timer)

Definition at line 442 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().

443 {
444  gw_assert(timer != NULL);
445  lock(timer->timerset);
446 
447  /*
448  * If the timer is active, make it inactive and remove it from
449  * the heap.
450  */
451  if (timer->elapses > 0) {
452  timer->elapses = -1;
453  gw_assert(timer->timerset->heap->tab[timer->index] == timer);
454  heap_delete(timer->timerset->heap, timer->index);
455  }
456 
457  abort_elapsed(timer);
458 
459  unlock(timer->timerset);
460 }
gw_assert(wtls_machine->packet_to_send !=NULL)
static void abort_elapsed(Timer *timer)
Definition: gw-timer.c:583
Timer ** tab
Definition: gw-timer.c:87
TimerHeap * heap
Definition: gw-timer.c:110
static void lock(Timerset *set)
Definition: gw-timer.c:563
Timerset * timerset
Definition: gw-timer.c:123
long elapses
Definition: gw-timer.c:142
long index
Definition: gw-timer.c:161
static void unlock(Timerset *set)
Definition: gw-timer.c:569
static void heap_delete(TimerHeap *heap, long index)
Definition: gw-timer.c:622

◆ gw_timerset_count()

long gw_timerset_count ( Timerset set)

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

References lock(), and unlock().

Referenced by main().

245 {
246  long ret;
247 
248  lock(set);
249  ret = set->heap->len;
250  unlock(set);
251 
252  return ret;
253 }
static void lock(Timerset *set)
Definition: gw-timer.c:563
static void unlock(Timerset *set)
Definition: gw-timer.c:569

◆ gw_timerset_create()

Timerset* gw_timerset_create ( void  )

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

References gwthread_create, heap_create(), mutex_create, and watch_timers().

Referenced by main().

191 {
192  Timerset *set;
193 
194  set = gw_malloc(sizeof(Timerset));
195  set->mutex = mutex_create();
196  set->heap = heap_create();
197  set->stopping = 0;
198  set->thread = gwthread_create(watch_timers, set);
199 
200  return set;
201 }
#define mutex_create()
Definition: thread.h:96
static void watch_timers(void *arg)
Definition: gw-timer.c:772
#define gwthread_create(func, arg)
Definition: gwthread.h:90
static TimerHeap * heap_create(void)
Definition: gw-timer.c:596

◆ gw_timerset_destroy()

void gw_timerset_destroy ( Timerset set)

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

References gw_timer_stop(), gwthread_join(), gwthread_wakeup(), heap_destroy(), and mutex_destroy().

Referenced by main().

204 {
205  if (set == NULL)
206  return;
207 
208  /* Stop all timers. */
209  while (set->heap->len > 0)
210  gw_timer_stop(set->heap->tab[0]);
211 
212  /* Kill timer thread */
213  set->stopping = 1;
214  gwthread_wakeup(set->thread);
215  gwthread_join(set->thread);
216 
217  /* Free resources */
218  heap_destroy(set->heap);
219  mutex_destroy(set->mutex);
220  gw_free(set);
221 }
void gwthread_join(long thread)
static void heap_destroy(TimerHeap *heap)
Definition: gw-timer.c:608
Timer ** tab
Definition: gw-timer.c:87
TimerHeap * heap
Definition: gw-timer.c:110
void gw_timer_stop(Timer *timer)
Definition: gw-timer.c:442
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

◆ gw_timerset_elapsed_destroy()

void gw_timerset_elapsed_destroy ( Timerset set)

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

References gw_timer_elapsed_destroy(), gwthread_join(), gwthread_wakeup(), heap_destroy(), and mutex_destroy().

224 {
225  if (set == NULL)
226  return;
227 
228  /* Stop all timers. */
229  while (set->heap->len > 0)
231 
232  /* Kill timer thread */
233  set->stopping = 1;
234  gwthread_wakeup(set->thread);
235  gwthread_join(set->thread);
236 
237  /* Free resources */
238  heap_destroy(set->heap);
239  mutex_destroy(set->mutex);
240  gw_free(set);
241 }
void gwthread_join(long thread)
static void heap_destroy(TimerHeap *heap)
Definition: gw-timer.c:608
Timer ** tab
Definition: gw-timer.c:87
TimerHeap * heap
Definition: gw-timer.c:110
void mutex_destroy(Mutex *mutex)
Definition: thread.c:97
long thread
Definition: gw-timer.c:115
void gw_timer_elapsed_destroy(Timer *timer)
Definition: gw-timer.c:284
void gwthread_wakeup(long thread)
long len
Definition: gw-timer.c:88
Mutex * mutex
Definition: gw-timer.c:105

◆ heap_adjust()

static int heap_adjust ( TimerHeap heap,
long  index 
)
static

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

References Timer::elapses, gw_assert(), heap_swap(), TimerHeap::len, and TimerHeap::tab.

Referenced by gw_timer_elapsed_start(), gw_timer_elapsed_start_cb(), gw_timer_start(), heap_delete(), and heap_insert().

686 {
687  Timer *t;
688  Timer *parent;
689  long child_index;
690 
691  /*
692  * We can assume that the heap was fine before this element's
693  * elapse time was changed. There are three cases to deal
694  * with:
695  * - Element's new elapse time is too small; it should be
696  * moved toward the top.
697  * - Element's new elapse time is too large; it should be
698  * moved toward the bottom.
699  * - Element's new elapse time still fits here, we don't
700  * have to do anything.
701  */
702 
703  gw_assert(index >= 0);
704  gw_assert(index < heap->len);
705 
706  /* Move to top? */
707  t = heap->tab[index];
708  parent = heap->tab[index / 2];
709  if (t->elapses < parent->elapses) {
710  /* This will automatically terminate when it reaches
711  * the top, because in that t == parent. */
712  do {
713  heap_swap(heap, index, index / 2);
714  index = index / 2;
715  parent = heap->tab[index / 2];
716  } while (t->elapses < parent->elapses);
717  /* We're done. Return 1 if we changed the top. */
718  return index == 0;
719  }
720 
721  /* Move to bottom? */
722  for (; ; ) {
723  child_index = index * 2;
724  if (child_index >= heap->len)
725  return 0; /* Already at bottom */
726  if (child_index == heap->len - 1) {
727  /* Only one child */
728  if (heap->tab[child_index]->elapses < t->elapses)
729  heap_swap(heap, index, child_index);
730  break;
731  }
732 
733  /* Find out which child elapses first */
734  if (heap->tab[child_index + 1]->elapses <
735  heap->tab[child_index]->elapses) {
736  child_index++;
737  }
738 
739  if (heap->tab[child_index]->elapses < t->elapses) {
740  heap_swap(heap, index, child_index);
741  index = child_index;
742  } else {
743  break;
744  }
745  }
746 
747  return 0;
748 }
gw_assert(wtls_machine->packet_to_send !=NULL)
Timer ** tab
Definition: gw-timer.c:87
static void heap_swap(TimerHeap *heap, long index1, long index2)
Definition: gw-timer.c:659
long elapses
Definition: gw-timer.c:142
long len
Definition: gw-timer.c:88

◆ heap_create()

static TimerHeap * heap_create ( void  )
static

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

References TimerHeap::len, TimerHeap::size, and TimerHeap::tab.

Referenced by gw_timerset_create().

597 {
598  TimerHeap *heap;
599 
600  heap = gw_malloc(sizeof(*heap));
601  heap->tab = gw_malloc(sizeof(heap->tab[0]));
602  heap->size = 1;
603  heap->len = 0;
604 
605  return heap;
606 }
long size
Definition: gw-timer.c:89
Timer ** tab
Definition: gw-timer.c:87
long len
Definition: gw-timer.c:88

◆ heap_delete()

static void heap_delete ( TimerHeap heap,
long  index 
)
static

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

References gw_assert(), heap_adjust(), heap_swap(), Timer::index, TimerHeap::len, and TimerHeap::tab.

Referenced by gw_timer_break(), gw_timer_elapsed_stop(), gw_timer_elapsed_stop_cb(), gw_timer_stop(), and watch_timers().

623 {
624  long last;
625 
626  gw_assert(index >= 0);
627  gw_assert(index < heap->len);
628  gw_assert(heap->tab[index]->index == index);
629 
630  last = heap->len - 1;
631  heap_swap(heap, index, last);
632  heap->tab[last]->index = -1;
633  heap->len--;
634  if (index != last)
635  heap_adjust(heap, index);
636 }
gw_assert(wtls_machine->packet_to_send !=NULL)
Timer ** tab
Definition: gw-timer.c:87
static void heap_swap(TimerHeap *heap, long index1, long index2)
Definition: gw-timer.c:659
long index
Definition: gw-timer.c:161
long len
Definition: gw-timer.c:88
static int heap_adjust(TimerHeap *heap, long index)
Definition: gw-timer.c:685

◆ heap_destroy()

static void heap_destroy ( TimerHeap heap)
static

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

References TimerHeap::tab.

Referenced by gw_timerset_destroy(), and gw_timerset_elapsed_destroy().

609 {
610  if (heap == NULL)
611  return;
612 
613  gw_free(heap->tab);
614  gw_free(heap);
615 }
Timer ** tab
Definition: gw-timer.c:87

◆ heap_insert()

static void heap_insert ( TimerHeap heap,
Timer timer 
)
static

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

References heap_adjust(), Timer::index, TimerHeap::len, TimerHeap::size, and TimerHeap::tab.

Referenced by gw_timer_elapsed_start(), gw_timer_elapsed_start_cb(), and gw_timer_start().

643 {
644  heap->len++;
645  if (heap->len > heap->size) {
646  heap->tab = gw_realloc(heap->tab,
647  heap->len * sizeof(heap->tab[0]));
648  heap->size = heap->len;
649  }
650  heap->tab[heap->len - 1] = timer;
651  timer->index = heap->len - 1;
652  heap_adjust(heap, timer->index);
653 }
long size
Definition: gw-timer.c:89
Timer ** tab
Definition: gw-timer.c:87
long index
Definition: gw-timer.c:161
long len
Definition: gw-timer.c:88
static int heap_adjust(TimerHeap *heap, long index)
Definition: gw-timer.c:685

◆ heap_swap()

static void heap_swap ( TimerHeap heap,
long  index1,
long  index2 
)
static

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

References gw_assert(), Timer::index, and TimerHeap::tab.

Referenced by heap_adjust(), and heap_delete().

660 {
661  Timer *t;
662 
663  gw_assert(index1 >= 0);
664  gw_assert(index1 < heap->len);
665  gw_assert(index2 >= 0);
666  gw_assert(index2 < heap->len);
667 
668  if (index1 == index2)
669  return;
670 
671  t = heap->tab[index1];
672  heap->tab[index1] = heap->tab[index2];
673  heap->tab[index2] = t;
674  heap->tab[index1]->index = index1;
675  heap->tab[index2]->index = index2;
676 }
gw_assert(wtls_machine->packet_to_send !=NULL)
Timer ** tab
Definition: gw-timer.c:87
long index
Definition: gw-timer.c:161

◆ lock()

static void lock ( Timerset set)
static

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

References gw_assert(), and mutex_lock.

Referenced by gw_timer_break(), gw_timer_elapsed_start(), gw_timer_elapsed_stop(), gw_timer_start(), gw_timer_stop(), gw_timerset_count(), and watch_timers().

564 {
565  gw_assert(set != NULL);
566  mutex_lock(set->mutex);
567 }
gw_assert(wtls_machine->packet_to_send !=NULL)
Mutex * mutex
Definition: gw-timer.c:105
#define mutex_lock(m)
Definition: thread.h:130

◆ unlock()

static void unlock ( Timerset set)
static

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

References gw_assert(), and mutex_unlock.

Referenced by gw_timer_break(), gw_timer_elapsed_start(), gw_timer_elapsed_stop(), gw_timer_start(), gw_timer_stop(), gw_timerset_count(), and watch_timers().

570 {
571  gw_assert(set != NULL);
572  mutex_unlock(set->mutex);
573 }
gw_assert(wtls_machine->packet_to_send !=NULL)
#define mutex_unlock(m)
Definition: thread.h:136
Mutex * mutex
Definition: gw-timer.c:105

◆ watch_timers()

static void watch_timers ( void *  arg)
static

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

References elapse_timer(), gwthread_sleep(), heap_delete(), lock(), and unlock().

Referenced by gw_timerset_create().

773 {
774  Timerset *set;
775  long top_time;
776  long now;
777 
778  set = arg;
779 
780  while (!set->stopping) {
781  lock(set);
782 
783  now = time(NULL);
784 
785  while (set->heap->len > 0 && set->heap->tab[0]->elapses <= now) {
786  Timer *timer = set->heap->tab[0];
787  heap_delete(set->heap, 0);
788  elapse_timer(timer);
789  }
790 
791  /*
792  * Now sleep until the next timer elapses. If there isn't one,
793  * then just sleep very long. We will get woken up if the
794  * top of the heap changes before we wake.
795  */
796 
797  if (set->heap->len == 0) {
798  unlock(set);
799  gwthread_sleep(1000000.0);
800  } else {
801  top_time = set->heap->tab[0]->elapses;
802  unlock(set);
803  gwthread_sleep(top_time - now);
804  }
805  }
806 }
static void lock(Timerset *set)
Definition: gw-timer.c:563
static void elapse_timer(Timer *timer)
Definition: gw-timer.c:753
void gwthread_sleep(double seconds)
static void unlock(Timerset *set)
Definition: gw-timer.c:569
static void heap_delete(TimerHeap *heap, long index)
Definition: gw-timer.c:622
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.