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