*/
public class ReentrantLock implements /*Lock, java.io.*/Serializable {
private static final long serialVersionUID = 7373984872572414699L;
+ boolean isLocked = false;
+ Thread lockedBy = null;
+ int lockedCount = 0;
+
/** Synchronizer providing all implementation mechanics */
//private final Sync sync;
* purposes and lies dormant until the lock has been acquired,
* at which time the lock hold count is set to one.
*/
- public void lock() {
+ public synchronized void lock() {
//sync.lock();
- System.out.println("Unimplemented ReentrantLock.lock()!");
+ //System.out.println("Unimplemented ReentrantLock.lock()!");
+ Thread callingThread = Thread.currentThread();
+ while(isLocked && lockedBy != callingThread){
+ wait();
+ }
+ isLocked = true;
+ lockedCount++;
+ lockedBy = callingThread;
}
/**
* @throws InterruptedException if the current thread is interrupted
*/
public void lockInterruptibly() throws InterruptedException {
- System.out.println("Unimplemented ReentrantLock.lockInterruptibly()!");
+ Thread callingThread = Thread.currentThread();
+ while(isLocked && lockedBy != callingThread){
+ wait();
+ }
+ isLocked = true;
+ lockedCount++;
+ lockedBy = callingThread;
+ //System.out.println("Unimplemented ReentrantLock.lockInterruptibly()!");
//sync.acquireInterruptibly(1);
}
* @throws IllegalMonitorStateException if the current thread does not
* hold this lock
*/
- public void unlock() {
+ public synchronized void unlock() {
//sync.release(1);
- System.out.println("Unimplemented ReentrantLock.unlock()!");
+ //System.out.println("Unimplemented ReentrantLock.unlock()!");
+ if(Thread.currentThread() == this.lockedBy){
+ lockedCount--;
+
+ if(lockedCount == 0){
+ isLocked = false;
+ notify();
+ }
+ }
}
/**
* @return the Condition object
*/
public Condition newCondition() {
- System.out.println("Unimplemented ReentrantLock.newCondition()!");
- return null;
+ //System.out.println("Unimplemented ReentrantLock.newCondition()!");
+ //return null;
//return sync.newCondition();
+ return new ConditionObject();
}
/**
public class Semaphore implements /*java.io.*/Serializable {
private static final long serialVersionUID = -3222578661600680210L;
+ private final int count;
+ private final boolean fair;
/** All mechanics via AbstractQueuedSynchronizer subclass */
//private final Sync sync;
*/
public Semaphore(int permits) {
//sync = new NonfairSync(permits);
+ count = permits;
}
/**
*/
public Semaphore(int permits, boolean fair) {
//sync = (fair)? new FairSync(permits) : new NonfairSync(permits);
+ count = permits;
+ fair = fair;
}
/**
*
* @throws InterruptedException if the current thread is interrupted
*/
- public void acquire() throws InterruptedException {
+ public synchronized void acquire() throws InterruptedException {
//sync.acquireSharedInterruptibly(1);
- System.out.println("Unimplemented Semaphore.acquire()!");
+ //System.out.println("Unimplemented Semaphore.acquire()!");
+ while(this.count == 0) wait();
+ this.count--;
+ this.notify();
}
/**
* Correct usage of a semaphore is established by programming convention
* in the application.
*/
- public void release() {
+ public synchronized void release() {
//sync.releaseShared(1);
- System.out.println("Unimplemented Semaphore.release()!");
+ //System.out.println("Unimplemented Semaphore.release()!");
+ this.count++;
+ this.notify();
}
/**
public void acquire(int permits) throws InterruptedException {
/*if (permits < 0) throw new IllegalArgumentException();
sync.acquireSharedInterruptibly(permits);*/
- System.out.println("Unimplemented Semaphore.acquire(int)!");
+ //System.out.println("Unimplemented Semaphore.acquire(int)!");
+ while(this.count < permits) wait();
+ this.count-=permits;
+ this.notify();
}
/**
public void release(int permits) {
/*if (permits < 0) throw new IllegalArgumentException();
sync.releaseShared(permits);*/
- System.out.println("Unimplemented Semaphore.release()!");
+ //System.out.println("Unimplemented Semaphore.release()!");
+ this.count+=permits;
+ this.notify();
}
/**