From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Chris Vine Newsgroups: gmane.lisp.guile.user Subject: Re: Potluck - thread safe event loop with await semantics Date: Mon, 22 Feb 2016 17:40:56 +0000 Message-ID: <20160222174056.2091d806@dell.homenet> References: <20160216214512.42e6fd39@bother.homenet> <878u2dosxu.fsf@gnu.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1456163281 13726 80.91.229.3 (22 Feb 2016 17:48:01 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 22 Feb 2016 17:48:01 +0000 (UTC) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Mon Feb 22 18:47:54 2016 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1aXuaP-0006z8-IZ for guile-user@m.gmane.org; Mon, 22 Feb 2016 18:47:53 +0100 Original-Received: from localhost ([::1]:50869 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aXuaP-0003tV-4C for guile-user@m.gmane.org; Mon, 22 Feb 2016 12:47:53 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:57898) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aXuTo-0000z3-Jv for guile-user@gnu.org; Mon, 22 Feb 2016 12:41:05 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aXuTk-0008VD-EM for guile-user@gnu.org; Mon, 22 Feb 2016 12:41:04 -0500 Original-Received: from avasout07.plus.net ([84.93.230.235]:48754) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aXuTk-0008TO-8q for guile-user@gnu.org; Mon, 22 Feb 2016 12:41:00 -0500 Original-Received: from dell.homenet ([87.115.102.251]) by avasout07 with smtp id MVgw1s0045RSpqF01VgyQM; Mon, 22 Feb 2016 17:40:58 +0000 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=QqujpgGd c=1 sm=1 tr=0 a=LKYHlaG36XqnqgCEqUWx/g==:117 a=LKYHlaG36XqnqgCEqUWx/g==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=IkcTkHD0fZMA:10 a=jFJIQSaiL_oA:10 a=mDV3o1hIAAAA:8 a=Fwo3Pw8wAAAA:8 a=kk91PF5mkg3SA71WNVMA:9 a=-YNqvQ8PmVra9-54:21 a=I_0BqL6MKUv0TD0x:21 a=QEXdDO2ut3YA:10 Original-Received: from dell.homenet (localhost [127.0.0.1]) by dell.homenet (Postfix) with ESMTP id C4C05443A39 for ; Mon, 22 Feb 2016 17:40:56 +0000 (GMT) In-Reply-To: <878u2dosxu.fsf@gnu.org> X-Mailer: Claws Mail 3.13.1 (GTK+ 2.24.29; x86_64-unknown-linux-gnu) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] X-Received-From: 84.93.230.235 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:12424 Archived-At: On Mon, 22 Feb 2016 13:01:01 +0100 ludo@gnu.org (Ludovic Court=C3=A8s) wrote: > Chris Vine skribis: >=20 > > It features an a-sync procedure (in coroutines.scm) which can be > > used to provide await semantics on asynchronous code (so as to > > remedy inversion of control), and will work with callbacks for any > > event loop, including the glib event loop wrapped by guile-gnome. > > More to the point, it also provides a thread safe event loop for > > guile (event-loop.scm) with support for watches on ports/file > > descriptors, and now supports proper timeouts, and permits events > > to be posted by other tasks. This includes tasks running on other > > threads, for which there is a helper procedure > > a-sync-run-task-in-thread. =20 >=20 > Interesting. Have you tried to integrate it with one of the > object-oriented event loops like in GLib? (Back in the day I thinking > about something like that to avoid the callback hell in Guile-Avahi.) >=20 > Thanks for the tasty dish! :-) >=20 > Ludo=E2=80=99. This is an example of how you might use a-sync with guile-gnome: ;;;;;;;; (use-modules (gnome glib) (coroutines)) (define main-loop (g-main-loop-new #f #f)) (a-sync (lambda (await resume) ;; launch asynchronous task (g-idle-add (lambda () (display "In first async callback\n") (resume "Hello via async\n") #f)) (display "About to make first wait\n") (display (string-append "Back in waitable procedure, and the callback say= s: " (await))) ;; launch another asynchronous task (g-idle-add (lambda () (display "In second async callback\n") (g-main-loop-quit main-loop) (resume) #f)) (display "About to make second wait\n") (await) (display "Quitting\n"))) (display "Starting main loop\n") (g-main-loop-run main-loop) ;;;;;;;; However, it is more useful with guile-gnome's GTK+ callbacks, or with glib file watches or timeouts, because although the glib main loop is thread safe, the guile-gnome wrapper for it is not, and I have had problems with worker threads posting with g-idle-add. That was one of the things that impelled me to write my own thread safe event loop. I have gone a little further with this and have added more convenience wrapper procedures which makes a-sync rather easy to use. I am preparing a guile-a-sync package which I will put on github. I have everything going except that I am finishing off adding a wrapper for clock_gettime() so that a monotonic clock is available for timeouts. It also has some bug fixes for the code I posted. The other thing that may require further work is the documentation. I am used to doxygen or gtk-doc, neither of which I imagine will parse guile scheme code, so I will have to look into what is available (I don't like info). I'll post the URL when I have put it up. Chris