mass rename and rebranding from xwt to ibex - fixed to use ixt files
[org.ibex.core.git] / src / org / ibex / util / Semaphore.java
diff --git a/src/org/ibex/util/Semaphore.java b/src/org/ibex/util/Semaphore.java
new file mode 100644 (file)
index 0000000..79dc268
--- /dev/null
@@ -0,0 +1,37 @@
+// Copyright (C) 2003 Adam Megacz <adam@ibex.org> all rights reserved.
+//
+// You may modify, copy, and redistribute this code under the terms of
+// the GNU Library Public License version 2.1, with the exception of
+// the portion of clause 6a after the semicolon (aka the "obnoxious
+// relink clause")
+
+package org.ibex.util;
+
+/** Simple implementation of a blocking, counting semaphore. */
+public class Semaphore {
+    
+    private int val = 0;
+
+    public Semaphore() { };
+
+    /** Decrement the counter, blocking if zero. */
+    public synchronized void block() {
+        while(val == 0) {
+            try {
+                wait();
+            } catch (InterruptedException e) {
+            } catch (Throwable e) {
+                if (Log.on) Log.info(this, "Exception in Semaphore.block(); this should never happen");
+                if (Log.on) Log.info(this, e);
+            }
+        }
+        val--;
+    }
+    
+    /** Incremenet the counter. */
+    public synchronized void release() {
+        val++;
+        notify();
+    }
+
+}