[fpc-pascal] Semaphore problems

Vinzent Höfler JeLlyFish.software at gmx.net
Mon Jul 24 23:38:26 CEST 2006


Vinzent Höfler wrote:

> Hmm. So we'd need a mutex inside a mutex. Now I know why they call it 
> recursive. ;) So it'll be something like that:
> 
> function Recursive_Mutex.Lock : ...;
> begin
>    // Lock mutex inside mutex.
>    self.Owner_Check_Lock.Lock;
> 
>    // Owned by current thread?
>    if CurrentThreadId <> self.ThreadId then
>    begin
>       // Nope! Get the hell outta here.
>       self.Owner_Check_Lock.Unlock;
>       exit (NOT_OWNED);
>    end {if};
> 
>    // Now try locking the real mutex.
>    if pthread_mutex_lock (self...) = 0 then
>    begin
>       self.Count := self.Count + 1;
>       self.Owner_Check.Unlock;
>       exit (SUCCESS);
>    end {if};
> 
>    self.Owner_Check.Unlock;
>    exit (FAILURE);
> end {Mutex.Lock};
> 
> Something like that. Don't nail me on that, it's quite late and the heat 
> is still killing me. ;)

I knew it. Of course this is wrong. We can only lock the mutex once, 
because we assume it's non-recursive, that was the whole point. So we 
should *first* check the count and then may lock/unlock the mutex 
accordingly.


Vinzent.



More information about the fpc-pascal mailing list