[Pitch] Cell, ConstCell, and Volatile

Cell guarantees address stability while under any read/write accesses. So calling a method like withUnsafeMutablePointer guarantees the address stability from the start of the method call until the end of the call. Cell itself can still be moved just like any other noncopyable type. The same is true for Mutex, it can still be moved! It's just that places where you'd typically use it prevent it from being moved i.e. globals, class ivars, etc.

There is a slight caveat to this too though because nesting a Cell in another type gives it that same guarantee which presents the following scenario:

struct Nested: ~Copyable {
  let x: Cell<Int>

  func something() {
    // This is technically ok because 'Nested' is
    // guaranteed to have a stable address, to which
    // 'x' is a part of, until 'something'
    // has finished.
    let ptr = x.withUMP { $0 }
  }
}

but the following would not be ok:

func foo() {
  let x = Cell(123)

  // NOT OK; DANGER
  // 'x' can be moved!
  let ptr = x.withUMP { $0 }
}
1 Like