/*
 *  This file is part of the X10 project (http://x10-lang.org).
 *
 *  This file is licensed to You under the Eclipse Public License (EPL);
 *  You may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *      http://www.opensource.org/licenses/eclipse-1.0.php
 *
 *  (C) Copyright IBM Corporation 2010.
 */

/** A Cell which can be filled and emptied atomically 
 */
class AtomicCell {
  var full : Boolean;
  var contents : Int;
  def this(init:Int) {full = true; contents = init;}
  def fill(newVal:Int) {
    when(!full) {
      full = true; contents = newVal;
    }
  }
  def empty(): Int {
    when(full) { full = false; return contents; }
  }

  public static def main(Array[String]){
    val c = new AtomicCell[Int](0);
    finish {
      async {
        for( [i] in 1 .. 100) {
           c.fill(10*i);
        }
      }
      async {
        for( [j] in 1..100) {
           val v = c.empty();
           Console.OUT.println("v=" + v);
        }
      }
    }
  }
}