Commit cc5d17ec authored by Jan Maria Matejka's avatar Jan Maria Matejka
Browse files

Mutex abstraction layer; used in timeloop update calls

parent c3d0f919
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -76,6 +76,14 @@ current_time(void)
  return timeloop_current()->last_time;
}

btime
current_fresh_time(void)
{
  struct timeloop *loop = timeloop_current();
  times_update(loop);
  return loop->last_time;
}

btime
current_real_time(void)
{
+5 −5
Original line number Diff line number Diff line
@@ -13,10 +13,9 @@
#include "nest/bird.h"
#include "lib/buffer.h"
#include "lib/resource.h"
#include CONFIG_INCLUDE_LOCKING_H


typedef struct timer
{
typedef struct timer {
  resource r;
  void (*hook)(struct timer *);
  void *data;
@@ -28,11 +27,11 @@ typedef struct timer
  int index;
} timer;

struct timeloop
{
struct timeloop {
  BUFFER_(timer *) timers;
  btime last_time;
  btime real_time;
  mutex update_lock;
};

static inline uint timers_count(struct timeloop *loop)
@@ -44,6 +43,7 @@ static inline timer *timers_first(struct timeloop *loop)
extern struct timeloop main_timeloop;

btime current_time(void);
btime current_fresh_time(void);
btime current_real_time(void);

//#define now (current_time() TO_S)
+2 −0
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@
#define CONFIG_INCLUDE_SYSIO_H "sysdep/bsd/sysio.h"
#define CONFIG_INCLUDE_KRTSYS_H "sysdep/bsd/krt-sys.h"

#define CONFIG_INCLUDE_LOCKING_H "sysdep/unix/locking.h"

/*
Link: sysdep/unix
Link: sysdep/bsd
+1 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#define CONFIG_RESTRICTED_PRIVILEGES
#define CONFIG_INCLUDE_SYSPRIV_H "sysdep/linux/syspriv.h"

#define CONFIG_INCLUDE_LOCKING_H "sysdep/unix/locking.h"

#ifndef AF_MPLS
#define AF_MPLS 28
+10 −0
Original line number Diff line number Diff line
@@ -115,6 +115,8 @@ times_init(struct timeloop *loop)
  struct timespec ts;
  int rv;

  mutex_init(&loop->update_lock);

  rv = clock_gettime(CLOCK_MONOTONIC, &ts);
  if (rv < 0)
    die("Monotonic clock is missing");
@@ -132,6 +134,8 @@ times_update(struct timeloop *loop)
  struct timespec ts;
  int rv;

  mutex_lock(&loop->update_lock);

  rv = clock_gettime(CLOCK_MONOTONIC, &ts);
  if (rv < 0)
    die("clock_gettime: %m");
@@ -143,6 +147,8 @@ times_update(struct timeloop *loop)

  loop->last_time = new_time;
  loop->real_time = 0;

  mutex_unlock(&loop->update_lock);
}

void
@@ -151,11 +157,15 @@ times_update_real_time(struct timeloop *loop)
  struct timespec ts;
  int rv;

  mutex_lock(&loop->update_lock);

  rv = clock_gettime(CLOCK_REALTIME, &ts);
  if (rv < 0)
    die("clock_gettime: %m");

  loop->real_time = ts.tv_sec S + ts.tv_nsec NS;

  mutex_unlock(&loop->update_lock);
}


Loading