001    /*
002     * cs101 boolean semaphore utility
003     * $Id: BS.java,v 1.2 2003/09/23 15:35:17 gus Exp $
004     *
005     * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group.
006     * For more information, see <a href="http://www.ai.mit.edu/projects/cs101/">the
007     * CS101 homepage</a> or email <las@ai.mit.edu>.
008     *
009     * Copyright (C) 1996 Massachusetts Institute of Technology.
010     * Please do not redistribute without obtaining permission.
011     */
012    
013    package cs101.util.semaphore;
014    
015    /**
016     * This class implements simple binary semaphores in java. <br>
017     * Interface is bs.request(), bs.release().
018     * <p>
019     * Copyright 1996 Massachusetts Institute of Technology
020     *
021     * @author  Lynn Andrea Stein, las@ai.mit.edu
022     * @version $Id: BS.java,v 1.2 2003/09/23 15:35:17 gus Exp $ 
023     *
024     */
025    public class BS {
026      private boolean busy;
027    
028      //  BS( boolean )
029      /**
030       * If the constructor is passed a boolean value, the semaphore will
031       * be initialized with this value (true => in use, false => free).
032       *
033       * @param    initVal   Initial value for semaphore.  (true => in use)
034       */
035      public BS (boolean initVal) {
036        this.busy = initVal;
037      }
038    
039    
040      // BS()
041      /**
042       * If no argument is supplied, the semaphore is created in its free
043       * state. 
044       */
045      public BS () {
046        this(false);
047      }
048    
049      // request()
050      /**
051       * Requests the semaphore.  If the semaphore is currently busy,
052       * causes the requesting process to wait() until the semaphore is
053       * release()d.  Unlike java.lang.Object.wait(), the requesting
054       * process is not suspended if the semaphore is currently free.
055       *
056       * @see #release
057       * @see java.lang.Object#wait
058       */
059      synchronized public void request () {
060        while (this.busy) {
061          try {this.wait();} catch (InterruptedException e) {}
062        }
063        this.busy = true;
064      }
065    
066      // release()
067      /**
068       * Releases the semaphore.  Any objects currently wait()ing on the
069       * semaphore are notify()d (and one of them will be granted the
070       * semaphore).  Unlike java.lang.Object.notify(), the semaphore is
071       * also freed so that if there are no wait()ing objects, the next
072       * object to request() the semaphore will receive it.
073       *
074       * @see #request
075       * @see java.lang.Object#notifyAll()
076       */
077      synchronized public void release () {
078        this.busy = false;
079        this.notifyAll();
080      }
081    }
082    
083    
084    /* Comments:
085     *
086     * History:
087     *     $Log: BS.java,v $
088     *     Revision 1.2  2003/09/23 15:35:17  gus
089     *     javadoc fix
090     *
091     *     Revision 1.1.1.1  2002/06/05 21:56:32  root
092     *     CS101 comes to Olin finally.
093     *
094     *     Revision 1.1  2000/04/24 22:17:22  nathanw
095     *     Bulk reorganization
096     *
097     *     Revision 1.3  1998/07/24 17:19:22  tparnell
098     *     Placate new javadoc behavior
099     *
100     *     Revision 1.2  1998/06/24 20:59:27  tparnell
101     *     formatting
102     *
103     *     Revision 1.1  1998/03/13 22:18:02  tparnell
104     *     Import from server crash.  I think the src and class files match up.
105     *
106     *     Revision 1.5  1996/08/01 18:26:15  reuben
107     *     More javadoc tweaking (hopefully the final pass)
108     *
109     *     Revision 1.4  1996/07/30 17:25:59  reuben
110     *     Added/corrected javadoc comments.
111     *
112     *     Revision 1.3  1996/07/25 18:27:41  reuben
113     *     Added all kinds of comments.
114     *     Compiled and tested.
115     *
116     *     Revision 1.2  1996/07/25 15:21:51  reuben
117     *     test
118     *
119     *     Revision 1.1.1.1  1996/07/18 17:38:24  sit
120     *     Import from /mit/6.096/share/classes after 6.80s session
121     *
122     *     Revision 1.2  1996/06/19 22:48:05  las
123     *     Cleaned up documentation.
124     *
125     *     Revision 1.1  1996/06/19 16:47:43  las
126     *     Initial revision
127     *     6-19-96  Documentation cleaned up by las@ai.mit.edu 
128     *     6-18-96  Created by las@ai.mit.edu 
129     *
130     */
131