From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jan Wielkiewicz Newsgroups: gmane.lisp.guile.user Subject: Re: guile fibers - looking for adivce Date: Thu, 10 Sep 2020 03:05:15 +0200 Message-ID: <20200910025543.114bbe38@interia.pl> References: <20200906024757.58dd34cd@interia.pl> <20200907185627.72eca419@interia.pl> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="25863"; mail-complaints-to="usenet@ciao.gmane.io" Cc: guile-user To: Aleix Conchillo =?UTF-8?B?RmxhcXXDqQ==?= Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Thu Sep 10 01:05:49 2020 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kG99w-0006cI-LF for guile-user@m.gmane-mx.org; Thu, 10 Sep 2020 01:05:48 +0200 Original-Received: from localhost ([::1]:57796 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kG99v-0000ky-Kd for guile-user@m.gmane-mx.org; Wed, 09 Sep 2020 19:05:47 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:35452) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kG99g-0000kp-Lg for guile-user@gnu.org; Wed, 09 Sep 2020 19:05:33 -0400 Original-Received: from smtpo.poczta.interia.pl ([217.74.65.238]:46813) by eggs.gnu.org with esmtps (TLS1.2:RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kG99d-0007k0-PD for guile-user@gnu.org; Wed, 09 Sep 2020 19:05:32 -0400 X-Interia-R: Interia X-Interia-R-IP: 89.64.43.25 X-Interia-R-Helo: Original-Received: from localhost (89-64-43-25.dynamic.chello.pl [89.64.43.25]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by poczta.interia.pl (INTERIA.PL) with ESMTPSA; Thu, 10 Sep 2020 01:05:24 +0200 (CEST) In-Reply-To: X-Mailer: Claws Mail 3.17.6 (GTK+ 2.24.32; x86_64-unknown-linux-gnu) X-Interia-Antivirus: OK DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=interia.pl; s=biztos; t=1599692724; bh=+tpnMY1kTyegnYbUoNQKL3Xmq4YO8robVeTJC3MYauM=; h=X-Interia-R:X-Interia-R-IP:X-Interia-R-Helo:Date:From:To:Cc: Subject:Message-ID:In-Reply-To:References:X-Mailer:MIME-Version: Content-Type:Content-Transfer-Encoding:X-Interia-Antivirus; b=quwNR+m6f1w8pn7m2c2Udso/1ckltzrm3nkI3YrM58wOEdk86SVGfLc+aFhZqBnDH QRG/WafuxvwsabQVyEiohsOkFIopof2WWtrfaQSCuYVxpkt3wBfAyy05G2dZh15Mvm GK8CaPoDYfXvbPXdakfgtEuJjtCYYMzf/XMIiQ9E= Received-SPF: pass client-ip=217.74.65.238; envelope-from=tona_kosmicznego_smiecia@interia.pl; helo=smtpo.poczta.interia.pl X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/09 19:05:25 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 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-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:16898 Archived-At: Hello, Dnia 2020-09-07, o godz. 17:25:38 Aleix Conchillo Flaqu=C3=A9 napisa=C5=82(a): > Hi Jan, >=20 > To be honest, I've never used GOOPS so things might be a bit more > complicated there, I don't know. But it sounds like you have two > options: >=20 > - Create a fiber with the object and pass data from the object using > channels to other fibers. Then return data to the main fiber (or the > fiber that has the object) through a channel and update your object. > - Make the object global and have fibers that update the object. In > this case you would need to use mutexes. >=20 > Or maybe you find another way? >=20 > > That said, I might be wrong though. > > > > > > Hope this helps, > > > > > > Aleix > > > > Thanks for explanation, this made my thinking much cleaner. I can > > get back to experimenting now! > > > > > Cool, let us know! > Aleix I actually made a working prototype. Basically I created a new allocation option for GOOPS slots - #:message. The default allocation type is #:instance, as explained here: https://www.gnu.org/software/guile/manual/html_node/Slot-Options.html The #:message allocation type I introduced treats setting a slot like sending a message and reading the slot like receiving a message. I used the example from GOOPS manual as inspiration: https://www.gnu.org/software/guile/manual/html_node/Customizing-Class-Defin= ition.html Here's the code. It introduces a new metaclass to modify the default behavior and introduces the #:message allocation type. (define-class ()) (define-method (compute-get-n-set (class ) s) (define set-channel (make-channel)) (define get-channel (make-channel)) (define stored-value '()) (define initialized? #f) (define (value-updater ch) (spawn-fiber (lambda () (let loop () (set! stored-value (get-message ch)) (loop))))) (case (slot-definition-allocation s) ((#:message) (begin (list (lambda (o) (if initialized? (begin (spawn-fiber (lambda () (put-message get-channel stored-value))) (get-message get-channel)))) (lambda (o v) (spawn-fiber (lambda () (if (not initialized?) (begin (set! initialized? #t) (value-updater set-channel))) (put-message set-channel v))))))) (else (next-method)))) Using this in practice looks something like this: (define-class () (number-of-legs #:setter set-number-of-legs #:getter get-number-of-legs #:allocation #:message) (toys #:setter set-toys #:getter get-toys #:allocation #:message) #:metaclass ) (define (main) (run-fibers (lambda () (define dog1 (make )) (spawn-fiber (lambda () (set-number-of-legs dog1 4) (display "number of legs:\n") (display (get-number-of-legs dog1 4)) (set-toys dog1 (list 'bone 'dead-rat 'something))))) #:drain? #t)) (main) This solution allows setting and getting values safely over fibers through messages, eliminating the problem of parallely editing the same slot of an object (thanks to just one fiber being able to edit the real value (value-updater)). If I'm not missing something important, this is a real solution for a real problem. I've already tested it with my toy program. If you have any suggestions, feel free to tell me, also feel free to use my code, if you find it useful. I could push this a bit further by making the allocate all slots by default as #:message - this way every object would work safely on fibers, without the boilerplate code. Thanks for help, everyone.=20 Jan Wielkiewicz