On 1.3 master there is: clojure.core/deref ([ref] [ref timeout-ms timeout-val]) Also reader macro: @ref/@agent/@var/@atom/@delay/@future/@promise. Within a transaction, returns the in-transaction-value of ref, else returns the most-recently-committed value of ref. When applied to a var, agent or atom, returns its current state. When applied to a delay, forces it if not already forced. When applied to a future, will block if computation not complete. When applied to a promise, will block until a value is delivered. The variant taking a timeout can be used for blocking references (futures and promises), and will return timeout-val if the timeout (in milliseconds) is reached before a value is available. See also - realized?.
Stu > Anyway, maybe this will come in handy: > > (defn timeout-promise > "A promise with a timeout. When dereffed, returns the > delivered value if already delivered, otherwise blocks > until delivered or the deadline expires, whichever comes > first. Returns the delivered value if delivered in time and > if-timeout if the deadline expires first. > > Ex.: user=> (def q (promise 1000 :timed-out)) > user=>(do (future (Thread/sleep 900) (deliver q 42)) @q) > 42 > user=> (def q (promise 1000 :timed-out)) > user=>(do (future (Thread/sleep 1100) (deliver q 42)) @q) > :timed-out" > [timeout-in-ms if-timeout] > (let [d (java.util.concurrent.CountDownLatch. 1) > v (atom nil)] > (reify > clojure.lang.IDeref > (deref [_] (if (.await d timeout-in-ms ms) > @v > if-timeout)).lang.IFn > (invoke [this x] > (locking d > (if (pos? (.getCount d)) > (do (reset! v x) > (.countDown d) > this) > (throw (IllegalStateException. > "Multiple deliver calls to a promise")))))))) > > Since the above code is derived from the existing implementation of > promise, it therefore automatically inherits the Clojure license terms > as the terms for its use, if I understand copyright law correctly. > > The only changes from the implementation of promise are: > > 1. Docstring. > 2. The two arguments. > 3. Calls the await method of CountDownLatch that takes a timeout. > 4. Conditions on the return value and returns if-timeout on failure. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en