From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Andrew Tropin Newsgroups: gmane.lisp.guile.devel Subject: Re: [PATCH] Add atomic-box-update! function to (ice-9 atomic) Date: Tue, 22 Aug 2023 14:51:59 +0400 Message-ID: <87ttsr8i40.fsf@trop.in> References: <87jzvztzi3.fsf@trop.in> Mime-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha512; protocol="application/pgp-signature" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="31431"; mail-complaints-to="usenet@ciao.gmane.io" To: guile-devel@gnu.org, Andy Wingo , Chris Lemmer-Webber Original-X-From: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Tue Aug 22 12:52:47 2023 Return-path: Envelope-to: guile-devel@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 1qYP0A-0007uk-QY for guile-devel@m.gmane-mx.org; Tue, 22 Aug 2023 12:52:46 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qYOzn-0003I6-45; Tue, 22 Aug 2023 06:52:23 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYOze-0003Fy-4L for guile-devel@gnu.org; Tue, 22 Aug 2023 06:52:14 -0400 Original-Received: from relay9-d.mail.gandi.net ([2001:4b98:dc4:8::229]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYOzZ-0000wt-3Z for guile-devel@gnu.org; Tue, 22 Aug 2023 06:52:12 -0400 Original-Received: by mail.gandi.net (Postfix) with ESMTPSA id 7AE68FF803; Tue, 22 Aug 2023 10:52:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=trop.in; s=gm1; t=1692701525; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=5Dw1csCQE85YDwoh1EaWbJr1/7LHKu5bLe26QnFuSjY=; b=XZ/14Amkkw4HocJlNsHdjjVTIFLSCiCnpATJYurVuQ3rLrUPxn4lm3GH3fCr38HHrjt0dE ojwKaYjsILJ4EYXLXI4Nq2cNa0Fnyax8GA7ygrQt2rdTf2wNNRmKUGH+O4MKYlNDzx9CTE B/hAxyAcjKcMFLlW0T2qt9ismCxQHH6jaBQMmRjBax4MufLX1sjCIVUAfljc62/ARueScw E6crw3Gwn49s2M/cFSe2k3JkyoAz6+1EkMCaQ/5FYU/ZqbLh4TjesePigZ7+9udPoGX8wQ COJrKGOh9nDXnpdfi+lO8V9s2pUkQQ5r30aCP+YS5lOv+Jbwokc9Gi3BlzS6vQ== In-Reply-To: <87jzvztzi3.fsf@trop.in> X-GND-Sasl: andrew@trop.in Received-SPF: pass client-ip=2001:4b98:dc4:8::229; envelope-from=andrew@trop.in; helo=relay9-d.mail.gandi.net 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, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.devel:21920 Archived-At: --=-=-= Content-Type: text/plain Content-Transfer-Encoding: quoted-printable On 2023-06-19 16:20, Andrew Tropin wrote: > * module/ice-9/atomic.scm (atomic-box-update!): New variable. > --- > I was implementing some concurrent code and started to use (ice-9 > atomic), when found out that there is no procedure for updating the > value of the atom using another function. > > IMHO, atomic-box-update! fits well FP paradigm (which is widely used > across guile-based projects) in general and this module in particular > and should be provided out of the box. Made a draft implementation. > LMKWYT. > > module/ice-9/atomic.scm | 14 +++++++++++++- > 1 file changed, 13 insertions(+), 1 deletion(-) > > diff --git a/module/ice-9/atomic.scm b/module/ice-9/atomic.scm > index 2a8af901d..6bfa2e8ee 100644 > --- a/module/ice-9/atomic.scm > +++ b/module/ice-9/atomic.scm > @@ -25,7 +25,8 @@ > atomic-box-ref > atomic-box-set! > atomic-box-swap! > - atomic-box-compare-and-swap!)) > + atomic-box-compare-and-swap! > + atomic-box-update!)) >=20=20 > (eval-when (expand load eval) > (load-extension (string-append "libguile-" (effective-version)) > @@ -36,3 +37,14 @@ > (add-interesting-primitive! 'atomic-box-set!) > (add-interesting-primitive! 'atomic-box-swap!) > (add-interesting-primitive! 'atomic-box-compare-and-swap!)) > + > +(define (atomic-box-update! box proc . proc-args) > + "Atomically updates value of BOX to (APPLY PROC BOX-VALUE PROC-ARGS), > +returns new value. PROC may be called multiple times, and thus PROC > +should be free of side effects." > + (let loop () > + (let* ((old-value (atomic-box-ref box)) > + (new-value (apply proc old-value proc-args))) > + (if (eq? old-value (atomic-box-compare-and-swap! box old-value new= -value)) > + new-value > + (loop))))) Found one more example of the usage of this pattern in fibers library. Those lines by Christine Lemmer-Webber: https://github.com/wingo/fibers/blob/581319ff13b97f2bb51dff713524aad696c5d5= 2b/fibers/counter.scm#L37 =2D-=20 Best regards, Andrew Tropin --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEKEGaxlA4dEDH6S/6IgjSCVjB3rAFAmTkk08ACgkQIgjSCVjB 3rBSsxAAjL004zo1volJJm1CmOxGjG9FFsiRdOuJWUFISpC2XIEZkmGyI9ZrdDmN wrAHToQ/HtF07iAMDxU+ArH/BmnoY/V3OUMltY2lzAtW+b/Wl+bW84Gipuv41335 AbVRV+aSyj9neQg1lHQorkb2Y0iIvcbg7jxBtR/axECd9ICJXMd7K+o+dbO7XVm3 2ivoZPrd7xAXxx/7I1ZMLHj78cp7w/y3AtY5UsYsX+y66OkqIi3uXoTfFetANj2Z QJ6nhxlhCP7NnhNTksSYwWouciruav2X3oQRjIudBTK6no1DOwUEn+GAz8VGUtTf ROFNzJ+LQ1cibwSvMae4NNBfwB2x2JXdpWkK707zTLHEJm8U7GC7eMyyu6UpWCa8 dvxr+EGUVkdU4FwqD77PFwtx+4FfXST1Vj2rfcU75vL4ylWytINomn3CjdGT/F/c xPRYFXylJnsV59g0SMTddETWNVtj5altBE4P3ETfafd6VCeQ8XLe2lDKTCxmxkzN KMJlhUfMkThzBrF27MLTwf2Z6e2fcSXdXYip1umBVDadmgjYeGa48He7hE2U1mcc aPhMp1Ynyj5V0sWJaFSZFx0f48Dg3CwjykdL2s78OEggpXxBiyxLOrNLviCPsrB9 vUUL5jbuu9M4LdD8q7zhRKiRtZD4JPKV0Ei52O6Og7gaHTF7mbU= =7cV+ -----END PGP SIGNATURE----- --=-=-=--