// MVar implementation.
// Since Haste isn't concurrent, takeMVar and putMVar don't block on empty
// and full MVars respectively, but terminate the program since they would
// otherwise be blocking forever.
function newMVar() {
return ({empty: true});
}
function tryTakeMVar(mv) {
if(mv.empty) {
return {_:0, a:0, b:undefined};
} else {
var val = mv.x;
mv.empty = true;
mv.x = null;
return {_:0, a:1, b:val};
}
}
function takeMVar(mv) {
if(mv.empty) {
// TODO: real BlockedOnDeadMVar exception, perhaps?
err("Attempted to take empty MVar!");
}
var val = mv.x;
mv.empty = true;
mv.x = null;
return val;
}
function putMVar(mv, val) {
if(!mv.empty) {
// TODO: real BlockedOnDeadMVar exception, perhaps?
err("Attempted to put full MVar!");
}
mv.empty = false;
mv.x = val;
}
function tryPutMVar(mv, val) {
if(!mv.empty) {
return 0;
} else {
mv.empty = false;
mv.x = val;
return 1;
}
}
function sameMVar(a, b) {
return (a == b);
}
function isEmptyMVar(mv) {
return mv.empty ? 1 : 0;
}