[openrtm-commit:00976] r684 - in trunk/jp.go.aist.rtm.RTC: src/jp/go/aist/rtm/RTC/buffer tests/src/jp/go/aist/rtm/RTC/buffer

openrtm @ openrtm.org openrtm @ openrtm.org
2013年 2月 3日 (日) 13:17:17 JST


Author: ga
Date: 2013-02-03 13:17:17 +0900 (Sun, 03 Feb 2013)
New Revision: 684

Modified:
   trunk/jp.go.aist.rtm.RTC/src/jp/go/aist/rtm/RTC/buffer/RingBuffer.java
   trunk/jp.go.aist.rtm.RTC/tests/src/jp/go/aist/rtm/RTC/buffer/RingBufferTest.java
Log:
The bugs of RingBuffer class have been fixed. refs #2327

Modified: trunk/jp.go.aist.rtm.RTC/src/jp/go/aist/rtm/RTC/buffer/RingBuffer.java
===================================================================
--- trunk/jp.go.aist.rtm.RTC/src/jp/go/aist/rtm/RTC/buffer/RingBuffer.java	2013-02-03 04:13:25 UTC (rev 683)
+++ trunk/jp.go.aist.rtm.RTC/src/jp/go/aist/rtm/RTC/buffer/RingBuffer.java	2013-02-03 04:17:17 UTC (rev 684)
@@ -225,7 +225,6 @@
      */
     public ReturnCode write(final DataType value, int sec, int nsec) {
         synchronized (m_full.mutex) {
-      
             if (full()) {
                 
                 boolean timedwrite = m_timedwrite;
@@ -245,15 +244,17 @@
                 else if (!overwrite && timedwrite) { // "block" mode
                     if (sec < 0) {
                         sec = 
-                            (int)(m_rtimeout.sec()*1000+m_rtimeout.usec()/1000);
-                        nsec = (int)(m_rtimeout.usec() % 1000)*1000;
+                            (int)(m_wtimeout.sec()*1000+m_wtimeout.usec()/1000);
+                        nsec = (int)(m_wtimeout.usec() % 1000)*1000;
                     }
                     try {
                         if(sec==0 && nsec==0){
                             return ReturnCode.TIMEOUT;
                         }
                         m_full.mutex.wait(sec, (int)nsec);
-                        return ReturnCode.TIMEOUT;
+                        if (full()) {
+                            return ReturnCode.TIMEOUT;
+                        }
                     }
                     catch(InterruptedException e ){
                         throw new RuntimeException(e.toString()); 
@@ -266,20 +267,21 @@
                     return ReturnCode.PRECONDITION_NOT_MET;
                 }
             }
-          
-            boolean empty_ = empty();
-          
+
             put(value);
           
             synchronized (m_empty.mutex) {
-                advanceWptr(1);
-                if (empty_) {
+                if (empty()) {
+                    advanceWptr(1);
                     try {
                         m_empty.mutex.notify();
                     }
                     catch(IllegalMonitorStateException e) {
                     }
                 }
+                else {
+                    advanceWptr(1);
+                }
             }
             return ReturnCode.BUFFER_OK;
         }
@@ -422,8 +424,15 @@
                     return ReturnCode.BUFFER_EMPTY;
                 }
                 else if (!readback && timedread) { // "block" mode
-                    //  true: signaled, false: timeout
+                    if (sec < 0) {
+                        local_msec = 
+                            (int)(m_rtimeout.sec()*1000+m_rtimeout.usec()/1000);
+                        local_nsec = (int)(m_rtimeout.usec() % 1000)*1000;
+                    }
                     try {
+                        if(local_msec==0 && local_nsec==0){
+                            return ReturnCode.TIMEOUT;
+                        }
                         m_empty.mutex.wait(local_msec, local_nsec);
                         if (empty()) {
                             return ReturnCode.TIMEOUT;
@@ -445,19 +454,20 @@
             }
         }
 
-        boolean  full_ = full();
-      
         get(valueRef);
 
         synchronized(m_full.mutex){
-            advanceRptr(1);
-            if (full_) {
+            if (full()) {
+                advanceRptr(1);
                 try {
                     m_full.mutex.notify();
                 }
                 catch(IllegalMonitorStateException e) {
                 }
             }
+            else {
+                advanceRptr(1);
+            }
         }
       
             return ReturnCode.BUFFER_OK;

Modified: trunk/jp.go.aist.rtm.RTC/tests/src/jp/go/aist/rtm/RTC/buffer/RingBufferTest.java
===================================================================
--- trunk/jp.go.aist.rtm.RTC/tests/src/jp/go/aist/rtm/RTC/buffer/RingBufferTest.java	2013-02-03 04:13:25 UTC (rev 683)
+++ trunk/jp.go.aist.rtm.RTC/tests/src/jp/go/aist/rtm/RTC/buffer/RingBufferTest.java	2013-02-03 04:17:17 UTC (rev 684)
@@ -5,6 +5,8 @@
 import jp.go.aist.rtm.RTC.util.Properties;
 import junit.framework.TestCase;
 
+import java.util.*;
+
 /**
  * <p>RingBufferのためのテストケースです。</p>
  */
@@ -531,6 +533,8 @@
         }
         return;
     }
+
+    
     public void test_write_read2() {
         //(2)Can the buffer correctly read one written data 
         //in the state of full?
@@ -540,7 +544,7 @@
         for (int i = 0; i < length2; i++) {
             buff2.write(i + 123);
         }
-			
+            
         // 1データ書込・読出を行う
         for (int writeValue = 0; writeValue < 100; writeValue++) {
             // 書込み
@@ -561,7 +565,7 @@
                              readValue.v.intValue());
             }
         }
-			
+            
         // (3) バッファに幾分データが書き込まれている状態で1データ書込・読出を行い、書き込んだデータを正しく読み出せるか?
         int length3 = 10;
         RingBuffer<Integer> buff3 = new RingBuffer<Integer>(length3);
@@ -590,6 +594,71 @@
             }
         }
     }
