[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2] Add atomic-box-update! function to (ice-9 atomic)
From: |
Andrew Tropin |
Subject: |
[PATCH v2] Add atomic-box-update! function to (ice-9 atomic) |
Date: |
Tue, 22 Aug 2023 14:59:13 +0400 |
* module/ice-9/atomic.scm (atomic-box-update!): New variable.
---
Changes since v1. Use single-argument proc to avoid potential perfomance
problems cause of call to apply.
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)
+ "Atomically updates value of BOX to (PROC BOX-VALUE), 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 (proc old-value)))
+ (if (eq? old-value (atomic-box-compare-and-swap! box old-value
new-value))
+ new-value
+ (loop)))))
--
2.40.1
signature.asc
Description: PGP signature
- [PATCH v2] Add atomic-box-update! function to (ice-9 atomic),
Andrew Tropin <=