albertjan
albertjan

Reputation: 7817

Asynchronously wait for n things to happen

I'm looking for something like a Semaphore but that resolves after all the slots have been released.

Something like this:

use semaphore = new SemaphoreSlim(0,100)

anEvent.add(fun _ -> semaphore.Release(1) |> ignore);

async {
   do! thingThatCausesAnEventToFire100Times()
   //where 100 is the available slots instead of the timeout.
   let! thingsHappened = semaphore.WaitAsync(100) |> Async.AwaitTask
   thingsHappened |> should be True
}

Upvotes: 3

Views: 129

Answers (1)

Kevin
Kevin

Reputation: 2291

Sounds like a job for a MailboxProcessor. How about this:

type SemaphoreCommand =
  |Release
  |Wait of AsyncReplyChannel<unit>

let semaphore slots =
    Agent.Start
    <| fun inbox ->
        let rec loop c (w:AsyncReplyChannel<unit> list) =
          async {
            let! command = inbox.Receive()
            match command with
            |Release -> if (c + 1) = slots then w |> List.iter(fun t -> t.Reply()) 
                        else return! loop (c + 1) w
            |Wait a -> return! loop c (a::w)
        }
        loop 0 []

let slotWaiter = semaphore 100

//Events will fill up slots
Release |> slotWaiter.Post
Release |> slotWaiter.Post

async{
  //Wait for all slots to be filled
  do! slotWaiter.PostAndAsyncReply(fun t -> Wait t)
  //All slots filled - continue
}

I'm not handling the case where you may not have registered a AsyncReplyChannel by the time all the slots are filled or the reset once all slots are filled but this is fairly trivial and I will leave it as an exercise for the reader :)

Upvotes: 3

Related Questions