PageRenderTime 27ms CodeModel.GetById 22ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

/interpreter/tags/at2-build270707/src/edu/vub/at/actors/eventloops/BlockingFuture.java

http://ambienttalk.googlecode.com/
Java | 138 lines | 48 code | 14 blank | 76 comment | 5 complexity | d5bbd6baec2ead506d01b29d48f7a48f MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * BlockingFuture.java created on 21-dec-2006 at 10:16:26
  4 * (c) Programming Technology Lab, 2006 - 2007
  5 * Authors: Tom Van Cutsem & Stijn Mostinckx
  6 * 
  7 * Permission is hereby granted, free of charge, to any person
  8 * obtaining a copy of this software and associated documentation
  9 * files (the "Software"), to deal in the Software without
 10 * restriction, including without limitation the rights to use,
 11 * copy, modify, merge, publish, distribute, sublicense, and/or
 12 * sell copies of the Software, and to permit persons to whom the
 13 * Software is furnished to do so, subject to the following
 14 * conditions:
 15 *
 16 * The above copyright notice and this permission notice shall be
 17 * included in all copies or substantial portions of the Software.
 18 *
 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 26 * OTHER DEALINGS IN THE SOFTWARE.
 27 */
 28package edu.vub.at.actors.eventloops;
 29
 30
 31/**
 32 * A BlockingFuture represents a synchronous, blocking future used by the AT/2 implementation
 33 * to synchronize between native threads.
 34 * 
 35 * Usage:
 36 *  <code>
 37 *    BlockingFuture future = object.doAsynchronousComputation();
 38 *    try {
 39 *      Object result = future.get();
 40 *    } catch(Exception e) {
 41 *      // async computation raised an exception
 42 *    }
 43 *  </code>
 44 *
 45 * Based on source code written by Doug Lea with assistance from members of JCP JSR-166
 46 * Expert Group and released to the public domain.
 47 *
 48 * @author tvcutsem
 49 */
 50public final class BlockingFuture implements Future {
 51	
 52    private Object result;
 53    
 54    private Exception exception;
 55    
 56    private boolean fulfilled = false;
 57    
 58    /** Returns whether the future has been either resolved with a value or ruined by an exception */
 59    public synchronized boolean isDetermined() {
 60        return fulfilled;
 61    }
 62	
 63	public synchronized Object get() throws Exception {
 64		waitFor();
 65		return getResult();
 66	}
 67	
 68    /**
 69     * Sets the result of this Future to the given value unless
 70     * this future has already been set or has been cancelled.
 71     * @param v the value
 72     */
 73    public void resolve(Object v) {
 74        setCompleted(v);
 75    }
 76
 77    /**
 78     * Causes this future to report an <tt>ExecutionException</tt>
 79     * with the given throwable as its cause, unless this Future has
 80     * already been set or has been cancelled.
 81     * @param e the cause of failure
 82     */
 83    public void ruin(Exception e) {
 84        setFailed(e);
 85    }
 86    
 87    /**
 88     * Marks the task as completed.
 89     * @param result the result of a task.
 90     */
 91    private void setCompleted(Object result) {
 92        synchronized (this) {
 93            if (fulfilled) return;
 94            fulfilled = true;
 95            this.result = result;
 96            notifyAll();
 97        }
 98    }
 99
100    /**
101     * Marks the task as failed.
102     * @param exception the cause of abrupt completion.
103     */
104    private void setFailed(Exception exception) {
105        synchronized (this) {
106            if (fulfilled) return;
107            fulfilled = true;
108            this.exception = exception;
109            notifyAll();
110        }
111    }
112	
113    /**
114     * Waits for the task to complete.
115     * PRE: lock owned
116     */
117    private void waitFor() {
118        while (!isDetermined()) {
119            try {
120				wait();
121			} catch (InterruptedException e) { }
122        }
123    }
124
125    /**
126     * Gets the result of the task.
127     *
128     * PRE: task completed
129     * PRE: lock owned
130     */
131    private Object getResult() throws Exception {
132        if (exception != null) {
133            throw exception;
134        }
135        return result;
136    }
137    
138}