Created
June 21, 2018 20:58
-
-
Save eulerfx/96d0a09e33cc100aa5792e9481805134 to your computer and use it in GitHub Desktop.
F# Async Race
This file contains 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
let race (a:Async<'a>) (b:Async<'a>) : Async<'a * Async<'a>> = async { | |
return! | |
Async.FromContinuations <| fun (ok,err,cnc) -> | |
let state = ref 0 | |
let iv = new TaskCompletionSource<_>() | |
let ok a = | |
if (Interlocked.CompareExchange(state, 1, 0) = 0) then | |
ok (a, iv.Task |> Async.AwaitTask) | |
else | |
iv.SetResult a | |
let err (ex:exn) = | |
if (Interlocked.CompareExchange(state, 1, 0) = 0) then err ex | |
else iv.SetException ex | |
let cnc ex = | |
if (Interlocked.CompareExchange(state, 1, 0) = 0) then cnc ex | |
else iv.SetCanceled () | |
Async.StartThreadPoolWithContinuations (a, ok, err, cnc) | |
Async.StartThreadPoolWithContinuations (b, ok, err, cnc) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment