001 /*
002 * cs101 Graphical Boolean semaphore utility
003 * $Id: GBS.java,v 1.3 2003/09/23 15:36:21 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 import java.awt.*;
016 import cs101.awt.ColorField;
017
018 /**
019 * Implements simple binary semaphores in java witha graphical display. <br>
020 * Interface is gbs.request(), gbs.release().<br>
021 * It also provides a graphical display of the semaphores status.<br>
022 * <p>
023 * This utility borrows heavily from the design and implementation
024 * of cs101.util.BS. It mearly adds a graphical display.
025 * <br>
026 * Copyright 1996 Massachusetts Institute of Technology
027 *
028 * @author Todd C. Parnell, tparnell@ai.mit.edu
029 * @author Joshua R. Brown, reuben@ai.mit.edu (added graphics)
030 * @author Lynn Andrea Stein, las@ai.mit.edu (semaphore design)
031 * @version $Id: GBS.java,v 1.3 2003/09/23 15:36:21 gus Exp $
032 *
033 */
034 public class GBS extends GraphicalSemaphore {
035
036 /** The current state of the semaphore.
037 * true => busy , false => free
038 */
039 private boolean busy;
040
041 /** The graphical display of the semaphore */
042 private ColorField sqr;
043
044 //GBS(boolean, String)
045 /**
046 * Constructs a binary semaphore with the intial value passed in.
047 *
048 * @param initVal Initial value for semaphore. (true => in use)
049 * @param label Used to identfy the semaphore in the display.
050 */
051 public GBS (boolean initVal, String label) {
052 super(label);
053 this.busy = initVal;
054
055 // now that everything is initialized setup the GUI
056 this.setupGUI();
057
058 }
059
060 /**
061 * Does all of the graphical setup on this level
062 * then calls the superclasses method to finish the setup.
063 *
064 * This method is primarly responsible for setting up the
065 * display Panel.
066 */
067 protected void setupGUI() {
068 // Graphics setup on this level
069 this.sqr = new ColorField(this.busy, new Dimension(25,25),
070 Color.red, Color.green);
071
072 this.display = new Panel();
073 this.display.add(this.sqr);
074
075 // Graphics setup in super-class
076 super.setupGUI();
077
078 }
079
080 /**
081 * Requests the semaphore. If the semaphore is currently busy,
082 * causes the requesting process to wait() until the semaphore is
083 * release()d. Unlike java.lang.Object.wait(), the requesting
084 * process is not suspended if the semaphore is currently free.
085 *
086 * @see #release
087 * @see java.lang.Object#wait
088 */
089 synchronized public void request () {
090 while (this.busy) {
091 try {this.wait();} catch (InterruptedException e) {}
092 }
093 this.busy = true;
094 this.showStatus();
095 }
096
097 /**
098 * Releases the semaphore. Any objects currently wait()ing on the
099 * semaphore are notify()d (and one of them will be granted the
100 * semaphore). Unlike java.lang.Object.notify(), the semaphore is
101 * also freed so that if there are no wait()ing objects, the next
102 * object to request() the semaphore will receive it.
103 *
104 * @see #request
105 * @see java.lang.Object#notifyAll()
106 */
107 synchronized public void release () {
108 this.busy = false;
109 this.notifyAll();
110 this.showStatus();
111 }
112
113 /**
114 * Prints out the current state of the semaphore.
115 * Changes the graphical display.
116 */
117 protected void showStatus() {
118 // tab over an approprate amount
119 for (int i = 0; i<this.myNumber; i++)
120 System.out.print(" ");
121
122 // now print the status
123 System.out.print(this.label.getText()+": ");
124 if (this.busy)
125 System.out.println("BUSY");
126 else
127 System.out.println("FREE");
128
129 // update the status field
130 this.sqr.changeState(this.busy);
131 }
132
133 }
134
135 /* Comments:
136 *
137 * History:
138 * $Log: GBS.java,v $
139 * Revision 1.3 2003/09/23 15:36:21 gus
140 * javadoc fix
141 *
142 * Revision 1.2 2003/09/23 14:43:13 gus
143 * javadoc fix
144 *
145 * Revision 1.1.1.1 2002/06/05 21:56:32 root
146 * CS101 comes to Olin finally.
147 *
148 * Revision 1.1 2000/04/24 22:17:22 nathanw
149 * Bulk reorganization
150 *
151 * Revision 1.4 1998/07/24 17:19:26 tparnell
152 * Placate new javadoc behavior
153 *
154 * Revision 1.3 1998/07/22 18:18:55 tparnell
155 * migration from cs101.util to cs101.*
156 *
157 * Revision 1.2 1998/06/03 19:42:00 tparnell
158 * update from Java 1.0 to 1.1
159 *
160 * Revision 1.1 1998/03/13 22:18:13 tparnell
161 * Import from server crash. I think the src and class files match up.
162 *
163 * Revision 1.5 1996/08/01 18:26:22 reuben
164 * More javadoc tweaking (hopefully the final pass)
165 *
166 * Revision 1.4 1996/07/30 17:25:59 reuben
167 * Added/corrected javadoc comments.
168 *
169 * Revision 1.3 1996/07/25 18:27:41 reuben
170 * Added all kinds of comments.
171 * Compiled and tested.
172 *
173 * Revision 1.2 1996/07/25 15:33:27 reuben
174 * testing
175 *
176 */