Skip to content

Instantly share code, notes, and snippets.

@jonludlam
Created February 17, 2016 14:40
Show Gist options
  • Save jonludlam/ded444babff59a833400 to your computer and use it in GitHub Desktop.
Save jonludlam/ded444babff59a833400 to your computer and use it in GitHub Desktop.
SMAPI parallel operations limiter
module Smapi_limiter = struct
let limit = ref 10
let m = Mutex.create ()
let c = Condition.create ()
let set_limit n = limit := n
let get_token () =
Mutex.execute m (fun () ->
while !limit=0 do
Condition.wait c m
done;
limit := !limit - 1)
let release_token () =
Mutex.execute m (fun () ->
limit := !limit + 1;
Condition.signal c)
let with_limit f =
get_token ();
Pervasiveext.finally f release_token
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment