diff --git a/example/template/main.ml b/example/template/main.ml index 3ba7ec58..e1ad9188 100644 --- a/example/template/main.ml +++ b/example/template/main.ml @@ -43,6 +43,8 @@ class lsp_server = val buffers : (Lsp.Types.DocumentUri.t, state_after_processing) Hashtbl.t = Hashtbl.create 32 + method spawn_query_handler f = Linol_lwt.spawn f + (* We define here a helper method that will: - process a document - store the state resulting from the processing diff --git a/src/blocking_IO.ml b/src/blocking_IO.ml index 2c4c9d00..66c57eb4 100644 --- a/src/blocking_IO.ml +++ b/src/blocking_IO.ml @@ -11,7 +11,7 @@ let fail = raise let stdin = stdin let stdout = stdout -let default_spawn_ f = +let default_spawn f = let run () = try f () with e -> @@ -21,9 +21,6 @@ let default_spawn_ f = in ignore (Thread.create run ()) -let spawn_ref_ = ref default_spawn_ -let set_spawn_function f = spawn_ref_ := f -let spawn f = !spawn_ref_ f let catch f g = try f () with e -> g e let rec read ic buf i len = diff --git a/src/blocking_IO.mli b/src/blocking_IO.mli index 8f87e62b..71fe5f9c 100644 --- a/src/blocking_IO.mli +++ b/src/blocking_IO.mli @@ -6,6 +6,6 @@ include and type in_channel = in_channel and type out_channel = out_channel -val set_spawn_function : ((unit -> unit) -> unit) -> unit -(** Change the way the LSP server spawns new threads to handle - client queries. For example, one might use a thread pool. *) +val default_spawn : (unit -> unit) -> unit +(** Start a new thread. + @since NEXT_RELEASE *) diff --git a/src/jsonrpc2.ml b/src/jsonrpc2.ml index 927dc4d8..dc0c52a8 100644 --- a/src/jsonrpc2.ml +++ b/src/jsonrpc2.ml @@ -319,7 +319,7 @@ module Make (IO : IO) : S with module IO = IO = struct let* r = read_msg self in match r with | Ok r -> - IO.spawn (fun () -> process_msg r); + self.s#spawn_query_handler (fun () -> process_msg r); loop () | Error e -> IO.fail e in diff --git a/src/lwt/linol_lwt.ml b/src/lwt/linol_lwt.ml index e3b65fac..5c8f7ca0 100644 --- a/src/lwt/linol_lwt.ml +++ b/src/lwt/linol_lwt.ml @@ -29,15 +29,17 @@ module IO_lwt : let read_line = Lwt_io.read_line let catch = Lwt.catch let fail = Lwt.fail - - let spawn f = - Lwt.async (fun () -> - Lwt.catch f (fun exn -> - Printf.eprintf "uncaught exception in `spawn`:\n%s\n%!" - (Printexc.to_string exn); - Lwt.return ())) end +(** Spawn function. + @since NEXT_RELEASE *) +let spawn f = + Lwt.async (fun () -> + Lwt.catch f (fun exn -> + Printf.eprintf "uncaught exception in `spawn`:\n%s\n%!" + (Printexc.to_string exn); + Lwt.return ())) + include Lsp.Types include IO_lwt diff --git a/src/server.ml b/src/server.ml index a7f16b17..eb44de86 100644 --- a/src/server.ml +++ b/src/server.ml @@ -67,6 +67,11 @@ module Make (IO : IO) = struct method must_quit = false (** Set to true if the client requested to exit *) + + method virtual spawn_query_handler : (unit -> unit IO.t) -> unit + (** How to start a new future/task/thread concurrently. This is used + to process incoming user queries. + @since NEXT_RELEASE *) end (** A wrapper to more easily reply to notifications *) diff --git a/src/sigs.ml b/src/sigs.ml index c4fe0a34..24f03dc9 100644 --- a/src/sigs.ml +++ b/src/sigs.ml @@ -17,10 +17,6 @@ module type IO = sig val read_line : in_channel -> string t val write : out_channel -> bytes -> int -> int -> unit t val write_string : out_channel -> string -> unit t - - val spawn : (unit -> unit t) -> unit - (** Spawn a new task that executes concurrently. *) - val fail : exn -> unit t val catch : (unit -> 'a t) -> (exn -> 'a t) -> 'a t end