Skip to content

Commit 5a3de21

Browse files
committed
Add Arc::new_with() function for constructing self-referential data structures
1 parent 2873165 commit 5a3de21

File tree

3 files changed

+148
-3
lines changed

3 files changed

+148
-3
lines changed

src/liballoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
#![feature(alloc_layout_extra)]
126126
#![feature(try_trait)]
127127
#![feature(associated_type_bounds)]
128+
#![feature(raw_ref_op)]
128129

129130
// Allow testing this library
130131

src/liballoc/sync.rs

+80-3
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,79 @@ impl<T> Arc<T> {
328328
Self::from_inner(Box::leak(x).into())
329329
}
330330

331+
/// Constructs a new `Arc<T>` using a weak reference to itself. Attempting
332+
/// to upgrade the weak reference before this function returns will result
333+
/// in a `None` value. However, the weak reference may be cloned freely and
334+
/// stored for use at a later time.
335+
///
336+
/// # Examples
337+
/// ```
338+
/// #![feature(arc_new_with)]
339+
/// #![allow(dead_code)]
340+
///
341+
/// use std::sync::{Arc, Weak};
342+
///
343+
/// struct Foo {
344+
/// me: Weak<Foo>,
345+
/// }
346+
///
347+
/// let foo = Arc::new_with(|me| Foo {
348+
/// me: me.clone(),
349+
/// });
350+
/// ```
351+
#[inline]
352+
#[unstable(feature = "arc_new_with", issue = "none")]
353+
pub fn new_with(data_fn: impl FnOnce(&Weak<T>) -> T) -> Arc<T> {
354+
// Construct the inner in the "uninitialized" state with a single
355+
// weak reference.
356+
let uninit_ptr: NonNull<_> = Box::leak(box ArcInner {
357+
strong: atomic::AtomicUsize::new(0),
358+
weak: atomic::AtomicUsize::new(1),
359+
data: mem::MaybeUninit::<T>::uninit(),
360+
})
361+
.into();
362+
let init_ptr: NonNull<ArcInner<T>> = uninit_ptr.cast();
363+
364+
let weak = Weak { ptr: init_ptr };
365+
366+
// It's important we don't give up ownership of the weak pointer, or
367+
// else the memory might be freed by the time `data_fn` returns. If
368+
// we really wanted to pass ownership, we could create an additional
369+
// weak pointer for ourselves, but this would result in additional
370+
// updates to the weak reference count which might not be necessary
371+
// otherwise.
372+
let data = data_fn(&weak);
373+
374+
// Now we can properly initialize the inner value and turn our weak
375+
// reference into a strong reference.
376+
unsafe {
377+
let inner = init_ptr.as_ptr();
378+
ptr::write(&raw mut (*inner).data, data);
379+
380+
// The above write to the data field must be visible to any threads which
381+
// observe a non-zero strong count. Therefore we need at least "Release" ordering
382+
// in order to synchronize with the `compare_exchange_weak` in `Weak::upgrade`.
383+
//
384+
// "Acquire" ordering is not required. When considering the possible behaviours
385+
// of `data_fn` we only need to look at what it could do with a reference to a
386+
// non-upgradeable `Weak`:
387+
// - It can *clone* the `Weak`, increasing the weak reference count.
388+
// - It can drop those clones, decreasing the weak reference count (but never to zero).
389+
//
390+
// These side effects do not impact us in any way, and no other side effects are
391+
// possible with safe code alone.
392+
let prev_value = (*inner).strong.fetch_add(1, Release);
393+
debug_assert_eq!(prev_value, 0, "No prior strong references should exist");
394+
}
395+
396+
let strong = Arc::from_inner(init_ptr);
397+
398+
// Strong references should collectively own a shared weak reference,
399+
// so don't run the destructor for our old weak reference.
400+
mem::forget(weak);
401+
strong
402+
}
403+
331404
/// Constructs a new `Arc` with uninitialized contents.
332405
///
333406
/// # Examples
@@ -1608,7 +1681,8 @@ impl<T: ?Sized> Weak<T> {
16081681
#[stable(feature = "arc_weak", since = "1.4.0")]
16091682
pub fn upgrade(&self) -> Option<Arc<T>> {
16101683
// We use a CAS loop to increment the strong count instead of a
1611-
// fetch_add because once the count hits 0 it must never be above 0.
1684+
// fetch_add as this function should never take the reference count
1685+
// from zero to one.
16121686
let inner = self.inner()?;
16131687

16141688
// Relaxed load because any write of 0 that we can observe
@@ -1631,8 +1705,11 @@ impl<T: ?Sized> Weak<T> {
16311705
}
16321706
}
16331707

1634-
// Relaxed is valid for the same reason it is on Arc's Clone impl
1635-
match inner.strong.compare_exchange_weak(n, n + 1, Relaxed, Relaxed) {
1708+
// Relaxed is fine for the failure case because we don't have any expectations about the new state.
1709+
// Acquire is necessary for the success case to synchronise with `Arc::new_with`, when the inner
1710+
// value can be initialized after `Weak` references have already been created. In that case, we
1711+
// expect to observe the fully initialized value.
1712+
match inner.strong.compare_exchange_weak(n, n + 1, Acquire, Relaxed) {
16361713
Ok(_) => return Some(Arc::from_inner(self.ptr)), // null checked above
16371714
Err(old) => n = old,
16381715
}

src/liballoc/sync/tests.rs

+67
Original file line numberDiff line numberDiff line change
@@ -492,3 +492,70 @@ fn test_array_from_slice() {
492492
let a: Result<Arc<[u32; 2]>, _> = r.clone().try_into();
493493
assert!(a.is_err());
494494
}
495+
496+
#[test]
497+
fn test_arc_new_with_zero_refs() {
498+
struct ZeroRefs {
499+
inner: Weak<ZeroRefs>,
500+
}
501+
let zero_refs = Arc::new_with(|inner| {
502+
assert_eq!(inner.strong_count(), 0);
503+
assert!(inner.upgrade().is_none());
504+
ZeroRefs { inner: Weak::new() }
505+
});
506+
507+
assert_eq!(Arc::strong_count(&zero_refs), 1);
508+
assert_eq!(Arc::weak_count(&zero_refs), 0);
509+
assert_eq!(zero_refs.inner.strong_count(), 0);
510+
assert_eq!(zero_refs.inner.weak_count(), 0);
511+
}
512+
513+
#[test]
514+
fn test_arc_new_with_one_ref() {
515+
struct OneRef {
516+
inner: Weak<OneRef>,
517+
}
518+
let one_ref = Arc::new_with(|inner| {
519+
assert_eq!(inner.strong_count(), 0);
520+
assert!(inner.upgrade().is_none());
521+
OneRef { inner: inner.clone() }
522+
});
523+
524+
assert_eq!(Arc::strong_count(&one_ref), 1);
525+
assert_eq!(Arc::weak_count(&one_ref), 1);
526+
527+
let one_ref2 = Weak::upgrade(&one_ref.inner).unwrap();
528+
assert!(Arc::ptr_eq(&one_ref, &one_ref2));
529+
530+
assert_eq!(Arc::strong_count(&one_ref), 2);
531+
assert_eq!(Arc::weak_count(&one_ref), 1);
532+
}
533+
534+
#[test]
535+
fn test_arc_new_with_two_refs() {
536+
struct TwoRefs {
537+
inner1: Weak<TwoRefs>,
538+
inner2: Weak<TwoRefs>,
539+
}
540+
let two_refs = Arc::new_with(|inner| {
541+
assert_eq!(inner.strong_count(), 0);
542+
assert!(inner.upgrade().is_none());
543+
544+
let inner1 = inner.clone();
545+
let inner2 = inner1.clone();
546+
547+
TwoRefs { inner1, inner2 }
548+
});
549+
550+
assert_eq!(Arc::strong_count(&two_refs), 1);
551+
assert_eq!(Arc::weak_count(&two_refs), 2);
552+
553+
let two_refs1 = Weak::upgrade(&two_refs.inner1).unwrap();
554+
assert!(Arc::ptr_eq(&two_refs, &two_refs1));
555+
556+
let two_refs2 = Weak::upgrade(&two_refs.inner2).unwrap();
557+
assert!(Arc::ptr_eq(&two_refs, &two_refs2));
558+
559+
assert_eq!(Arc::strong_count(&two_refs), 3);
560+
assert_eq!(Arc::weak_count(&two_refs), 2);
561+
}

0 commit comments

Comments
 (0)