Commit | Line | Data |
---|---|---|
786ee85b MD |
1 | #ifndef _URCU_DEFER_STATIC_H |
2 | #define _URCU_DEFER_STATIC_H | |
90075a50 MD |
3 | |
4 | /* | |
786ee85b | 5 | * urcu-defer-static.h |
90075a50 | 6 | * |
658225a7 | 7 | * Userspace RCU header - memory reclamation. |
90075a50 | 8 | * |
786ee85b | 9 | * TO BE INCLUDED ONLY IN LGPL-COMPATIBLE CODE. See urcu-defer.h for linking |
658225a7 | 10 | * dynamically with the userspace rcu reclamation library. |
90075a50 MD |
11 | * |
12 | * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> | |
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 | ||
ec4e58a3 MD |
35 | #include <urcu/compiler.h> |
36 | #include <urcu/arch.h> | |
48d848c7 | 37 | #include <urcu/uatomic_arch.h> |
dbc6128f | 38 | #include <urcu/list.h> |
90075a50 | 39 | |
36bc70a8 MD |
40 | #ifdef __cplusplus |
41 | extern "C" { | |
42 | #endif | |
90075a50 MD |
43 | |
44 | /* | |
786ee85b | 45 | * Number of entries in the per-thread defer queue. Must be power of 2. |
90075a50 | 46 | */ |
786ee85b MD |
47 | #define DEFER_QUEUE_SIZE (1 << 12) |
48 | #define DEFER_QUEUE_MASK (DEFER_QUEUE_SIZE - 1) | |
90075a50 | 49 | |
804b4375 MD |
50 | /* |
51 | * Typically, data is aligned at least on the architecture size. | |
52 | * Use lowest bit to indicate that the current callback is changing. | |
53 | * Assumes that (void *)-2L is not used often. Used to encode non-aligned | |
54 | * functions and non-aligned data using extra space. | |
55 | * We encode the (void *)-2L fct as: -2L, fct, data. | |
56 | * We encode the (void *)-2L data as: -2L, fct, data. | |
57 | * Here, DQ_FCT_MARK == ~DQ_FCT_BIT. Required for the test order. | |
58 | */ | |
59 | #define DQ_FCT_BIT (1 << 0) | |
60 | #define DQ_IS_FCT_BIT(x) ((unsigned long)(x) & DQ_FCT_BIT) | |
61 | #define DQ_SET_FCT_BIT(x) \ | |
62 | (x = (void *)((unsigned long)(x) | DQ_FCT_BIT)) | |
63 | #define DQ_CLEAR_FCT_BIT(x) \ | |
64 | (x = (void *)((unsigned long)(x) & ~DQ_FCT_BIT)) | |
65 | #define DQ_FCT_MARK ((void *)(~DQ_FCT_BIT)) | |
66 | ||
90075a50 MD |
67 | /* |
68 | * Identify a shared load. A smp_rmc() or smp_mc() should come before the load. | |
69 | */ | |
70 | #define _LOAD_SHARED(p) ACCESS_ONCE(p) | |
71 | ||
72 | /* | |
73 | * Load a data from shared memory, doing a cache flush if required. | |
74 | */ | |
75 | #define LOAD_SHARED(p) \ | |
76 | ({ \ | |
77 | smp_rmc(); \ | |
78 | _LOAD_SHARED(p); \ | |
79 | }) | |
80 | ||
81 | /* | |
82 | * Identify a shared store. A smp_wmc() or smp_mc() should follow the store. | |
83 | */ | |
84 | #define _STORE_SHARED(x, v) ({ ACCESS_ONCE(x) = (v); }) | |
85 | ||
86 | /* | |
87 | * Store v into x, where x is located in shared memory. Performs the required | |
88 | * cache flush after writing. Returns v. | |
89 | */ | |
90 | #define STORE_SHARED(x, v) \ | |
91 | ({ \ | |
92 | _STORE_SHARED(x, v); \ | |
93 | smp_wmc(); \ | |
94 | (v); \ | |
95 | }) | |
96 | ||
97 | /* | |
98 | * This code section can only be included in LGPL 2.1 compatible source code. | |
99 | * See below for the function call wrappers which can be used in code meant to | |
100 | * be only linked with the Userspace RCU library. This comes with a small | |
101 | * performance degradation on the read-side due to the added function calls. | |
102 | * This is required to permit relinking with newer versions of the library. | |
103 | */ | |
104 | ||
105 | #ifdef DEBUG_RCU | |
106 | #define rcu_assert(args...) assert(args) | |
107 | #else | |
108 | #define rcu_assert(args...) | |
109 | #endif | |
110 | ||
804b4375 MD |
111 | /* |
112 | * defer queue. | |
113 | * Contains pointers. Encoded to save space when same callback is often used. | |
114 | * When looking up the next item: | |
115 | * - if DQ_FCT_BIT is set, set the current callback to DQ_CLEAR_FCT_BIT(ptr) | |
116 | * - next element contains pointer to data. | |
117 | * - else if item == DQ_FCT_MARK | |
118 | * - set the current callback to next element ptr | |
119 | * - following next element contains pointer to data. | |
120 | * - else current element contains data | |
121 | */ | |
786ee85b | 122 | struct defer_queue { |
90075a50 | 123 | unsigned long head; /* add element at head */ |
804b4375 | 124 | void *last_fct_in; /* last fct pointer encoded */ |
90075a50 | 125 | unsigned long tail; /* next element to remove at tail */ |
804b4375 | 126 | void *last_fct_out; /* last fct pointer encoded */ |
90075a50 | 127 | void **q; |
dbc6128f MD |
128 | /* registry information */ |
129 | unsigned long last_head; | |
130 | struct list_head list; /* list of thread queues */ | |
90075a50 MD |
131 | }; |
132 | ||
36bc70a8 MD |
133 | #ifdef __cplusplus |
134 | } | |
135 | #endif | |
136 | ||
786ee85b | 137 | #endif /* _URCU_DEFER_STATIC_H */ |