summaryrefslogtreecommitdiff
path: root/lib/timer.c
diff options
context:
space:
mode:
authorMaria Matejka <mq@ucw.cz>2021-06-18 18:10:42 +0200
committerMaria Matejka <mq@ucw.cz>2021-11-22 19:05:43 +0100
commitfeb17ced234bad13ae64b52a3f86241f74517997 (patch)
treeac334b42f11ae89d650eb39f6e2d736ed78e9ae7 /lib/timer.c
parentb5061659d3cc011118024861c2f048e67affbd39 (diff)
Dropping the POSIX thread-local variables in favor of much easier-to-use C11 thread-local variables
Diffstat (limited to 'lib/timer.c')
-rw-r--r--lib/timer.c47
1 files changed, 15 insertions, 32 deletions
diff --git a/lib/timer.c b/lib/timer.c
index f978a0f3..6efcadb4 100644
--- a/lib/timer.c
+++ b/lib/timer.c
@@ -43,38 +43,23 @@ struct timeloop main_timeloop;
#include <pthread.h>
/* Data accessed and modified from proto/bfd/io.c */
-pthread_key_t current_time_key;
-
-static inline struct timeloop *
-timeloop_current(void)
-{
- return pthread_getspecific(current_time_key);
-}
-
-static inline void
-timeloop_init_current(void)
-{
- pthread_key_create(&current_time_key, NULL);
- pthread_setspecific(current_time_key, &main_timeloop);
-}
+_Thread_local struct timeloop *local_timeloop;
void wakeup_kick_current(void);
btime
current_time(void)
{
- return timeloop_current()->last_time;
+ return local_timeloop->last_time;
}
btime
current_real_time(void)
{
- struct timeloop *loop = timeloop_current();
-
- if (!loop->real_time)
- times_update_real_time(loop);
+ if (!local_timeloop->real_time)
+ times_update_real_time(local_timeloop);
- return loop->real_time;
+ return local_timeloop->real_time;
}
@@ -128,30 +113,29 @@ tm_new(pool *p)
void
tm_set(timer *t, btime when)
{
- struct timeloop *loop = timeloop_current();
- uint tc = timers_count(loop);
+ uint tc = timers_count(local_timeloop);
if (!t->expires)
{
t->index = ++tc;
t->expires = when;
- BUFFER_PUSH(loop->timers) = t;
- HEAP_INSERT(loop->timers.data, tc, timer *, TIMER_LESS, TIMER_SWAP);
+ BUFFER_PUSH(local_timeloop->timers) = t;
+ HEAP_INSERT(local_timeloop->timers.data, tc, timer *, TIMER_LESS, TIMER_SWAP);
}
else if (t->expires < when)
{
t->expires = when;
- HEAP_INCREASE(loop->timers.data, tc, timer *, TIMER_LESS, TIMER_SWAP, t->index);
+ HEAP_INCREASE(local_timeloop->timers.data, tc, timer *, TIMER_LESS, TIMER_SWAP, t->index);
}
else if (t->expires > when)
{
t->expires = when;
- HEAP_DECREASE(loop->timers.data, tc, timer *, TIMER_LESS, TIMER_SWAP, t->index);
+ HEAP_DECREASE(local_timeloop->timers.data, tc, timer *, TIMER_LESS, TIMER_SWAP, t->index);
}
#ifdef CONFIG_BFD
/* Hack to notify BFD loops */
- if ((loop != &main_timeloop) && (t->index == 1))
+ if ((local_timeloop != &main_timeloop) && (t->index == 1))
wakeup_kick_current();
#endif
}
@@ -168,11 +152,10 @@ tm_stop(timer *t)
if (!t->expires)
return;
- struct timeloop *loop = timeloop_current();
- uint tc = timers_count(loop);
+ uint tc = timers_count(local_timeloop);
- HEAP_DELETE(loop->timers.data, tc, timer *, TIMER_LESS, TIMER_SWAP, t->index);
- BUFFER_POP(loop->timers);
+ HEAP_DELETE(local_timeloop->timers.data, tc, timer *, TIMER_LESS, TIMER_SWAP, t->index);
+ BUFFER_POP(local_timeloop->timers);
t->index = -1;
t->expires = 0;
@@ -230,7 +213,7 @@ void
timer_init(void)
{
timers_init(&main_timeloop, &root_pool);
- timeloop_init_current();
+ local_timeloop = &main_timeloop;
}