Table of Contents

Modelling with STGs: Writer-biased read/write lock

Read/write lock

A concurrency primitive called read/write lock (or RW lock for short) is often used to control access to a shared resource that can be read concurrently but written exclusively. Each client, when requesting access, specifies whether it is for writing or for reading – read/read concurrency is allowed, but read/write and write/write concurrency is forbidden. A client can be modelled by the following STG:

tutorial:model:read_write_lock:rw_1process.svg

Note that:

Idea of implementation

Suppose there are N clients, and from time to time some client indicates that it wishes to read or to write the resource. We create N ‘tickets’. A reader needs a single ticket to enter its critical section, whereas a writer needs all N tickets to enter its critical section. Having finished using the resource, the client returns the ticket(s) it had been granted, so that other clients can get them and access the resource. For example, the following STG models an RW lock for two clients.

tutorial:model:read_write_lock:rw.svg

Unfortunately the above implementation is prone to writer starvation – the scenarios when there are always some readers executing their critical sections, and hence a writer is blocked for a long time or even indefinitely. To prevent such scenarios, new readers should be prevented from entering their critical sections if there is a waiting writer. That is, a writer must first acquire a usual (i.e. non-RW) lock (competing with other writers) and indicate that there is a waiting writer to prevent new readers from entering their critical sections. Then this writer has to wait for all the existing readers to exit their critical sections, at which point it can acquire N tickets and enter its critical section. Such an implementation is called a writer-biased RW lock. A client’s model now becomes as follows.

tutorial:model:read_write_lock:rw_biased_1process.svg

Note that the writer now has an extra transition wait_r to wait for the existing readers to exit their critical sections – this happens when all N tickets have been returned and this transition can consume them.

Exercise 1: Modelling a writer-biased RW lock

The following STG provides a skeleton for the Writer-Biased RW lock for three clients (three is the minimum number of clients for the model to be interesting). Note that besides the places modelling tickets, there is a place w_lock modelling a lock for which the writers compete – in the final STG this place will become an arbitrating choice between transitions w1+, w2+ and w3+.

tutorial:model:read_write_lock:rw_biased_template.svg

Download this STG stg-rw_biased_template.work and complete the model by providing the necessary arcs between the transitions of the clients and the places w_lock and those modelling the tickets.

Exercise 2: Simulation and verification

Simulate your model. Try to re-create the following scenarios:

Verify the following properties of the model:

===== Feedback =====
Comments or suggestions for improving this tutorial
  • As discussed in https://www.dokuwiki.org/plugin:include#controlling_header_size_in_included_pages, by default, the headers in included pages start one level lower than the last header in the current page. This can be tweaked by adding an empty header above the include:\\
    ====== ======
    {{page>:tutorial:feedback&inline}}
  • For offline help generation the content of feedback page should be temporary wrapped in <WRAP hide>. Note that the headers still propagate to the table of contents even if inside the hidden wrap. Therefore the Feedback title needs to be converted to something else, e.g. to code by adding two spaces in front.