Created
May 15, 2013 17:19
-
-
Save pmbauer/5585639 to your computer and use it in GitHub Desktop.
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
;; based on my code from test.benchmark | |
(ns raw-threadring | |
(:import [java.util.concurrent Exchanger])) | |
(def ^Exchanger output-pipe (Exchanger.)) | |
(defn relay [state msg] | |
(if (> msg 0) | |
(send (:next state) relay (dec msg)) | |
(.exchange output-pipe (:id state))) | |
state) | |
(defn make-ring [n] | |
(let [tail (agent {:id n}) | |
head (reduce (fn [next id] (agent {:id id :next next})) | |
tail | |
(range (dec n) 0 -1))] | |
(await (send tail assoc :next head)) | |
head)) | |
(defn run [agents iterations] | |
(let [head (make-ring agents)] | |
(send head relay iterations) | |
(.exchange output-pipe nil))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment