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