| 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_COMMON_STATIC_H |
| 7 | #define _URCU_COMMON_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/annotate.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 | |
| 33 | #ifdef __cplusplus |
| 34 | extern "C" { |
| 35 | #endif |
| 36 | |
| 37 | enum urcu_state { |
| 38 | URCU_READER_ACTIVE_CURRENT, |
| 39 | URCU_READER_ACTIVE_OLD, |
| 40 | URCU_READER_INACTIVE, |
| 41 | }; |
| 42 | |
| 43 | /* |
| 44 | * The trick here is that URCU_GP_CTR_PHASE must be a multiple of 8 so |
| 45 | * we can use a full 8-bits, 16-bits or 32-bits bitmask for the lower |
| 46 | * order bits. |
| 47 | */ |
| 48 | #define URCU_GP_COUNT (1UL << 0) |
| 49 | /* Use the amount of bits equal to half of the architecture long size */ |
| 50 | #define URCU_GP_CTR_PHASE (1UL << (sizeof(unsigned long) << 2)) |
| 51 | #define URCU_GP_CTR_NEST_MASK (URCU_GP_CTR_PHASE - 1) |
| 52 | |
| 53 | struct urcu_gp { |
| 54 | /* |
| 55 | * Global grace period counter. |
| 56 | * Contains the current URCU_GP_CTR_PHASE. |
| 57 | * Also has a URCU_GP_COUNT of 1, to accelerate the reader fast path. |
| 58 | * Written to only by writer with mutex taken. |
| 59 | * Read by both writer and readers. |
| 60 | */ |
| 61 | unsigned long ctr; |
| 62 | |
| 63 | int32_t futex; |
| 64 | } __attribute__((aligned(CAA_CACHE_LINE_SIZE))); |
| 65 | |
| 66 | struct urcu_reader { |
| 67 | /* Data used by both reader and synchronize_rcu() */ |
| 68 | unsigned long ctr; |
| 69 | char need_mb; |
| 70 | /* Data used for registry */ |
| 71 | struct cds_list_head node __attribute__((aligned(CAA_CACHE_LINE_SIZE))); |
| 72 | pthread_t tid; |
| 73 | /* Reader registered flag, for internal checks. */ |
| 74 | unsigned int registered:1; |
| 75 | }; |
| 76 | |
| 77 | /* |
| 78 | * Wake-up waiting synchronize_rcu(). Called from many concurrent threads. |
| 79 | */ |
| 80 | static inline void urcu_common_wake_up_gp(struct urcu_gp *gp) |
| 81 | { |
| 82 | if (caa_unlikely(uatomic_read(&gp->futex) == -1)) { |
| 83 | uatomic_set(&gp->futex, 0); |
| 84 | /* |
| 85 | * Ignoring return value until we can make this function |
| 86 | * return something (because urcu_die() is not publicly |
| 87 | * exposed). |
| 88 | */ |
| 89 | (void) futex_async(&gp->futex, FUTEX_WAKE, 1, |
| 90 | NULL, NULL, 0); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static inline enum urcu_state urcu_common_reader_state(struct urcu_gp *gp, |
| 95 | unsigned long *ctr, |
| 96 | cmm_annotate_t *group) |
| 97 | { |
| 98 | unsigned long v; |
| 99 | |
| 100 | /* |
| 101 | * Make sure both tests below are done on the same version of *value |
| 102 | * to insure consistency. |
| 103 | */ |
| 104 | v = uatomic_load(ctr, CMM_RELAXED); |
| 105 | cmm_annotate_group_mem_acquire(group, ctr); |
| 106 | |
| 107 | if (!(v & URCU_GP_CTR_NEST_MASK)) |
| 108 | return URCU_READER_INACTIVE; |
| 109 | if (!((v ^ gp->ctr) & URCU_GP_CTR_PHASE)) |
| 110 | return URCU_READER_ACTIVE_CURRENT; |
| 111 | return URCU_READER_ACTIVE_OLD; |
| 112 | } |
| 113 | |
| 114 | #ifdef __cplusplus |
| 115 | } |
| 116 | #endif |
| 117 | |
| 118 | #endif /* _URCU_COMMON_STATIC_H */ |