1    // Copyright (C) 2003 Adam Megacz <adam@xwt.org> all rights reserved.
2    //
3    // You may modify, copy, and redistribute this code under the terms of
4    // the GNU Library Public License version 2.1, with the exception of
5    // the portion of clause 6a after the semicolon (aka the "obnoxious
6    // relink clause")
7    
8    package org.xwt.util;
9    
10   /** Simple implementation of a blocking, counting semaphore. */
11   public class Semaphore {
12       
13       private int val = 0;
14   
15       public Semaphore() { };
16   
17       /** Decrement the counter, blocking if zero. */
18       public synchronized void block() {
19           while(val == 0) {
20               try {
21                   wait();
22               } catch (InterruptedException e) {
23               } catch (Throwable e) {
24                   if (Log.on) Log.info(this, "Exception in Semaphore.block(); this should never happen");
25                   if (Log.on) Log.info(this, e);
26               }
27           }
28           val--;
29       }
30       
31       /** Incremenet the counter. */
32       public synchronized void release() {
33           val++;
34           notify();
35       }
36   
37   }
38