unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Andrew Tropin <andrew@trop.in>
To: guile-devel@gnu.org, Maxime Devos <maximedevos@telenet.be>
Cc: Andrew Tropin <andrew@trop.in>
Subject: [PATCH v4] Add atomic-box-update! function to (ice-9 atomic)
Date: Wed, 20 Sep 2023 12:17:17 +0400	[thread overview]
Message-ID: <20230920081734.6229-1-andrew@trop.in> (raw)
In-Reply-To: <87jzvztzi3.fsf@trop.in>

* module/ice-9/atomic.scm (atomic-box-update!): New variable.
---
Oops, v3 was incomplete, sending a new version.

Changes since v3:
Removed stale proc-args.

 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..c9d49112c 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!))
 
 (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)
+  "Atomically update the value of BOX to (PROC BOX-VALUE) and return the
+new value.  PROC may be called multiple times, and thus PROC should be
+free of side effects."
+  (let loop ((old-value (atomic-box-ref box)))
+    (let* ((new-value (proc old-value))
+           (cas-value (atomic-box-compare-and-swap! box old-value new-value)))
+      (if (eq? old-value cas-value)
+          new-value
+          (loop cas-value)))))
-- 
2.41.0




      parent reply	other threads:[~2023-09-20  8:17 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-19 12:20 [PATCH] Add atomic-box-update! function to (ice-9 atomic) Andrew Tropin
2023-06-21  9:06 ` Jean Abou Samra
2023-06-21  9:06   ` Jean Abou Samra
2023-06-21 16:46     ` Andrew Tropin
2023-06-21 16:54       ` Jean Abou Samra
2023-06-22  3:59         ` Andrew Tropin
2023-06-22  5:21           ` Philip McGrath
2023-06-22  9:02             ` Andrew Tropin
2023-06-22 21:42               ` Philip McGrath
2023-07-13  3:30                 ` Andrew Tropin
2023-08-22 10:51 ` Andrew Tropin
2023-08-22 10:59 ` [PATCH v2] " Andrew Tropin
2023-08-22 17:51   ` Maxime Devos
2023-08-24 16:19     ` Andrew Tropin
2023-08-24 15:38 ` [PATCH v3] " Andrew Tropin
2023-09-20  8:17 ` Andrew Tropin [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230920081734.6229-1-andrew@trop.in \
    --to=andrew@trop.in \
    --cc=guile-devel@gnu.org \
    --cc=maximedevos@telenet.be \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).