fix: make server requests thread safe

This commit is contained in:
Simon Cruanes 2024-10-23 16:41:02 -04:00
parent a779942f95
commit 41a6c3e306
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
4 changed files with 6 additions and 5 deletions

View file

@ -31,6 +31,7 @@
(and (and
(>= "1.17") (>= "1.17")
(< "1.18"))) (< "1.18")))
atomic
("jsonrpc" ("jsonrpc"
(and (and
(>= "1.17") (>= "1.17")

View file

@ -13,6 +13,7 @@ depends: [
"logs" "logs"
"trace" {>= "0.4"} "trace" {>= "0.4"}
"lsp" {>= "1.17" & < "1.18"} "lsp" {>= "1.17" & < "1.18"}
"atomic"
"jsonrpc" {>= "1.17" & < "1.18"} "jsonrpc" {>= "1.17" & < "1.18"}
"ocaml" {>= "4.14"} "ocaml" {>= "4.14"}
"odoc" {with-doc} "odoc" {with-doc}

View file

@ -3,4 +3,4 @@
(public_name linol) (public_name linol)
(private_modules log) (private_modules log)
(flags :standard -warn-error -a+8) (flags :standard -warn-error -a+8)
(libraries yojson lsp logs threads trace.core)) (libraries yojson lsp logs threads trace.core atomic))

View file

@ -69,7 +69,7 @@ module Make (IO : IO) : S with module IO = IO = struct
on_sent: json -> unit; on_sent: json -> unit;
on_received: json -> unit; on_received: json -> unit;
s: server; s: server;
mutable id_counter: int; id_counter: int Atomic.t;
pending_responses: (Req_id.t, server_request_handler_pair) Hashtbl.t; pending_responses: (Req_id.t, server_request_handler_pair) Hashtbl.t;
} }
@ -78,7 +78,7 @@ module Make (IO : IO) : S with module IO = IO = struct
ic; ic;
oc; oc;
s = server; s = server;
id_counter = 0; id_counter = Atomic.make 0;
on_sent; on_sent;
on_received; on_received;
pending_responses = Hashtbl.create 8; pending_responses = Hashtbl.create 8;
@ -116,8 +116,7 @@ module Make (IO : IO) : S with module IO = IO = struct
(** Returns a new, unused [Req_id.t] to send a server request. *) (** Returns a new, unused [Req_id.t] to send a server request. *)
let fresh_lsp_id (self : t) : Req_id.t = let fresh_lsp_id (self : t) : Req_id.t =
let id = self.id_counter in let id = Atomic.fetch_and_add self.id_counter 1 in
self.id_counter <- id + 1;
`Int id `Int id
(** Registers a new handler for a request response. The return indicates (** Registers a new handler for a request response. The return indicates