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