Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions code/common/src/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ timer_dequeue_bucket(timer_s *timer)
timer_bucket->timers--;
timer->timer_bucket = NULL;

/* Defer deleting empty buckets to timer_process_changes(). */
/*
* Defer deleting empty buckets to timer_walk().
*/
}

static void
Expand Down Expand Up @@ -336,6 +338,23 @@ timer_del(timer_s *timer)
}
}

/**
* Delete a timer bucket
*/
void
timer_del_bucket(timer_root_s *root, timer_bucket_s *timer_bucket)
{
CIRCLEQ_REMOVE(&root->timer_bucket_qhead, timer_bucket, timer_bucket_qnode);

#ifdef BNGBLASTER_TIMER_LOGGING
LOG(TIMER_DETAIL, " Delete timer bucket %lu.%06lus\n",
timer_bucket->sec, timer_bucket->nsec/1000);
#endif

free(timer_bucket);
root->buckets--;
}

/**
* Deferred processing of all timers.
*/
Expand Down Expand Up @@ -475,7 +494,7 @@ void
timer_walk(timer_root_s *root)
{
timer_s *timer;
timer_bucket_s *timer_bucket;
timer_bucket_s *timer_bucket, *timer_bucket_next;
struct timespec now, min, sleep, rem;
int res;

Expand All @@ -495,7 +514,8 @@ timer_walk(timer_root_s *root)
min.tv_nsec = 0;

/* Walk all buckets. */
CIRCLEQ_FOREACH(timer_bucket, &root->timer_bucket_qhead, timer_bucket_qnode) {
CIRCLEQ_FOREACH_SAFE(timer_bucket, &root->timer_bucket_qhead,
timer_bucket_qnode, timer_bucket_next) {

#ifdef BNGBLASTER_TIMER_LOGGING
LOG(TIMER_DETAIL, " Checking timer bucket %lu.%06lus\n",
Expand Down Expand Up @@ -532,6 +552,11 @@ timer_walk(timer_root_s *root)
timer_del(timer);
}
}

/* If the bucket is empty, remove it. */
if(!timer_bucket->timers) {
timer_del_bucket(root, timer_bucket);
}
}

/* Process all changes from the last timer run. */
Expand Down
8 changes: 8 additions & 0 deletions code/common/src/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
(var) = (tvar))
#endif

#ifndef CIRCLEQ_FOREACH_SAFE
#define CIRCLEQ_FOREACH_SAFE(var, head, field, tvar) \
for ((var) = CIRCLEQ_FIRST((head)); \
(var) != (void *)(head) && \
((tvar) = CIRCLEQ_NEXT((var), field), 1); \
(var) = (tvar))
#endif

/* Top level data structure for timers. */
typedef struct timer_root_
{
Expand Down
Loading