Skip to content

Commit

Permalink
Add degenerate implementation of MVars.
Browse files Browse the repository at this point in the history
This implementation will error out whenever a code path blocks on an MVar,
as the non-concurrent environment guarantees that no other thread will come
along to unblock the MVar.

This commit fixes half of #11.
  • Loading branch information
valderman committed Aug 31, 2012
1 parent 43149ab commit cbc61e3
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 8 deletions.
1 change: 1 addition & 0 deletions haste.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Data-Files:
stdlib.js
eval.js
eval-trampoline.js
MVar.js

Library
Hs-Source-Dirs: src
Expand Down
56 changes: 56 additions & 0 deletions lib/MVar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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(st) {
return [1, st, {empty: true}];
}

function tryTakeMVar(mv, st) {
if(mv.empty) {
return [1, st, 0, undefined];
} else {
mv.empty = true;
mv.x = null;
return [1, st, 1, mv.x];
}
}

function takeMVar(mv, st) {
if(mv.empty) {
// TODO: real BlockedOnDeadMVar exception, perhaps?
err("Attempted to take empty MVar!");
}
mv.empty = true;
mv.x = null;
return [1,st,mv.x];
}

function putMVar(mv, val, st) {
if(!mv.empty) {
// TODO: real BlockedOnDeadMVar exception, perhaps?
err("Attempted to put full MVar!");
}
mv.empty = false;
mv.x = val;
return [1,st];
}

function tryPutMVar(mv, val, st) {
if(!mv.empty) {
return [1, st, 0];
} else {
mv.empty = false;
mv.x = val;
return [1, st, 1];
}
}

function sameMVar(a, b) {
return (a == b);
}

function isEmptyMVar(mv, st) {
return [1, st, mv.empty ? 1 : 0];
}
2 changes: 1 addition & 1 deletion src/BootVer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ data RebuildInfo = Dont | Libs | Everything
bootVer :: BootVer
bootVer = BootVer {
codegenVer = 1,
libVer = 1
libVer = 2
}

-- | Returns which parts of Haste need rebooting. A change in the boot file
Expand Down
13 changes: 6 additions & 7 deletions src/CodeGen/Javascript/Config.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module CodeGen.Javascript.Config (
Config (..), AppStart, defConfig, stdRtsLib, stdJSLib, startASAP,
Config (..), AppStart, defConfig, stdJSLibs, startASAP,
startOnLoadComplete, appName, sysLibPath, hastePath, evalTrampolining,
eval, fastMultiply, safeMultiply) where
import CodeGen.Javascript.PrettyM (PrettyOpts, compact)
Expand All @@ -12,11 +12,10 @@ import Paths_haste_compiler (getDataFileName)

type AppStart = String -> String

stdRtsLib :: FilePath
stdRtsLib = unsafePerformIO $ getDataFileName "rts.js"

stdJSLib :: FilePath
stdJSLib = unsafePerformIO $ getDataFileName "stdlib.js"
stdJSLibs :: [FilePath]
stdJSLibs = unsafePerformIO $ mapM getDataFileName [
"rts.js", "stdlib.js", "MVar.js"
]

-- | Name of the application; decides which directories to keep app specific
-- data in.
Expand Down Expand Up @@ -102,7 +101,7 @@ data Config = Config {
-- | Default compiler configuration.
defConfig :: Config
defConfig = Config {
rtsLibs = [stdRtsLib,stdJSLib],
rtsLibs = stdJSLibs,
evalLib = eval,
libPath = sysLibPath,
targetLibPath = ".",
Expand Down
9 changes: 9 additions & 0 deletions src/CodeGen/Javascript/PrimOps.hs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ genOp cfg op xs =
UnsafeFreezeByteArrayOp -> Right $ Array $ [litN 1,xs!!1,xs!!0]
ByteArrayContents_Char -> Right $ head xs

-- MVars
NewMVarOp -> call "newMVar"
TakeMVarOp -> call "takeMVar"
TryTakeMVarOp -> call "tryTakeMVar"
PutMVarOp -> call "putMVar"
TryPutMVarOp -> call "tryPutMVar"
SameMVarOp -> call "sameMVar"
IsEmptyMVarOp -> call "isEmptyMVar"

-- Misc. ops
-- Get the data constructor tag from a value.
DataToTagOp -> Right $ Index (head xs) (xs !! 0)
Expand Down

0 comments on commit cbc61e3

Please sign in to comment.