From: jzhou Date: Thu, 3 May 2012 01:31:23 +0000 (+0000) Subject: Changes for galois X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=2074476eaf7f980789f0833c72d560bfcfdb923b;p=IRC.git Changes for galois --- diff --git a/Robust/src/ClassLibrary/MGC/Thread.java b/Robust/src/ClassLibrary/MGC/Thread.java index 55653077..4c3ec64a 100644 --- a/Robust/src/ClassLibrary/MGC/Thread.java +++ b/Robust/src/ClassLibrary/MGC/Thread.java @@ -87,5 +87,5 @@ public class Thread implements Runnable { System.out.println("Unimplemented Thread.getAllStackTraces()"); return new HashMap(); }*/ - + } diff --git a/Robust/src/ClassLibrary/MGC/gnu/ArrayBlockingQueue.java b/Robust/src/ClassLibrary/MGC/gnu/ArrayBlockingQueue.java index 658f166a..8df35f84 100644 --- a/Robust/src/ClassLibrary/MGC/gnu/ArrayBlockingQueue.java +++ b/Robust/src/ClassLibrary/MGC/gnu/ArrayBlockingQueue.java @@ -94,6 +94,7 @@ public class ArrayBlockingQueue/**/ extends AbstractQueue/**/ putIndex = inc(putIndex); ++count; notEmpty.signal(); + //lock.notifyAll(); } /** diff --git a/Robust/src/ClassLibrary/MGC/gnu/ReentrantLock.java b/Robust/src/ClassLibrary/MGC/gnu/ReentrantLock.java index d5914b57..a6abc7b4 100644 --- a/Robust/src/ClassLibrary/MGC/gnu/ReentrantLock.java +++ b/Robust/src/ClassLibrary/MGC/gnu/ReentrantLock.java @@ -79,6 +79,10 @@ import java.util.concurrent.atomic.*;*/ */ 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; @@ -257,9 +261,16 @@ public class ReentrantLock implements /*Lock, java.io.*/Serializable { * 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; } /** @@ -309,7 +320,14 @@ public class ReentrantLock implements /*Lock, java.io.*/Serializable { * @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); } @@ -428,9 +446,17 @@ public class ReentrantLock implements /*Lock, java.io.*/Serializable { * @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(); + } + } } /** @@ -473,9 +499,10 @@ public class ReentrantLock implements /*Lock, java.io.*/Serializable { * @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(); } /** diff --git a/Robust/src/ClassLibrary/MGC/gnu/Semaphore.java b/Robust/src/ClassLibrary/MGC/gnu/Semaphore.java index 76eda010..29d36d9a 100644 --- a/Robust/src/ClassLibrary/MGC/gnu/Semaphore.java +++ b/Robust/src/ClassLibrary/MGC/gnu/Semaphore.java @@ -131,6 +131,8 @@ import java.util.concurrent.atomic.*;*/ 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; @@ -236,6 +238,7 @@ public class Semaphore implements /*java.io.*/Serializable { */ public Semaphore(int permits) { //sync = new NonfairSync(permits); + count = permits; } /** @@ -251,6 +254,8 @@ public class Semaphore implements /*java.io.*/Serializable { */ public Semaphore(int permits, boolean fair) { //sync = (fair)? new FairSync(permits) : new NonfairSync(permits); + count = permits; + fair = fair; } /** @@ -281,9 +286,12 @@ public class Semaphore implements /*java.io.*/Serializable { * * @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(); } /** @@ -396,9 +404,11 @@ public class Semaphore implements /*java.io.*/Serializable { * 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(); } /** @@ -440,7 +450,10 @@ public class Semaphore implements /*java.io.*/Serializable { 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(); } /** @@ -584,7 +597,9 @@ public class Semaphore implements /*java.io.*/Serializable { 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(); } /**