File tree Expand file tree Collapse file tree 2 files changed +20
-6
lines changed Expand file tree Collapse file tree 2 files changed +20
-6
lines changed Original file line number Diff line number Diff line change @@ -12,20 +12,20 @@ type 'a t = {
1212let create ?(clear = ignore) ~mk_item ?(max_size = 512 ) () : _ t =
1313 { mk_item; clear; max_size; items = A. make Nil }
1414
15- let rec acquire_ self =
15+ let rec acquire self =
1616 match A. get self.items with
1717 | Nil -> self.mk_item ()
1818 | Cons (_ , x , tl ) as l ->
1919 if A. compare_and_set self.items l tl then
2020 x
2121 else
22- acquire_ self
22+ acquire self
2323
2424let [@ inline] size_ = function
2525 | Cons (sz , _ , _ ) -> sz
2626 | Nil -> 0
2727
28- let release_ self x : unit =
28+ let release self x : unit =
2929 let rec loop () =
3030 match A. get self.items with
3131 | Cons (sz , _ , _ ) when sz > = self.max_size ->
@@ -40,12 +40,17 @@ let release_ self x : unit =
4040 loop ()
4141
4242let with_resource (self : _ t ) f =
43- let x = acquire_ self in
43+ let x = acquire self in
4444 try
4545 let res = f x in
46- release_ self x;
46+ release self x;
4747 res
4848 with e ->
4949 let bt = Printexc. get_raw_backtrace () in
50- release_ self x;
50+ release self x;
5151 Printexc. raise_with_backtrace e bt
52+
53+ module Raw = struct
54+ let release = release
55+ let acquire = acquire
56+ end
Original file line number Diff line number Diff line change @@ -23,3 +23,12 @@ val with_resource : 'a t -> ('a -> 'b) -> 'b
2323(* * [with_resource pool f] runs [f x] with [x] a resource;
2424 when [f] fails or returns, [x] is returned to the pool for
2525 future reuse. *)
26+
27+ (* * Low level control over the pool.
28+ This is easier to get wrong (e.g. releasing the same resource twice)
29+ so use with caution.
30+ @since NEXT_RELEASE *)
31+ module Raw : sig
32+ val acquire : 'a t -> 'a
33+ val release : 'a t -> 'a -> unit
34+ end
You can’t perform that action at this time.
0 commit comments