CSE221 - lec05: Synchronization: Monitor & Mesa

date
Oct 22, 2024
slug
cse221-lec05
status
Published
tags
System
summary
type
Post

05 - Synchronization: Monitor & Mesa

Monitors: An Operating System Structuring Concept

clarification on terms: process in the paper refer to similar thing as thread today, which share the same address space.

The structure of a monitor

monitor is a structuring method for manage resources. a monitor is like an object, has its own method and private data.
at any time, only one process (thread) can be in the monitor, which is ensured by holding a mutex when entering the monitor procedure.
There are two kinds of synchronization between processes:
  • mutual exclusion, which could be achieved by using a mutex
  • coordination: sometimes a process may want to wait for some conditions to become true, at this time we need some mechanisms to let the process wait and release lock, and resume at suitable time.

Condition Variables

condition variables are used for process coordination.
a condition variable is typically associated with some state variables, when these state variables do not fulfill some certain predicates, some process may want to wait, and when conditions are fulfilled resume execution.
therefore, there are 2 operations we can do with the condition variables:
  • wait: suspend a thread & release lock
  • signal: wake up a waiter & the waiter should re-accquire the lock before resume

Hoare Semantics v.s. Mesa Semantics

Hoare Semantics:
  • wait: suspend a thread
  • signal: wake a thread & context switch to it immediately
notion image
Invariant I:
the monitor resolve race condition by maintaining some invariant, so as to keep the internal data structure in a consistent state.
A thread leaves monitor in the following case:
  • exit procedure
  • signal
  • wait
When can you assume the invariant holds:
  • before enter monitor
  • after leaving monitor
Condition B:
  • some predicates that should be true for a thread to proceed
When can you assume condition is true:
  • before calling signal
  • after resume from wait
Above can be summarized as:
wait: I → wait → I & B
signal: I & B → signal → I
Mesa Semantics
weaker semantics, more flexible
  • wait: same as hoare semantics
  • signal (notify): put one thread on runnable queue (do not context switch to it immediately, so when the thread wake up the condition may not be true since there may be other threads)
notion image
Mesa semantics can be summarized as:
  • wait: I → wait → I
  • signal: [] → signal → []
 

Usage fashion

notion image

Experience with Processes and Monitors in Mesa

Hoare Semantics v.s. Mesa Semantics v.s. Monitor in Java

notion image

Trends overtime

  • Hoare & Mesa semantics rely on compiler support
  • Java supports both compiler-supported implicit way & manual explicit way
  • C++ / Rust: programmer accquire the lock, but release is automatic (through RAII?)

Summary & take-away

  • language support for synchronization via monitor
  • Hoare v.s. Mesa semantics

© Lifan Sun 2023 - 2024