[cells-gtk-devel] Multithreading patch, v2
Peter Hildebrandt
peter.hildebrandt at gmail.com
Wed Dec 12 01:35:44 UTC 2007
Here's a new version of my patch
http://www.washbear-network.de/peterblog/wp-content/uploads/2007/12/cells-gtk-threading-121207.patch
Again, it builds against the CVS version.
I did quite a bit of testing and I would call it "stable" for my
purposes. I'd appreciate if you guys could try to break it and let me
know if you succeeded.
This patch includes an answer to the question of thread safety. In short,
it wraps every call to gdk/gtk in a thread-safe lock (with-gdk-threads
...), thus creating thread safety at the lowest level while at the same
time not blocking longer than necessary.
Cheers,
Peter
-----
The longer story (in case anyone is interested):
First and foremost, I modified with-gdk-threads to simulate a recursive
lock, that is, if you call with-gdk-threads from within a with-gdk-thread,
nothing bad happens (i.e. no dead lock). Additionally, since we know that
all callbacks from gtk-main are from within a lock, everything from the
gtk-main thread gets a free pass (the overhead boils down to a pointer
comparisson).
This enables us to use with-gdk-threads generously. In other words, every
call to a gtk/gdk library function is now automatically wrapped and can
thus be used from any thread. At least with test-gtk, there seems to be
hardly any performance penalty, and it makes it ultimately easy to deal
with threading -- just ignore it.
Additionally I fixed start-gtk-main. Now in case it has forgotton the
handle of the gtk-main thread, it checks the environment and reconnects if
it finds one. This way, a running cells-gtk even survives complete
package recompilations etc.
Here's another bit of sample code for the repl: Hello World, interactive
style:
;; fire up a hello world window
(start-win 'gtk-app :title "Hello World" :kids (c-in nil))
;; create a little playground
(defparameter vbox (first (push (mk-vbox :kids (c-in nil)) (kids *))))
;; make a label
(push (mk-label :text (c-in "Heyho!")) (kids vbox))
;; have some fun
(push (mk-button :label "Click me!" :on-clicked (callback (w e d) (print
:hello-world))) (kids vbox))
;; now something exciting
(push (mk-label :text (c-in "") :md-name :counter) (kids vbox))
(push (mk-button :label "Count" :on-clicked (let ((c 0))
(callback (w e d)
(setf (text (fm-other
:counter))
(format nil "~r"
(incf c)))))) (kids vbox))
More information about the cells-gtk-devel
mailing list