[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v3] Add atomic-box-update! function to (ice-9 atomic)
From: |
Andrew Tropin |
Subject: |
[PATCH v3] Add atomic-box-update! function to (ice-9 atomic) |
Date: |
Thu, 24 Aug 2023 19:38:39 +0400 |
* module/ice-9/atomic.scm (atomic-box-update!): New variable.
---
Changes since v2:
1. Removed unecessary atomic-box-ref.
2. Update docstring to imperative mood.
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..aaa7238fc 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 proc-args))
+ (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
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [PATCH v3] Add atomic-box-update! function to (ice-9 atomic),
Andrew Tropin <=