|
4023 | 4023 |
|
4024 | 4024 | Defaults to true") |
4025 | 4025 |
|
| 4026 | +(defn future? |
| 4027 | + "Returns true if x is a future" |
| 4028 | + [x] (instance? java.util.concurrent.Future x)) |
| 4029 | + |
| 4030 | +(defn future-done? |
| 4031 | + "Returns true if future f is done" |
| 4032 | + [#^java.util.concurrent.Future f] (.isDone f)) |
| 4033 | + |
4026 | 4034 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; helper files ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
4027 | 4035 | (alter-meta! (find-ns 'clojure.core) assoc :doc "Fundamental library of the Clojure language") |
4028 | 4036 | (load "core_proxy") |
|
4052 | 4060 | not yet finished, calls to deref/@ will block." |
4053 | 4061 | [& body] `(future-call (fn [] ~@body))) |
4054 | 4062 |
|
| 4063 | + |
| 4064 | +(defn future-cancel |
| 4065 | + "Cancels the future, if possible." |
| 4066 | + [#^java.util.concurrent.Future f] (.cancel f true)) |
| 4067 | + |
| 4068 | +(defn future-cancelled? |
| 4069 | + "Returns true if future f is cancelled" |
| 4070 | + [#^java.util.concurrent.Future f] (.isCancelled f)) |
| 4071 | + |
4055 | 4072 | (defn pmap |
4056 | 4073 | "Like map, except f is applied in parallel. Semi-lazy in that the |
4057 | 4074 | parallel computation stays ahead of the consumption, but doesn't |
|
4131 | 4148 | (str "-" q)) |
4132 | 4149 | (when (:interim *clojure-version*) |
4133 | 4150 | "-SNAPSHOT"))) |
| 4151 | + |
| 4152 | +(defn promise |
| 4153 | + "Experimental. |
| 4154 | + Returns a promise object that can be read with deref/@, and set, |
| 4155 | + once only, with deliver. Calls to deref/@ prior to delivery will |
| 4156 | + block. All subsequent derefs will return the same delivered value |
| 4157 | + without blocking." |
| 4158 | + [] |
| 4159 | + (let [d (java.util.concurrent.CountDownLatch. 1) |
| 4160 | + v (atom nil)] |
| 4161 | + (proxy [clojure.lang.AFn clojure.lang.IDeref] [] |
| 4162 | + (deref [] (.await d) @v) |
| 4163 | + (invoke [x] |
| 4164 | + (locking d |
| 4165 | + (if (pos? (.getCount d)) |
| 4166 | + (do (.countDown d) |
| 4167 | + (reset! v x) |
| 4168 | + this) |
| 4169 | + (throw (IllegalStateException. "Multiple deliver calls to a promise")))))))) |
| 4170 | + |
| 4171 | +(defn deliver |
| 4172 | + "Experimental. |
| 4173 | + Delivers the supplied value to the promise, releasing any pending |
| 4174 | + derefs. A subsequent call to deliver on a promise will throw an exception." |
| 4175 | + [promise val] (promise val)) |
0 commit comments