Changes for galois
authorjzhou <jzhou>
Thu, 3 May 2012 01:31:23 +0000 (01:31 +0000)
committerjzhou <jzhou>
Thu, 3 May 2012 01:31:23 +0000 (01:31 +0000)
Robust/src/ClassLibrary/MGC/Thread.java
Robust/src/ClassLibrary/MGC/gnu/ArrayBlockingQueue.java
Robust/src/ClassLibrary/MGC/gnu/ReentrantLock.java
Robust/src/ClassLibrary/MGC/gnu/Semaphore.java

index 55653077afeed9683907a0ad3c420ae83664c4cc..4c3ec64af74c9f432dd874b837c83bb455aa63fd 100644 (file)
@@ -87,5 +87,5 @@ public class Thread implements Runnable {
     System.out.println("Unimplemented Thread.getAllStackTraces()");
     return new HashMap();
   }*/
-
+  
 }
index 658f166a505f392c95e7c2ccc31d82e36b3ad188..8df35f84c7e599c6ec877524664a49aa349df0d4 100644 (file)
@@ -94,6 +94,7 @@ public class ArrayBlockingQueue/*<E>*/ extends AbstractQueue/*<E>*/
         putIndex = inc(putIndex);
         ++count;
         notEmpty.signal();
+        //lock.notifyAll();
     }
 
     /**
index d5914b5703e5c23b97564eb037fc04ee431ef56e..a6abc7b46d83f1eedcc6e514c38940d941bd5775 100644 (file)
@@ -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();
     }
 
     /**
index 76eda01098f1462a429f20580a5f1f2158b5dd32..29d36d9a39b2d774e3ea7c4c0ff8396dc9bf65f9 100644 (file)
@@ -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();
     }
 
     /**