/interpreter/tags/reactive-pattern-matching/src/edu/vub/at/actors/eventloops/BlockingFuture.java

http://ambienttalk.googlecode.com/ · Java · 147 lines · 56 code · 14 blank · 77 comment · 7 complexity · 7bbe4417781ca6db574777ec95903a3e 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. */
  28. package edu.vub.at.actors.eventloops;
  29. /**
  30. * A BlockingFuture represents a synchronous, blocking future used by the AT/2 implementation
  31. * to synchronize between native threads.
  32. *
  33. * Usage:
  34. * <code>
  35. * BlockingFuture future = object.doAsynchronousComputation();
  36. * try {
  37. * Object result = future.get();
  38. * } catch(Exception e) {
  39. * // async computation raised an exception
  40. * }
  41. * </code>
  42. *
  43. * Based on source code written by Doug Lea with assistance from members of JCP JSR-166
  44. * Expert Group and released to the public domain.
  45. *
  46. * @author tvcutsem
  47. */
  48. public final class BlockingFuture implements Future {
  49. private Object result;
  50. private Exception exception;
  51. private boolean fulfilled = false;
  52. /** Returns whether the future has been either resolved with a value or ruined by an exception */
  53. public synchronized boolean isDetermined() {
  54. return fulfilled;
  55. }
  56. public Object get() throws Exception {
  57. Object result;
  58. synchronized(this) {
  59. waitFor();
  60. result = getResult();
  61. }
  62. // 'blocking future' pipelining...
  63. if (result instanceof BlockingFuture) {
  64. return ((BlockingFuture) result).get();
  65. } else {
  66. return result;
  67. }
  68. }
  69. /**
  70. * Sets the result of this Future to the given value unless
  71. * this future has already been set or has been cancelled.
  72. * @param v the value
  73. */
  74. public void resolve(Object v) {
  75. setCompleted(v);
  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. * 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. * Marks the task as failed.
  100. * @param exception the cause of abrupt completion.
  101. */
  102. private void setFailed(Exception exception) {
  103. synchronized (this) {
  104. if (fulfilled) return;
  105. fulfilled = true;
  106. this.exception = exception;
  107. notifyAll();
  108. }
  109. }
  110. /**
  111. * Waits for the task to complete.
  112. * PRE: lock owned
  113. */
  114. private void waitFor() {
  115. while (!isDetermined()) {
  116. try {
  117. wait();
  118. } catch (InterruptedException e) { }
  119. }
  120. }
  121. /**
  122. * Gets the result of the task.
  123. *
  124. * PRE: task completed
  125. * PRE: lock owned
  126. */
  127. private Object getResult() throws Exception {
  128. if (exception != null) {
  129. throw exception;
  130. }
  131. return result;
  132. }
  133. }