+
+
+    class writing_thread extends Thread {
+        RingBuffer<Integer> m_buff;
+        int m_loopcnt;
+        public writing_thread(RingBuffer<Integer> buff, int loop) {
+            m_buff = buff;
+            m_loopcnt = loop;
+        }
+        
+        public void start() {
+            super.start();
+        }
+        
+        public void run() {
+            //DataRef<Integer> readValue = new DataRef<Integer>(0);
+            for (int i = 0; i < m_loopcnt; ++i) {
+                //m_buff.read(readValue);
+                m_buff.write(i);
+            }
+        }
+        
+        ArrayList m_data = new ArrayList();
+        public ArrayList getData() {
+            return m_data;
+        }
+    }
+
+    public void test_write_read3() {
+        int loopcnt = 1000000;
+        Properties prop = new Properties();
+        prop.setProperty("write.full_policy","block");
+        prop.setProperty("write.timeout","5.0");
+        prop.setProperty("read.empty_policy","block");
+        prop.setProperty("read.timeout","5.0");
+        RingBuffer<Integer> buff = new RingBuffer<Integer>(8);
+        DataRef<Integer> readValue = new DataRef<Integer>(0);
+        buff.init(prop);
+        writing_thread wt = new writing_thread(buff, loopcnt);
+        wt.start();
+        try {
+            Thread.sleep(10);
+        }
+        catch(InterruptedException e) {
+        }
+        ArrayList rdata = new ArrayList();
+        for (int i = 0; i < loopcnt; i++) {
+            buff.read(readValue);
+            rdata.add(i);
+            //buff.write(i);
+        }
+        try {
+            wt.join();
+        } catch (InterruptedException e) {
+            System.out.println(e);
+        }
+        ArrayList wdata = wt.getData();
+        Iterator w_ite = wdata.iterator();
+        Iterator r_ite = rdata.iterator();
+        while(w_ite.hasNext()) { 
+            assertEquals("rdata==wdata",w_ite.next(),r_ite.next());
+        }
+    }
+    
+    
     /**
      * <p>write()メソッドおよびread()メソッドのテスト(バッファ長2の場合)
      * <ul>
@@ -648,7 +717,7 @@
         //and the increment does the pointer on the reading side.
 	buff2.write(0);
 				
-        assertEquals("(2)-2",true, buff2.full());
+    assertEquals("(2)-2",true, buff2.full());
         //Readinfg
         // Because it reads out data from the reading side and 
         // the number of pointers is increased, 



More information about the openrtm-commit mailing list