pub struct Transaction { /* private fields */ }Expand description
Transaction tracks all the read and written variables.
It is used for checking vars, to ensure atomicity.
Implementations§
Source§impl Transaction
Public API
impl Transaction
Public API
Sourcepub fn with<T, F>(f: F) -> T
pub fn with<T, F>(f: F) -> T
Run a function with a transaction.
It is equivalent to atomically.
Sourcepub fn with_control<T, F, C>(control: C, f: F) -> Option<T>
pub fn with_control<T, F, C>(control: C, f: F) -> Option<T>
Run a function with a transaction.
with_control takes another control function, that
can steer the control flow and possible terminate early.
control can react to counters, timeouts or external inputs.
It allows the user to fall back to another strategy, like a global lock in the case of too much contention.
Please not, that the transaction may still infinitely wait for changes when retry is
called and control does not abort.
If you need a timeout, another thread should signal this through a TVar.
Sourcepub fn with_err<T, F, E>(f: F) -> Result<T, E>
pub fn with_err<T, F, E>(f: F) -> Result<T, E>
Run a function with a transaction.
The transaction will be retried until:
- it is validated, or
- it is explicitly aborted from the function, using the
TODOfunction.
Sourcepub fn with_control_and_err<T, F, C, E>(
control: C,
f: F,
) -> TransactionResult<T, E>where
F: Fn(&mut Transaction) -> TransactionClosureResult<T, E>,
C: FnMut(StmError) -> TransactionControl,
pub fn with_control_and_err<T, F, C, E>(
control: C,
f: F,
) -> TransactionResult<T, E>where
F: Fn(&mut Transaction) -> TransactionClosureResult<T, E>,
C: FnMut(StmError) -> TransactionControl,
Run a function with a transaction.
with_control takes another control function, that
can steer the control flow and possible terminate early.
control can react to counters, timeouts or external inputs.
It allows the user to fall back to another strategy, like a global lock in the case of too much contention.
Please not, that the transaction may still infinitely wait for changes when retry is
called and control does not abort.
If you need a timeout, another thread should signal this through a TVar.
Source§impl Transaction
Public profiling API
impl Transaction
Public profiling API
Sourcepub fn profile_with<T, F>(f: F) -> (T, TransactionTallies)
Available on crate feature profiling only.
pub fn profile_with<T, F>(f: F) -> (T, TransactionTallies)
profiling only.Run a function with a transaction.
It is equivalent to atomically.
Sourcepub fn profile_with_control<T, F, C>(
control: C,
f: F,
) -> (Option<T>, TransactionTallies)
Available on crate feature profiling only.
pub fn profile_with_control<T, F, C>( control: C, f: F, ) -> (Option<T>, TransactionTallies)
profiling only.Run a function with a transaction.
with_control takes another control function, that
can steer the control flow and possible terminate early.
control can react to counters, timeouts or external inputs.
It allows the user to fall back to another strategy, like a global lock in the case of too much contention.
Please not, that the transaction may still infinitely wait for changes when retry is
called and control does not abort.
If you need a timeout, another thread should signal this through a TVar.
Sourcepub fn profile_with_err<T, F, E>(f: F) -> (Result<T, E>, TransactionTallies)
Available on crate feature profiling only.
pub fn profile_with_err<T, F, E>(f: F) -> (Result<T, E>, TransactionTallies)
profiling only.Run a function with a transaction.
The transaction will be retried until:
- it is validated, or
- it is explicitly aborted from the function, using the
TODOfunction.
Sourcepub fn profile_with_control_and_err<T, F, C, E>(
control: C,
f: F,
) -> (TransactionResult<T, E>, TransactionTallies)where
F: Fn(&mut Transaction) -> TransactionClosureResult<T, E>,
C: FnMut(StmError) -> TransactionControl,
Available on crate feature profiling only.
pub fn profile_with_control_and_err<T, F, C, E>(
control: C,
f: F,
) -> (TransactionResult<T, E>, TransactionTallies)where
F: Fn(&mut Transaction) -> TransactionClosureResult<T, E>,
C: FnMut(StmError) -> TransactionControl,
profiling only.Run a function with a transaction.
with_control takes another control function, that
can steer the control flow and possible terminate early.
control can react to counters, timeouts or external inputs.
It allows the user to fall back to another strategy, like a global lock in the case of too much contention.
Please not, that the transaction may still infinitely wait for changes when retry is
called and control does not abort.
If you need a timeout, another thread should signal this through a TVar.
Source§impl Transaction
In-closure routines
impl Transaction
In-closure routines
Sourcepub fn read<T: Send + Sync + Any + Clone>(
&mut self,
var: &TVar<T>,
) -> StmClosureResult<T>
pub fn read<T: Send + Sync + Any + Clone>( &mut self, var: &TVar<T>, ) -> StmClosureResult<T>
Read a variable and return the value.
The returned value is not always consistent with the current value of the var, but may be an outdated or or not yet commited value.
The used code should be capable of handling inconsistent states without running into infinite loops. Just the commit of wrong values is prevented by STM.
Sourcepub fn write<T: Any + Send + Sync + Clone>(
&mut self,
var: &TVar<T>,
value: T,
) -> StmClosureResult<()>
pub fn write<T: Any + Send + Sync + Clone>( &mut self, var: &TVar<T>, value: T, ) -> StmClosureResult<()>
Write a variable.
The write is not immediately visible to other threads, but atomically commited at the end of the computation.
Sourcepub fn modify<T: Any + Send + Sync + Clone, F>(
&mut self,
var: &TVar<T>,
f: F,
) -> StmClosureResult<()>where
F: FnOnce(T) -> T,
pub fn modify<T: Any + Send + Sync + Clone, F>(
&mut self,
var: &TVar<T>,
f: F,
) -> StmClosureResult<()>where
F: FnOnce(T) -> T,
Modify a variable.
The write is not immediately visible to other threads, but atomically commited at the end of the computation.
Prefer this method over calling read then write for performance.
Sourcepub fn replace<T: Any + Send + Sync + Clone>(
&mut self,
var: &TVar<T>,
value: T,
) -> StmClosureResult<T>
pub fn replace<T: Any + Send + Sync + Clone>( &mut self, var: &TVar<T>, value: T, ) -> StmClosureResult<T>
Replace a variable.
The write is not immediately visible to other threads, but atomically commited at the end of the computation.
Prefer this method over calling read then write for performance.
Sourcepub fn or<T, F1, F2>(&mut self, first: F1, second: F2) -> StmClosureResult<T>where
F1: Fn(&mut Transaction) -> StmClosureResult<T>,
F2: Fn(&mut Transaction) -> StmClosureResult<T>,
pub fn or<T, F1, F2>(&mut self, first: F1, second: F2) -> StmClosureResult<T>where
F1: Fn(&mut Transaction) -> StmClosureResult<T>,
F2: Fn(&mut Transaction) -> StmClosureResult<T>,
Combine two calculations. When one blocks with retry,
run the other, but don’t commit the changes in the first.
If both block, Transaction::or still waits for TVars in both functions.
Use Transaction::or instead of handling errors directly with the Result::or.
The later does not handle all the blocking correctly.