Refactor liburcu to support many flavors per compile unit
[userspace-rcu.git] / include / urcu / static / urcu-mb.h
CommitLineData
4477a870
MD
1#ifndef _URCU_MB_STATIC_H
2#define _URCU_MB_STATIC_H
3
4/*
5 * urcu-mb-static.h
6 *
7 * Userspace RCU header.
8 *
9 * TO BE INCLUDED ONLY IN CODE THAT IS TO BE RECOMPILED ON EACH LIBURCU
10 * RELEASE. See urcu.h for linking dynamically with the userspace rcu library.
11 *
12 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
13 * Copyright (c) 2009 Paul E. McKenney, IBM Corporation.
14 *
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 *
29 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
30 */
31
32#include <stdlib.h>
33#include <pthread.h>
34#include <unistd.h>
35#include <stdint.h>
36
37#include <urcu/config.h>
38#include <urcu/compiler.h>
39#include <urcu/arch.h>
40#include <urcu/system.h>
41#include <urcu/uatomic.h>
42#include <urcu/list.h>
43#include <urcu/futex.h>
44#include <urcu/tls-compat.h>
45#include <urcu/debug.h>
46#include <urcu/static/urcu-common.h>
47
48#ifdef __cplusplus
49extern "C" {
50#endif
51
52/*
53 * This code section can only be included in LGPL 2.1 compatible source code.
54 * See below for the function call wrappers which can be used in code meant to
55 * be only linked with the Userspace RCU library. This comes with a small
56 * performance degradation on the read-side due to the added function calls.
57 * This is required to permit relinking with newer versions of the library.
58 */
59
60extern struct urcu_gp urcu_mb_gp;
61
62extern DECLARE_URCU_TLS(struct urcu_reader, urcu_mb_reader);
63
64/*
65 * Helper for _urcu_mb_read_lock(). The format of urcu_mb_gp.ctr (as well as
66 * the per-thread rcu_reader.ctr) has the upper bits containing a count of
67 * _urcu_mb_read_lock() nesting, and a lower-order bit that contains either zero
68 * or URCU_GP_CTR_PHASE. The cmm_smp_mb() ensures that the accesses in
69 * _urcu_mb_read_lock() happen before the subsequent read-side critical section.
70 */
71static inline void _urcu_mb_read_lock_update(unsigned long tmp)
72{
73 if (caa_likely(!(tmp & URCU_GP_CTR_NEST_MASK))) {
74 _CMM_STORE_SHARED(URCU_TLS(urcu_mb_reader).ctr, _CMM_LOAD_SHARED(urcu_mb_gp.ctr));
75 cmm_smp_mb();
76 } else
77 _CMM_STORE_SHARED(URCU_TLS(urcu_mb_reader).ctr, tmp + URCU_GP_COUNT);
78}
79
80/*
81 * Enter an RCU read-side critical section.
82 *
83 * The first cmm_barrier() call ensures that the compiler does not reorder
84 * the body of _urcu_mb_read_lock() with a mutex.
85 *
86 * This function and its helper are both less than 10 lines long. The
87 * intent is that this function meets the 10-line criterion in LGPL,
88 * allowing this function to be invoked directly from non-LGPL code.
89 */
90static inline void _urcu_mb_read_lock(void)
91{
92 unsigned long tmp;
93
94 urcu_assert(URCU_TLS(urcu_mb_reader).registered);
95 cmm_barrier();
96 tmp = URCU_TLS(urcu_mb_reader).ctr;
97 urcu_assert((tmp & URCU_GP_CTR_NEST_MASK) != URCU_GP_CTR_NEST_MASK);
98 _urcu_mb_read_lock_update(tmp);
99}
100
101/*
102 * This is a helper function for _urcu_mb_read_unlock().
103 *
104 * The first cmm_smp_mb() call ensures that the critical section is
105 * seen to precede the store to rcu_reader.ctr.
106 * The second cmm_smp_mb() call ensures that we write to rcu_reader.ctr
107 * before reading the update-side futex.
108 */
109static inline void _urcu_mb_read_unlock_update_and_wakeup(unsigned long tmp)
110{
111 if (caa_likely((tmp & URCU_GP_CTR_NEST_MASK) == URCU_GP_COUNT)) {
112 cmm_smp_mb();
113 _CMM_STORE_SHARED(URCU_TLS(urcu_mb_reader).ctr, tmp - URCU_GP_COUNT);
114 cmm_smp_mb();
115 urcu_common_wake_up_gp(&urcu_mb_gp);
116 } else
117 _CMM_STORE_SHARED(URCU_TLS(urcu_mb_reader).ctr, tmp - URCU_GP_COUNT);
118}
119
120/*
121 * Exit an RCU read-side crtical section. Both this function and its
122 * helper are smaller than 10 lines of code, and are intended to be
123 * usable by non-LGPL code, as called out in LGPL.
124 */
125static inline void _urcu_mb_read_unlock(void)
126{
127 unsigned long tmp;
128
129 urcu_assert(URCU_TLS(urcu_mb_reader).registered);
130 tmp = URCU_TLS(urcu_mb_reader).ctr;
131 urcu_assert(tmp & URCU_GP_CTR_NEST_MASK);
132 _urcu_mb_read_unlock_update_and_wakeup(tmp);
133 cmm_barrier(); /* Ensure the compiler does not reorder us with mutex */
134}
135
136/*
137 * Returns whether within a RCU read-side critical section.
138 *
139 * This function is less than 10 lines long. The intent is that this
140 * function meets the 10-line criterion for LGPL, allowing this function
141 * to be invoked directly from non-LGPL code.
142 */
143static inline int _urcu_mb_read_ongoing(void)
144{
145 return URCU_TLS(urcu_mb_reader).ctr & URCU_GP_CTR_NEST_MASK;
146}
147
148#ifdef __cplusplus
149}
150#endif
151
152#endif /* _URCU_MB_STATIC_H */
This page took 0.029838 seconds and 4 git commands to generate.