/interpreter/tags/at2-build060407/src/edu/vub/at/actors/eventloops/BlockingFuture.java
Java | 137 lines | 48 code | 14 blank | 75 comment | 5 complexity | 84ab0619c864cd9808f1928e220e2286 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 public synchronized boolean isDetermined() { 59 return fulfilled; 60 } 61 62 public synchronized Object get() throws Exception { 63 waitFor(); 64 return getResult(); 65 } 66 67 /** 68 * Sets the result of this Future to the given value unless 69 * this future has already been set or has been cancelled. 70 * @param v the value 71 */ 72 public void resolve(Object v) { 73 setCompleted(v); 74 } 75 76 /** 77 * Causes this future to report an <tt>ExecutionException</tt> 78 * with the given throwable as its cause, unless this Future has 79 * already been set or has been cancelled. 80 * @param e the cause of failure 81 */ 82 public void ruin(Exception e) { 83 setFailed(e); 84 } 85 86 /** 87 * Marks the task as completed. 88 * @param result the result of a task. 89 */ 90 private void setCompleted(Object result) { 91 synchronized (this) { 92 if (fulfilled) return; 93 fulfilled = true; 94 this.result = result; 95 notifyAll(); 96 } 97 } 98 99 /** 100 * Marks the task as failed. 101 * @param exception the cause of abrupt completion. 102 */ 103 private void setFailed(Exception exception) { 104 synchronized (this) { 105 if (fulfilled) return; 106 fulfilled = true; 107 this.exception = exception; 108 notifyAll(); 109 } 110 } 111 112 /** 113 * Waits for the task to complete. 114 * PRE: lock owned 115 */ 116 private void waitFor() { 117 while (!isDetermined()) { 118 try { 119 wait(); 120 } catch (InterruptedException e) { } 121 } 122 } 123 124 /** 125 * Gets the result of the task. 126 * 127 * PRE: task completed 128 * PRE: lock owned 129 */ 130 private Object getResult() throws Exception { 131 if (exception != null) { 132 throw exception; 133 } 134 return result; 135 } 136 137}