Reentrant mutex

In computer science, the reentrant mutex (recursive mutex, recursive lock) is a particular type of mutual exclusion (mutex) device that may be locked multiple times by the same process/thread, without causing a deadlock.

While any attempt to perform the "lock" operation on an ordinary mutex (lock) would either fail or block when the mutex is already locked, on a recursive mutex this operation will succeed if and only if the locking thread is the one that already holds the lock. Typically, a recursive mutex tracks the number of times it has been locked, and requires equally many unlock operations to be performed before other threads may lock it.

Motivation

Recursive mutexes solve the problem of non-reentrancy with regular mutexes: if a function that takes a lock and executes a callback is itself called by the callback, deadlock ensues.[1] In pseudocode, that is the following situation:

var m : Mutex  // A non-recursive mutex, initially unlocked.

function lock_and_call(i : Integer)
    m.lock()
    callback(i)
    m.unlock()

function callback(i : Integer)
    if i > 0
        lock_and_call(i - 1)

lock_and_call(1)  // Invoking the function

Given these definitions, the function call lock_and_call(1) will cause the following sequence of events:

  • m.lock() — mutex locked
  • callback(1)
  • lock_and_call(0) — because i > 0
  • m.lock() — deadlock, because m is already locked, so the executing thread will block, waiting for itself.

Replacing the mutex with a recursive one solves the problem, because the final m.lock() will succeed without blocking.

Practical use

W. Richard Stevens notes that recursive locks are "tricky" to use correctly, and recommends their use for adapting single-threaded code without changing APIs, but "only when no other solution is possible".[2]

The Java language's native synchronization mechanism, monitor, uses recursive locks. Syntactically, a lock is a block of code with the 'synchronized' keyword preceding it and any Object reference in parentheses that will be used as the mutex. Inside the synchronized block, the given object can be used as a condition variable by doing a wait(), notify(), or notifyAll() on it. Thus all Objects are both recursive mutexes and condition variables.[3]

Example

  1. Thread A calls function F which acquires a reentrant lock for itself before proceeding
  2. Thread B calls function F which attempts to acquire a reentrant lock for itself but cannot due to one already outstanding, resulting in either a block (it waits), or a timeout if requested
  3. Thread A's F calls itself recursively. It already owns the lock, so it will not block itself (no deadlock). This is the central idea of a reentrant mutex, and is what makes it different from a regular lock.
  4. Thread B's F is still waiting, or has caught the timeout and worked around it
  5. Thread A's F finishes and releases its lock(s)
  6. Thread B's F can now acquire a reentrant lock and proceed if it was still waiting

Software emulation

Software emulation can be accomplished using the following structure:

  • A "control" condition using a regular lock
  • Owner identifier, unique to each thread (defaulting to empty / not set)
  • Acquisition count (defaulting to zero)

Acquisition

  1. Acquire the control condition.
  2. If the owner is set and not the current thread, wait for the control condition to be notified (this also releases the condition).
  3. Set the owner to the current thread. The owner identifier should have already been cleared at this point unless the acquirer is already the owner.
  4. Increment the acquisition count (should always result in 1 for new owners).
  5. Release the control condition.

Release

  1. Acquire the control condition, asserting that the owner is the releaser.
  2. Decrement the acquisition count, asserting that the count is greater than or equal to zero.
  3. If the acquisition count is zero, clear the owner information and notify the control condition.
  4. Release the control condition.
gollark: I'm not sure what you would believe instead, really?
gollark: I mean, that's obvious. You don't need a paper about it. And just accepting it because a paper says it would be, well, ironically stupid.
gollark: There's lots of software around now for archiving web pages.
gollark: I do that for lots of the web content I like, I've got a big folder of that.
gollark: Then you should have downloaded them or something.

References

  1. Buschmann, Frank; Henney, Kevlin; Schmidt, Douglas C. (2007). Pattern-Oriented Software Architecture, A Pattern Language for Distributed Computing. John Wiley & Sons. p. 374.
  2. Stevens, W. Richard; Rago, Stephen A. (2013). Advanced Programming in the UNIX Environment. Addison-Wesley. p. 434.
  3. David Hovemeyer. "Lecture 17: Java Threads, Synchronization". CS 365 - Parallel and Distributed Computing. Lecture notes, York College of Pennsylvania. Retrieved 4 June 2015.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.