[Haskell-cafe] What does unpacking an MVar really mean?

Leon Smith leon.p.smith at gmail.com
Tue Jul 31 06:25:51 CEST 2012

I admit I don't know exactly how MVars are implemented,  but given that
they can be aliased and have indefinite extent,   I would think that they
look something vaguely like a  cdatatype ** var,  basically a pointer to an
MVar (which is itself a pointer,  modulo some other things such as a thread

And,  I would think that "unpacking" such an structure would basically be
eliminating one layer of indirection,  so it would then look vague like a
cdatatype * var.    But again,  given aliasing and indefinite extent, this
would seem to be a difficult thing to do.

Actually this isn't too difficult if an MVar only exists in a single
unpacked structure:   other references to the MVar can simply be pointers
into the structure.   But the case where an MVar is unpacked into two
different structures suggests that,  at least in some cases,  an "unpacked"
MVar is still a cdatatype ** var;

So, is my understanding more or less correct?  Does anybody have a good,
succinct explanation of how MVars are implemented,  and how they are

One final question,   assuming that unpacking an MVar really does eliminate
a layer of indirection,  and that other references to that MVar are simply
pointers into a larger structure,   what happens to that larger structure
when there are no more references to it (but still some references to the
MVar?)    Given the complications that must arise out of a doubly
"unpacked" MVar,  I'm going to guess that the larger structure does get
garbage collected in this case,  and that the MVar becomes dislodged from
this structure.   Would that MVar then be placed directly inside another
unpacked reference, if one is available?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120731/c4a9a8f7/attachment-0001.htm>

More information about the Haskell-Cafe mailing list