diff options
| author | Estevan Castilho <tevo@riajuu.net> | 2026-08-13 02:35:36 +0000 |
|---|---|---|
| committer | Estevan Castilho <tevo@riajuu.net> | 2026-08-23 03:06:55 +0000 |
| commit | afbab0968340abcec044d9ac70c4a3ec0756933e (patch) | |
| tree | 327ef2f45d8360e774a44586650b8baee14d5927 /lib/Utilities.ml | |
| parent | f885a8f7dd85d79c5c8aa93265f5dea781b345a9 (diff) | |
| download | RNT-afbab0968340abcec044d9ac70c4a3ec0756933e.tar.gz RNT-afbab0968340abcec044d9ac70c4a3ec0756933e.zip | |
Atomic swap (and monadic swap)
Diffstat (limited to 'lib/Utilities.ml')
| -rw-r--r-- | lib/Utilities.ml | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/Utilities.ml b/lib/Utilities.ml index 9ae0119..0a5bd9d 100644 --- a/lib/Utilities.ml +++ b/lib/Utilities.ml @@ -10,3 +10,20 @@ module Result = struct ms (Ok []) end + +module Atomic = struct + let rec swap atom f = + let v = Atomic.get atom in + let v' = f v in + if Atomic.compare_and_set atom v v' + then atom + else swap atom f + + let rec mswap atom f = + let open Result in + let v = Atomic.get atom in + let* v' = f v in + if Atomic.compare_and_set atom v v' + then Ok atom + else mswap atom f +end |
