mirror of
https://gitlab.nic.cz/labs/bird.git
synced 2024-12-22 17:51:53 +00:00
Dropping the POSIX thread-local variables in favor of much easier-to-use C11 thread-local variables
This commit is contained in:
parent
b5061659d3
commit
feb17ced23
47
lib/timer.c
47
lib/timer.c
@ -43,38 +43,23 @@ struct timeloop main_timeloop;
|
|||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
||||||
/* Data accessed and modified from proto/bfd/io.c */
|
/* Data accessed and modified from proto/bfd/io.c */
|
||||||
pthread_key_t current_time_key;
|
_Thread_local struct timeloop *local_timeloop;
|
||||||
|
|
||||||
static inline struct timeloop *
|
|
||||||
timeloop_current(void)
|
|
||||||
{
|
|
||||||
return pthread_getspecific(current_time_key);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void
|
|
||||||
timeloop_init_current(void)
|
|
||||||
{
|
|
||||||
pthread_key_create(¤t_time_key, NULL);
|
|
||||||
pthread_setspecific(current_time_key, &main_timeloop);
|
|
||||||
}
|
|
||||||
|
|
||||||
void wakeup_kick_current(void);
|
void wakeup_kick_current(void);
|
||||||
|
|
||||||
btime
|
btime
|
||||||
current_time(void)
|
current_time(void)
|
||||||
{
|
{
|
||||||
return timeloop_current()->last_time;
|
return local_timeloop->last_time;
|
||||||
}
|
}
|
||||||
|
|
||||||
btime
|
btime
|
||||||
current_real_time(void)
|
current_real_time(void)
|
||||||
{
|
{
|
||||||
struct timeloop *loop = timeloop_current();
|
if (!local_timeloop->real_time)
|
||||||
|
times_update_real_time(local_timeloop);
|
||||||
|
|
||||||
if (!loop->real_time)
|
return local_timeloop->real_time;
|
||||||
times_update_real_time(loop);
|
|
||||||
|
|
||||||
return loop->real_time;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -128,30 +113,29 @@ tm_new(pool *p)
|
|||||||
void
|
void
|
||||||
tm_set(timer *t, btime when)
|
tm_set(timer *t, btime when)
|
||||||
{
|
{
|
||||||
struct timeloop *loop = timeloop_current();
|
uint tc = timers_count(local_timeloop);
|
||||||
uint tc = timers_count(loop);
|
|
||||||
|
|
||||||
if (!t->expires)
|
if (!t->expires)
|
||||||
{
|
{
|
||||||
t->index = ++tc;
|
t->index = ++tc;
|
||||||
t->expires = when;
|
t->expires = when;
|
||||||
BUFFER_PUSH(loop->timers) = t;
|
BUFFER_PUSH(local_timeloop->timers) = t;
|
||||||
HEAP_INSERT(loop->timers.data, tc, timer *, TIMER_LESS, TIMER_SWAP);
|
HEAP_INSERT(local_timeloop->timers.data, tc, timer *, TIMER_LESS, TIMER_SWAP);
|
||||||
}
|
}
|
||||||
else if (t->expires < when)
|
else if (t->expires < when)
|
||||||
{
|
{
|
||||||
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)
|
else if (t->expires > when)
|
||||||
{
|
{
|
||||||
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
|
#ifdef CONFIG_BFD
|
||||||
/* Hack to notify BFD loops */
|
/* Hack to notify BFD loops */
|
||||||
if ((loop != &main_timeloop) && (t->index == 1))
|
if ((local_timeloop != &main_timeloop) && (t->index == 1))
|
||||||
wakeup_kick_current();
|
wakeup_kick_current();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@ -168,11 +152,10 @@ tm_stop(timer *t)
|
|||||||
if (!t->expires)
|
if (!t->expires)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
struct timeloop *loop = timeloop_current();
|
uint tc = timers_count(local_timeloop);
|
||||||
uint tc = timers_count(loop);
|
|
||||||
|
|
||||||
HEAP_DELETE(loop->timers.data, tc, timer *, TIMER_LESS, TIMER_SWAP, t->index);
|
HEAP_DELETE(local_timeloop->timers.data, tc, timer *, TIMER_LESS, TIMER_SWAP, t->index);
|
||||||
BUFFER_POP(loop->timers);
|
BUFFER_POP(local_timeloop->timers);
|
||||||
|
|
||||||
t->index = -1;
|
t->index = -1;
|
||||||
t->expires = 0;
|
t->expires = 0;
|
||||||
@ -230,7 +213,7 @@ void
|
|||||||
timer_init(void)
|
timer_init(void)
|
||||||
{
|
{
|
||||||
timers_init(&main_timeloop, &root_pool);
|
timers_init(&main_timeloop, &root_pool);
|
||||||
timeloop_init_current();
|
local_timeloop = &main_timeloop;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -42,6 +42,7 @@ static inline timer *timers_first(struct timeloop *loop)
|
|||||||
{ return (loop->timers.used > 1) ? loop->timers.data[1] : NULL; }
|
{ return (loop->timers.used > 1) ? loop->timers.data[1] : NULL; }
|
||||||
|
|
||||||
extern struct timeloop main_timeloop;
|
extern struct timeloop main_timeloop;
|
||||||
|
extern _Thread_local struct timeloop *local_timeloop;
|
||||||
|
|
||||||
btime current_time(void);
|
btime current_time(void);
|
||||||
btime current_real_time(void);
|
btime current_real_time(void);
|
||||||
|
@ -52,29 +52,15 @@ struct birdloop
|
|||||||
* Current thread context
|
* Current thread context
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static pthread_key_t current_loop_key;
|
static _Thread_local struct birdloop *birdloop_current;
|
||||||
extern pthread_key_t current_time_key;
|
|
||||||
|
|
||||||
static inline struct birdloop *
|
|
||||||
birdloop_current(void)
|
|
||||||
{
|
|
||||||
return pthread_getspecific(current_loop_key);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
birdloop_set_current(struct birdloop *loop)
|
birdloop_set_current(struct birdloop *loop)
|
||||||
{
|
{
|
||||||
pthread_setspecific(current_loop_key, loop);
|
birdloop_current = loop;
|
||||||
pthread_setspecific(current_time_key, loop ? &loop->time : &main_timeloop);
|
local_timeloop = loop ? &loop->time : &main_timeloop;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
|
||||||
birdloop_init_current(void)
|
|
||||||
{
|
|
||||||
pthread_key_create(¤t_loop_key, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Wakeup code for birdloop
|
* Wakeup code for birdloop
|
||||||
*/
|
*/
|
||||||
@ -162,10 +148,8 @@ wakeup_kick(struct birdloop *loop)
|
|||||||
void
|
void
|
||||||
wakeup_kick_current(void)
|
wakeup_kick_current(void)
|
||||||
{
|
{
|
||||||
struct birdloop *loop = birdloop_current();
|
if (birdloop_current && birdloop_current->poll_active)
|
||||||
|
wakeup_kick(birdloop_current);
|
||||||
if (loop && loop->poll_active)
|
|
||||||
wakeup_kick(loop);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -195,15 +179,13 @@ events_fire(struct birdloop *loop)
|
|||||||
void
|
void
|
||||||
ev2_schedule(event *e)
|
ev2_schedule(event *e)
|
||||||
{
|
{
|
||||||
struct birdloop *loop = birdloop_current();
|
if (birdloop_current->poll_active && EMPTY_LIST(birdloop_current->event_list))
|
||||||
|
wakeup_kick(birdloop_current);
|
||||||
if (loop->poll_active && EMPTY_LIST(loop->event_list))
|
|
||||||
wakeup_kick(loop);
|
|
||||||
|
|
||||||
if (e->n.next)
|
if (e->n.next)
|
||||||
rem_node(&e->n);
|
rem_node(&e->n);
|
||||||
|
|
||||||
add_tail(&loop->event_list, &e->n);
|
add_tail(&birdloop_current->event_list, &e->n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -238,9 +220,7 @@ sockets_add(struct birdloop *loop, sock *s)
|
|||||||
void
|
void
|
||||||
sk_start(sock *s)
|
sk_start(sock *s)
|
||||||
{
|
{
|
||||||
struct birdloop *loop = birdloop_current();
|
sockets_add(birdloop_current, s);
|
||||||
|
|
||||||
sockets_add(loop, s);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -261,14 +241,12 @@ sockets_remove(struct birdloop *loop, sock *s)
|
|||||||
void
|
void
|
||||||
sk_stop(sock *s)
|
sk_stop(sock *s)
|
||||||
{
|
{
|
||||||
struct birdloop *loop = birdloop_current();
|
sockets_remove(birdloop_current, s);
|
||||||
|
|
||||||
sockets_remove(loop, s);
|
if (birdloop_current->poll_active)
|
||||||
|
|
||||||
if (loop->poll_active)
|
|
||||||
{
|
{
|
||||||
loop->close_scheduled = 1;
|
birdloop_current->close_scheduled = 1;
|
||||||
wakeup_kick(loop);
|
wakeup_kick(birdloop_current);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
close(s->fd);
|
close(s->fd);
|
||||||
@ -392,11 +370,6 @@ static void * birdloop_main(void *arg);
|
|||||||
struct birdloop *
|
struct birdloop *
|
||||||
birdloop_new(void)
|
birdloop_new(void)
|
||||||
{
|
{
|
||||||
/* FIXME: this init should be elsewhere and thread-safe */
|
|
||||||
static int init = 0;
|
|
||||||
if (!init)
|
|
||||||
{ birdloop_init_current(); init = 1; }
|
|
||||||
|
|
||||||
pool *p = rp_new(NULL, "Birdloop root");
|
pool *p = rp_new(NULL, "Birdloop root");
|
||||||
struct birdloop *loop = mb_allocz(p, sizeof(struct birdloop));
|
struct birdloop *loop = mb_allocz(p, sizeof(struct birdloop));
|
||||||
loop->pool = p;
|
loop->pool = p;
|
||||||
|
Loading…
Reference in New Issue
Block a user