<div class="gmail_quote"><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>GHC already has a CAS primitive on MutVar#, it just needs to be extended to MutableArray# and MutableByteArray# (at all of the bit-widths the CAS instruction would support, e.g. see readWordXxArray# in <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/ghc-prim-0.2.0.0/GHC-Prim.html" target="_blank">http://www.haskell.org/ghc/docs/latest/html/libraries/ghc-prim-0.2.0.0/GHC-Prim.html</a>). The implementation should be almost identical to casMutVar#.</div>


<div>In particular I think you need:</div><div>    casMutArray# :: MutableArray# s a -&gt; Int# -&gt; a -&gt; a -&gt; State# s -&gt; (# State# s, Int#, a #)</div><div>    casWord16MutByteArray :: MutableByteArray# s -&gt; Int# -&gt; Word# -&gt; Word# -&gt; State# s -&gt; (# State# s, Int#, Word#)</div>


</blockquote><div><br></div><div>FYI, I started working on adding these.  I&#39;m hoping to have it working in GHC HEAD for any students who need to use it.  To my knowledge the only two patches required to implement casMutVar# were these two (plus the preexisting cas() definition in SMP.h):</div>


<div><br></div><div>    <a href="https://github.com/ghc/ghc/commit/521b792553bacbdb0eec138b150ab0626ea6f36b" target="_blank">https://github.com/ghc/ghc/commit/521b792553bacbdb0eec138b150ab0626ea6f36b</a></div><div>    <a href="https://github.com/ghc/ghc/commit/606f6e1cfcb2e79abaadcc5ed643817d2a4585d8" target="_blank">https://github.com/ghc/ghc/commit/606f6e1cfcb2e79abaadcc5ed643817d2a4585d8</a></div>


<div><br></div><div>The latter is a bugfix to the former.</div><div><br></div><div>Florian, your proposal looks good to me (<a href="http://www.google-melange.com/gsoc/proposal/review/google/gsoc2012/florianhartwig/1" target="_blank">http://www.google-melange.com/gsoc/proposal/review/google/gsoc2012/florianhartwig/1</a>).  You touched on the major things we need to know.</div>


<div><br></div><div>I just read in your proposal that you started looking into the casMutArray# issue as well.  How far have you gotten with that?  Do you want to work on this together a bit?</div><div><br></div><div>I&#39;ve got an implementation of a casArray# primop that passes a basic test, but I&#39;m not sure if the GC write barrier is correct:</div>

<div><br></div><div>    <a href="https://github.com/rrnewton/ghc/commit/18ed460be111b47a759486677960093d71eef386">https://github.com/rrnewton/ghc/commit/18ed460be111b47a759486677960093d71eef386</a></div><div><br></div><div>

The ByteArray versions will be more annoying, requiring more variations, but they are also less essential, because the user can always use ForeignPtr and bits-atomic in this case, and I believe for our concurrent data structures we want to store arbitrary pointers (hence casArray#).</div>

<div><br></div><div>Cheers,</div>
<div>  -Ryan</div><div><br></div></div>