Skip to content
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

Fix mapPipelinedJob #215

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions Libs/Hopac/Stream.fs
Original file line number Diff line number Diff line change
Expand Up @@ -530,17 +530,17 @@ module Stream =
mapJob x2yJ xs
else
delay <| fun () ->
let inCh, outCh = Ch(), Ch()
let inCh, outCh, closeCh = Ch(), Ch(), Ch()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be IVar for closing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will fix it, but a little later

let mutable usage = 0
let mutable closing = false

let rec loop() =
Alt.choose [
outCh ^-> function
| Choice1Of2 y -> usage <- usage - 1
Cons (y, loop ())
| Choice2Of2 e -> raise e
(if not closing && usage < degree then

if usage < degree then
inCh ^=> fun x ->
usage <- usage + 1
Job.tryInDelay
Expand All @@ -549,15 +549,16 @@ module Stream =
(Choice2Of2 >> Ch.give outCh)
|> Job.queue
>>= loop
else Alt.never())
(if closing && usage = 0
then Alt.always Nil
else Alt.never())
else Alt.never()

if usage = 0 then
closeCh ^=> fun () -> Alt.always Nil
else Alt.never()
] |> memo

Job.tryIn
(xs |> iterJob (Ch.give inCh))
(fun () -> closing <- true; Job.unit() )
(Ch.give closeCh)
(fun e -> outCh *<- Choice2Of2 e)
|> Job.start >>= loop

Expand Down
4 changes: 3 additions & 1 deletion Tests/AdHocTests/StreamTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ let run () =
timeOutMillis (Math.Abs (x % 10)) >>- fun () ->
Interlocked.Decrement n |> ignore
x
|> Stream.toList |> run = xs
|> Stream.toList
|> run
|> List.sort = List.sort xs
if !m <> 10
then printfn "Not OK? Effective degree only %d with mapPipelinedJob %d." !m 10
else printfn "OK, reached effective degree %d with mapPipelinedJob." !m
Expand Down