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

Graeme Geldenhuys graeme at geldenhuys.co.uk
Thu Feb 7 12:27:07 CET 2013


Hi Jonas,

Interesting, there is no mention of those limitations in the FPC 2.6.0
documentation. I looked in the RTL docs. It this is a concern, it is
something that should be mentioned in the docs.

Do you know more specifically what platforms or CPU's are affected, so
Michael could update the docs accordingly.

Does this limitation apply to all InterlockedXXX() functions?

Regards,
  - Graeme -


On 2013-02-07 10:57, Jonas Maebe wrote:
>> ===> 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



More information about the fpc-devel mailing list