Commit fdb0a4da authored by Maria Matejka's avatar Maria Matejka
Browse files

Timer: Converting thread-local variables to C11 syntax

parent c38b7327
Loading
Loading
Loading
Loading
+6 −33
Original line number Diff line number Diff line
@@ -38,48 +38,21 @@


struct timeloop main_timeloop;
_Thread_local struct timeloop *timeloop_current;


#ifdef USE_PTHREADS

#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);
}

void wakeup_kick_current(void);

#else

/* Just use main timelooop */
static inline struct timeloop * timeloop_current(void) { return &main_timeloop; }
static inline void timeloop_init_current(void) { }

#endif

btime
current_time(void)
{
  return timeloop_current()->last_time;
  return timeloop_current->last_time;
}

btime
current_real_time(void)
{
  struct timeloop *loop = timeloop_current();
  struct timeloop *loop = timeloop_current;

  if (!loop->real_time)
    times_update_real_time(loop);
@@ -138,7 +111,7 @@ tm_new(pool *p)
void
tm_set(timer *t, btime when)
{
  struct timeloop *loop = timeloop_current();
  struct timeloop *loop = timeloop_current;
  uint tc = timers_count(loop);

  if (!t->expires)
@@ -178,7 +151,7 @@ tm_stop(timer *t)
  if (!t->expires)
    return;

  struct timeloop *loop = timeloop_current();
  struct timeloop *loop = timeloop_current;
  uint tc = timers_count(loop);

  HEAP_DELETE(loop->timers.data, tc, timer *, TIMER_LESS, TIMER_SWAP, t->index);
@@ -240,7 +213,7 @@ void
timer_init(void)
{
  timers_init(&main_timeloop, &root_pool);
  timeloop_init_current();
  timeloop_current = &main_timeloop;
}


+8 −25
Original line number Diff line number Diff line
@@ -52,29 +52,16 @@ struct birdloop
 *	Current thread context
 */

static pthread_key_t current_loop_key;
extern pthread_key_t current_time_key;

static inline struct birdloop *
birdloop_current(void)
{
  return pthread_getspecific(current_loop_key);
}
extern _Thread_local struct timeloop *timeloop_current;
_Thread_local struct birdloop *birdloop_current;

static inline void
birdloop_set_current(struct birdloop *loop)
{
  pthread_setspecific(current_loop_key, loop);
  pthread_setspecific(current_time_key, loop ? &loop->time : &main_timeloop);
  birdloop_current = loop;
  timeloop_current = loop ? &loop->time : &main_timeloop;
}

static inline void
birdloop_init_current(void)
{
  pthread_key_create(&current_loop_key, NULL);
}


/*
 *	Wakeup code for birdloop
 */
@@ -162,7 +149,7 @@ wakeup_kick(struct birdloop *loop)
void
wakeup_kick_current(void)
{
  struct birdloop *loop = birdloop_current();
  struct birdloop *loop = birdloop_current;

  if (loop && loop->poll_active)
    wakeup_kick(loop);
@@ -195,7 +182,7 @@ events_fire(struct birdloop *loop)
void
ev2_schedule(event *e)
{
  struct birdloop *loop = birdloop_current();
  struct birdloop *loop = birdloop_current;

  if (loop->poll_active && EMPTY_LIST(loop->event_list))
    wakeup_kick(loop);
@@ -238,7 +225,7 @@ sockets_add(struct birdloop *loop, sock *s)
void
sk_start(sock *s)
{
  struct birdloop *loop = birdloop_current();
  struct birdloop *loop = birdloop_current;

  sockets_add(loop, s);
}
@@ -261,7 +248,7 @@ sockets_remove(struct birdloop *loop, sock *s)
void
sk_stop(sock *s)
{
  struct birdloop *loop = birdloop_current();
  struct birdloop *loop = birdloop_current;

  sockets_remove(loop, s);

@@ -393,10 +380,6 @@ struct birdloop *
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");
  struct birdloop *loop = mb_allocz(p, sizeof(struct birdloop));
  loop->pool = p;