From: Michael Fellinger Date: 2010-02-22T02:50:45+09:00 Subject: Re: Tk on Windows and Mac OS X 10.6 On Sun, Feb 21, 2010 at 3:21 PM, Hidetoshi NAGAI wrote: > > From: mdiam > Subject: Re: Tk on Windows and Mac OS X 10.6 > Date: Sat, 20 Feb 2010 19:16:41 +0900 > Message-ID: > > The FFI ffi-tk ruby project should work with any plateform > > (e.g.  jruby but also macruby, etc...). > > But if Hidetoshi hasn't choose that way (i.e. ffi) for tk, > > propably there is a good reason (like the threads, or simply > > it much more work ?) > > Because I'm not familiar with FFI, I don't have good ideas to soleve > complexity of combination of callbacks, threads, and exceptions. > > For example ... > The eventloop is running on thread1. > thread2 calls Tk function1 (send to thread1; serialized on event queue). > thread1 get thread2's request from the event queue. > thread1 call callback1 on Ruby. > callback1 (running on thread1) calls Tk function2. > function2 call callback2 on Ruby. > And, an exception is raised in callback2. > Then, how should we treat the exception? > Tcl/Tk functions know nothing about Ruby threads. > > Of course, there are some methods to solve it. > However, those methods may have other problems; > slow operation or response (with lock process), > depend on global variables, explosion of entry tables, > CPU power eater (by busy loop), and so on. > To solve or avoid all of them on Ruby only may be difficult. > > # I agree that ffi-tk is valuable even if it has some restrictions. I've solved this by creating a separate thread that handles all calls using the interp. http://github.com/manveru/ffi-tk/blob/master/lib/ffi-tk/ffi/tcl.rb#L57 http://github.com/manveru/ffi-tk/blob/master/lib/ffi-tk/thread_sender.rb By using Queue#pop, the code will simply block the Thread until it can handle a send, so it's very little overhead, and has zero CPU usage when idle. On OSX, sometimes only the master thread is allowed to be a sender, so I handle that case as well. http://github.com/manveru/ffi-tk/blob/master/lib/ffi-tk/ffi/tcl.rb#L76 -- Michael Fellinger CTO, The Rubyists, LLC