licensing update to APSL 2.0
[org.ibex.util.git] / src / org / ibex / util / Semaphore.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.util;
6
7 /** Simple implementation of a blocking, counting semaphore. */
8 public class Semaphore {
9     
10     private int val = 0;
11
12     public synchronized void dec() {
13         val--;
14     }
15     
16     /** Decrement the counter, blocking if zero. */
17     public synchronized void block() {
18         while(val <= 0) {
19             try {
20                 wait();
21             } catch (InterruptedException e) {
22             } catch (Throwable e) {
23                 if (Log.on) Log.info(this, "Exception in Semaphore.block(); this should never happen");
24                 if (Log.on) Log.info(this, e);
25             }
26         }
27         val--;
28     }
29     
30     /** Incremenet the counter. */
31     public synchronized void release() {
32         val++;
33         notify();
34     }
35
36 }