| 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 | |
| 6 | #ifndef _URCU_SIGNAL_STATIC_H |
| 7 | #define _URCU_SIGNAL_STATIC_H |
| 8 | |
| 9 | /* |
| 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 | * |
| 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 | |
| 23 | #include <urcu/debug.h> |
| 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> |
| 32 | #include <urcu/static/urcu-common.h> |
| 33 | #include <urcu/static/urcu-signal-nr.h> |
| 34 | |
| 35 | #ifdef __cplusplus |
| 36 | extern "C" { |
| 37 | #endif |
| 38 | |
| 39 | /* |
| 40 | * This code section can only be included in LGPL 2.1 compatible source code. |
| 41 | * See below for the function call wrappers which can be used in code meant to |
| 42 | * be only linked with the Userspace RCU library. This comes with a small |
| 43 | * performance degradation on the read-side due to the added function calls. |
| 44 | * This is required to permit relinking with newer versions of the library. |
| 45 | */ |
| 46 | |
| 47 | extern struct urcu_gp urcu_signal_gp; |
| 48 | |
| 49 | extern DECLARE_URCU_TLS(struct urcu_reader, urcu_signal_reader); |
| 50 | |
| 51 | /* |
| 52 | * Helper for _rcu_read_lock(). The format of urcu_signal_gp.ctr (as well as |
| 53 | * the per-thread rcu_reader.ctr) has the lower-order bits containing a count of |
| 54 | * _rcu_read_lock() nesting, and a single high-order URCU_BP_GP_CTR_PHASE bit |
| 55 | * that contains either zero or one. The cmm_barrier() ensures that the accesses in |
| 56 | * _rcu_read_lock() happen before the subsequent read-side critical section. |
| 57 | */ |
| 58 | static inline void _urcu_signal_read_lock_update(unsigned long tmp) |
| 59 | { |
| 60 | if (caa_likely(!(tmp & URCU_GP_CTR_NEST_MASK))) { |
| 61 | _CMM_STORE_SHARED(URCU_TLS(urcu_signal_reader).ctr, _CMM_LOAD_SHARED(urcu_signal_gp.ctr)); |
| 62 | cmm_smp_mb(); |
| 63 | } else |
| 64 | _CMM_STORE_SHARED(URCU_TLS(urcu_signal_reader).ctr, tmp + URCU_GP_COUNT); |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Enter an RCU read-side critical section. |
| 69 | * |
| 70 | * The first cmm_barrier() call ensures that the compiler does not reorder |
| 71 | * the body of _rcu_read_lock() with a mutex. |
| 72 | * |
| 73 | * This function and its helper are both less than 10 lines long. The |
| 74 | * intent is that this function meets the 10-line criterion in LGPL, |
| 75 | * allowing this function to be invoked directly from non-LGPL code. |
| 76 | */ |
| 77 | static inline void _urcu_signal_read_lock(void) |
| 78 | { |
| 79 | unsigned long tmp; |
| 80 | |
| 81 | urcu_assert_debug(URCU_TLS(urcu_signal_reader).registered); |
| 82 | cmm_barrier(); |
| 83 | tmp = URCU_TLS(urcu_signal_reader).ctr; |
| 84 | urcu_assert_debug((tmp & URCU_GP_CTR_NEST_MASK) != URCU_GP_CTR_NEST_MASK); |
| 85 | _urcu_signal_read_lock_update(tmp); |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * This is a helper function for _rcu_read_unlock(). |
| 90 | * |
| 91 | * The first cmm_barrier() call ensures that the critical section is |
| 92 | * seen to precede the store to rcu_reader.ctr. |
| 93 | * The second cmm_barrier() call ensures that we write to rcu_reader.ctr |
| 94 | * before reading the update-side futex. |
| 95 | */ |
| 96 | static inline void _urcu_signal_read_unlock_update_and_wakeup(unsigned long tmp) |
| 97 | { |
| 98 | unsigned long *ctr = &URCU_TLS(urcu_signal_reader).ctr; |
| 99 | |
| 100 | if (caa_likely((tmp & URCU_GP_CTR_NEST_MASK) == URCU_GP_COUNT)) { |
| 101 | uatomic_store(ctr, tmp - URCU_GP_COUNT, CMM_SEQ_CST); |
| 102 | urcu_common_wake_up_gp(&urcu_signal_gp); |
| 103 | } else { |
| 104 | uatomic_store(ctr, tmp - URCU_GP_COUNT, CMM_RELAXED); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Exit an RCU read-side critical section. Both this function and its |
| 110 | * helper are smaller than 10 lines of code, and are intended to be |
| 111 | * usable by non-LGPL code, as called out in LGPL. |
| 112 | */ |
| 113 | static inline void _urcu_signal_read_unlock(void) |
| 114 | { |
| 115 | unsigned long tmp; |
| 116 | |
| 117 | urcu_assert_debug(URCU_TLS(urcu_signal_reader).registered); |
| 118 | tmp = URCU_TLS(urcu_signal_reader).ctr; |
| 119 | urcu_assert_debug(tmp & URCU_GP_CTR_NEST_MASK); |
| 120 | _urcu_signal_read_unlock_update_and_wakeup(tmp); |
| 121 | cmm_barrier(); /* Ensure the compiler does not reorder us with mutex */ |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Returns whether within a RCU read-side critical section. |
| 126 | * |
| 127 | * This function is less than 10 lines long. The intent is that this |
| 128 | * function meets the 10-line criterion for LGPL, allowing this function |
| 129 | * to be invoked directly from non-LGPL code. |
| 130 | */ |
| 131 | static inline int _urcu_signal_read_ongoing(void) |
| 132 | { |
| 133 | return URCU_TLS(urcu_signal_reader).ctr & URCU_GP_CTR_NEST_MASK; |
| 134 | } |
| 135 | |
| 136 | #ifdef __cplusplus |
| 137 | } |
| 138 | #endif |
| 139 | |
| 140 | #endif /* _URCU_SIGNAL_STATIC_H */ |