is it possible to check if a "promise" has been "deliver"ed to without blocking?

8 views
Skip to first unread message

Sunil S Nandihalli

unread,
Sep 14, 2010, 2:51:50 AM9/14/10
to clo...@googlegroups.com
Hello Everybody,
 Let us say I have something like

(def s (promise))
.
.
.
.
now can I check if the promise hase been delivered ..

Thanks 
Sunil.
  

Laurent PETIT

unread,
Sep 14, 2010, 3:13:57 AM9/14/10
to clo...@googlegroups.com
Nope.

here's the source:

Clojure classpath initialized by cljr.
user=> (source promise)
(defn promise
  "Alpha - subject to change.
  Returns a promise object that can be read with deref/@, and set,
  once only, with deliver. Calls to deref/@ prior to delivery will
  block. All subsequent derefs will return the same delivered value
  without blocking."
  {:added "1.1"}
  []
  (let [d (java.util.concurrent.CountDownLatch. 1)
        v (atom nil)]
    (reify 
     clojure.lang.IDeref
      (deref [_] (.await d) @v)
     clojure.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"))))))))


2010/9/14 Sunil S Nandihalli <sunil.na...@gmail.com>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Meikel Brandmeyer

unread,
Sep 14, 2010, 3:18:00 AM9/14/10
to Clojure

Hubert Iwaniuk

unread,
Sep 14, 2010, 3:24:56 AM9/14/10
to clo...@googlegroups.com
Hi Sunil,

Before it gets to clojure.core you could use it like that:
http://github.com/neotyk/http.async.client/blob/master/src/http/async/client/util.clj#L21

Cheers,
Hubert.

Sunil S Nandihalli

unread,
Sep 14, 2010, 3:39:46 AM9/14/10
to clo...@googlegroups.com
Thanks Hubert,
 That is exactly what I was looking for.
Sunil.
--
************************************************
Sunil S Nandihalli,
Manager and Technical Lead,
GridPro India Operations,
Program Development Co. LLC India Branch
Bangalore.
ph: 9980520430

Hubert Iwaniuk

unread,
Sep 14, 2010, 8:11:12 AM9/14/10
to clo...@googlegroups.com
Glad I could help,
Hubert.
Reply all
Reply to author
Forward
0 new messages