<div dir="ltr">Thank you very much for your help, I just updated the patches on the ticket.<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sun, Apr 21, 2013 at 10:50 AM, Edward Z. Yang <span dir="ltr">&lt;<a href="mailto:ezyang@cs.stanford.edu" target="_blank">ezyang@cs.stanford.edu</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">In your ticket, you mention this patch introduces a race condition.  One<br>
possible fix is to have addCFinalizerToWeak# check if the pointer is already<br>
dead, and just run the finalizer immediately if it is.  I think this<br>
preserves the semantics, but this needs to be checked closely.<br>
<br>
Edward<br>
<br>
Excerpts from Akio Takano&#39;s message of Fri Apr 19 02:58:<a href="tel:50%20-0700%202013" value="+15007002013">50 -0700 2013</a>:<br>
<div class="im">&gt; I removed the invariant by adding a new primop, addCFinalizerToWeak#. I<br>
&gt; opened a ticket for the issue.<br>
&gt;<br>
&gt; <a href="http://hackage.haskell.org/trac/ghc/ticket/7847" target="_blank">http://hackage.haskell.org/trac/ghc/ticket/7847</a><br>
&gt;<br>
&gt; - Akio<br>
&gt;<br>
&gt; On Thu, Mar 21, 2013 at 2:40 PM, Simon Marlow &lt;<a href="mailto:marlowsd@gmail.com">marlowsd@gmail.com</a>&gt; wrote:<br>
&gt;<br>
&gt; &gt; On 11/03/13 03:17, Akio Takano wrote:<br>
&gt; &gt;<br>
&gt; &gt;&gt; Hi,<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; I&#39;m working on implementing per-generation lists of weak pointers to<br>
&gt; &gt;&gt; speed up garbage collection in programs that allocate a lot of weak<br>
&gt; &gt;&gt; pointers. I have a patch [1] that validates and gives a 3x speed up on<br>
&gt; &gt;&gt; a benchmark. However I&#39;d like to ask for some advise before finishing<br>
&gt; &gt;&gt; and submitting the patch.<br>
&gt; &gt;&gt;<br>
</div>&gt; &gt;&gt; [1] <a href="https://github.com/takano-**akio/ghc/commit/**" target="_blank">https://github.com/takano-**akio/ghc/commit/**</a><br>
&gt; &gt;&gt; c7345c68eaa1e7f9572e693b5e352e**386df7d680&lt;<a href="https://github.com/takano-akio/ghc/commit/c7345c68eaa1e7f9572e693b5e352e386df7d680" target="_blank">https://github.com/takano-akio/ghc/commit/c7345c68eaa1e7f9572e693b5e352e386df7d680</a>&gt;<br>

<div class="im">&gt; &gt;&gt;<br>
&gt; &gt;&gt; The problem is that since my patch splits the weak pointer list<br>
&gt; &gt;&gt; between generations, it no longer maintains the right order of weak<br>
&gt; &gt;&gt; pointers. This could cause finalizers added with<br>
&gt; &gt;&gt; addForeignPtrFinalizer to run in the wrong order.<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; I can think of one way to fix it; to make sure that when a WEAK object<br>
&gt; &gt;&gt; gets promoted, it is always added to the front of the new list. So my<br>
&gt; &gt;&gt; questions are:<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; - Would it be a correct fix?<br>
&gt; &gt;&gt; - If so, is it an acceptable fix? For example, is it too fragile a<br>
&gt; &gt;&gt; reasoning to rely on?<br>
&gt; &gt;&gt;<br>
&gt; &gt;<br>
&gt; &gt; I don&#39;t like the way that we rely on the ordering of the weak pointer list<br>
&gt; &gt; right now.  I think this invariant arose accidentally when the support for<br>
&gt; &gt; C finalizers was added.  It was wrong for some time, see:<br>
&gt; &gt;<br>
</div>&gt; &gt; <a href="http://hackage.haskell.org/**trac/ghc/ticket/7160" target="_blank">http://hackage.haskell.org/**trac/ghc/ticket/7160</a>&lt;<a href="http://hackage.haskell.org/trac/ghc/ticket/7160" target="_blank">http://hackage.haskell.org/trac/ghc/ticket/7160</a>&gt;<br>

<div class="HOEnZb"><div class="h5">&gt; &gt;<br>
&gt; &gt; and as per my comments in that commit log, I think we should do it<br>
&gt; &gt; differently.  I don&#39;t know how hard that would be though.<br>
&gt; &gt;<br>
&gt; &gt; Incidentally, I implemented per-generation weak pointers in the local-gc<br>
&gt; &gt; branch, but didn&#39;t get around to porting it back over into the mainline (I<br>
&gt; &gt; still have a ToDo for that).  My version probably has the ordering bug, but<br>
&gt; &gt; you could always look at the branch to see how my approach compares to<br>
&gt; &gt; yours.<br>
&gt; &gt;<br>
&gt; &gt; Cheers,<br>
&gt; &gt;         Simon<br>
&gt; &gt;<br>
&gt; &gt;<br>
</div></div></blockquote></div><br></div>