|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcz.zcu.fav.kiv.jsim.ipc.JSimSemaphore
public class JSimSemaphore
A JSimSemaphore is a mean of synchronization of two or more J-Sim processes. Semaphores were invented by Edsger W. Dijkstra, Professor Emeritus of Computer Sciences and Mathematics at The University of Texas. Semaphores typically protect shared data from being accessed concurrently by two or more processes. Semaphore functions P() and V() serve as the beginning and the end of a critical section, preventing any process from passing over P() if another process has already passed over it. When the other process, being currently inside the critical section, invokes V() on the semaphore, it releases the semaphore and the process waiting on P() is woken up and enters the critical section. An integer counter and a queue are used for blocking and resuming processes. Caution: This implementation of semaphore can be used inside a J-Sim simulation only! It is not a mean of synchronization of Java threads! Important note: Semaphores can only be used by processes from the same simulation. An attempt to invoke a method of a semaphore of simulation S1 from a process of simulation S2 will cause an unpredictable and probably faulty behavior.
Field Summary | |
---|---|
private long |
counter
A counter of processes that can pass over P() without blocking. |
private static java.util.logging.Logger |
logger
Common logger for all instances of this class. |
private java.lang.String |
myName
The name of the semaphore. |
private long |
myNumber
This semaphore's number. |
protected JSimSimulation |
myParent
The simulation in which the semaphore is placed. |
private java.util.LinkedList<JSimProcess> |
queue
A queue of processes blocked on this semaphore's P() function. |
Constructor Summary | |
---|---|
JSimSemaphore(java.lang.String name,
JSimSimulation parent,
long initCounter)
Creates a new semaphore with the specified name and initial value of the counter. |
Method Summary | |
---|---|
int |
compareTo(JSimSemaphore s)
Compares this semaphore with another one. |
javax.swing.JDialog |
createDetailedInfoWindow(JSimMainWindow parentWindow)
Creates a detailed info window that shows information about the semaphore. |
boolean |
equals(java.lang.Object o)
Indicates whether some other object is equal to this one. |
long |
getCounter()
Returns this semaphore's counter. |
java.util.Collection<JSimPair> |
getDetailedInformationArray()
Returns a collection with the semaphore's characteristics. |
int |
getNumberOfWaitingProcesses()
Returns the number of processes blocked on this semaphore's P() function. |
java.lang.String |
getObjectListItemDescription()
Returns a string containing basic information about the semaphore. |
JSimSimulation |
getParent()
Returns the simulation that this semaphore is a part of. |
java.lang.String |
getSemaphoreName()
Returns the semaphore's name. |
long |
getSemaphoreNumber()
Returns the semaphore's number. |
int |
hashCode()
Returns a hash code value for the object. |
void |
P()
P() is a potentially blocking operation that denotes the beginning of a critical section. |
java.lang.String |
toString()
Returns a string representation of the semaphore. |
void |
V()
V() denotes the end of a critical section. |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
private static final java.util.logging.Logger logger
private final java.lang.String myName
private final long myNumber
protected final JSimSimulation myParent
private long counter
private java.util.LinkedList<JSimProcess> queue
Constructor Detail |
---|
public JSimSemaphore(java.lang.String name, JSimSimulation parent, long initCounter) throws JSimInvalidParametersException, JSimTooManySemaphoresException
name
- The name of the semaphore.parent
- The parent simulation that this semaphore belongs to.initCounter
- The initial value of the semaphore. It must be non-negative. The initial value determines the number of processes that can
invoke P() without being blocked. A reasonable value is 1 in most cases. If you want to simulate a critical section, the
value of 1 is the only choice.
JSimInvalidParametersException
- This exception is thrown out if the parent simulation is null or the initial value of the counter is invalid.
JSimTooManySemaphoresException
- This exception is thrown out if no more semaphore can be added to the simulation.Method Detail |
---|
public void P() throws JSimInvalidContextException
JSimInvalidContextException
- This exception is thrown out if this method is called from outside a process. This means that no process is currently
running.public void V() throws JSimInvalidContextException
JSimInvalidContextException
- This exception is thrown out if this method is called from outside a process. This means that no process is currently
running.public int getNumberOfWaitingProcesses()
public long getCounter()
public final long getSemaphoreNumber()
public java.lang.String getSemaphoreName()
public JSimSimulation getParent()
public java.lang.String getObjectListItemDescription()
getObjectListItemDescription
in interface JSimDisplayable
public java.util.Collection<JSimPair> getDetailedInformationArray()
getDetailedInformationArray
in interface JSimDisplayable
public javax.swing.JDialog createDetailedInfoWindow(JSimMainWindow parentWindow)
createDetailedInfoWindow
in interface JSimDisplayable
public java.lang.String toString()
toString
in class java.lang.Object
public int compareTo(JSimSemaphore s)
compareTo
in interface java.lang.Comparable<JSimSemaphore>
java.lang.ClassCastException
- This exception is thrown out when the specified object cannot be typecasted to JSimSemaphore.public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
o
- The reference object with which to compare.
public int hashCode()
hashCode
in class java.lang.Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |