673f7952c3f254b79f6f157188b8b812f569fd41
[urcu.git] / urcu.h
1 #ifndef _URCU_H
2 #define _URCU_H
3
4 /*
5 * urcu.h
6 *
7 * Userspace RCU header
8 *
9 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
10 *
11 * Distributed under GPLv2
12 */
13
14 /* The "volatile" is due to gcc bugs */
15 #define barrier() __asm__ __volatile__("": : :"memory")
16
17 /* x86 32/64 specific */
18 #define mb() asm volatile("mfence":::"memory")
19 #define rmb() asm volatile("lfence":::"memory")
20 #define wmb() asm volatile("sfence" ::: "memory")
21
22
23
24 /* x86 32 */
25 static inline void atomic_inc(int *v)
26 {
27 asm volatile("lock; incl %0"
28 : "+m" (*v));
29 }
30
31 /* Nop everywhere except on alpha. */
32 #define smp_read_barrier_depends()
33
34 /*
35 * Prevent the compiler from merging or refetching accesses. The compiler
36 * is also forbidden from reordering successive instances of ACCESS_ONCE(),
37 * but only when the compiler is aware of some particular ordering. One way
38 * to make the compiler aware of ordering is to put the two invocations of
39 * ACCESS_ONCE() in different C statements.
40 *
41 * This macro does absolutely -nothing- to prevent the CPU from reordering,
42 * merging, or refetching absolutely anything at any time. Its main intended
43 * use is to mediate communication between process-level code and irq/NMI
44 * handlers, all running on the same CPU.
45 */
46 #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
47
48 /**
49 * rcu_dereference - fetch an RCU-protected pointer in an
50 * RCU read-side critical section. This pointer may later
51 * be safely dereferenced.
52 *
53 * Inserts memory barriers on architectures that require them
54 * (currently only the Alpha), and, more importantly, documents
55 * exactly which pointers are protected by RCU.
56 */
57
58 #define rcu_dereference(p) ({ \
59 typeof(p) _________p1 = ACCESS_ONCE(p); \
60 smp_read_barrier_depends(); \
61 (_________p1); \
62 })
63
64 #define SIGURCU SIGUSR1
65
66 /* Global quiescent period parity */
67 extern int urcu_qparity;
68
69 extern int __thread urcu_active_readers[2];
70
71 static inline int get_urcu_qparity(void)
72 {
73 return urcu_qparity;
74 }
75
76 /*
77 * returns urcu_parity.
78 */
79 static inline int rcu_read_lock(void)
80 {
81 int urcu_parity = get_urcu_qparity();
82 urcu_active_readers[urcu_parity]++;
83 /*
84 * Increment active readers count before accessing the pointer.
85 * See force_mb_all_threads().
86 */
87 barrier();
88 return urcu_parity;
89 }
90
91 static inline void rcu_read_unlock(int urcu_parity)
92 {
93 barrier();
94 /*
95 * Finish using rcu before decrementing the pointer.
96 * See force_mb_all_threads().
97 */
98 urcu_active_readers[urcu_parity]--;
99 }
100
101 extern void rcu_write_lock(void);
102 extern void rcu_write_unlock(void);
103
104 extern void *urcu_publish_content(void **ptr, void *new);
105
106 /*
107 * Reader thread registration.
108 */
109 extern void urcu_register_thread(void);
110 extern void urcu_register_thread(void);
111
112 #endif /* _URCU_H */
This page took 0.031475 seconds and 4 git commands to generate.