[jboss-cvs] JBoss Messaging SVN: r4077 - branches/trunk_tmp_aio/src/main/org/jboss/messaging/core/asyncio/impl.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Apr 17 16:44:34 EDT 2008


Author: clebert.suconic at jboss.com
Date: 2008-04-17 16:44:33 -0400 (Thu, 17 Apr 2008)
New Revision: 4077

Modified:
   branches/trunk_tmp_aio/src/main/org/jboss/messaging/core/asyncio/impl/AsynchronousFileImpl.java
Log:
Fixing tests

Modified: branches/trunk_tmp_aio/src/main/org/jboss/messaging/core/asyncio/impl/AsynchronousFileImpl.java
===================================================================
--- branches/trunk_tmp_aio/src/main/org/jboss/messaging/core/asyncio/impl/AsynchronousFileImpl.java	2008-04-17 17:52:54 UTC (rev 4076)
+++ branches/trunk_tmp_aio/src/main/org/jboss/messaging/core/asyncio/impl/AsynchronousFileImpl.java	2008-04-17 20:44:33 UTC (rev 4077)
@@ -8,6 +8,7 @@
 package org.jboss.messaging.core.asyncio.impl;
 
 import java.nio.ByteBuffer;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReadWriteLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
@@ -34,6 +35,9 @@
     Lock writeLock = lock.writeLock();
     Lock readLock = lock.readLock();
     
+    ReadWriteLock lockPoller = new ReentrantReadWriteLock();
+    Lock pollerLock = lockPoller.writeLock();
+    
     /**
      *  Warning: Beware of the C++ pointer! It will bite you! :-)
      */ 
@@ -61,44 +65,57 @@
     
 
     
-    public synchronized void open(String fileName, int maxIO)
+    public void open(String fileName, int maxIO)
     {
-       readLock.lock(); // to be released when the poller goes down
-       if (opened)
+       try
        {
-          throw new IllegalStateException("AsynchronousFile is already opened");
+          writeLock.lock();
+          if (opened)
+          {
+             throw new IllegalStateException("AsynchronousFile is already opened");
+          }
+          opened = true;
+          this.fileName=fileName;
+          handler = init (fileName, maxIO, log);
+          startPoller();
        }
-       opened = true;
-       this.fileName=fileName;
-       handler = init (fileName, maxIO, log);
-       startPoller();
+       finally
+       {
+          writeLock.unlock();
+       }
     }
     
     class PollerThread extends Thread
     {
-        PollerThread ()
+        CountDownLatch latch;
+        PollerThread (CountDownLatch latch)
         {
             super("NativePoller for " + fileName);
+            this.latch = latch;
         }
         public void run()
         {
+           pollerLock.lock();
+           // informing caller that this thread already has the lock
+           latch.countDown();
            try
            {
             pollEvents();
            }
            finally
            {
-              readLock.unlock();
+              pollerLock.unlock();
            }
         }
     }
     
-    public synchronized void close()
+    public void close()
     {
        checkOpened();
-       stopPoller(handler);
        
        writeLock.lock();
+       stopPoller(handler);
+       pollerLock.lock();
        try
        {
         closeInternal(handler);
@@ -108,6 +125,7 @@
        finally
        {
           writeLock.unlock();
+          pollerLock.unlock();
        }
     }
     
@@ -190,10 +208,14 @@
     private synchronized void  startPoller()
     {
         checkOpened();
-        poller = new PollerThread(); 
+        CountDownLatch latch = new CountDownLatch(1);
+        poller = new PollerThread(latch); 
         try
         {
            poller.start();
+           // We should wait the Poller to hold the Lock, or we could have a race condition between closes and open if they are happening too fast
+           // what could cause core dumps or GPFs
+           latch.await();
         }
         catch (Exception ex)
         {




More information about the jboss-cvs-commits mailing list