https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112016-01-25T19:50:38ZRuby Issue Tracking SystemRuby master - Feature #12021: Final instance variableshttps://bugs.ruby-lang.org/issues/12021?journal_id=566572016-01-25T19:50:38Zpitr.ch (Petr Chalupa)
<ul></ul><p>The above declares the final variables explicitly, there is also an alternative approach to threat all instance variable assignments in constructor as a final variable. Therefore protecting them implicitly, ensuring their visibility after the object is constructed. If one of the instance variables is reassigned later it will loose any visibility guaranties. This has to be explored more deeply though. The actual cost of having the protection always on implicitly has to be determined.</p> Ruby master - Feature #12021: Final instance variableshttps://bugs.ruby-lang.org/issues/12021?journal_id=566712016-01-25T22:19:48ZEregon (Benoit Daloze)
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-1 priority-4 priority-default" href="/issues/12019">Feature #12019</a>: Better low-level support for writing concurrent libraries</i> added</li></ul> Ruby master - Feature #12021: Final instance variableshttps://bugs.ruby-lang.org/issues/12021?journal_id=566852016-01-26T01:21:39Znormalperson (Eric Wong)normalperson@yhbt.net
<ul></ul><p><a href="mailto:email@pitr.ch" class="email">email@pitr.ch</a> wrote:</p>
<blockquote>
<p><a href="https://docs.google.com/document/d/1c07qfDArx0bhK9sMr24elaIUdOGudiqBhTIRALEbrYY/edit#" class="external">https://docs.google.com/document/d/1c07qfDArx0bhK9sMr24elaIUdOGudiqBhTIRALEbrYY/edit#</a>.</p>
</blockquote>
<p>Also inaccessible without JavaScript.</p> Ruby master - Feature #12021: Final instance variableshttps://bugs.ruby-lang.org/issues/12021?journal_id=567062016-01-26T15:23:44Zpitr.ch (Petr Chalupa)
<ul></ul><p>A version accessible without JS is here <a href="https://docs.google.com/document/d/1c07qfDArx0bhK9sMr24elaIUdOGudiqBhTIRALEbrYY/pub" class="external">https://docs.google.com/document/d/1c07qfDArx0bhK9sMr24elaIUdOGudiqBhTIRALEbrYY/pub</a>. Sorry for not thinking about that.</p> Ruby master - Feature #12021: Final instance variableshttps://bugs.ruby-lang.org/issues/12021?journal_id=584512016-05-03T09:13:32ZEregon (Benoit Daloze)
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-1 priority-4 priority-default" href="/issues/12334">Feature #12334</a>: Final/Readonly Support for Fields / Instance Variables</i> added</li></ul> Ruby master - Feature #12021: Final instance variableshttps://bugs.ruby-lang.org/issues/12021?journal_id=587512016-05-19T21:02:32Zpitr.ch (Petr Chalupa)
<ul></ul><p>Consider following code:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">QUEUE</span> <span class="o">=</span> <span class="no">Queue</span><span class="p">.</span><span class="nf">new</span>
<span class="no">WORKER</span> <span class="o">=</span> <span class="no">Thread</span><span class="p">.</span><span class="nf">new</span> <span class="p">{</span> <span class="kp">loop</span> <span class="p">{</span> <span class="no">QUEUE</span><span class="p">.</span><span class="nf">pop</span><span class="p">.</span><span class="nf">call</span> <span class="p">}</span> <span class="p">}</span>
<span class="k">def</span> <span class="nf">async</span><span class="p">(</span><span class="o">&</span><span class="n">job</span><span class="p">)</span>
<span class="no">QUEUE</span><span class="p">.</span><span class="nf">push</span> <span class="n">job</span>
<span class="kp">nil</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">the_example</span>
<span class="n">local_var</span> <span class="o">=</span> <span class="ss">:value</span>
<span class="n">async</span> <span class="p">{</span> <span class="nb">p</span> <span class="n">local_var</span> <span class="p">}</span>
<span class="k">end</span>
</code></pre>
<p>Currently by RMM rules and on current implementations there is no documented<br>
guarantee that the job executed asynchronously will print the :value. (JRuby<br>
actually documents and warns that it may not print :value). (It safe on MRI<br>
because of undocumented behavior of GIL.)</p>
<p>This behavior is quite inconvenient. It requires that APIs for threads, fibers<br>
and other concurrent libraries (e.g. async, promises) need to pass the values<br>
to the block through the factory methods as follows and internally ensure<br>
visibility.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Thread</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="n">local_var</span><span class="p">)</span> <span class="p">{</span> <span class="o">|</span><span class="n">v</span><span class="o">|</span> <span class="nb">p</span> <span class="n">v</span> <span class="p">}</span>
<span class="n">async</span><span class="p">(</span><span class="n">local_var</span><span class="p">)</span> <span class="p">{</span> <span class="o">|</span><span class="n">v</span><span class="o">|</span> <span class="nb">p</span> <span class="n">v</span> <span class="p">}</span>
<span class="c1"># etc.</span>
</code></pre>
<p>Using this proposal also for Proc and its hidden field which holds the captured<br>
scope could be used to fix this issue.</p>
<p>If the hidden field holding scope of Proc is classified as final then it<br>
implies that any of the local variable assignments before the Proc construction<br>
cannot be reordered with publishing of the proc instance. Therefore when the<br>
proc is executed on a different thread it's guaranteed that the values in<br>
captured local variables assigned before the proc construction will be visible.</p>
<p>Therefore following would be correct and safe:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">local_var</span> <span class="o">=</span> <span class="ss">:value</span>
<span class="no">Thread</span><span class="p">.</span><span class="nf">new</span> <span class="p">{</span> <span class="nb">p</span> <span class="n">local_var</span> <span class="p">}</span>
<span class="n">async</span> <span class="p">{</span> <span class="nb">p</span> <span class="n">local_var</span> <span class="p">}</span>
</code></pre>
<p>In relation to discussion happening in <a href="https://bugs.ruby-lang.org/issues/12020" class="external">https://bugs.ruby-lang.org/issues/12020</a>,<br>
where is being discussed distinction between low and high level documentation:<br>
The explanation above would fall into the low-level part, high-level<br>
documentation for users of Proc would simply say that: "When Proc is created it<br>
captures local variables and theirs latest values in the scope. (Subsequent<br>
updates to local variables may or may not be visible to the Proc's body.)". The<br>
visibility of values assigned before the Proc instance creation is currently<br>
expected behavior due to GIL undocumented behavior.</p> Ruby master - Feature #12021: Final instance variableshttps://bugs.ruby-lang.org/issues/12021?journal_id=955942021-12-23T23:43:59Zhsbt (Hiroshi SHIBATA)hsbt@ruby-lang.org
<ul><li><strong>Project</strong> changed from <i>14</i> to <i>Ruby master</i></li></ul>