janestreet / async_unix

Jane Street Capital's asynchronous execution library (unix)
MIT License
33 stars 21 forks source link

Add `Writer.transfer'` #1

Closed NicolasT closed 10 years ago

NicolasT commented 11 years ago

Note I tested this on top of 109.34.00, then rebased to master but didn't compile since I don't have git versions of dependencies installed (which seem to be required).

Writer.transfer' is similar to Writer.transfer, but allowing to use async actions inside the given callback.

See https://groups.google.com/forum/#!topic/ocaml-core/nFpAMKoWRYo for a related discussion.

Here's a demonstration:

open Core.Std
open Async.Std

let main () =
    let r = Pipe.of_list ["Foo"; "Bar"; "Baz"] in

    Writer.with_file
        ?perm:(Some 0o600)
        ?append:(Some false)
        ?exclusive:(Some true)
        "data.dat"
        ~f:(fun w ->
            Writer.transfer' w r (
                let rec loop q = match Queue.dequeue q with
                  | None -> return ()
                  | Some s ->
                      Writer.write_line w s;
                      Writer.flushed w >>= fun () ->
                      Writer.fdatasync w >>= fun () ->
                      loop q
                in
                loop))
    >>| fun () ->
    Shutdown.shutdown 0
;;

let _ = main () in
never_returns (Scheduler.go ())
$ strace -ff -e writev,fdatasync ./file.native 
Process 902 attached
Process 903 attached
Process 904 attached
Process 905 attached
[pid   905] writev(5, [{"Foo\n", 4}], 1) = 4
[pid   902] fdatasync(5)                = 0
[pid   905] writev(5, [{"Bar\n", 4}], 1) = 4
[pid   902] fdatasync(5)                = 0
[pid   905] writev(5, [{"Baz\n", 4}], 1) = 4
[pid   902] fdatasync(5)                = 0
[pid   903] --- SIGRTMIN {si_signo=SIGRTMIN, si_code=SI_TKILL, si_pid=901, si_uid=1000} ---
[pid   903] <... futex resumed> )       = ? <unavailable>
[pid   905] +++ exited with 0 +++
[pid   904] +++ exited with 0 +++
[pid   903] +++ exited with 0 +++
[pid   902] +++ exited with 0 +++
+++ exited with 0 +++
$ cat data.dat 
Foo
Bar
Baz

Note this demonstration is not safe against power loss (even though fdatasync is used).

avsm commented 10 years ago

@diml what's the status of merging this? I could use this too.

ghost commented 10 years ago

Sorry for the delay. I refactored it a bit and submitted it for review.

bmillwood commented 10 years ago

Released in core 109.55.00, thanks.