<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8">
</head>
<body>
<div style="font-family:sans-serif"><div style="white-space:normal">
<p dir="auto">Hello,</p>

<p dir="auto">I’m the author of the <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">MVar</code> implementation in Cats-Effect.</p>

</div>
<div style="white-space:normal"><blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px"><p dir="auto">Recently I had an interesting discussion on MVars with cats-effect library<br>
designers. Cats-effect brings MVar synchronization primitive along with<br>
other IO stuff to the Scala programming language. I tried to persuade them<br>
to include some Control.Concurrent.MVar’s functions to  the library but has<br>
failed completely. Moreover, now I think that MVar is a poor choice for<br>
basic synchronization primitive.</p>
</blockquote></div>
<div style="white-space:normal">

<p dir="auto">I believe <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">MVar</code> is a superb choice for synchronisation, because it behaves like a blocking queue, which in computer science is a pretty fundamental tool.</p>

<p dir="auto">It is true that in Cats-Effect an <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">MVar</code> might not be a primitive, but as I explained in that thread, on top of the JVM the real primitives are the low level building blocks like <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">AtomicReference</code>.</p>

</div>
<div style="white-space:normal"><blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px"><p dir="auto">1. It’s complex. Each MVar has 2 state transitions, each may block.</p>
</blockquote></div>
<div style="white-space:normal">

<p dir="auto">Blocking is a feature. If you have to build that logic via <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">Ref</code> (<code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">IORef</code>), you’d have to model the state machine by yourself and that’s complexity being pushed to the user.</p>

</div>
<div style="white-space:normal"><blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px"><p dir="auto">2. It does not play well in presence of asynchronous exceptions. More<br>
specifically, `take` and `put` operations should be balanced (each `take`<br>
must be followed by `put`)</p>
</blockquote></div>
<div style="white-space:normal">

<p dir="auto">The <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">take</code> followed by a <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">put</code> rule is only for your “modify” use-case. </p>

<p dir="auto">The problem is that a <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">modify</code> function that’s described via <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">take</code> + <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">put</code> is not an atomic operation and this is a problem, but only if any of the actors accessing the <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">MVar</code> are doing so in a different order.</p>

<p dir="auto">This isn’t a problem for other use-cases tough.</p>

</div>
<div style="white-space:normal"><blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px"><p dir="auto">this force programmer to mask asynchronous<br>
exceptions during the MVar acquisition and since `take` function may block,<br>
this will delay task cancelation.</p>
</blockquote></div>
<div style="white-space:normal">

<p dir="auto">I don’t have much experience with Haskell’s async exceptions, but if you mean that <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">take</code> must be executed as <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">uncancelable</code>, via <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">bracket</code>, then this is a problem that we can fix in Cats-Effect 2.x, as mentioned in that thread.</p>

</div>
<div style="white-space:normal"><blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px"><p dir="auto">What could be the sensible alternative? Guy from the cats-effect suggested<br>
me IVar + atomic reference (IORef). This pattern separates concern of<br>
blocking (synchronization) from the atomic mutation. So everything can be<br>
represented as atomic reference with IVar inside. Just look at this<br>
beautiful mutex implementation<br>
<a href="https://github.com/ovotech/fs2-kafka/blob/master/src/main/scala/fs2/kafka/internal/Synchronized.scala" style="color:#777">https://github.com/ovotech/fs2-kafka/blob/master/src/main/scala/fs2/kafka/internal/Synchronized.scala</a></p>
</blockquote></div>
<div style="white-space:normal">

<p dir="auto">As I said above, most things on top of the JVM can be implemented via an <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">AtomicReference</code> due to its memory model and this is no exception.</p>

<p dir="auto">But it’s not elegant, or simple ;-)</p>

</div>
<div style="white-space:normal"><blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px"><p dir="auto">(By ”beautiful” I mean approach itself of course, but not the Scala’s<br>
syntax. Scala is one of most ugliest girls after C++ I was forced to date<br>
with by my employer for money. Thankfully he didn’t force me to do the same<br>
things with her grandma Java).</p>
</blockquote></div>
<div style="white-space:normal">

<p dir="auto">That’s unnecessary and TBH a turnoff.</p>

<p dir="auto">Cheers,</p>

<p dir="auto">-- <br>
Alexandru Nedelcu<br>
<a href="https://alexn.org" style="color:#3983C4">https://alexn.org</a></p>
</div>
</div>
</body>
</html>