[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: a recursive lock prototype

From: Niels Möller
Subject: Re: a recursive lock prototype
Date: 05 Jul 2001 10:39:11 +0200

tb@becket.net (Thomas Bushnell, BSG) writes:

> What are the expected semantics of this function?  (A complete
> implementation of something always includes explanatory comments.)  

I think a "recursive mutex" usually refers to a lock that keeps track
of an owner and a count. recursive_lock() should work as follows:

  If the lock isn't held by anybody, grab it, and set the owner field
  to the current thread id, and the count to 1.

  If the lock is held, check the owner field. If it is us, just
  increment the count and return. If somebody else owns the lock,
  block until it is free.

The recursive_unlock() function should decrement the count, and
release the lock only if the count gets to zero.

This is useful if you have some function manipulating a resource,
which grabs a resource lock while doing its work, and you want to call
that function from some context where you have already grabbed the
lock. With an ordinary mutex, the second attempt to lock it would
cause a deadlock.

A thread can successfully call recursive_lock as many times as it
pleases, it just has to call recursive_unlock() the same number of
times when it is done with the lock. 


reply via email to

[Prev in Thread] Current Thread [Next in Thread]