]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib/seqlock: add timed-wait operation
authorDavid Lamparter <equinox@diac24.net>
Wed, 19 Jun 2019 10:52:38 +0000 (12:52 +0200)
committerDavid Lamparter <equinox@diac24.net>
Wed, 31 Jul 2019 01:33:41 +0000 (03:33 +0200)
seqlock_timedwait() puts an (absolute, CLOCK_MONOTONIC) deadline on how
long we wait.  The RCU code uses this for its watchdog implementation.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
lib/seqlock.c
lib/seqlock.h

index 32dff5173d8d755c2d23aab69a1497f63e44b7e6..6ec9bbcf5f019d735f23e50e761ba9dec3ed2c88 100644 (file)
@@ -25,6 +25,7 @@
 #include "config.h"
 #endif
 
+#include <string.h>
 #include <unistd.h>
 #include <limits.h>
 #include <errno.h>
 #include <sys/syscall.h>
 #include <linux/futex.h>
 
-static long sys_futex(void *addr1, int op, int val1, struct timespec *timeout,
-               void *addr2, int val3)
+static long sys_futex(void *addr1, int op, int val1,
+                     const struct timespec *timeout, void *addr2, int val3)
 {
        return syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3);
 }
 
 #define wait_once(sqlo, val)   \
        sys_futex((int *)&sqlo->pos, FUTEX_WAIT, (int)val, NULL, NULL, 0)
+#define wait_time(sqlo, val, time, reltime)    \
+       sys_futex((int *)&sqlo->pos, FUTEX_WAIT_BITSET, (int)val, time, \
+                 NULL, ~0U)
 #define wait_poke(sqlo)                \
        sys_futex((int *)&sqlo->pos, FUTEX_WAKE, INT_MAX, NULL, NULL, 0)
 
 #elif defined(HAVE_SYNC_OPENBSD_FUTEX)
-/* OpenBSD variant of the above.  untested, not upstream in OpenBSD. */
+/* OpenBSD variant of the above. */
 #include <sys/syscall.h>
 #include <sys/futex.h>
 
+#define TIME_RELATIVE          1
+
 #define wait_once(sqlo, val)   \
        futex((int *)&sqlo->pos, FUTEX_WAIT, (int)val, NULL, NULL, 0)
+#define wait_time(sqlo, val, time, reltime)    \
+       futex((int *)&sqlo->pos, FUTEX_WAIT, (int)val, reltime, NULL, 0)
 #define wait_poke(sqlo)                \
        futex((int *)&sqlo->pos, FUTEX_WAKE, INT_MAX, NULL, NULL, 0)
 
@@ -67,6 +75,17 @@ static long sys_futex(void *addr1, int op, int val1, struct timespec *timeout,
 
 #define wait_once(sqlo, val)   \
        _umtx_op((void *)&sqlo->pos, UMTX_OP_WAIT_UINT, val, NULL, NULL)
+static int wait_time(struct seqlock *sqlo, uint32_t val,
+                     const struct timespec *abstime,
+                     const struct timespec *reltime)
+{
+       struct _umtx_time t;
+       t._flags = UMTX_ABSTIME;
+       t._clockid = CLOCK_MONOTONIC;
+       memcpy(&t._timeout, abstime, sizeof(t._timeout));
+       return _umtx_op((void *)&sqlo->pos, UMTX_OP_WAIT_UINT, val,
+                (void *)(uintptr_t) sizeof(t), &t);
+}
 #define wait_poke(sqlo)                \
        _umtx_op((void *)&sqlo->pos, UMTX_OP_WAKE, INT_MAX, NULL, NULL)
 
@@ -74,12 +93,17 @@ static long sys_futex(void *addr1, int op, int val1, struct timespec *timeout,
 /* generic version.  used on *BSD, Solaris and OSX.
  */
 
+#define TIME_ABS_REALTIME      1
+
 #define wait_init(sqlo)                do { \
                pthread_mutex_init(&sqlo->lock, NULL); \
                pthread_cond_init(&sqlo->wake, NULL); \
        } while (0)
 #define wait_prep(sqlo)                pthread_mutex_lock(&sqlo->lock)
 #define wait_once(sqlo, val)   pthread_cond_wait(&sqlo->wake, &sqlo->lock)
+#define wait_time(sqlo, val, time, reltime) \
+                               pthread_cond_timedwait(&sqlo->wake, \
+                                                      &sqlo->lock, time);
 #define wait_done(sqlo)                pthread_mutex_unlock(&sqlo->lock)
 #define wait_poke(sqlo)                do { \
                pthread_mutex_lock(&sqlo->lock); \
@@ -124,6 +148,84 @@ void seqlock_wait(struct seqlock *sqlo, seqlock_val_t val)
        wait_done(sqlo);
 }
 
