]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Mutex abstraction layer; used in timeloop update calls mq-taskqueue
authorJan Maria Matejka <mq@ucw.cz>
Mon, 24 Sep 2018 13:48:27 +0000 (15:48 +0200)
committerJan Maria Matejka <mq@ucw.cz>
Mon, 24 Sep 2018 13:48:27 +0000 (15:48 +0200)
lib/timer.c
lib/timer.h
sysdep/cf/bsd.h
sysdep/cf/linux.h
sysdep/unix/io.c
sysdep/unix/locking.h [new file with mode: 0644]

index ed731d26e7b88a0b42b5a0f8545027f4ca813d8f..d07bf0f7fc381c5b943578ff83530e24cfe2989f 100644 (file)
@@ -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)
 {
index ed8f0d025035a4b38d2690fbcdd23af23f934a93..fa5f2f9d5d53c597f190263eb718bae7ff9b33e8 100644 (file)
 #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)
index 22c54277283be1fd113d3564622b7703e55a0bbf..2d1354e5c8cb57d3d54860e2debefb58a078227a 100644 (file)
@@ -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
index 047d3764e1ef88df381dbe896195a7776858b630..b0eb2fd4314f9aed7d323a8f9395ba8d0e58c8e0 100644 (file)
@@ -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
index 4455fc1991999dcdc7b387cf98e928ad913a7c1f..8fd471184b92a2940d31c45d0feea9fdd03f8b27 100644 (file)
@@ -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);
 }
 
 
diff --git a/sysdep/unix/locking.h b/sysdep/unix/locking.h
new file mode 100644 (file)
index 0000000..8d3fcd4
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ *     BIRD Locking Subsystem
+ *
+ *     (c) 2018 Maria Matejka <mq@jmq.cz>
+ *
+ *     Can be freely distributed and used under the terms of the GNU GPL.
+ */
+
+#ifndef _BIRD_SYSDEP_MUTEX_H_
+#define _BIRD_SYSDEP_MUTEX_H_
+
+#define MUTEX_DEBUG 1
+
+#if MUTEX_DEBUG
+#define MUTEX_TYPE PTHREAD_MUTEX_ERRORCHECK
+#else
+#define MUTEX_TYPE PTHREAD_MUTEX_NORMAL
+#endif
+
+#include <pthread.h>
+typedef pthread_mutex_t mutex;
+
+static inline void mutex_init(mutex *m)
+{
+  pthread_mutexattr_t mat;
+  if (pthread_mutexattr_init(&mat) < 0)
+    bug("pthread_mutexattr_init() failed: %m");
+  if (pthread_mutexattr_settype(&mat, MUTEX_TYPE) < 0)
+    bug("pthread_mutexattr_settype() failed: %m");
+  if (pthread_mutex_init(m, &mat) < 0)
+    bug("pthread_mutex_init() failed: %m");
+}
+
+#if MUTEX_DEBUG
+#define mutex_lock(m) do { \
+    if (pthread_mutex_lock(m)) \
+      bug("pthread_mutex_lock() failed: %m"); \
+  } while (0)
+
+#define mutex_unlock(m) do { \
+    if (pthread_mutex_unlock(m)) \
+      bug("pthread_mutex_unlock() failed: %m"); \
+  } while (0)
+#else
+#define mutex_lock(m) pthread_mutex_lock(m)
+#define mutex_unlock(m) pthread_mutex_unlock(m)
+#endif
+
+#endif