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
(>= "1.17")
(< "1.18")))
atomic
("jsonrpc"
(and
(>= "1.17")

View file

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

View file

@ -3,4 +3,4 @@
(public_name linol)
(private_modules log)
(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_received: json -> unit;
s: server;
mutable id_counter: int;
id_counter: int Atomic.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;
oc;
s = server;
id_counter = 0;
id_counter = Atomic.make 0;
on_sent;
on_received;
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. *)
let fresh_lsp_id (self : t) : Req_id.t =
let id = self.id_counter in
self.id_counter <- id + 1;
let id = Atomic.fetch_and_add self.id_counter 1 in
`Int id
(** Registers a new handler for a request response. The return indicates