Published on

COMP2310 - Week 6 - Centralised Synchronisation

Table of Contents

Monitors

  • Form of encapsulation -> Centralise all operations on a shared data structure in one place

    • Encapsulate data along with operations which operate on it.
  • Prohibits access other than by the monitor procedures and functions

  • Assures mutual exclusion of all monitor procedures and functions

monitor buffer;
export append, take;
var (* declare protected vars *)
procedure append (I : integer);
...
procedure take (var I : integer);
...
begin
(* initialisation)
end;

Condition Synchronisation

  • Hoare Monitors
    • Condition variables implemented by semaphores (Wait and Signal)
    • Queues for tasks suspended on condition variables are realised
    • Suspended task releases its lock on the monitor, enabling another task to enter
      • If they don't, no other task could access, possible deadlock

      • More efficient evaluation of guards -> Task leaving monitor can evaluate all guards, allowing right tasks to be activated

      • Blocked tasks may be ordered and livelocks prevented

Monitors with Condition Synchronisation

  • Signal is allowed only as the last action of a process before it leaves the monitor

  • Signal operations have the side-effect of executing a return statement

    • I.e. nothing can go after the signal operation
  • Signal operation of unblocking another process has the side-effect of blocking the current process

    • Executes again once monitor is unlocked again
  • Signal operation which unblocks a process does not block the caller, however unblocked process must re-gain access to the monitor

Monitors in POSIX

  • Attributes include
    • Semantics for trying to lock a mutex
    • Sharing of mutexes and condition variables
    • Priority ceiling
    • Clock used for timeouts

Object-Orientation and Synchronisation

  • SInce mutual exclusion and condition synchronisation schemes must consider all involved methods and guards, new behaviour cannot be added without re-evalutating the class

  • Re-use through inheritance does not translate to synchronised classes

  • Parent class might need to be adapted in order to suit the global synchronisation scheme

Nested Monitor Calls

  • Monitors are not composible easily

  • Assuming a thread in a monitor is calling an operation in another monitor and is suspended at a conditional variable there

    • Called monitor is aware of the suspension and allows other threads to enter
    • Monitor is possible not aware of the suspension and keeps its lock
    • Unjustified locked calling reduces system performance and leads to potential deadlocks

Criticism of Monitors

  • Mutual exclusion solved elegantly and safely
  • All criticism about semaphorse applies to monitors
  • Mixture of low and high-level synchronisation constructs