State Machines

Access control can be viewed as a special case of a state machine. For example, a mutex is a two-state machine:

locked unlocked Mutex::unlock() Mutex::new() Mutex::lock()

State machines have two core concepts: states (the circles) and transitions (the arrows). When APIs represent state machines, the important question is whether the transitions are consistent with the states, e.g. you should not be able to unlock an unlocked mutex. Here are a few more examples of state machines in systems:

  • A shopper can only checkout while their cart is not empty.
  • A file can only be closed while its file descriptor is open.
  • A GPIO pin can only be written to when in write mode.