[fpc-devel] Why FreeBSD sem_init() works different to Linux?

Jonas Maebe jonas.maebe at elis.ugent.be
Thu Feb 7 11:57:27 CET 2013


On 06 Feb 2013, at 23:16, Ewald wrote:

> Concerning the locking mechanism, you can uses mutex(en/es/ii) or you
> can do this by a busy waiting loop with an integer (of course there  
> are
> other possibilities). To elaborate on the latter a bit more:
>
> *  TLockType = Integer;
> *  Initialization: `Lock:= 0;`
> *  Lock & unlock:
>
> ===> Code Begin <===
>
> Procedure WaitLockVar(var aLock: Integer);
> Begin
>    Repeat
>    Until InterLockedCompareExchange(aLock, 1, 0) = 0;
> End;
>
> Procedure UnlockVar(var aLock: Integer);
> Begin
>    InterlockedExchange(aLock, 0);
> End;
>
> ===> Code End <===
>
> This last code is tested and works.


It only works on some platforms (and even there it may change  
depending on which exact processor you are using). InterlockedExchange  
does not guarantee any kind of memory barrier, and hence you will get  
occasional data races on platforms with weakly consistent memory  
models. You have to add memory barriers.


Jonas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20130207/a25b2b45/attachment.html>


More information about the fpc-devel mailing list