|
3 | 3 | This package provides an alternative direct style interface to programming with
|
4 | 4 | Lwt via {!Picos}.
|
5 | 5 |
|
| 6 | +{1 Libraries} |
| 7 | + |
6 | 8 | {!modules:
|
7 | 9 | Picos_lwt
|
8 | 10 | Picos_lwt_unix
|
9 | 11 | }
|
| 12 | + |
| 13 | +{1 Examples} |
| 14 | + |
| 15 | +Perhaps one of the main reasons someone might want to use a {!Picos} based |
| 16 | +direct style interface to programming with Lwt is for the purpose of working |
| 17 | +with an existing codebase and gradually porting the codebase to be effects based |
| 18 | +and to use an effects based scheduler. So, as an example, let's construct a |
| 19 | +program that runs both Lwt and, in another domain, an effects based scheduler. |
| 20 | + |
| 21 | +As an aside, {!Picos} is specifically designed to allow an application to run |
| 22 | +multiple schedulers and for code running on those schedulers to be able to |
| 23 | +communicate and synchronize. Specifically, {!Picos} is an interface to |
| 24 | +communicating with schedulers. A concurrent abstraction implemented in terms of |
| 25 | +the {!Picos} interface automatically works with any {!Picos} compatible |
| 26 | +scheduler. Furthermore, a correctly implemented scheduler allows certain |
| 27 | +operations, such as operations that cause fibers running on the scheduler to be |
| 28 | +resumed, to work across schedulers. |
| 29 | + |
| 30 | +For the example, we first open a couple of libraries: |
| 31 | + |
| 32 | +{[ |
| 33 | + # open Picos_std_finally (* let@, finally, lastly *) |
| 34 | + # open Picos_std_sync (* Stream, Ivar *) |
| 35 | +]} |
| 36 | + |
| 37 | +{{!Picos_mux_fifo} The FIFO scheduler} we will use normally automatically checks |
| 38 | +that the {{!Picos_io_select} IO event loop library} it uses for timeouts has |
| 39 | +been configured. However, as we will be spawning a new domain for the |
| 40 | +scheduler, we need to make sure to configure the IO library from the main thread |
| 41 | +of the application: |
| 42 | + |
| 43 | +{[ |
| 44 | + Picos_io_select.check_configured () |
| 45 | +]} |
| 46 | + |
| 47 | +Below is our example program: |
| 48 | + |
| 49 | +{[ |
| 50 | + let main () = |
| 51 | + let stream = Stream.create () in |
| 52 | + |
| 53 | + let@ _ = |
| 54 | + finally Domain.join @@ fun () -> |
| 55 | + let cursor = Stream.tap stream in |
| 56 | + Domain.spawn @@ fun () -> |
| 57 | + Picos_mux_fifo.run @@ fun () -> |
| 58 | + let rec loop cursor = |
| 59 | + let ((who, out), cursor) = |
| 60 | + Stream.read cursor |
| 61 | + in |
| 62 | + Printf.sprintf "Hello, %s!" who |
| 63 | + |> Ivar.fill out; |
| 64 | + loop cursor |
| 65 | + in |
| 66 | + try loop cursor with Exit -> () |
| 67 | + in |
| 68 | + |
| 69 | + let@ _ = lastly @@ fun () -> |
| 70 | + Stream.poison stream Exit |
| 71 | + in |
| 72 | + |
| 73 | + ["Mii"; "Uuu"] |
| 74 | + |> List.iter (fun who -> |
| 75 | + let reply = Ivar.create () in |
| 76 | + Stream.push stream (who, reply); |
| 77 | + Ivar.read reply |
| 78 | + |> Lwt_io.printl |
| 79 | + |> Picos_lwt.await); |
| 80 | + |
| 81 | + Picos_lwt.await Lwt_io.(flush stdout) |
| 82 | +]} |
| 83 | + |
| 84 | +The above program first creates a stream for communication. Then it spawns a |
| 85 | +domain for running the FIFO scheduler making sure that the domain will be joined |
| 86 | +and that a cursor to the stream is obtained before the rest of the program |
| 87 | +pushes messages to the stream. The loop running on the FIFO scheduler reads |
| 88 | +messages from the stream and responds to them until the stream is poisoned. The |
| 89 | +rest of the program runs on Lwt on the main thread. It first makes sure to |
| 90 | +poison the stream at the end. Then it runs a loop that sends a couple of |
| 91 | +messages to the stream, reads the responses, and prints them using Lwt. |
| 92 | + |
| 93 | +Finally we run the program with Lwt: |
| 94 | + |
| 95 | +{[ |
| 96 | + # Picos_lwt_unix.run_main main |
| 97 | + Hello, Mii! |
| 98 | + Hello, Uuu! |
| 99 | + - : unit = () |
| 100 | +]} |
| 101 | + |
| 102 | +Importantly, the above program shows that one can use communication and |
| 103 | +synchronization primitives like {{!Picos_std_sync.Stream} [Stream]} and |
| 104 | +{{!Picos_std_sync.Ivar} [Ivar]} across schedulers. |
0 commit comments