add posix backend to test and to echo.ml

This commit is contained in:
Simon Cruanes 2025-05-01 11:39:45 -04:00
parent adc468b59d
commit bbd77c4730
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
5 changed files with 66 additions and 8 deletions

View file

@ -1,4 +1,4 @@
(executable
(name echo)
(libraries nanoev nanoev.unix moonpool moonpool.fib trace trace-tef
nanoev_tiny_httpd))
(libraries nanoev nanoev.unix nanoev-posix moonpool moonpool.fib trace
trace-tef nanoev_tiny_httpd))

View file

@ -86,6 +86,13 @@ let () =
let port_ = ref 8080 in
let max_conn = ref 1024 in
let j = ref 8 in
let backend = ref `Posix in
let set_backend = function
| "posix" | "poll" | "default" -> backend := `Posix
| "unix" | "select" -> backend := `Unix
| s -> failwith @@ Printf.sprintf "unknown backend %S" s
in
Arg.parse
(Arg.align
[
@ -94,15 +101,30 @@ let () =
"-j", Arg.Set_int j, " number of threads";
"--debug", Arg.Unit setup_logging, " enable debug";
"--max-conns", Arg.Set_int max_conn, " maximum concurrent connections";
( "--backend",
Arg.Symbol
([ "posix"; "default"; "unix"; "select"; "poll" ], set_backend),
" event loop backend" );
])
(fun _ -> raise (Arg.Bad ""))
"echo [option]*";
let@ pool = Moonpool.Ws_pool.with_ ~num_threads:!j () in
let@ _runner = Moonpool_fib.main in
let@ pool =
fun yield ->
if !j > 1 then
let@ pool = Moonpool.Ws_pool.with_ ~num_threads:!j () in
let@ _runner = Moonpool_fib.main in
yield pool
else
Moonpool_fib.main yield
in
let ev = Nanoev_unix.create () in
Nanoev_picos.setup_bg_thread ev;
let ev =
match !backend with
| `Posix -> Nanoev_posix.create ()
| `Unix -> Nanoev_unix.create ()
in
let@ () = Nanoev_picos.with_setup_bg_thread ev in
let server =
Nanoev_tiny_httpd.create ~new_thread:(Moonpool.run_async pool) ~port:!port_
@ -273,8 +295,9 @@ let () =
let s = to_string_top h in
Response.make_string ~headers:[ "content-type", "text/html" ] @@ Ok s);
Printf.printf "listening on http://%s:%d\n%!" (Server.addr server)
(Server.port server);
Printf.printf
"listening on http://%s:%d with %d threads, %d max connections\n%!"
(Server.addr server) (Server.port server) !j !max_conn;
match Server.run server with
| Ok () -> ()
| Error e -> raise e

3
tests/posix/dune Normal file
View file

@ -0,0 +1,3 @@
(tests
(names t1)
(libraries nanoev nanoev-posix threads))

3
tests/posix/t1.expected Normal file
View file

@ -0,0 +1,3 @@
writing
can read
done writing

29
tests/posix/t1.ml Normal file
View file

@ -0,0 +1,29 @@
module E = Nanoev_posix
let mkpipe () : Unix.file_descr * Unix.file_descr =
let f1, f2 = Unix.pipe () in
Unix.set_nonblock f1;
Unix.set_nonblock f2;
f1, f2
let loop (e : E.t) =
while true do
E.step e
done
let () =
let ev = E.create () in
ignore (Thread.create loop ev : Thread.t);
let rd, wr = mkpipe () in
E.on_readable ev rd () () (fun ~closed () () ->
if closed then
print_endline "closed!"
else
print_endline "can read");
Thread.delay 0.05;
print_endline "writing";
ignore
(Unix.write wr (Bytes.unsafe_of_string "hello") 0 (String.length "hello")
: int);
Thread.delay 0.1;
print_endline "done writing"