Skip to content

Commit

Permalink
Update Lwt tutorial examples to match new tutorial
Browse files Browse the repository at this point in the history
In particular:

- Change `C.log` to `log_s`. `log` is the emergency non-blocking version
  that discards data if the buffer is full. We should not encourage its
  use in a tutorial. Also, it doesn't demonstrate using Lwt.

- Use `>>=` rather than `>|=` because the tutorial didn't introduce it
  yet.

- Don't use `_` to ignore exceptions in timeout example.

- Moved some functions out of `start` for clarity.

- Remove mvar and stream examples, as they're no longer in the tutorial.
  • Loading branch information
talex5 committed May 20, 2016
1 parent 5faa73e commit 28ff35c
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 197 deletions.
4 changes: 2 additions & 2 deletions console/unikernel.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ module Main (C: V1_LWT.CONSOLE) = struct
let rec loop = function
| 0 -> Lwt.return_unit
| n ->
C.log c "hello";
C.log_s c "hello" >>= fun () ->
OS.Time.sleep 1.0 >>= fun () ->
C.log c "world";
C.log_s c "world" >>= fun () ->
loop (n-1)
in
loop 4
Expand Down
2 changes: 1 addition & 1 deletion lwt/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include ../Makefile.config

TARGETS=heads1 heads2 heads3 timeout1 timeout2 \
echo_server1 echo_server2 int_server stream_server
echo_server1 echo_server2

configure: $(patsubst %,%-configure,$(TARGETS))
build: $(patsubst %,%-build,$(TARGETS))
Expand Down
5 changes: 0 additions & 5 deletions lwt/src/config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ let (name, main) =

| "echo_server1" -> ("echo_server1", "Unikernels.Echo_server1")

| "echo_server2" -> ("echo_server2", "Mvar_unikernels.Echo_server2")
| "int_server" -> ("int_server", "Mvar_unikernels.Int_server")

| "stream_server" -> ("stream_server", "Stream_server.Unikernel")

with Not_found -> failwith "Must specify target"

let () =
Expand Down
99 changes: 0 additions & 99 deletions lwt/src/mvar_unikernels.ml

This file was deleted.

54 changes: 0 additions & 54 deletions lwt/src/stream_server.ml

This file was deleted.

67 changes: 31 additions & 36 deletions lwt/src/unikernels.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ module Heads1 (C: V1_LWT.CONSOLE) = struct

let start c =
Lwt.join [
(Time.sleep 1.0 >|= fun () ->
C.log c "Heads");
(Time.sleep 2.0 >|= fun () ->
C.log c "Tails");
] >|= fun () ->
C.log c ("Finished")
(Time.sleep 1.0 >>= fun () -> C.log_s c "Heads");
(Time.sleep 2.0 >>= fun () -> C.log_s c "Tails")
] >>= fun () ->
C.log_s c ("Finished")

end

Expand Down Expand Up @@ -43,56 +41,53 @@ end

module Timeout1 (C: V1_LWT.CONSOLE) = struct

let timeout delay t =
Time.sleep delay >>= fun () ->
match Lwt.state t with
| Lwt.Sleep -> Lwt.cancel t; Lwt.return None
| Lwt.Return v -> Lwt.return (Some v)
| Lwt.Fail ex -> Lwt.fail ex

let start c =
Random.self_init ();

let timeout f t =
Time.sleep f >>= fun () ->
match Lwt.state t with
| Lwt.Return v -> Lwt.return (Some v)
| _ -> Lwt.cancel t; Lwt.return None
in

let t = Time.sleep (Random.float 3.0) >|= fun () -> "Heads" in
timeout 2.0 t >>= fun v ->
C.log c (match v with None -> "cancelled" | Some v -> v);
C.log c "Finished";
Lwt.return_unit
timeout 2.0 t >>= function
| None -> C.log_s c "Cancelled"
| Some v -> C.log_s c (Printf.sprintf "Returned %S" v)

end

module Timeout2 (C: V1_LWT.CONSOLE) = struct

let timeout delay t =
let tmout = Time.sleep delay in
Lwt.pick [
(tmout >|= fun () -> None);
(t >|= fun v -> Some v);
]

let start c =
Random.self_init ();
let timeout f t =
let tmout = Time.sleep f in
Lwt.pick [
(tmout >|= fun () -> None);
(t >|= fun v -> Some v);
]
in
let t = Time.sleep (Random.float 3.0) >|= fun () -> "Heads" in
timeout 2.0 t >>= fun v ->
C.log c (match v with None -> "Cancelled" | Some v -> v);
C.log c "Finished";
Lwt.return_unit
timeout 2.0 t >>= function
| None -> C.log_s c "Cancelled"
| Some v -> C.log_s c (Printf.sprintf "Returned %S" v)

end

module Echo_server1 (C: V1_LWT.CONSOLE) = struct

let read_line () =
OS.Time.sleep (Random.float 2.5) >|= fun () ->
String.make (Random.int 20) 'a'

let start c =
let read_line () =
Time.sleep (Random.float 2.5)
>|= fun () ->String.make (Random.int 20) 'a'
in
let rec echo_server = function
| 0 -> Lwt.return ()
| n ->
read_line () >>= fun s ->
C.log c s;
echo_server (n-1)
read_line () >>= fun s ->
C.log_s c s >>= fun () ->
echo_server (n - 1)
in
echo_server 10

Expand Down

0 comments on commit 28ff35c

Please sign in to comment.