caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: yminsky@CS.Cornell.EDU
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] interactive graphics with Tcl/Tk
Date: Fri, 09 Aug 2002 10:22:00 +0900	[thread overview]
Message-ID: <20020809102200M.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <59147.209.9.234.140.1028822939.squirrel@dragonfly.localdomain>

From: "Yaron M. Minsky" <yminsky@CS.Cornell.EDU>

> I'm trying to use tcl/tk for doing interactive graphics from the toplevel.
>  And, lord help me, I'm trying to do it on cygwin.
> My basic solution I came up with is this:  I have one thread doing all the
> labltk calls.  That thread also polls a channel where it effectively picks
> up RPC requests.  SO, when I want to draw something on the screen, I stuff
> the appropriate function into the channel, the TclTk thread picks it up
> and executes that function, and then sends back a response, at which point
> the calling thread continues.

Sound OK.

> Anyway, it all seems well and good, but when I actually try to do it, for
> some reason the i/o on the caml toplevel locks up.  So if I type:
> # Graphing.init (); print_string "Hello World!\n";;
> Hello World!
> - : unit = ()
> #
> 
> Graphing.init starts up the Tcl/Tk window as expected, and the print
> works, also as expected.  But from that point on until I kill the Tcl/Tk
> window, I can't get the toplevel to respond to keypresses.  It's as if the
> Tcl/Tk thread has stolen stdin.  Does anyone know how to work around this?
> y

There's one trouble: you can only switch between ocaml threads when
they are executing ocaml code.
This means that you should setup a timeout with Timer.add in your tk
thread, and call Thread.yield there. Note that this works with
posix and win32 threads, but not bytecode threads, since you cannot
switch threads in callbacks with them.

Here is the code. I only tested it with MSVC version, but this should
be ok on cygwin too (it works on Unix).
In fact, I wrote this code for lablgtk, because although it is
reentrant (contrary to labltk), on windows you can only call it from
one thread. This will be in the next lablgtk release.

Other threads should only use the sync and async functions.

$ ocaml -I +threads -I +labltk unix.cma threads.cma labltk.cma

        Objective Caml version 3.06

# let jobs : (unit -> unit) Queue.t = Queue.create ()
  let m = Mutex.create ()
  let with_jobs f =
    Mutex.lock m; let y = f jobs in Mutex.unlock m; y
  
  let loop_id = ref None
  let cannot_sync () =
    match !loop_id with None -> true
    | Some id -> Thread.id (Thread.self ()) = id
  
  let gui_safe () =
    not (Sys.os_type = "Win32") || !loop_id = Some(Thread.id (Thread.self ()))
  
  let has_jobs () = not (with_jobs Queue.is_empty)
  let n_jobs () = with_jobs Queue.length
  let do_next_job () = with_jobs Queue.take ()
  let async j x = with_jobs (Queue.add (fun () -> j x))
  let sync f x =
    if cannot_sync () then f x else
    let m = Mutex.create () in
    let res = ref None in
    Mutex.lock m;
    let c = Condition.create () in
    let j x =
      let y = f x in Mutex.lock m; res := Some y; Mutex.unlock m;
      Condition.signal c
    in
    async j x;
    Condition.wait c m;
    match !res with Some y -> y | None -> assert false
  ;;
val jobs : (unit -> unit) Queue.t = <abstr>
val m : Mutex.t = <abstr>
val with_jobs : ((unit -> unit) Queue.t -> 'a) -> 'a = <fun>
val loop_id : int option ref = {contents = None}
val cannot_sync : unit -> bool = <fun>
val gui_safe : unit -> bool = <fun>
val has_jobs : unit -> bool = <fun>
val n_jobs : unit -> int = <fun>
val do_next_job : unit -> unit = <fun>
val async : ('a -> unit) -> 'a -> unit = <fun>
val sync : ('a -> 'b) -> 'a -> 'b = <fun>
# open Tk;;
# let tk_thread () =  
    let top = openTk () in
    let rec cb () = for i = 1 to n_jobs () do do_next_job () done;
        Timer.set 1 cb; Thread.yield () in
      Timer.set 1 cb;
      mainLoop ();;
val tk_thread : unit -> unit = <fun>
# Thread.create tk_thread ();;
- : Thread.t = <abstr>
# let top = Widget.default_toplevel ;;
val top : Widget.toplevel Widget.widget = <abstr>
# let b = sync (Button.create ~text:"Hello world!") top;;
val b : Widget.button Widget.widget = <abstr>
# async pack [b];;
- : unit = ()
# async (Button.configure ~command:(fun () -> prerr_endline "Hello")) b;;
- : unit = ()

---------------------------------------------------------------------------
Jacques Garrigue      Kyoto University     garrigue at kurims.kyoto-u.ac.jp
		<A HREF=http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/>JG</A>
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


      parent reply	other threads:[~2002-08-09  1:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-08-08 16:08 Yaron M. Minsky
2002-08-05 17:51 ` [Caml-list] Debbuging interactive program Christophe Raffalli
2002-08-09  1:22 ` Jacques Garrigue [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020809102200M.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=yminsky@CS.Cornell.EDU \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).