Module sync::mutex

source ·
Expand description

Mutex type whose methods panic rather than returning error in case of poison.

The Mutex type in this module wraps the standard library Mutex and mirrors the same methods, except that they panic where the standard library would return a PoisonError. This API codifies our error handling strategy around poisoned mutexes in crosvm.

  • Crosvm releases are built with panic=abort so poisoning never occurs. A panic while a mutex is held (or ever) takes down the entire process. Thus we would like for code not to have to consider the possibility of poison.

  • We could ask developers to always write .lock().unwrap() on a standard library mutex. However, we would like to stigmatize the use of unwrap. It is confusing to permit unwrap but only on mutex lock results. During code review it may not always be obvious whether a particular unwrap is unwrapping a mutex lock result or a different error that should be handled in a more principled way.

Developers should feel free to use sync::Mutex anywhere in crosvm that they would otherwise be using std::sync::Mutex.

Structs

  • A mutual exclusion primitive useful for protecting shared data.
  • The lock could not be acquired at this time because the operation would otherwise block.