Expand description
Strategies for protecting the reference counts.
There are multiple algorithms how to protect the reference counts while they’re being updated
by multiple threads, each with its own set of pros and cons. The DefaultStrategy is used by
default and should generally be the least surprising option. It is possible to pick a different
strategy.
For now, the traits in here are sealed and don’t expose any methods to the users of the crate. This is because we are not confident about the details just yet. In the future it may be possible for downstream users to implement their own, but for now it is only so users can choose one of the provided.
It is expected that future strategies would come with different capabilities and limitations. In particular, some that are not “tight” in the cleanup (delay the cleanup) or not support the compare and swap operations.
Currently, we have these strategies:
DefaultStrategy(this one is used implicitly)RwLock<()>
§The RwLock<()> strategy
With the rwlock-strategy feature, RwLock<()> may be used as a strategy
too. It doesn’t have the performance characteristics or lock-free guarantees of the others, but
it is much simpler and contains less unsafe code (actually, less code altogether): a load
takes the read lock just long enough to increment the reference count, so a reader always holds
an owned reference. Therefore, it can be used for testing purposes and cross-checking, or as a
fallback by code that owns its ArcSwapAny instances and wants to rule the default strategy
out. The strategy is chosen per instance, so enabling the feature never changes
DefaultStrategy for anyone else in the dependency graph:
use std::sync::{Arc, RwLock};
use arc_swap::ArcSwapAny;
let shared: ArcSwapAny<Arc<u32>, RwLock<()>> = ArcSwapAny::with_strategy(Arc::new(1), RwLock::new(()));
shared.store(Arc::new(2));
assert_eq!(**shared.load(), 2);Note that generally, using RwLock<Arc<T>> is likely to be better
performance wise. So if the goal is to not use third-party unsafe code, only the one in
std, that is the better option. This is provided mostly for investigation and testing of
ArcSwap itself or algorithms written to use ArcSwap.
This is not meant to be used in production code.
Traits§
- CaS
- An extension of the
Strategy, allowing for compare and swap operation. - Strategy
- A strategy for protecting the reference counted pointer
T.
Type Aliases§
- Default
Strategy - The default strategy.