<br><br><div class="gmail_quote">On Wed, Oct 13, 2010 at 11:53 AM, Igor Stasenko <span dir="ltr">&lt;<a href="mailto:siguctua@gmail.com">siguctua@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
2010/10/13 Levente Uzonyi &lt;<a href="mailto:leves@elte.hu">leves@elte.hu</a>&gt;:<br>
<div><div></div><div class="h5">&gt; On Tue, 12 Oct 2010, Igor Stasenko wrote:<br>
&gt;<br>
&gt;&gt; On 12 October 2010 16:51, Levente Uzonyi &lt;<a href="mailto:leves@elte.hu">leves@elte.hu</a>&gt; wrote:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; On Tue, 12 Oct 2010, Igor Stasenko wrote:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; Hello, i just thought, that it would be cool to have a special bytecode,<br>
&gt;&gt;&gt;&gt; which guarantees atomicity for swapping values between two variables.<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; To swap two values, you usually do:<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; | var1 var2 temp |<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; temp := var1.<br>
&gt;&gt;&gt;&gt; var1 := var2.<br>
&gt;&gt;&gt;&gt; var2 := temp.<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; But since its non-atomic, a process can be interrupted and such<br>
&gt;&gt;&gt;&gt; operation<br>
&gt;&gt;&gt;&gt; is not thread-safe.<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; In order to make it thread safe, you must add even more boilerplate:<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; | var1 var2 temp |<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; semaphore critical: [<br>
&gt;&gt;&gt;&gt;  temp := var1.<br>
&gt;&gt;&gt;&gt;  var1 := var2.<br>
&gt;&gt;&gt;&gt;  var2 := temp.<br>
&gt;&gt;&gt;&gt; ]<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; An alternative solution:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; | a b |<br>
&gt;&gt;&gt; a := 1.<br>
&gt;&gt;&gt; b := 2.<br>
&gt;&gt;&gt; [<br>
&gt;&gt;&gt;        | tmp |<br>
&gt;&gt;&gt;        tmp := a.<br>
&gt;&gt;&gt;        a := b.<br>
&gt;&gt;&gt;        b := tmp ] valueUnpreemptively<br>
&gt;&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; Yeah, another boilerplate under the hood, also highly dependent from<br>
&gt;&gt; scheduling nuances :)<br>
&gt;<br>
&gt; I don&#39;t get the &quot;dependency from scheduling nuances part&quot;, but here&#39;s my<br>
&gt; idea:<br>
<br>
</div></div>i don&#39;t like the code, which assuming that scheduling works in some<br>
specific way,<br>
or some code can&#39;t be interrupted in the middle.<br>
<br>
See Semaphore&gt;&gt;critical: for excersise.<br>
<br>
| caught |<br>
        caught := false.<br>
        ^[<br>
                caught := true.<br>
                self wait.<br>
                mutuallyExcludedBlock value<br>
        ] ensure: [ caught ifTrue: [self signal] ]<br>
<br>
this code assuming that between<br>
                caught := true.<br>
and<br>
                self wait.<br>
<br>
no interrupt possible.<br>
But if it is, then the above implementation is incorrect.<br>
<div class="im"><br>
<br>
&gt;<br>
&gt; Add the compiler changes to support :=: as atomic swap. We don&#39;t really need<br>
&gt; a bytecode for now, since a sequence of assignments is currently atomic. So<br>
&gt; the compiler could compile :=: as three assignments using a hidden temporary<br>
&gt; variable. On other systems, :=: can be compiled differently.<br>
&gt;<br>
<br>
</div>For same reason, there is no any guarantees from VM side that three<br>
assignments in a row will be<br>
atomic: storing pointer could trigger a root check, and if roots table<br>
is full, it could trigger GC,<br>
and after GC, there is a finalization semaphore, which could switch an<br>
active process immediately.<br>
<br>
VM is evolving and subject to change. Having a clear rule which<br>
guarantees a specific behavior is far more<br>
beneficial comparing to intimate knowledge about how VM works (now).<br></blockquote><div><br></div><div>+1.  I think there&#39;s general agreement on this.  All this uninterruptibiity goes out of the window (as does Processor activeProcess) as soon as anyone does a truly concurrent implementation.</div>
<div><br></div><div>best</div><div>Eliot </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5"><br>
<br>
&gt;<br>
&gt; Levente<br>
&gt;<br>
&gt;&gt;<br>
&gt;&gt; valueUnpreemptively<br>
&gt;&gt;        &quot;Evaluate the receiver (block), without the possibility of<br>
&gt;&gt; preemption<br>
&gt;&gt; by higher priority processes. Use this facility VERY sparingly!&quot;<br>
&gt;&gt;        &quot;Think about using Block&gt;&gt;valueUninterruptably first, and think<br>
&gt;&gt; about<br>
&gt;&gt; using Semaphore&gt;&gt;critical: before that, and think about redesigning<br>
&gt;&gt; your application even before that!<br>
&gt;&gt;        After you&#39;ve done all that thinking, go right ahead and use it...&quot;<br>
&gt;&gt;        | activeProcess oldPriority result |<br>
&gt;&gt;        activeProcess := Processor activeProcess.<br>
&gt;&gt;        oldPriority := activeProcess priority.<br>
&gt;&gt;        activeProcess priority: Processor highestPriority.<br>
&gt;&gt;        result := self ensure: [activeProcess priority: oldPriority].<br>
&gt;&gt;        &quot;Yield after restoring priority to give the preempted processes a<br>
&gt;&gt; chance to run&quot;<br>
&gt;&gt;        Processor yield.<br>
&gt;&gt;        ^result<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Levente<br>
&gt;&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; --<br>
&gt;&gt; Best regards,<br>
&gt;&gt; Igor Stasenko AKA sig.<br>
&gt;&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
<br>
<br>
<br>
--<br>
Best regards,<br>
Igor Stasenko AKA sig.<br>
<br>
</div></div></blockquote></div><br>