seqlock.h revision 1.2 1 /* $NetBSD: seqlock.h,v 1.2 2021/12/19 00:47:40 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef _LINUX_SEQLOCK_H_
33 #define _LINUX_SEQLOCK_H_
34
35 #include <sys/types.h>
36 #include <sys/atomic.h>
37 #include <sys/lock.h>
38
39 #include <lib/libkern/libkern.h>
40
41 struct seqlock {
42 uint64_t sql_gen;
43 };
44
45 typedef struct seqlock seqlock_t;
46
47 static inline void
48 seqlock_init(struct seqlock *seqlock)
49 {
50
51 seqlock->sql_gen = 0;
52 }
53
54 static inline void
55 write_seqlock(struct seqlock *seqlock)
56 {
57
58 KASSERT((seqlock->sql_gen & 1) == 0);
59 seqlock->sql_gen |= 1;
60 membar_producer();
61 }
62
63 static inline void
64 write_sequnlock(struct seqlock *seqlock)
65 {
66
67 KASSERT((seqlock->sql_gen & 1) == 1);
68 membar_producer();
69 seqlock->sql_gen |= 1; /* paraonia */
70 seqlock->sql_gen++;
71 }
72
73 #define write_seqlock_irqsave(SEQLOCK, FLAGS) do { \
74 (FLAGS) = (unsigned long)splvm(); \
75 write_seqlock(SEQLOCK); \
76 } while (0)
77
78 #define write_sequnlock_irqrestore(SEQLOCK, FLAGS) do { \
79 write_seqlock(SEQLOCK); \
80 splx((int)(FLAGS)); \
81 } while (0)
82
83 static inline uint64_t
84 read_seqbegin(struct seqlock *seqlock)
85 {
86 uint64_t gen;
87
88 while ((gen = seqlock->sql_gen) & 1)
89 SPINLOCK_BACKOFF_HOOK;
90 membar_consumer();
91
92 return gen;
93 }
94
95 static inline bool
96 read_seqretry(struct seqlock *seqlock, uint64_t gen)
97 {
98
99 return gen != seqlock->sql_gen;
100 }
101
102 #endif /* _LINUX_SEQLOCK_H_ */
103