caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] [lwt] multiplexing several threads
@ 2013-01-31  5:37 Ivan Gotovchits
  2013-01-31  9:53 ` Mauricio Fernandez
  0 siblings, 1 reply; 3+ messages in thread
From: Ivan Gotovchits @ 2013-01-31  5:37 UTC (permalink / raw)
  To: caml-list


Dear list,

I'm stuck with a simple problem when using lwt. I have two lwt
threads. One on them reads commands from user (using Lwt_read_line),
another reads events from system. A need to multiplex them. Speaking in
ocaml, I have three functions:

  type state    (* program state to process *)
  type command  (* command to be executed (issued either by user or by
                   system event) *)
  
  val read_from_user: unit -> command Lwt.t
  val read_event: unit -> command Lwt.t
  val process: state -> command -> state Lwt.t

 
And a need to write a function loop

  val loop: state -> state Lwt.t

accepting some state, taking a command from user or an event from system
(whichever comes first) and passing it to `process' function, and doing
it in a cycle, until explicitly ordered to stop (with Exit command). 

The following solution

   let rec loop sc =
     let cmd = read_from_user () <?> read_event () in
     cmd >>= (process state)

   and process sc = function
     | Exit -> return sc
     | cmd  -> (* modify sc*) loop sc

   let r = Lwt_main.run (loop state)

doesn't work when the event thread wakes up first. In that case the next
step of the loop will call Lwt_read_line.read_line before the previous
invocation finishes and it breaks user input.

Please, help me to find a feasible solution to my problem!

-- 
         (__) 
         (oo) 
   /------\/ 
  / |    ||   
 *  /\---/\ 
    ~~   ~~   
...."Have you mooed today?"...

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] [lwt] multiplexing several threads
  2013-01-31  5:37 [Caml-list] [lwt] multiplexing several threads Ivan Gotovchits
@ 2013-01-31  9:53 ` Mauricio Fernandez
  2013-01-31 10:26   ` Mauricio Fernandez
  0 siblings, 1 reply; 3+ messages in thread
From: Mauricio Fernandez @ 2013-01-31  9:53 UTC (permalink / raw)
  To: caml-list

On Thu, Jan 31, 2013 at 09:37:29AM +0400, Ivan Gotovchits wrote:
> The following solution
> 
>    let rec loop sc =
>      let cmd = read_from_user () <?> read_event () in
>      cmd >>= (process state)
> 
>    and process sc = function
>      | Exit -> return sc
>      | cmd  -> (* modify sc*) loop sc
> 
>    let r = Lwt_main.run (loop state)
> 
> doesn't work when the event thread wakes up first. In that case the next
> step of the loop will call Lwt_read_line.read_line before the previous
> invocation finishes and it breaks user input.
> 
> Please, help me to find a feasible solution to my problem!


let process s = function
    Exit -> return (`Exit s)
  | cmd -> let s = ... in return (`State s)

let rec loop state user_cmd event =
  match_lwt
    Lwt.choose
      [ (lwt x = user_cmd in return (`User x));
        (lwt x = event in return (`Event x));
    ]
  with
    `User cmd -> begin match_lwt process state cmd with
                   `Exit s -> return s
                 | `State s -> loop s (read_from_user ()) event
                 end
  | `Event cmd -> begin match_lwt process state cmd with
                   `Exit s -> return s
                 | `State s -> loop s user_cmd (read_event ())
                 end

-- 
Mauricio Fernandez

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] [lwt] multiplexing several threads
  2013-01-31  9:53 ` Mauricio Fernandez
@ 2013-01-31 10:26   ` Mauricio Fernandez
  0 siblings, 0 replies; 3+ messages in thread
From: Mauricio Fernandez @ 2013-01-31 10:26 UTC (permalink / raw)
  To: caml-list

On Thu, Jan 31, 2013 at 10:53:31AM +0100, Mauricio Fernandez wrote:
> On Thu, Jan 31, 2013 at 09:37:29AM +0400, Ivan Gotovchits wrote:
> > The following solution
> > 
> >    let rec loop sc =
> >      let cmd = read_from_user () <?> read_event () in
> >      cmd >>= (process state)
> > 
> >    and process sc = function
> >      | Exit -> return sc
> >      | cmd  -> (* modify sc*) loop sc
> > 
> >    let r = Lwt_main.run (loop state)
> > 
> > doesn't work when the event thread wakes up first. In that case the next
> > step of the loop will call Lwt_read_line.read_line before the previous
> > invocation finishes and it breaks user input.
> > 
> > Please, help me to find a feasible solution to my problem!
> 

Just remembered that you might want to cancel the other thread before
returning too:

> let process s = function
>     Exit -> return (`Exit s)
>   | cmd -> let s = ... in return (`State s)
> 
> let rec loop state user_cmd event =
>   match_lwt
>     Lwt.choose
>       [ (lwt x = user_cmd in return (`User x));
>         (lwt x = event in return (`Event x));
>     ]
>   with
>     `User cmd -> begin match_lwt process state cmd with
>                    `Exit s -> return s

                      ===================

                      `Exit s -> Lwt.cancel event; return s

>                  | `State s -> loop s (read_from_user ()) event
>                  end
>   | `Event cmd -> begin match_lwt process state cmd with
>                    `Exit s -> return s
                                Lwt.cancel user_cmd; return s

>                  | `State s -> loop s user_cmd (read_event ())
>                  end
-- 
Mauricio Fernandez

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-01-31 10:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-31  5:37 [Caml-list] [lwt] multiplexing several threads Ivan Gotovchits
2013-01-31  9:53 ` Mauricio Fernandez
2013-01-31 10:26   ` Mauricio Fernandez

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).