11open Common_ws_
22
3+ module With_lock = struct
4+ type t = { with_lock : 'a . (unit -> 'a ) -> 'a }
5+ type builder = unit -> t
6+
7+ let default_builder : builder =
8+ fun () ->
9+ let mutex = Mutex. create () in
10+ {
11+ with_lock =
12+ (fun f ->
13+ Mutex. lock mutex;
14+ try
15+ let x = f () in
16+ Mutex. unlock mutex;
17+ x
18+ with e ->
19+ Mutex. unlock mutex;
20+ raise e);
21+ }
22+
23+ let builder : builder ref = ref default_builder
24+ end
25+
326type handler = unit Request .t -> IO.Input .t -> IO.Output .t -> unit
427
528module Frame_type = struct
@@ -52,7 +75,7 @@ module Writer = struct
5275 mutable offset : int ; (* * number of bytes already in [buf] *)
5376 oc : IO.Output .t ;
5477 mutable closed : bool ;
55- mutex : Mutex .t ;
78+ mutex : With_lock .t ;
5679 }
5780
5881 let create ?(buf_size = 16 * 1024 ) ~oc () : t =
@@ -63,19 +86,9 @@ module Writer = struct
6386 offset = 0 ;
6487 oc;
6588 closed = false ;
66- mutex = Mutex. create () ;
89+ mutex = ! With_lock. builder () ;
6790 }
6891
69- let [@ inline] with_mutex_ (self : t ) f =
70- Mutex. lock self.mutex;
71- try
72- let x = f () in
73- Mutex. unlock self.mutex;
74- x
75- with e ->
76- Mutex. unlock self.mutex;
77- raise e
78-
7992 let [@ inline] close self = self.closed < - true
8093 let int_of_bool : bool -> int = Obj. magic
8194
@@ -142,7 +155,7 @@ module Writer = struct
142155 if self.offset = Bytes. length self.buf then really_output_buf_ self
143156
144157 let send_pong (self : t ) : unit =
145- let @ () = with_mutex_ self in
158+ let @ () = self.mutex.with_lock in
146159 self.header.fin < - true ;
147160 self.header.ty < - Frame_type. pong;
148161 self.header.payload_len < - 0 ;
@@ -151,7 +164,7 @@ module Writer = struct
151164 write_header_ self
152165
153166 let output_char (self : t ) c : unit =
154- let @ () = with_mutex_ self in
167+ let @ () = self.mutex.with_lock in
155168 let cap = Bytes. length self.buf - self.offset in
156169 (* make room for [c] *)
157170 if cap = 0 then really_output_buf_ self;
@@ -161,7 +174,7 @@ module Writer = struct
161174 if cap = 1 then really_output_buf_ self
162175
163176 let output (self : t ) buf i len : unit =
164- let @ () = with_mutex_ self in
177+ let @ () = self.mutex.with_lock in
165178 let i = ref i in
166179 let len = ref len in
167180 while ! len > 0 do
@@ -179,16 +192,16 @@ module Writer = struct
179192 flush_if_full self
180193
181194 let flush self : unit =
182- let @ () = with_mutex_ self in
195+ let @ () = self.mutex.with_lock in
183196 flush_ self
184197end
185198
186199module Reader = struct
187200 type state =
188201 | Begin (* * At the beginning of a frame *)
189202 | Reading_frame of { mutable remaining_bytes : int ; mutable num_read : int }
190- (* * Currently reading the payload of a frame with [remaining_bytes]
191- left to read from the underlying [ic] *)
203+ (* * Currently reading the payload of a frame with [remaining_bytes] left
204+ to read from the underlying [ic] *)
192205 | Close
193206
194207 type t = {
@@ -266,7 +279,7 @@ module Reader = struct
266279 external apply_masking_ :
267280 key :bytes -> key_offset :int -> buf :bytes -> int -> int -> unit
268281 = " tiny_httpd_ws_apply_masking"
269- [@@ noalloc]
282+ [@@ noalloc]
270283 (* * Apply masking to the parsed data *)
271284
272285 let [@ inline] apply_masking ~mask_key ~mask_offset (buf : bytes ) off len : unit
@@ -414,7 +427,8 @@ let upgrade ic oc : _ * _ =
414427 in
415428 ws_ic, ws_oc
416429
417- (* * Turn a regular connection handler (provided by the user) into a websocket upgrade handler *)
430+ (* * Turn a regular connection handler (provided by the user) into a websocket
431+ upgrade handler *)
418432module Make_upgrade_handler (X : sig
419433 val accept_ws_protocol : string -> bool
420434 val handler : handler
0 commit comments