You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Both of these essentially check that inner().strong == 1 && inner().weak == 1 to check that &mut self is the only reference to the Arc's data. However in between the checks a Weak pointer could be promoted and the Weak pointer dropped, resulting in both checks succeeding. Adding an additional strong check afterwards won't help as the second strong pointer could then be demoted again.
I don't see an obvious way without a two-word atomic read or a generation counter or something of that sort. On 64-bit splitting an atomic64 into two 32-bit sections /might/ be ok, but on 32-bit only allowing 16 bits of refcount seems bad. (There might be something with using a high bit as a lock bit or such?)
I'm tempted to say this could be fixed by switching from a strong/weak pair to a num_ptrs/num_weak pair, where every pointer (whether strong or weak) is counted in num_ptrs and then weak pointers are also counted in num_weak. This does lower the number of total pointers (strong + weak) that we can have outstanding, but it's not as restrictive as actually dividing a usize into two halves.
Another alternative might be to use the high bit of the strong pointer as a "there are weak references" marker. I'm not sure how well that would actually work.
And of course we could always just use a spinlock instead.
Using num_ptrs + num_weak shouldn't actually lower the total number of pointers since each Arc or Weak is larger than 1 byte so you'd exhaust the address space before overflowing num_ptrs.
Upon reflection I suspect that num_ptrs + num_weak fails for the same reason that get_mut fails, because the destructor of Arc needs to know how many strong references there are, and the only way to get num_strong is with two loads.
If that works (which sounds plausible) it has the disadvantage of requiring two atomics on clone() (though they're probably the same cacheline, so it's not as horrible as it might be).
I still haven't thought it entirely all the way through, but I think that num_ptrs + num_strong will work (as long as num_strong is always modified before num_ptrs). As you say, this will require two atomics on clone().
The other alternative that I think would work is using the low bit of the strong counter as a "has weak" flag that, when set, is never un-set. Once it's set, a spinlock is then used around all relevant accesses to the strong and weak counters. This is more complicated than num_ptrs + num_strong, but has the benefit of meaning that Arcs that don't use Weak won't have to pay any cost (and in fact they'll be slightly cheaper than today, because no atomic load of weak is necessary if the flag is 0).
Incidentally, it appears we do not have a spinlock construct in our stdlib. That's rather unfortunate, as ad-hoc simple spinlocks are not generally ideal. Preferably we'd have a construct that spins some number of times (it looks like pthread on OS X defines that number as 1000) on multi-processor machines before yielding the thread, and yields immediately on single-processor machines.
Split the Arc struct in two: Give Arc another marker type parameter that decides statically if the type should support weak pointers or not. Use default type parameters to make Arc<T> equivalent Arc<T, NoWeak>
Only the NoWeak Arc will permit get_mut and make_unique. Since the Arc source references boost a lot, I tried to find any indication of how this is solved in the boost sphere, and I can't find any solution when weak pointers are involved.
Default type parameters make this seamless for users that don't need weak pointers.
Note: boost shared_ptr has a member function bool unique() but it only considers stong references.
@bluss That seems like a very heavy-handed approach. The creator of the Arc instance may not know ahead of time whether any code is going to want to create a Weak instance. And at such time as that decision is made, all code that touches the Arc would have to be updated even though that code doesn't really care whether there's a Weak reference.
As for Boost, I glanced at it yesterday and it didn't appear to me that it supported weak pointers.
It is heavy-handed, but it is also flexible, and allows users to only pay for what they use. Didn't we already want to make Weak pointers optional? (What other reason are they unstable for?)
@bluss I'm not really sure why they're unstable, the message just says "Weak pointers may not belong in this module".
And you're right, Boost does have weak_ptr, the link I followed before (from the comments of Arc) didn't show it. Looking through Boost's weak_ptr and shared_ptr it appears that shared_ptr::unique() ignores weak pointers entirely and returns true even if there are existing weak pointers (it appears that shared_ptr works the same way our Arc does, with a shared counter (i.e. our strong) and a weak counter (that includes an extra +1 count as long as shared is non-zero).
So the difference here is our Arc's unique-testing needs to care about weak pointers (in order to meet the memory safety guarantees of Rust) but Boost doesn't enforce that.
I think what we should do today is remove Arc::make_unique and alloc::arc::get_mut. They're both marked as #[unstable] so this is acceptable. Without those two functions, our existing reference-counting implementation works fine. Then we can explore ways to re-add that functionality in the future. It appears that there are zero clients of Arc::make_unique in rust-lang/rust, although checking for get_mut is not so trivial as that name is not unique.
@kballard I added arc::get_mut() in order to use it for parallel ECS proof of concept. I don't know if any real projects use it at the moment, so it should be safe to kill, but I'm going to miss it anyways.
@kvark I would really like to have Arc::get_mut(), I think it's potentially useful, but as long as there's no clients in rust-lang/rust that depend on it right now, I think we should just kill it for 1.0 and investigate ways to safely re-add it in the future.
Since weak_count and strong_count are public, get_mut can be implemented as it is today, outside of the crate too. At least I think you can, you would be effectively converting &T to &mut T, but isn't that legal if you go through raw pointers? Of course you'll only use this unsafe-marked get_mut if you know no Weak pointers are used in your application.
@bluss True, you could reimplement the current logic of get_mut, but since that logic is broken I don't think it's something we should be telling people to do.
At least I think you can, you would be effectively converting &T to &mut T, but isn't that legal if you go through raw pointers?.
Well no, that doesn't make it legal. If you construct a &mut T, you need to guarantee it's not aliased by anything else. This means you need to guarantee that there are no &T references to the same value (and that you can't construct one). That said, I'm not 100% positive about the actual hard requirements here; I think it's just the aliasing requirement, which means if you can guarantee that there are no (accessible) &T references extant, then your constructed &mut T is valid.
Based on that, I believe that it would be legal to construct this third-party fn arc_get_mut<T>(ptr: &mut Arc<T>) -> Option<&mut T> even though the implementation would have to transmute a &T into a &mut T. The implementation of the function can ensure that it doesn't construct any other &T reference after creating the &mut T (or more generally, that it doesn't touch the ptr again), and the API of the function means that the resulting &mut T borrows the Arc.
All that said, given that the current scheme of separate atomic strong and weak counts does not allow you to do this unique-test safely, none of this really matters. get_mut() can't be implemented safely without changing the implementation details of Arc.
since that logic is broken I don't think it's something we should be telling people to do.
It's a systems language, we don't tell them what to do. We tell them what's possible and where the boundaries lie. We give them unsafe blocks for when they feel they can take over the reins themselves.
Well no, that doesn't make it legal. If you construct a &mut T, you need to guarantee it's not aliased by anything else.
Obviously. I didn't say it was a sufficient criterium. Anyway, let's not do this kind of thing where we out-nitpick each other.. All I suggested was if wouldn't it be possible to implement outside of crate.
Activity
lilyball commentedon Apr 28, 2015
I think you're right.
I'm tempted to say this could be fixed by switching from a strong/weak pair to a num_ptrs/num_weak pair, where every pointer (whether strong or weak) is counted in num_ptrs and then weak pointers are also counted in num_weak. This does lower the number of total pointers (strong + weak) that we can have outstanding, but it's not as restrictive as actually dividing a
usizeinto two halves.Another alternative might be to use the high bit of the strong pointer as a "there are weak references" marker. I'm not sure how well that would actually work.
And of course we could always just use a spinlock instead.
sfackler commentedon Apr 28, 2015
Using num_ptrs + num_weak shouldn't actually lower the total number of pointers since each
ArcorWeakis larger than 1 byte so you'd exhaust the address space before overflowing num_ptrs.lilyball commentedon Apr 28, 2015
Upon reflection I suspect that num_ptrs + num_weak fails for the same reason that get_mut fails, because the destructor of
Arcneeds to know how many strong references there are, and the only way to get num_strong is with two loads.lilyball commentedon Apr 28, 2015
Although it's possible that using num_ptrs + num_strong instead might possibly work.
talchas commentedon Apr 28, 2015
If that works (which sounds plausible) it has the disadvantage of requiring two atomics on clone() (though they're probably the same cacheline, so it's not as horrible as it might be).
lilyball commentedon Apr 28, 2015
I still haven't thought it entirely all the way through, but I think that num_ptrs + num_strong will work (as long as num_strong is always modified before num_ptrs). As you say, this will require two atomics on clone().
The other alternative that I think would work is using the low bit of the
strongcounter as a "has weak" flag that, when set, is never un-set. Once it's set, a spinlock is then used around all relevant accesses to thestrongandweakcounters. This is more complicated than num_ptrs + num_strong, but has the benefit of meaning thatArcs that don't useWeakwon't have to pay any cost (and in fact they'll be slightly cheaper than today, because no atomic load ofweakis necessary if the flag is0).Incidentally, it appears we do not have a spinlock construct in our stdlib. That's rather unfortunate, as ad-hoc simple spinlocks are not generally ideal. Preferably we'd have a construct that spins some number of times (it looks like pthread on OS X defines that number as
1000) on multi-processor machines before yielding the thread, and yields immediately on single-processor machines.bluss commentedon Apr 28, 2015
Split the Arc struct in two: Give Arc another marker type parameter that decides statically if the type should support weak pointers or not. Use default type parameters to make
Arc<T>equivalentArc<T, NoWeak>Only the NoWeak Arc will permit
get_mutandmake_unique. Since the Arc source references boost a lot, I tried to find any indication of how this is solved in the boost sphere, and I can't find any solution when weak pointers are involved.Default type parameters make this seamless for users that don't need weak pointers.
Note: boost shared_ptr has a member function
bool unique()but it only considers stong references.lilyball commentedon Apr 28, 2015
@bluss That seems like a very heavy-handed approach. The creator of the
Arcinstance may not know ahead of time whether any code is going to want to create aWeakinstance. And at such time as that decision is made, all code that touches theArcwould have to be updated even though that code doesn't really care whether there's aWeakreference.As for Boost, I glanced at it yesterday and it didn't appear to me that it supported weak pointers.
bluss commentedon Apr 28, 2015
It is heavy-handed, but it is also flexible, and allows users to only pay for what they use. Didn't we already want to make Weak pointers optional? (What other reason are they unstable for?)
See boost Smart Pointers doc, it has weak_ptr
lilyball commentedon Apr 28, 2015
@bluss I'm not really sure why they're unstable, the message just says "Weak pointers may not belong in this module".
And you're right, Boost does have
weak_ptr, the link I followed before (from the comments ofArc) didn't show it. Looking through Boost'sweak_ptrandshared_ptrit appears thatshared_ptr::unique()ignores weak pointers entirely and returnstrueeven if there are existing weak pointers (it appears thatshared_ptrworks the same way ourArcdoes, with asharedcounter (i.e. ourstrong) and aweakcounter (that includes an extra+1count as long assharedis non-zero).So the difference here is our
Arc's unique-testing needs to care about weak pointers (in order to meet the memory safety guarantees of Rust) but Boost doesn't enforce that.lilyball commentedon Apr 28, 2015
I think what we should do today is remove
Arc::make_uniqueandalloc::arc::get_mut. They're both marked as#[unstable]so this is acceptable. Without those two functions, our existing reference-counting implementation works fine. Then we can explore ways to re-add that functionality in the future. It appears that there are zero clients ofArc::make_uniquein rust-lang/rust, although checking forget_mutis not so trivial as that name is not unique.lilyball commentedon Apr 28, 2015
@alexcrichton You added
Arc::make_uniquein the first place, and @aturon you updated it to check weak refs. Any thoughts?kvark commentedon Apr 28, 2015
@kballard I added
arc::get_mut()in order to use it for parallel ECS proof of concept. I don't know if any real projects use it at the moment, so it should be safe to kill, but I'm going to miss it anyways.lilyball commentedon Apr 28, 2015
@kvark I would really like to have
Arc::get_mut(), I think it's potentially useful, but as long as there's no clients in rust-lang/rust that depend on it right now, I think we should just kill it for 1.0 and investigate ways to safely re-add it in the future.bluss commentedon Apr 28, 2015
Since weak_count and strong_count are public,
get_mutcan be implemented as it is today, outside of the crate too. At least I think you can, you would be effectively converting&Tto&mut T, but isn't that legal if you go through raw pointers? Of course you'll only use this unsafe-marked get_mut if you know no Weak pointers are used in your application.Removing unsound unstable API sounds fine.
lilyball commentedon Apr 28, 2015
@bluss True, you could reimplement the current logic of
get_mut, but since that logic is broken I don't think it's something we should be telling people to do.Well no, that doesn't make it legal. If you construct a
&mut T, you need to guarantee it's not aliased by anything else. This means you need to guarantee that there are no&Treferences to the same value (and that you can't construct one). That said, I'm not 100% positive about the actual hard requirements here; I think it's just the aliasing requirement, which means if you can guarantee that there are no (accessible)&Treferences extant, then your constructed&mut Tis valid.Based on that, I believe that it would be legal to construct this third-party
fn arc_get_mut<T>(ptr: &mut Arc<T>) -> Option<&mut T>even though the implementation would have totransmutea&Tinto a&mut T. The implementation of the function can ensure that it doesn't construct any other&Treference after creating the&mut T(or more generally, that it doesn't touch theptragain), and the API of the function means that the resulting&mut Tborrows theArc.All that said, given that the current scheme of separate atomic
strongandweakcounts does not allow you to do this unique-test safely, none of this really matters.get_mut()can't be implemented safely without changing the implementation details ofArc.huonw commentedon Apr 28, 2015
cc #24564
bluss commentedon Apr 29, 2015
It's a systems language, we don't tell them what to do. We tell them what's possible and where the boundaries lie. We give them unsafe blocks for when they feel they can take over the reins themselves.
Obviously. I didn't say it was a sufficient criterium. Anyway, let's not do this kind of thing where we out-nitpick each other.. All I suggested was if wouldn't it be possible to implement outside of crate.
bluss commentedon May 10, 2015
The racy functions still exist. We should document that or remove them.
Mark Arc function get_mut and method make_unique unsafe
Mark Arc function get_mut and method make_unique unsafe
Auto merge of #25908 - bluss:arc-mark-unsafe, r=sfackler
aturon commentedon Jun 26, 2015
For reference, these functions, together with weak pointers, are used heavily in Servo. I've made a PR to fix this problem more directly: #26610
Auto merge of #26610 - aturon:fix_make_unique, r=alexcrichton