+bool seqlock_timedwait(struct seqlock *sqlo, seqlock_val_t val,
+                      const struct timespec *abs_monotime_limit)
+{
+#if TIME_ABS_REALTIME
+#define time_arg1 &abs_rt
+#define time_arg2 NULL
+#define time_prep
+       struct timespec curmono, abs_rt;
+
+       clock_gettime(CLOCK_MONOTONIC, &curmono);
+       clock_gettime(CLOCK_REALTIME, &abs_rt);
+
+       abs_rt.tv_nsec += abs_monotime_limit->tv_nsec - curmono.tv_nsec;
+       if (abs_rt.tv_nsec < 0) {
+               abs_rt.tv_sec--;
+               abs_rt.tv_nsec += 1000000000;
+       } else if (abs_rt.tv_nsec >= 1000000000) {
+               abs_rt.tv_sec++;
+               abs_rt.tv_nsec -= 1000000000;
+       }
+       abs_rt.tv_sec += abs_monotime_limit->tv_sec - curmono.tv_sec;
+
+#elif TIME_RELATIVE
+       struct timespec reltime;
+
+#define time_arg1 abs_monotime_limit
+#define time_arg2 &reltime
+#define time_prep \
+       clock_gettime(CLOCK_MONOTONIC, &reltime);                              \
+       reltime.tv_sec = abs_monotime_limit.tv_sec - reltime.tv_sec;           \
+       reltime.tv_nsec = abs_monotime_limit.tv_nsec - reltime.tv_nsec;        \
+       if (reltime.tv_nsec < 0) {                                             \
+               reltime.tv_sec--;                                              \
+               reltime.tv_nsec += 1000000000;                                 \
+       }
+#else
+#define time_arg1 abs_monotime_limit
+#define time_arg2 NULL
+#define time_prep
+#endif
+
+       bool ret = true;
+       seqlock_val_t cur, cal;
+
+       seqlock_assert_valid(val);
+
+       wait_prep(sqlo);
+       cur = atomic_load_explicit(&sqlo->pos, memory_order_relaxed);
+
+       while (cur & SEQLOCK_HELD) {
+               cal = SEQLOCK_VAL(cur) - val - 1;
+               assert(cal < 0x40000000 || cal > 0xc0000000);
+               if (cal < 0x80000000)
+                       break;
+
+               if ((cur & SEQLOCK_WAITERS)
+                   || atomic_compare_exchange_weak_explicit(
+                               &sqlo->pos, &cur, cur | SEQLOCK_WAITERS,
+                               memory_order_relaxed, memory_order_relaxed)) {
+                       int rv;
+
+                       time_prep
+
+                       rv = wait_time(sqlo, cur | SEQLOCK_WAITERS, time_arg1,
+                                      time_arg2);
+                       if (rv) {
+                               ret = false;
+                               break;
+                       }
+                       cur = atomic_load_explicit(&sqlo->pos,
+                               memory_order_relaxed);
+               }
+       }
+       wait_done(sqlo);
+
+       return ret;
+}
+
 bool seqlock_check(struct seqlock *sqlo, seqlock_val_t val)
 {
        seqlock_val_t cur;
index 0f5dc47cd70fdc2c503978a1d329ef1f76582e62..3cce9ccf4987a96cd9972f9a90290022dc77fed4 100644 (file)
@@ -82,6 +82,8 @@ extern void seqlock_init(struct seqlock *sqlo);
 
 /* while (sqlo <= val) - wait until seqlock->pos > val, or seqlock unheld */
 extern void seqlock_wait(struct seqlock *sqlo, seqlock_val_t val);
+extern bool seqlock_timedwait(struct seqlock *sqlo, seqlock_val_t val,
+                             const struct timespec *abs_monotime_limit);
 extern bool seqlock_check(struct seqlock *sqlo, seqlock_val_t val);
 
 static inline bool seqlock_held(struct seqlock *sqlo)