From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ian Price Newsgroups: gmane.lisp.guile.user Subject: Re: guile and coroutines controlled from C Date: Wed, 01 Aug 2012 14:13:42 +0100 Message-ID: <873946vjp5.fsf@Kagami.home> References: <877gtlvoc4.fsf@Kagami.home> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1343826853 28984 80.91.229.3 (1 Aug 2012 13:14:13 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Wed, 1 Aug 2012 13:14:13 +0000 (UTC) Cc: guile-user@gnu.org To: Vincent Bernat Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Wed Aug 01 15:14:12 2012 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 1SwYkV-0007t2-1j for guile-user@m.gmane.org; Wed, 01 Aug 2012 15:14:03 +0200 Original-Received: from localhost ([::1]:43256 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SwYkU-0003Po-8l for guile-user@m.gmane.org; Wed, 01 Aug 2012 09:14:02 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:53485) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SwYkJ-0003PD-I1 for guile-user@gnu.org; Wed, 01 Aug 2012 09:13:59 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SwYkI-0005UC-5J for guile-user@gnu.org; Wed, 01 Aug 2012 09:13:51 -0400 Original-Received: from mail-wg0-f49.google.com ([74.125.82.49]:52010) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SwYkH-0005Tn-U6 for guile-user@gnu.org; Wed, 01 Aug 2012 09:13:50 -0400 Original-Received: by wgbez12 with SMTP id ez12so5327878wgb.30 for ; Wed, 01 Aug 2012 06:13:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-type; bh=lD8lB5K1VDhkpraUN34EsTZILqQ1Y1j4Ru7ATaqlnuo=; b=Vpb2FyZl/s3WRfKalxBKH4QQjGpOLmRPGeRfOu3B6Xat+eyZPrMvt0WNqTacocwvMp Ut8IvomkUZmNdVLQRwHQdDmsmMsJVGSp6RyFRvlFgenFmkOYgmq0Y/HZxxjSeDQAFbA0 KYsE3cyrhnt9OpnCrzDD5RjGXP+uCBeAqM0zEyJCj3vc6LP7mGbVfOfv1e3eIeIQi3MJ p0KMvuLnUjeeJ24VwU4yAwmy3UYp+19BFlNDQDtax+dmNa/rXTQBXDtS8DKOE2nIEW/x KlGEIaq3XT64Q3H/NaU0KDs3cYcpYFwX6ZaHAyME+3yv13TbPaeEvr0bufm5s8hQ9isE 8z/w== Original-Received: by 10.216.24.85 with SMTP id w63mr9246659wew.145.1343826828756; Wed, 01 Aug 2012 06:13:48 -0700 (PDT) Original-Received: from Kagami.home (host81-159-120-127.range81-159.btcentralplus.com. [81.159.120.127]) by mx.google.com with ESMTPS id ck9sm8683931wib.2.2012.08.01.06.13.46 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 01 Aug 2012 06:13:47 -0700 (PDT) In-Reply-To: (Vincent Bernat's message of "Mon, 30 Jul 2012 08:28:26 +0200") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 74.125.82.49 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:9541 Archived-At: Vincent Bernat writes: > Your example is great! It fits exactly what I need. Since I am still > pretty new, I need some time to understand each line but this seems a > very good start for what I want to do. I just need to translate some > parts in C since the event loop is here. From what I understand, only > the `run` function needs to be moved in C. The remaining of the code can > be hidden in functions or macros (but it would help if the `yield` part > could be translated in C). Well, it all depends on the exact API you want to provide/code to. So, let's take your "do-something-blocking" function. One way to do this is to have the yield function be a piece of local state that is only available when the "thread" is running, for that I'd use a parameter, or fluid variable. (see the sections 6.21.7/6.21.8 of the manual). Then these functions would query that, and call the yield function to return, after registering that the work needs to be done in a different thread (or however you plan on doing this). I'd want the thread to be known to the function, so that it could store, say store the return value in it, so that you don't need to explicitly provide this when you resume. You probably also want to provide a flag in the thread data type saying whether or not it could be resumed. Like I say, it all depends on the API you want to code to. I could whip up some example code that does the above if you'd like. > Just one other question. > >> (define thread1 >> (make-thread >> (lambda (yield) >> (let loop ((i 3)) >> (if (zero? i) >> 'done >> (begin >> (display "in thread 1...\n") >> (yield) >> (loop (- i 1)))))))) > > After `yield`, the routine is resumed when scheduled by the previous > `run` function. In my case, yielding happened because I was waiting for data > from the network. How could I return the appropriate data on resume? I > could call some function that will provide the revelant data from some > cache just after yield: > > #v+ > (yield) > (some-computation-with (grab-result-local-to-this-thread)) > #v- > > However, maybe `yield` could return the result? Right, when you invoke a continuation, you actually give it a value, which is used as the return value for the call. I actually make use of this in the code above, so that the return value of the threads function returns 'not-done. In this case, when you want a thread to return a value you just call that thread variable with the value. In the example I posted, the return value is actually always the yield function. Though, some rewriting could allow you to return other values. -- Ian Price "Programming is like pinball. The reward for doing it well is the opportunity to do it again" - from "The Wizardy Compiled"