fast_stm/transaction/mod.rs
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 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
pub mod control_block;
pub mod log_var;
use std::any::Any;
use std::cell::Cell;
use std::collections::btree_map::Entry;
use std::collections::BTreeMap;
use std::mem;
use std::sync::Arc;
use self::control_block::ControlBlock;
use self::log_var::LogVar;
use super::result::{StmError, StmResult};
use super::tvar::{TVar, VarControlBlock};
thread_local!(static TRANSACTION_RUNNING: Cell<bool> = const { Cell::new(false) });
/// `TransactionGuard` checks against nested STM calls.
///
/// Use guard, so that it correctly marks the Transaction as finished.
struct TransactionGuard;
impl TransactionGuard {
pub fn new() -> TransactionGuard {
TRANSACTION_RUNNING.with(|t| {
assert!(!t.get(), "STM: Nested Transaction");
t.set(true);
});
TransactionGuard
}
}
impl Drop for TransactionGuard {
fn drop(&mut self) {
TRANSACTION_RUNNING.with(|t| {
t.set(false);
});
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransactionControl {
Retry,
Abort,
}
/// Transaction tracks all the read and written variables.
///
/// It is used for checking vars, to ensure atomicity.
pub struct Transaction {
/// Map of all vars that map the `VarControlBlock` of a var to a `LogVar`.
/// The `VarControlBlock` is unique because it uses it's address for comparing.
///
/// The logs need to be accessed in a order to prevend dead-locks on locking.
vars: BTreeMap<Arc<VarControlBlock>, LogVar>,
}
impl Transaction {
/// Create a new log.
///
/// Normally you don't need to call this directly.
/// Use `atomically` instead.
fn new() -> Transaction {
Transaction {
vars: BTreeMap::new(),
}
}
/// Run a function with a transaction.
///
/// It is equivalent to `atomically`.
pub fn with<T, F>(f: F) -> T
where
F: Fn(&mut Transaction) -> StmResult<T>,
{
match Transaction::with_control(|_| TransactionControl::Retry, f) {
Some(t) => t,
None => unreachable!(),
}
}
/// 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`].
pub fn with_control<T, F, C>(mut control: C, f: F) -> Option<T>
where
F: Fn(&mut Transaction) -> StmResult<T>,
C: FnMut(StmError) -> TransactionControl,
{
let _guard = TransactionGuard::new();
// create a log guard for initializing and cleaning up
// the log
let mut transaction = Transaction::new();
// loop until success
loop {
// run the computation
match f(&mut transaction) {
// on success exit loop
Ok(t) => {
if transaction.commit() {
return Some(t);
}
}
Err(e) => {
// Check if the user wants to abort the transaction.
if let TransactionControl::Abort = control(e) {
return None;
}
// on retry wait for changes
if let StmError::Retry = e {
transaction.wait_for_change();
}
}
}
// clear log before retrying computation
transaction.clear();
}
}
#[allow(clippy::needless_pass_by_value)]
/// Perform a downcast on a var.
fn downcast<T: Any + Clone>(var: Arc<dyn Any>) -> T {
match var.downcast_ref::<T>() {
Some(s) => s.clone(),
None => unreachable!("TVar has wrong type"),
}
}
/// 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.
pub fn read<T: Send + Sync + Any + Clone>(&mut self, var: &TVar<T>) -> StmResult<T> {
let ctrl = var.control_block().clone();
// Check if the same var was written before.
let value = match self.vars.entry(ctrl) {
// If the variable has been accessed before, then load that value.
Entry::Occupied(mut entry) => entry.get_mut().read(),
// Else load the variable statically.
Entry::Vacant(entry) => {
// Read the value from the var.
let value = var.read_ref_atomic();
// Store in in an entry.
entry.insert(LogVar::Read(value.clone()));
value
}
};
// For now always succeeds, but that may change later.
Ok(Transaction::downcast(value))
}
/// Write a variable.
///
/// The write is not immediately visible to other threads,
/// but atomically commited at the end of the computation.
pub fn write<T: Any + Send + Sync + Clone>(
&mut self,
var: &TVar<T>,
value: T,
) -> StmResult<()> {
// box the value
let boxed = Arc::new(value);
// new control block
let ctrl = var.control_block().clone();
// update or create new entry
match self.vars.entry(ctrl) {
Entry::Occupied(mut entry) => entry.get_mut().write(boxed),
Entry::Vacant(entry) => {
entry.insert(LogVar::Write(boxed));
}
}
// For now always succeeds, but that may change later.
Ok(())
}
/// 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 `TVar`s in both functions.
/// Use `Transaction::or` instead of handling errors directly with the `Result::or`.
/// The later does not handle all the blocking correctly.
pub fn or<T, F1, F2>(&mut self, first: F1, second: F2) -> StmResult<T>
where
F1: Fn(&mut Transaction) -> StmResult<T>,
F2: Fn(&mut Transaction) -> StmResult<T>,
{
// Create a backup of the log.
let mut copy = Transaction {
vars: self.vars.clone(),
};
// Run the first computation.
let f = first(self);
match f {
// Run other on manual retry call.
Err(StmError::Retry) => {
// swap, so that self is the current run
mem::swap(self, &mut copy);
// Run other action.
let s = second(self);
// If both called retry then exit.
match s {
Err(StmError::Failure) => Err(StmError::Failure),
s => {
self.combine(copy);
s
}
}
}
// Return success and failure directly
x => x,
}
}
/// Combine two logs into a single log, to allow waiting for all reads.
fn combine(&mut self, other: Transaction) {
// combine reads
for (var, value) in other.vars {
// only insert new values
if let Some(value) = value.obsolete() {
self.vars.entry(var).or_insert(value);
}
}
}
/// Clear the log's data.
///
/// This should be used before redoing a computation, but
/// nowhere else.
fn clear(&mut self) {
self.vars.clear();
}
/// Wait for any variable to change,
/// because the change may lead to a new calculation result.
fn wait_for_change(&mut self) {
// Create control block for waiting.
let ctrl = Arc::new(ControlBlock::new());
#[allow(clippy::mutable_key_type)]
let vars = std::mem::take(&mut self.vars);
let mut reads = Vec::with_capacity(self.vars.len());
let blocking = vars
.into_iter()
.filter_map(|(a, b)| b.into_read_value().map(|b| (a, b)))
// Check for consistency.
.all(|(var, value)| {
var.wait(&ctrl);
let x = {
// Take read lock and read value.
let guard = var.value.read();
Arc::ptr_eq(&value, &guard)
};
reads.push(var);
x
});
// If no var has changed, then block.
if blocking {
// Propably wait until one var has changed.
ctrl.wait();
}
// Let others know that ctrl is dead.
// It does not matter, if we set too many
// to dead since it may slightly reduce performance
// but not break the semantics.
for var in &reads {
var.set_dead();
}
}
/// Write the log back to the variables.
///
/// Return true for success and false, if a read var has changed
fn commit(&mut self) -> bool {
// Use two phase locking for safely writing data back to the vars.
// First phase: acquire locks.
// Check for consistency of all the reads and perform
// an early return if something is not consistent.
// Created arrays for storing the locks
// vector of locks.
let mut read_vec = Vec::with_capacity(self.vars.len());
// vector of tuple (value, lock)
let mut write_vec = Vec::with_capacity(self.vars.len());
// vector of written variables
let mut written = Vec::with_capacity(self.vars.len());
for (var, value) in &self.vars {
// lock the variable and read the value
match *value {
// We need to take a write lock.
LogVar::Write(ref w) | LogVar::ReadObsoleteWrite(_, ref w) => {
// take write lock
let lock = var.value.write();
// add all data to the vector
write_vec.push((w, lock));
written.push(var);
}
// We need to check for consistency and
// take a write lock.
LogVar::ReadWrite(ref original, ref w) => {
// take write lock
let lock = var.value.write();
if !Arc::ptr_eq(&lock, original) {
return false;
}
// add all data to the vector
write_vec.push((w, lock));
written.push(var);
}
// Nothing to do. ReadObsolete is only needed for blocking, not
// for consistency checks.
LogVar::ReadObsolete(_) => {}
// Take read lock and check for consistency.
LogVar::Read(ref original) => {
// Take a read lock.
let lock = var.value.read();
if !Arc::ptr_eq(&lock, original) {
return false;
}
read_vec.push(lock);
}
}
}
// Second phase: write back and release
// Release the reads first.
// This allows other threads to continue quickly.
drop(read_vec);
for (value, mut lock) in write_vec {
// Commit value.
*lock = value.clone();
}
for var in written {
// Unblock all threads waiting for it.
var.wake_all();
}
// Commit succeded.
true
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn read() {
let mut log = Transaction::new();
let var = TVar::new(vec![1, 2, 3, 4]);
// The variable can be read.
assert_eq!(&*log.read(&var).unwrap(), &[1, 2, 3, 4]);
}
#[test]
fn write_read() {
let mut log = Transaction::new();
let var = TVar::new(vec![1, 2]);
log.write(&var, vec![1, 2, 3, 4]).unwrap();
// Consecutive reads get the updated version.
assert_eq!(log.read(&var).unwrap(), [1, 2, 3, 4]);
// The original value is still preserved.
assert_eq!(var.read_atomic(), [1, 2]);
}
#[test]
fn transaction_simple() {
let x = Transaction::with(|_| Ok(42));
assert_eq!(x, 42);
}
#[test]
fn transaction_read() {
let read = TVar::new(42);
let x = Transaction::with(|trans| read.read(trans));
assert_eq!(x, 42);
}
/// Run a transaction with a control function, that always aborts.
/// The transaction still tries to run a single time and should successfully
/// commit in this test.
#[test]
fn transaction_with_control_abort_on_single_run() {
let read = TVar::new(42);
let x = Transaction::with_control(|_| TransactionControl::Abort, |tx| read.read(tx));
assert_eq!(x, Some(42));
}
/// Run a transaction with a control function, that always aborts.
/// The transaction retries infinitely often. The control function will abort this loop.
#[test]
fn transaction_with_control_abort_on_retry() {
let x: Option<i32> =
Transaction::with_control(|_| TransactionControl::Abort, |_| Err(StmError::Retry));
assert_eq!(x, None);
}
#[test]
fn transaction_write() {
let write = TVar::new(42);
Transaction::with(|trans| write.write(trans, 0));
assert_eq!(write.read_atomic(), 0);
}
#[test]
fn transaction_copy() {
let read = TVar::new(42);
let write = TVar::new(0);
Transaction::with(|trans| {
let r = read.read(trans)?;
write.write(trans, r)
});
assert_eq!(write.read_atomic(), 42);
}
// Dat name. seriously?
#[test]
fn transaction_control_stuff() {
let read = TVar::new(42);
let write = TVar::new(0);
Transaction::with(|trans| {
let r = read.read(trans)?;
write.write(trans, r)
});
assert_eq!(write.read_atomic(), 42);
}
/// Test if nested transactions are correctly detected.
#[test]
#[should_panic]
fn transaction_nested_fail() {
Transaction::with(|_| {
Transaction::with(|_| Ok(42));
Ok(1)
});
}
}