urcu/annotate: Add CMM annotation
[urcu.git] / include / urcu / static / urcu-mb.h
CommitLineData
d3d3857f
MJ
1// SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2// SPDX-FileCopyrightText: 2009 Paul E. McKenney, IBM Corporation.
3//
4// SPDX-License-Identifier: LGPL-2.1-or-later
5
4477a870
MD
6#ifndef _URCU_MB_STATIC_H
7#define _URCU_MB_STATIC_H
8
9/*
4477a870
MD
10 * Userspace RCU header.
11 *
12 * TO BE INCLUDED ONLY IN CODE THAT IS TO BE RECOMPILED ON EACH LIBURCU
13 * RELEASE. See urcu.h for linking dynamically with the userspace rcu library.
14 *
4477a870
MD
15 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
16 */
17
18#include <stdlib.h>
19#include <pthread.h>
20#include <unistd.h>
21#include <stdint.h>
22
01477510 23#include <urcu/debug.h>
4477a870
MD
24#include <urcu/config.h>
25#include <urcu/compiler.h>
26#include <urcu/arch.h>
27#include <urcu/system.h>
28#include <urcu/uatomic.h>
29#include <urcu/list.h>
30#include <urcu/futex.h>
31#include <urcu/tls-compat.h>
4477a870
MD
32#include <urcu/static/urcu-common.h>
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38/*
39 * This code section can only be included in LGPL 2.1 compatible source code.
40 * See below for the function call wrappers which can be used in code meant to
41 * be only linked with the Userspace RCU library. This comes with a small
42 * performance degradation on the read-side due to the added function calls.
43 * This is required to permit relinking with newer versions of the library.
44 */
45
46extern struct urcu_gp urcu_mb_gp;
47
48extern DECLARE_URCU_TLS(struct urcu_reader, urcu_mb_reader);
49
50/*
51 * Helper for _urcu_mb_read_lock(). The format of urcu_mb_gp.ctr (as well as
be152bdb
LKO
52 * the per-thread rcu_reader.ctr) has the lower-order bits containing a count of
53 * _urcu_mb_read_lock() nesting, and a single high-order URCU_BP_GP_CTR_PHASE bit
54 * that contains either zero or one. The cmm_smp_mb() ensures that the accesses in
4477a870
MD
55 * _urcu_mb_read_lock() happen before the subsequent read-side critical section.
56 */
57static inline void _urcu_mb_read_lock_update(unsigned long tmp)
58{
59 if (caa_likely(!(tmp & URCU_GP_CTR_NEST_MASK))) {
60 _CMM_STORE_SHARED(URCU_TLS(urcu_mb_reader).ctr, _CMM_LOAD_SHARED(urcu_mb_gp.ctr));
61 cmm_smp_mb();
62 } else
63 _CMM_STORE_SHARED(URCU_TLS(urcu_mb_reader).ctr, tmp + URCU_GP_COUNT);
64}
65
66/*
67 * Enter an RCU read-side critical section.
68 *
69 * The first cmm_barrier() call ensures that the compiler does not reorder
70 * the body of _urcu_mb_read_lock() with a mutex.
71 *
72 * This function and its helper are both less than 10 lines long. The
73 * intent is that this function meets the 10-line criterion in LGPL,
74 * allowing this function to be invoked directly from non-LGPL code.
75 */
76static inline void _urcu_mb_read_lock(void)
77{
78 unsigned long tmp;
79
2a27e931 80 urcu_assert_debug(URCU_TLS(urcu_mb_reader).registered);
4477a870
MD
81 cmm_barrier();
82 tmp = URCU_TLS(urcu_mb_reader).ctr;
2a27e931 83 urcu_assert_debug((tmp & URCU_GP_CTR_NEST_MASK) != URCU_GP_CTR_NEST_MASK);
4477a870
MD
84 _urcu_mb_read_lock_update(tmp);
85}
86
87/*
88 * This is a helper function for _urcu_mb_read_unlock().
89 *
90 * The first cmm_smp_mb() call ensures that the critical section is
91 * seen to precede the store to rcu_reader.ctr.
92 * The second cmm_smp_mb() call ensures that we write to rcu_reader.ctr
93 * before reading the update-side futex.
94 */
95static inline void _urcu_mb_read_unlock_update_and_wakeup(unsigned long tmp)
96{
601922a8
OD
97 unsigned long *ctr = &URCU_TLS(urcu_mb_reader).ctr;
98
4477a870 99 if (caa_likely((tmp & URCU_GP_CTR_NEST_MASK) == URCU_GP_COUNT)) {
601922a8 100 uatomic_store(ctr, tmp - URCU_GP_COUNT, CMM_SEQ_CST);
4477a870 101 urcu_common_wake_up_gp(&urcu_mb_gp);
601922a8
OD
102 } else {
103 uatomic_store(ctr, tmp - URCU_GP_COUNT, CMM_RELAXED);
104 }
4477a870
MD
105}
106
107/*
f99c6e92 108 * Exit an RCU read-side critical section. Both this function and its
4477a870
MD
109 * helper are smaller than 10 lines of code, and are intended to be
110 * usable by non-LGPL code, as called out in LGPL.
111 */
112static inline void _urcu_mb_read_unlock(void)
113{
114 unsigned long tmp;
115
2a27e931 116 urcu_assert_debug(URCU_TLS(urcu_mb_reader).registered);
4477a870 117 tmp = URCU_TLS(urcu_mb_reader).ctr;
2a27e931 118 urcu_assert_debug(tmp & URCU_GP_CTR_NEST_MASK);
4477a870
MD
119 _urcu_mb_read_unlock_update_and_wakeup(tmp);
120 cmm_barrier(); /* Ensure the compiler does not reorder us with mutex */
121}
122
123/*
124 * Returns whether within a RCU read-side critical section.
125 *
126 * This function is less than 10 lines long. The intent is that this
127 * function meets the 10-line criterion for LGPL, allowing this function
128 * to be invoked directly from non-LGPL code.
129 */
130static inline int _urcu_mb_read_ongoing(void)
131{
132 return URCU_TLS(urcu_mb_reader).ctr & URCU_GP_CTR_NEST_MASK;
133}
134
135#ifdef __cplusplus
136}
137#endif
138
139#endif /* _URCU_MB_STATIC_H */
This page took 0.035749 seconds and 4 git commands to generate.