diff options
author | Rich Hickey <richhickey@gmail.com> | 2008-12-04 12:46:49 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2008-12-04 12:46:49 +0000 |
commit | 565019e78a7d093ab50e9b95e093df0ed3b3a40f (patch) | |
tree | a12b32baab1b41d7548edaf3581fa4b312f627b0 /src | |
parent | acfea200853290a260842ac645e9d579bd6d2ae2 (diff) |
swap! returns new value
Diffstat (limited to 'src')
-rw-r--r-- | src/clj/clojure/core_proxy.clj | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/src/clj/clojure/core_proxy.clj b/src/clj/clojure/core_proxy.clj index 94a7b324..897d15e9 100644 --- a/src/clj/clojure/core_proxy.clj +++ b/src/clj/clojure/core_proxy.clj @@ -348,8 +348,9 @@ (defn swap! "Atomically swaps the value of atom to be: - (apply f current-value-of-atom args). - Returns nil" + (apply f current-value-of-atom args). Note that f may be called + multiple times, and thus should be free of side effects. Returns + the value that was swapped in." [#^AtomicReference atom f & args] (let [validate (get-validator atom)] (loop [oldv (.get atom)] @@ -359,7 +360,8 @@ (validate newv) (catch Exception e (throw (IllegalStateException. "Invalid atom state" e))))) - (when-not (.compareAndSet atom oldv newv) + (if (.compareAndSet atom oldv newv) + newv (recur (.get atom))))))) (defn compare-and-set! |