1
1
open Common_ws_
2
2
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
+
3
26
type handler = unit Request .t -> IO.Input .t -> IO.Output .t -> unit
4
27
5
28
module Frame_type = struct
@@ -52,7 +75,7 @@ module Writer = struct
52
75
mutable offset : int ; (* * number of bytes already in [buf] *)
53
76
oc : IO.Output .t ;
54
77
mutable closed : bool ;
55
- mutex : Mutex .t ;
78
+ mutex : With_lock .t ;
56
79
}
57
80
58
81
let create ?(buf_size = 16 * 1024 ) ~oc () : t =
@@ -63,19 +86,9 @@ module Writer = struct
63
86
offset = 0 ;
64
87
oc;
65
88
closed = false ;
66
- mutex = Mutex. create () ;
89
+ mutex = ! With_lock. builder () ;
67
90
}
68
91
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
-
79
92
let [@ inline] close self = self.closed < - true
80
93
let int_of_bool : bool -> int = Obj. magic
81
94
@@ -142,7 +155,7 @@ module Writer = struct
142
155
if self.offset = Bytes. length self.buf then really_output_buf_ self
143
156
144
157
let send_pong (self : t ) : unit =
145
- let @ () = with_mutex_ self in
158
+ let @ () = self.mutex.with_lock in
146
159
self.header.fin < - true ;
147
160
self.header.ty < - Frame_type. pong;
148
161
self.header.payload_len < - 0 ;
@@ -151,7 +164,7 @@ module Writer = struct
151
164
write_header_ self
152
165
153
166
let output_char (self : t ) c : unit =
154
- let @ () = with_mutex_ self in
167
+ let @ () = self.mutex.with_lock in
155
168
let cap = Bytes. length self.buf - self.offset in
156
169
(* make room for [c] *)
157
170
if cap = 0 then really_output_buf_ self;
@@ -161,7 +174,7 @@ module Writer = struct
161
174
if cap = 1 then really_output_buf_ self
162
175
163
176
let output (self : t ) buf i len : unit =
164
- let @ () = with_mutex_ self in
177
+ let @ () = self.mutex.with_lock in
165
178
let i = ref i in
166
179
let len = ref len in
167
180
while ! len > 0 do
@@ -179,16 +192,16 @@ module Writer = struct
179
192
flush_if_full self
180
193
181
194
let flush self : unit =
182
- let @ () = with_mutex_ self in
195
+ let @ () = self.mutex.with_lock in
183
196
flush_ self
184
197
end
185
198
186
199
module Reader = struct
187
200
type state =
188
201
| Begin (* * At the beginning of a frame *)
189
202
| 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] *)
192
205
| Close
193
206
194
207
type t = {
@@ -266,7 +279,7 @@ module Reader = struct
266
279
external apply_masking_ :
267
280
key :bytes -> key_offset :int -> buf :bytes -> int -> int -> unit
268
281
= " tiny_httpd_ws_apply_masking"
269
- [@@ noalloc]
282
+ [@@ noalloc]
270
283
(* * Apply masking to the parsed data *)
271
284
272
285
let [@ inline] apply_masking ~mask_key ~mask_offset (buf : bytes ) off len : unit
@@ -414,7 +427,8 @@ let upgrade ic oc : _ * _ =
414
427
in
415
428
ws_ic, ws_oc
416
429
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 *)
418
432
module Make_upgrade_handler (X : sig
419
433
val accept_ws_protocol : string -> bool
420
434
val handler : handler
0 commit comments