[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.
Regards,
/Niels
- a recursive lock prototype, moshez, 2001/07/04
- Re: a recursive lock prototype, Thomas Bushnell, BSG, 2001/07/04
- Re: a recursive lock prototype,
Niels Möller <=
- Re: a recursive lock prototype, Marcus Brinkmann, 2001/07/05
- Re: a recursive lock prototype, Thomas Bushnell, BSG, 2001/07/05
- Re: a recursive lock prototype, Marcus Brinkmann, 2001/07/06
- Re: a recursive lock prototype, Thomas Bushnell, BSG, 2001/07/06
- Re: a recursive lock prototype, Marcus Brinkmann, 2001/07/07
- Re: a recursive lock prototype, Thomas Bushnell, BSG, 2001/07/08
- Re: a recursive lock prototype, Marcus Brinkmann, 2001/07/09
- Re: a recursive lock prototype, Thomas Bushnell, BSG, 2001/07/10
- Re: a recursive lock prototype, Thomas Bushnell, BSG, 2001/07/05