65e2aa495b5ce881211a87054a5bb4f8253a4545
[org.ibex.core.git] / src / org / xwt / util / Semaphore.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt.util;
3
4 /** Simple implementation of a blocking, counting semaphore. */
5 public class Semaphore {
6     
7     private int val = 0;
8
9     public Semaphore() { };
10
11     /** Decrement the counter, blocking if zero. */
12     public synchronized void block() {
13         while(val == 0) {
14             try {
15                 wait();
16             } catch (InterruptedException e) {
17             } catch (Throwable e) {
18                 if (Log.on) Log.log(this, "Exception in Semaphore.block(); this should never happen");
19                 if (Log.on) Log.log(this, e);
20             }
21         }
22         val--;
23     }
24     
25     /** Incremenet the counter. */
26     public synchronized void release() {
27         val++;
28         notify();
29     }
30
31 }