[Pharo-dev] [Ann] ReadWriteLock

Denis Kudriashov dionisiydk at gmail.com
Mon Jan 4 12:39:55 EST 2016


I implemented small package ReadWriteLock

Gofer it
smalltalkhubUser: 'Pharo' project: 'ReadWriteLock';
configurationOf: 'ReadWriteLock';

It is reentral read write lock which described in
https://en.wikipedia.org/wiki/Readers–writer_lock. From the article:

An ReadWriteLock allows concurrent access for read-only operations, while
> write operations require exclusive access. This means that multiple threads
> can read the data in parallel but an exclusive lock is needed for writing
> or modifying data. When a writer is writing the data, all other writers or
> readers will be blocked until the writer is finished writing.

Public API and Key Messages

- lock := ReadWriteLock new
- lock criticalRead: aBlock
- lock criticalWrite: aBlock

Implementation based on two semaphores and readers counter.

Main difficulty is carefully handle process termination during execution of
critical sections. This problem described in Semaphore>>critical: comment.
Same approach is used here. But synchronisation logic around two semaphores
for reading and writing complicates things very much. No simple way to
decompose logic on multiple methods because information about process
interruption become hidden.
>From the Semaphore comment:

> The main trick is assignment right before we go into the wait primitive
> (which is not a real send and therefore not interruptable either). So after
> we can check that waiting is happen or not.

Tests are implemented only for lock scenarios. No tests for described
termination process cases. It is not really clear how to write it.
I will be appreciate if people review code. Maybe you will suggest
simplifications. It is always difficult to implement concurrent code.

Best regards,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-dev_lists.pharo.org/attachments/20160104/dba65917/attachment.html>

More information about the Pharo-dev mailing list