MongoDBStore adds indices to mongo collections exactly Once (new class). Removed...
[aquarium] / src / main / scala / gr / grnet / aquarium / util / Once.scala
diff --git a/src/main/scala/gr/grnet/aquarium/util/Once.scala b/src/main/scala/gr/grnet/aquarium/util/Once.scala
new file mode 100644 (file)
index 0000000..6202039
--- /dev/null
@@ -0,0 +1,28 @@
+package gr.grnet.aquarium.util
+
+/**
+ * Run exactly once
+ *
+ * @author Prodromos Gerakios <pgerakios@grnet.gr>
+ */
+
+class Once {
+  private[this] val count=new java.util.concurrent.atomic.AtomicLong()
+  private[this] val ready=new java.util.concurrent.atomic.AtomicBoolean(false)
+
+  def run(once : => Unit): Unit = {
+    if(!ready.get){
+      if(count.addAndGet(1) == 1){
+        try {
+          once
+        } finally {
+          this.synchronized{
+            ready.set(true)
+            this.synchronized(this.notifyAll)
+          }
+        }
+      } else
+        this.synchronized {while(!ready.get) this.wait}
+  }
+ }
+}