Last active
August 29, 2015 14:11
-
-
Save raspasov/7f89bc69884fea298468 to your computer and use it in GitHub Desktop.
Clojure STM operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn new-ops-collector | |
"Create a new op collector | |
The collector is a vector of vectors, each internal vector | |
is a tuple of f and vector-of-params, i.e. [f vector-of-params]. | |
Later on we execute all the ops in a single transaction via (execute-ops collector) | |
Example structure: [[f [param1 param2 etc]] etc ] " | |
[] | |
(atom [])) | |
(defn add-op | |
"Schedule an op to be executed in a transaction" | |
[ops-collector f params] | |
(swap! ops-collector conj [f params])) | |
(defn execute-ops | |
"Execute all accumulated ops in a single STM transaction" | |
[ops-collector] | |
(dosync (doseq [[^IFn f ^APersistentVector params] @ops-collector] (apply f params)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment