MCPcopy Index your code
hub / github.com/clojure/clojure / Delay

Class Delay

src/jvm/clojure/lang/Delay.java:18–68  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16import java.util.concurrent.locks.ReentrantLock;
17
18public class Delay implements IDeref, IPending{
19Object val;
20Throwable exception;
21IFn fn;
22volatile Lock lock;
23
24public Delay(IFn f){
25 fn = f;
26 val = null;
27 exception = null;
28 lock = new ReentrantLock();
29}
30
31static public Object force(Object x) {
32 return (x instanceof Delay) ?
33 ((Delay) x).deref()
34 : x;
35}
36
37private void realize() {
38 Lock l = lock;
39 if(l != null) {
40 l.lock();
41 try {
42 if(fn!=null) {
43 try {
44 val = fn.invoke();
45 } catch (Throwable t) {
46 exception = t;
47 }
48 fn = null;
49 lock = null;
50 }
51 } finally {
52 l.unlock();
53 }
54 }
55}
56
57public Object deref() {
58 if(lock != null)
59 realize();
60 if(exception != null)
61 throw Util.sneakyThrow(exception);
62 return val;
63}
64
65public boolean isRealized(){
66 return lock == null;
67}
68}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected