<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">Once upon a time, on 02/07/2013 11:57
AM to be precise, Jonas Maebe said:<br>
</div>
<blockquote
cite="mid:0880D491-6472-4859-ABA8-A2EAFEB4C379@elis.ugent.be"
type="cite"><br>
<div>
<div>On 06 Feb 2013, at 23:16, Ewald wrote:</div>
<br class="Apple-interchange-newline">
<blockquote type="cite"><span class="Apple-style-span"
style="border-collapse: separate; color: rgb(0, 0, 0);
font-family: Monaco; font-style: normal; font-variant:
normal; font-weight: normal; letter-spacing: normal;
line-height: normal; orphans: 2; text-align: -webkit-auto;
text-indent: 0px; text-transform: none; white-space: normal;
widows: 2; word-spacing: 0px;
-webkit-border-horizontal-spacing: 0px;
-webkit-border-vertical-spacing: 0px;
-webkit-text-decorations-in-effect: none;
-webkit-text-size-adjust: auto; -webkit-text-stroke-width:
0px; font-size: medium; "><span class="Apple-style-span"
style="font-family: monospace; ">Concerning the locking
mechanism, you can uses mutex(en/es/ii) or you<br>
can do this by a busy waiting loop with an integer (of
course there are<br>
other possibilities). To elaborate on the latter a bit
more:<br>
<br>
* TLockType = Integer;<br>
* Initialization: `Lock:= 0;`<br>
* Lock & unlock:<br>
<br>
===> Code Begin <===<br>
<br>
Procedure WaitLockVar(var aLock: Integer);<br>
Begin<br>
Repeat<br>
Until InterLockedCompareExchange(aLock, 1, 0) = 0;<br>
End;<br>
<br>
Procedure UnlockVar(var aLock: Integer);<br>
Begin<br>
InterlockedExchange(aLock, 0);<br>
End;<br>
<br>
===> Code End <===<br>
<br>
This last code is tested and works.<br>
</span></span></blockquote>
</div>
<div><br>
</div>
<div>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.</div>
</blockquote>
Well, I always thought that the InterLoackedCompareExchange boiles
down to [**]<br>
.Lock CMPXCHG<br>
<br>
Or something quite like that. The `.Lock` there is the important
part since this does insure a memory barier. Then the only problem
would be the absence of the keyword `volatile` to ensure the integer
is always in the same location but I've read somewhere, long ago,
that fpc doesn't perform such optimizations.<br>
<br>
So I don't see the issue really.<br>
<br>
[**] I make this assumption because the documentation says it is
done in a thread safe way (reference:
<a class="moz-txt-link-freetext" href="http://www.freepascal.org/docs-html/rtl/system/interlockedcompareexchange.html">http://www.freepascal.org/docs-html/rtl/system/interlockedcompareexchange.html</a>),
and I don't see what other things can be made thread safe about this
function except for a memory barrier.<br>
<pre class="moz-signature" cols="72">--
Ewald
</pre>
</body>
</html>