]>
git.proxmox.com Git - mirror_frr.git/blob - lib/seqlock.h
2 * "Sequence" lock primitive
4 * Copyright (C) 2015 David Lamparter <equinox@diac24.net>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA
28 #include "frratomic.h"
31 * this locking primitive is intended to use in a 1:N setup.
33 * - one "counter" seqlock issuing increasing numbers
34 * - multiple seqlock users hold references on these numbers
36 * this is intended for implementing RCU reference-holding. There is one
37 * global counter, with threads locking a seqlock whenever they take a
38 * reference. A seqlock can also be idle/unlocked.
40 * The "counter" seqlock will always stay locked; the RCU cleanup thread
41 * continuously counts it up, waiting for threads to release or progress to a
42 * sequence number further ahead. If all threads are > N, references dropped
45 * generally, the lock function is:
50 * | running seqlock_wait(b) -- a <= b
51 * seqlock_release() | blocked
52 * OR: seqlock_acquire(a') | -- a' > b
56 /* use sequentially increasing "ticket numbers". lowest bit will always
57 * be 1 to have a 'cleared' indication (i.e., counts 1,5,9,13,etc. )
58 * 2nd lowest bit is used to indicate we have waiters.
60 typedef _Atomic
uint32_t seqlock_ctr_t
;
61 typedef uint32_t seqlock_val_t
;
62 #define seqlock_assert_valid(val) assert((val) & SEQLOCK_HELD)
64 /* NB: SEQLOCK_WAITERS is only allowed if SEQLOCK_HELD is also set; can't
65 * have waiters on an unheld seqlock
67 #define SEQLOCK_HELD (1U << 0)
68 #define SEQLOCK_WAITERS (1U << 1)
69 #define SEQLOCK_VAL(n) ((n) & ~SEQLOCK_WAITERS)
70 #define SEQLOCK_STARTVAL 1U
71 #define SEQLOCK_INCR 4U
73 /* TODO: originally, this was using "atomic_fetch_add", which is the reason
74 * bit 0 is used to indicate held state. With SEQLOCK_WAITERS added, there's
75 * no fetch_add anymore (cmpxchg loop instead), so we don't need to use bit 0
76 * for this anymore & can just special-case the value 0 for it and skip it in
83 /* used when futexes not available: (i.e. non-linux) */
89 /* sqlo = 0 - init state: not held */
90 extern void seqlock_init(struct seqlock
*sqlo
);
93 /* basically: "while (sqlo <= val) wait();"
94 * returns when sqlo > val || !seqlock_held(sqlo)
96 extern void seqlock_wait(struct seqlock
*sqlo
, seqlock_val_t val
);
98 /* same, but time-limited (limit is an absolute CLOCK_MONOTONIC value) */
99 extern bool seqlock_timedwait(struct seqlock
*sqlo
, seqlock_val_t val
,
100 const struct timespec
*abs_monotime_limit
);
102 /* one-shot test, returns true if seqlock_wait would return immediately */
103 extern bool seqlock_check(struct seqlock
*sqlo
, seqlock_val_t val
);
105 static inline bool seqlock_held(struct seqlock
*sqlo
)
107 return !!atomic_load_explicit(&sqlo
->pos
, memory_order_relaxed
);
110 /* sqlo - get seqlock position -- for the "counter" seqlock */
111 extern seqlock_val_t
seqlock_cur(struct seqlock
*sqlo
);
113 /* ++sqlo (but atomic & wakes waiters) - returns value that we bumped to.
116 * - each seqlock_bump call bumps the position by exactly one SEQLOCK_INCR.
117 * There are no skipped/missed or multiple increments.
118 * - each return value is only returned from one seqlock_bump() call
120 extern seqlock_val_t
seqlock_bump(struct seqlock
*sqlo
);
123 /* sqlo = val - can be used on held seqlock. */
124 extern void seqlock_acquire_val(struct seqlock
*sqlo
, seqlock_val_t val
);
126 /* sqlo = ref - standard pattern: acquire relative to other seqlock */
127 static inline void seqlock_acquire(struct seqlock
*sqlo
, struct seqlock
*ref
)
129 seqlock_acquire_val(sqlo
, seqlock_cur(ref
));
132 /* sqlo = 0 - set seqlock position to 0, marking as non-held */
133 extern void seqlock_release(struct seqlock
*sqlo
);
134 /* release should normally be followed by a bump on the "counter", if
135 * anything other than reading RCU items was done
138 #endif /* _SEQLOCK_H */