summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/timer.c29
-rw-r--r--lib/timer.h14
2 files changed, 15 insertions, 28 deletions
diff --git a/lib/timer.c b/lib/timer.c
index 6efcadb4..ff1fb5ef 100644
--- a/lib/timer.c
+++ b/lib/timer.c
@@ -32,6 +32,7 @@
#include "nest/bird.h"
+#include "lib/coro.h"
#include "lib/heap.h"
#include "lib/resource.h"
#include "lib/timer.h"
@@ -45,22 +46,10 @@ struct timeloop main_timeloop;
/* Data accessed and modified from proto/bfd/io.c */
_Thread_local struct timeloop *local_timeloop;
-void wakeup_kick_current(void);
-
-btime
-current_time(void)
-{
- return local_timeloop->last_time;
-}
+_Atomic btime last_time;
+_Atomic btime real_time;
-btime
-current_real_time(void)
-{
- if (!local_timeloop->real_time)
- times_update_real_time(local_timeloop);
-
- return local_timeloop->real_time;
-}
+void wakeup_kick_current(void);
#define TIMER_LESS(a,b) ((a)->expires < (b)->expires)
@@ -164,8 +153,6 @@ tm_stop(timer *t)
void
timers_init(struct timeloop *loop, pool *p)
{
- times_init(loop);
-
BUFFER_INIT(loop->timers, p, 4);
BUFFER_PUSH(loop->timers) = NULL;
}
@@ -178,8 +165,8 @@ timers_fire(struct timeloop *loop)
btime base_time;
timer *t;
- times_update(loop);
- base_time = loop->last_time;
+ times_update();
+ base_time = current_time();
while (t = timers_first(loop))
{
@@ -190,8 +177,8 @@ timers_fire(struct timeloop *loop)
{
btime when = t->expires + t->recurrent;
- if (when <= loop->last_time)
- when = loop->last_time + t->recurrent;
+ if (when <= base_time)
+ when = base_time + t->recurrent;
if (t->randomize)
when += random() % (t->randomize + 1);
diff --git a/lib/timer.h b/lib/timer.h
index bc568ee6..b201b8c8 100644
--- a/lib/timer.h
+++ b/lib/timer.h
@@ -14,6 +14,10 @@
#include "lib/buffer.h"
#include "lib/resource.h"
+#include <stdatomic.h>
+
+extern _Atomic btime last_time;
+extern _Atomic btime real_time;
typedef struct timer
{
@@ -31,8 +35,6 @@ typedef struct timer
struct timeloop
{
BUFFER_(timer *) timers;
- btime last_time;
- btime real_time;
};
static inline uint timers_count(struct timeloop *loop)
@@ -44,8 +46,8 @@ static inline timer *timers_first(struct timeloop *loop)
extern struct timeloop main_timeloop;
extern _Thread_local struct timeloop *local_timeloop;
-btime current_time(void);
-btime current_real_time(void);
+#define current_time() atomic_load_explicit(&last_time, memory_order_acquire)
+#define current_real_time() atomic_load_explicit(&real_time, memory_order_acquire)
//#define now (current_time() TO_S)
//#define now_real (current_real_time() TO_S)
@@ -95,9 +97,7 @@ tm_start_max(timer *t, btime after)
}
/* In sysdep code */
-void times_init(struct timeloop *loop);
-void times_update(struct timeloop *loop);
-void times_update_real_time(struct timeloop *loop);
+void times_update(void);
/* For I/O loop */
void timers_init(struct timeloop *loop, pool *p);