-
Notifications
You must be signed in to change notification settings - Fork 68
Implement MonadPar and MonadRace #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/.* | ||
!/.gitignore | ||
!/.travis.yml | ||
/output/ | ||
/node_modules/ | ||
/bower_components/ | ||
/node_modules/ | ||
/output/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
language: node_js | ||
sudo: false | ||
node_js: 5 | ||
dist: trusty | ||
sudo: required | ||
node_js: 6 | ||
install: | ||
- npm install -g bower | ||
- npm install | ||
- npm install bower -g | ||
- bower install | ||
script: | ||
- npm test | ||
- bower install --production | ||
- npm run -s build | ||
- bower install | ||
- npm -s test | ||
after_success: | ||
- >- | ||
test $TRAVIS_TAG && | ||
node_modules/.bin/psc-publish > .pursuit.json && | ||
curl -X POST http://pursuit.purescript.org/packages \ | ||
-d @.pursuit.json \ | ||
-H 'Accept: application/json' \ | ||
-H "Authorization: token ${GITHUB_TOKEN}" | ||
echo $GITHUB_TOKEN | pulp login && | ||
echo y | pulp publish --no-push |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ module Control.Monad.Aff | |
where | ||
|
||
import Prelude | ||
|
||
import Control.Alt (class Alt) | ||
import Control.Alternative (class Alternative) | ||
import Control.Monad.Cont.Class (class MonadCont) | ||
|
@@ -30,7 +31,9 @@ import Control.Monad.Eff.Exception (Error, EXCEPTION, throwException, error) | |
import Control.Monad.Error.Class (class MonadError, throwError) | ||
import Control.Monad.Rec.Class (class MonadRec) | ||
import Control.MonadPlus (class MonadZero, class MonadPlus) | ||
import Control.Parallel.Class (class MonadRace, class MonadPar) | ||
import Control.Plus (class Plus) | ||
|
||
import Data.Either (Either(..), either, isLeft) | ||
import Data.Foldable (class Foldable, foldl) | ||
import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3) | ||
|
@@ -202,6 +205,56 @@ instance semigroupCanceler :: Semigroup (Canceler e) where | |
instance monoidCanceler :: Monoid (Canceler e) where | ||
mempty = Canceler (const (pure true)) | ||
|
||
instance monadParAff :: MonadPar (Aff e) where | ||
par f ma mb = do | ||
va <- _makeVar nonCanceler | ||
vb <- _makeVar nonCanceler | ||
c1 <- forkAff (putOrKill va =<< attempt ma) | ||
c2 <- forkAff (putOrKill vb =<< attempt mb) | ||
f <$> (takeVar va) <*> (takeVar vb) | ||
where | ||
putOrKill :: forall a. AVar a -> Either Error a -> Aff e Unit | ||
putOrKill v = either (killVar v) (putVar v) | ||
|
||
instance monadRaceAff :: MonadRace (Aff e) where | ||
stall = throwError $ error "Stalled" | ||
race a1 a2 = do | ||
va <- _makeVar nonCanceler -- the `a` value | ||
ve <- _makeVar nonCanceler -- the error count (starts at 0) | ||
putVar ve 0 | ||
c1 <- forkAff $ either (maybeKill va ve) (putVar va) =<< attempt a1 | ||
c2 <- forkAff $ either (maybeKill va ve) (putVar va) =<< attempt a2 | ||
takeVar va `cancelWith` (c1 <> c2) | ||
where | ||
maybeKill :: forall a. AVar a -> AVar Int -> Error -> Aff e Unit | ||
maybeKill va ve err = do | ||
e <- takeVar ve | ||
if e == 1 then killVar va err else pure unit | ||
putVar ve (e + 1) | ||
|
||
-------------------------------- | ||
|
||
foreign import data AVar :: * -> * | ||
|
||
takeVar :: forall e a. AVar a -> Aff e a | ||
takeVar q = runFn2 _takeVar nonCanceler q | ||
|
||
putVar :: forall e a. AVar a -> a -> Aff e Unit | ||
putVar q a = runFn3 _putVar nonCanceler q a | ||
|
||
killVar :: forall e a. AVar a -> Error -> Aff e Unit | ||
killVar q e = runFn3 _killVar nonCanceler q e | ||
|
||
foreign import _makeVar :: forall e a. Canceler e -> Aff e (AVar a) | ||
|
||
foreign import _takeVar :: forall e a. Fn2 (Canceler e) (AVar a) (Aff e a) | ||
|
||
foreign import _putVar :: forall e a. Fn3 (Canceler e) (AVar a) a (Aff e Unit) | ||
|
||
foreign import _killVar :: forall e a. Fn3 (Canceler e) (AVar a) Error (Aff e Unit) | ||
|
||
-------------------------------- | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can indeed pull in cross-FFI-module dependencies, but we still need to replicate some of the |
||
|
||
foreign import _cancelWith :: forall e a. Fn3 (Canceler e) (Aff e a) (Canceler e) (Aff e a) | ||
|
||
foreign import _setTimeout :: forall e a. Fn3 (Canceler e) Int (Aff e a) (Aff e a) | ||
|
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this break if you are using psc-bundle without browserify?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think
pulp test
uses browserify, and it succeeds there, so no, I think it's ok. I was concerned about that too... I'm still a little suspicious, but it seemed to work for the cases I tested. I'll try withpsc-bundle
directly to ensure there isn't some magic inpulp
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
pulp test
just runs it in node (no bundling), so it would work. But I don't think psc-bundle will follow this reference, and you'll end up with a strayrequire
for it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, it doesn't exactly work. It works in node if the planets align correctly, as the
require
is resolved by node instead... but the bundle isn't browser-safe at that point, so yeah, this probably isn't going to work.