1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use std::sync::RwLock;
use many_cpus::MemoryRegionId;
use crate::{
hw_info_client::{HardwareInfoClient, HardwareInfoClientFacade},
hw_tracker_client::{HardwareTrackerClient, HardwareTrackerClientFacade},
};
/// The backing type behind static variables in a `region_cached!` block.
///
/// Refer to [crate-level documentation][crate] for more information.
#[derive(Debug)]
pub struct RegionCachedKey<T>
where
T: Clone + 'static,
{
state: RwLock<State<T>>,
hardware_info: HardwareInfoClientFacade,
hardware_tracker: HardwareTrackerClientFacade,
}
#[derive(Debug)]
enum State<T> {
// The instance has been created but not yet initialized, all we have is the initial value.
Created {
initializer: fn() -> T,
},
// The instance has been initialized, we have one slot per memory region.
Initialized {
// We cannot avoid region_values being across-region accessed but the Box at least ensures
// that the T inside lives in the correct memory region (assuming the allocator cooperates).
region_values: Box<[Option<Box<T>>]>,
},
}
impl<T> State<T> {
const fn new(initializer: fn() -> T) -> Self {
Self::Created { initializer }
}
}
impl<T> RegionCachedKey<T>
where
T: Clone + 'static,
{
/// Note: this function exists to serve the inner workings of the `region_cached!` macro and
/// should not be used directly. It is not part of the public API and may be removed or changed
/// at any time.
#[doc(hidden)]
pub const fn new(initializer: fn() -> T) -> Self {
Self::with_clients(
initializer,
HardwareInfoClientFacade::real(),
HardwareTrackerClientFacade::real(),
)
}
pub(crate) const fn with_clients(
initializer: fn() -> T,
hardware_info: HardwareInfoClientFacade,
hardware_tracker: HardwareTrackerClientFacade,
) -> Self {
Self {
state: RwLock::new(State::new(initializer)),
hardware_info,
hardware_tracker,
}
}
/// Executes the provided closure with a reference to the stored value.
pub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R {
// Horrible inefficient implementation just to get tests to pass.
//
// Optimize: avoid the various checks and such.
// Optimize: can we do better than RwLock?
// Optimize: region_values itself may be stored outside the current memory region...
// We fix the memory region ID at this point. It may be that the thread migrates to a
// different memory region during the rest of this function - we do not care about that.
let memory_region_id = self.hardware_tracker.current_memory_region_id();
self.with_in_region(memory_region_id, f)
}
fn with_in_region<R>(&self, memory_region_id: MemoryRegionId, f: impl FnOnce(&T) -> R) -> R {
{
// Optimistic case - a value already exists for this memory region.
let state = self.state.read().expect(ERR_POISONED_LOCK);
if let Some(value) = Self::try_read_core(memory_region_id, &*state) {
return f(value);
}
}
{
// Fallback case - we need to obtain a value for this memory region.
let mut state = self.state.write().expect(ERR_POISONED_LOCK);
// It could be that something already assigned the value, so check again first.
if let Some(value) = Self::try_read_core(memory_region_id, &*state) {
return f(value);
}
self.initialize_slot(memory_region_id, &mut *state);
// We release the write lock here to avoid holding it while calling the closure.
}
// If we got here, we did a write, so recurse back to go back to the optimistic case
// and read the value that we just wrote, because optimism is now guaranteed to win
// unless a concurrent write has reset the state again (in which case we keep trying).
// TODO: Unbounded recursion is not desirable, refactor this to not use recursion.
self.with_in_region(memory_region_id, f)
}
fn try_read_core(memory_region_id: MemoryRegionId, state: &State<T>) -> Option<&T> {
match state {
State::Created { .. } => {}
State::Initialized { region_values } => {
// This bounds check could only fail if the platform
// lied about the maximum memory region ID.
let slot = region_values.get(memory_region_id as usize);
if let Some(Some(value)) = slot {
return Some(value);
}
}
}
None
}
fn initialize_slot(&self, memory_region_id: MemoryRegionId, state: &mut State<T>) {
// If the state is not yet initialized, we initialize it now, consuming the initial
// value. Otherwise, we simply clone the initial value from the first filled slot.
//
// The values in the slots are all the same, so it does not strictly matter which we pick.
// There is some theoretical optimization opportunity here by picking the slot that is
// closest to the current memory region - this may decrease the cost of the clone.
match state {
State::Created { initializer } => {
let mut region_values =
vec![None; self.hardware_info.max_memory_region_id() as usize + 1];
region_values[memory_region_id as usize] = Some(Box::new(initializer()));
*state = State::Initialized {
region_values: region_values.into_boxed_slice(),
}
}
State::Initialized { region_values } => {
let boxed_value = region_values.iter().find_map(|v| v.as_ref()).expect(
"if we are initialized, we must have at least one value in region_values",
);
region_values[memory_region_id as usize] = Some(boxed_value.clone());
}
}
}
/// Updates the stored value.
///
/// The update will be applied to all memory regions in an eventually consistent manner.
/// It will be immediately visible in the current memory region.
pub fn set(&self, value: T) {
let memory_region_id = self.hardware_tracker.current_memory_region_id();
// In the current implementation, we just take a write lock and update the value.
// This is suboptimal - ideally, we would allow other threads to read the old value
// while we are doing our write. TODO: Avoid taking an exclusive lock for the write.
// Potentially we can (or may have to) condition this optimization to be lock-free if there
// is a single thread doing writes (conflicting writes may still require locks).
let mut state = self.state.write().expect(ERR_POISONED_LOCK);
// TODO: This can perform an unnecessary clone of the initial value
// that we just throw away immediately - optimize it away.
self.initialize_slot(memory_region_id, &mut state);
match &mut *state {
State::Created { .. } => {
unreachable!("initialize_slot should have transitioned the state away from this");
}
State::Initialized { region_values } => {
for (index, slot) in region_values.iter_mut().enumerate() {
if index == memory_region_id as usize {
// The current memory region can get a clone immediately.
//
// NB! We always do a clone because we have no idea what the contents
// of the value are (potentially the contents are stored in the wrong
// memory region, e.g. a `Vec` is a pointer to a buffer and we want to
// force a clone of the buffer here, not just the pointer).
// NB! The compiler is, of course, allowed to optimize away spurious clones.
// TODO: Is that a real thing it actually does? If so, can we stop it?
*slot = Some(Box::new(value.clone()));
} else {
// Other memory regions will copy on read.
*slot = None;
}
}
}
}
}
}
impl<T> RegionCachedKey<T>
where
T: Clone + Copy + 'static,
{
/// Gets a copy of the stored value.
pub fn get(&self) -> T {
self.with(|v| *v)
}
}
const ERR_POISONED_LOCK: &str = "poisoned lock - safe execution no longer possible";
/// Refer to [crate-level documentation][crate] for more information.
#[macro_export]
macro_rules! region_cached {
() => {};
($(#[$attr:meta])* $vis:vis static $NAME:ident: $t:ty = $e:expr; $($rest:tt)*) => (
$crate::region_cached!($(#[$attr])* $vis static $NAME: $t = $e);
$crate::region_cached!($($rest)*);
);
($(#[$attr:meta])* $vis:vis static $NAME:ident: $t:ty = $e:expr) => {
$(#[$attr])* $vis static $NAME: $crate::RegionCachedKey<$t> =
$crate::RegionCachedKey::new(move || $e);
};
}
#[cfg(test)]
mod tests {
use std::ptr;
use std::sync::Arc;
use crate::region_cached;
use crate::{
hw_info_client::MockHardwareInfoClient, hw_tracker_client::MockHardwareTrackerClient,
};
use super::*;
#[cfg(not(miri))] // Miri does not support talking to the real platform.
#[test]
fn real_smoke_test() {
region_cached! {
static FAVORITE_COLOR: &str = "blue";
}
FAVORITE_COLOR.with(|color| {
assert_eq!(*color, "blue");
});
FAVORITE_COLOR.set("red");
FAVORITE_COLOR.with(|color| {
assert_eq!(*color, "red");
});
}
#[cfg(not(miri))] // Miri does not support talking to the real platform.
#[test]
fn with_non_const_initial_value() {
region_cached!(static FAVORITE_COLOR: Arc<String> = Arc::new("blue".to_string()));
FAVORITE_COLOR.with(|color| {
assert_eq!(**color, "blue");
});
}
#[test]
fn different_regions_have_different_clones() {
let mut hardware_tracker = MockHardwareTrackerClient::new();
hardware_tracker
.expect_current_memory_region_id()
.times(1)
.return_const(0 as MemoryRegionId);
hardware_tracker
.expect_current_memory_region_id()
.times(1)
.return_const(1 as MemoryRegionId);
hardware_tracker
.expect_current_memory_region_id()
.times(1)
.return_const(9 as MemoryRegionId);
let hardware_tracker = HardwareTrackerClientFacade::from_mock(hardware_tracker);
let mut hardware_info = MockHardwareInfoClient::new();
hardware_info
.expect_max_memory_region_id()
.return_const(9 as MemoryRegionId);
let hardware_info = HardwareInfoClientFacade::from_mock(hardware_info);
let local =
RegionCachedKey::with_clients(|| "foo".to_string(), hardware_info, hardware_tracker);
let value1 = local.with(ptr::from_ref);
let value2 = local.with(ptr::from_ref);
let value3 = local.with(ptr::from_ref);
assert_ne!(value1, value2);
assert_ne!(value1, value3);
}
#[test]
fn initial_value_propagates_to_all_regions() {
let mut hardware_tracker = MockHardwareTrackerClient::new();
hardware_tracker
.expect_current_memory_region_id()
.times(1)
.return_const(0 as MemoryRegionId);
hardware_tracker
.expect_current_memory_region_id()
.times(1)
.return_const(1 as MemoryRegionId);
hardware_tracker
.expect_current_memory_region_id()
.times(1)
.return_const(9 as MemoryRegionId);
let hardware_tracker = HardwareTrackerClientFacade::from_mock(hardware_tracker);
let mut hardware_info = MockHardwareInfoClient::new();
hardware_info
.expect_max_memory_region_id()
.return_const(9 as MemoryRegionId);
let hardware_info = HardwareInfoClientFacade::from_mock(hardware_info);
let local = RegionCachedKey::with_clients(|| 42, hardware_info, hardware_tracker);
assert_eq!(local.get(), 42);
assert_eq!(local.get(), 42);
assert_eq!(local.get(), 42);
}
#[test]
fn update_propagates_to_all_regions() {
let mut hardware_tracker = MockHardwareTrackerClient::new();
// Initial read + write.
hardware_tracker
.expect_current_memory_region_id()
.times(2)
.return_const(5 as MemoryRegionId);
// Reads to verify.
hardware_tracker
.expect_current_memory_region_id()
.times(1)
.return_const(0 as MemoryRegionId);
hardware_tracker
.expect_current_memory_region_id()
.times(1)
.return_const(1 as MemoryRegionId);
hardware_tracker
.expect_current_memory_region_id()
.times(1)
.return_const(9 as MemoryRegionId);
let hardware_tracker = HardwareTrackerClientFacade::from_mock(hardware_tracker);
let mut hardware_info = MockHardwareInfoClient::new();
hardware_info
.expect_max_memory_region_id()
.return_const(9 as MemoryRegionId);
let hardware_info = HardwareInfoClientFacade::from_mock(hardware_info);
let local = RegionCachedKey::with_clients(|| 42, hardware_info, hardware_tracker);
assert_eq!(local.get(), 42);
local.set(43);
assert_eq!(local.get(), 43);
assert_eq!(local.get(), 43);
assert_eq!(local.get(), 43);
}
#[test]
fn immediate_set_propagates_to_all_regions() {
let mut hardware_tracker = MockHardwareTrackerClient::new();
// Initial write.
hardware_tracker
.expect_current_memory_region_id()
.times(1)
.return_const(5 as MemoryRegionId);
// Reads to verify.
hardware_tracker
.expect_current_memory_region_id()
.times(1)
.return_const(0 as MemoryRegionId);
hardware_tracker
.expect_current_memory_region_id()
.times(1)
.return_const(1 as MemoryRegionId);
hardware_tracker
.expect_current_memory_region_id()
.times(1)
.return_const(9 as MemoryRegionId);
let hardware_tracker = HardwareTrackerClientFacade::from_mock(hardware_tracker);
let mut hardware_info = MockHardwareInfoClient::new();
hardware_info
.expect_max_memory_region_id()
.return_const(9 as MemoryRegionId);
let hardware_info = HardwareInfoClientFacade::from_mock(hardware_info);
let local = RegionCachedKey::with_clients(|| 42, hardware_info, hardware_tracker);
local.set(43);
assert_eq!(local.get(), 43);
assert_eq!(local.get(), 43);
assert_eq!(local.get(), 43);
}
}