Created
February 17, 2016 14:40
-
-
Save jonludlam/ded444babff59a833400 to your computer and use it in GitHub Desktop.
SMAPI parallel operations limiter
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
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