unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* [PATCH] Add atomic-box-update! function to (ice-9 atomic)
@ 2023-06-19 12:20 Andrew Tropin
  2023-06-21  9:06 ` Jean Abou Samra
                   ` (4 more replies)
  0 siblings, 5 replies; 16+ messages in thread
From: Andrew Tropin @ 2023-06-19 12:20 UTC (permalink / raw)
  To: guile-devel

[-- Attachment #1: Type: text/plain, Size: 1750 bytes --]


* 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!))
 
 (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)))))
-- 
2.40.1

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2023-09-20  8:17 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH v4] " Andrew Tropin

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).