You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2011/05/14 22:17:22 UTC

svn commit: r1103205 - in /activemq/activemq-cpp/trunk/activemq-cpp/src: main/decaf/internal/util/concurrent/ main/decaf/util/concurrent/ test/decaf/util/concurrent/locks/

Author: tabish
Date: Sat May 14 20:17:22 2011
New Revision: 1103205

URL: http://svn.apache.org/viewvc?rev=1103205&view=rev
Log:
Updates for code now that threads are interruptible.  Adds tests for LockSupport interruption and fix Threading park methods to now throw interrupted exception but instead leave thread tagged as interrupted.

Modified:
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/Threading.cpp
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/ThreadPoolExecutor.cpp
    activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/locks/LockSupportTest.cpp
    activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/locks/LockSupportTest.h

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/Threading.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/Threading.cpp?rev=1103205&r1=1103204&r2=1103205&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/Threading.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/util/concurrent/Threading.cpp Sat May 14 20:17:22 2011
@@ -1257,17 +1257,8 @@ bool Threading::park( Thread* thread, lo
     handle->interruptible = false;
     handle->state = Thread::RUNNABLE;
 
-    if (handle->interrupted == true) {
-        interrupted = true;
-        handle->interrupted = false;
-    }
-
     PlatformThread::unlockMutex(handle->mutex);
 
-    if (interrupted) {
-        throw InterruptedException(__FILE__, __LINE__, "Parked Thread interrupted");
-    }
-
     return timedOut;
 }
 

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/ThreadPoolExecutor.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/ThreadPoolExecutor.cpp?rev=1103205&r1=1103204&r2=1103205&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/ThreadPoolExecutor.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/concurrent/ThreadPoolExecutor.cpp Sat May 14 20:17:22 2011
@@ -116,6 +116,8 @@ namespace concurrent{
 
         void drainQueue(ArrayList<Runnable*>& unexecutedTasks);
 
+        void interruptIdleWorkers();
+        void interruptIdleWorkers(bool onlyOne);
     };
 
     class Worker : public lang::Thread {
@@ -829,6 +831,13 @@ Runnable* ExecutorKernel::deQueueTask() 
                 break;
             }
 
+//            try {
+//                if ((task = workQueue->take()) != NULL) {
+//                    break;
+//                }
+//            } catch(InterruptedException& ex) {
+//            }
+
             if(isStoppedOrStopping() && workQueue->isEmpty()) {
                 break;
             }
@@ -1026,3 +1035,15 @@ void ExecutorKernel::tryTerminate() {
         this->termination.countDown();
     }
 }
+
+////////////////////////////////////////////////////////////////////////////////
+void ExecutorKernel::interruptIdleWorkers() {
+    this->interruptIdleWorkers(false);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ExecutorKernel::interruptIdleWorkers(bool onlyOne DECAF_UNUSED) {
+
+    synchronized(&this->mainLock) {
+    }
+}

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/locks/LockSupportTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/locks/LockSupportTest.cpp?rev=1103205&r1=1103204&r2=1103205&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/locks/LockSupportTest.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/locks/LockSupportTest.cpp Sat May 14 20:17:22 2011
@@ -40,23 +40,32 @@ LockSupportTest::~LockSupportTest() {
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-class ParkTestThread : public Thread {
-public:
+namespace {
 
-    virtual void run() {
-        try{
-            LockSupport::park();
-        } catch(...) {
-            CPPUNIT_FAIL("Caught an unexpected exception");
-        }
-    }
+    class ParkTestThread : public Thread {
+    private:
+
+        LockSupportTest* parent;
+
+    public:
 
-};
+        ParkTestThread(LockSupportTest* parent) : Thread(), parent(parent) {}
+        virtual ~ParkTestThread() {}
+
+        virtual void run() {
+            try{
+                LockSupport::park();
+            } catch(...) {
+                parent->threadUnexpectedException();
+            }
+        }
+    };
+}
 
 ////////////////////////////////////////////////////////////////////////////////
-void LockSupportTest::testPark() {
+void LockSupportTest::testPark1() {
 
-    ParkTestThread t;
+    ParkTestThread t(this);
 
     try {
 
@@ -71,24 +80,34 @@ void LockSupportTest::testPark() {
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-class ParkTest2Thread : public Thread {
-public:
+namespace {
+
+    class ParkTest2Thread : public Thread {
+    private:
 
-    virtual void run() {
-        try{
-            Thread::sleep( 1000 );
-            LockSupport::park();
-        } catch(...) {
-            CPPUNIT_FAIL("Caught an unexpected exception");
+        LockSupportTest* parent;
+
+    public:
+
+        ParkTest2Thread(LockSupportTest* parent) : Thread(), parent(parent) {}
+        virtual ~ParkTest2Thread() {}
+
+        virtual void run() {
+            try{
+                Thread::sleep( 1000 );
+                LockSupport::park();
+            } catch(...) {
+                parent->threadUnexpectedException();
+            }
         }
-    }
 
-};
+    };
+}
 
 ////////////////////////////////////////////////////////////////////////////////
 void LockSupportTest::testPark2() {
 
-    ParkTest2Thread t;
+    ParkTest2Thread t(this);
 
     try {
 
@@ -108,22 +127,116 @@ void LockSupportTest::testPark2() {
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-class ParkNanosTestThread : public Thread {
-public:
+namespace {
+
+    class Park3TestThread : public Thread {
+    private:
+
+        LockSupportTest* parent;
 
-    virtual void run() {
-        try{
-            LockSupport::parkNanos( TimeUnit::SECONDS.toNanos( 2 ) );
-        } catch(...) {
-            CPPUNIT_FAIL("Caught an unexpected exception");
+    public:
+
+        Park3TestThread(LockSupportTest* parent) : Thread(), parent(parent) {}
+        virtual ~Park3TestThread() {}
+
+        virtual void run() {
+            try{
+                LockSupport::park();
+            } catch(...) {
+                parent->threadUnexpectedException();
+            }
         }
+    };
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void LockSupportTest::testPark3() {
+
+    Park3TestThread t(this);
+
+    try {
+        t.start();
+        Thread::sleep(SHORT_DELAY_MS);
+        t.interrupt();
+        t.join();
     }
-};
+    catch(Exception e) {
+        unexpectedException();
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+namespace {
+
+    class Park4TestThread : public Thread {
+    private:
+
+        LockSupportTest* parent;
+        Mutex* lock;
+
+    public:
+
+        Park4TestThread(LockSupportTest* parent, Mutex* lock) : Thread(), parent(parent), lock(lock) {}
+        virtual ~Park4TestThread() {}
+
+        virtual void run() {
+            try{
+                lock->lock();
+                LockSupport::park();
+                lock->unlock();
+                parent->threadAssertTrue(Thread::currentThread()->isInterrupted());
+            } catch(...) {
+                parent->threadUnexpectedException();
+            }
+        }
+    };
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void LockSupportTest::testPark4() {
+
+    Mutex lock;
+    Park4TestThread t(this, &lock);
+    lock.lock();
+
+    try {
+        t.start();
+        t.interrupt();
+        lock.unlock();
+        t.join();
+    }
+    catch(Exception e) {
+        unexpectedException();
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+namespace {
+
+    class ParkNanosTestThread : public Thread {
+    private:
+
+        LockSupportTest* parent;
+
+    public:
+
+        ParkNanosTestThread(LockSupportTest* parent) : Thread(), parent(parent) {}
+        virtual ~ParkNanosTestThread() {}
+
+        virtual void run() {
+            try{
+                LockSupport::parkNanos( TimeUnit::SECONDS.toNanos( 2 ) );
+            } catch(...) {
+                parent->threadUnexpectedException();
+            }
+        }
+    };
+}
 
 ////////////////////////////////////////////////////////////////////////////////
 void LockSupportTest::testParkNanos() {
 
-    ParkNanosTestThread t;
+    ParkNanosTestThread t(this);
 
     long long before = System::currentTimeMillis();
     t.start();
@@ -135,23 +248,33 @@ void LockSupportTest::testParkNanos() {
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-class ParkUntilTestThread : public Thread {
-public:
+namespace {
+
+    class ParkUntilTestThread : public Thread {
+    private:
+
+        LockSupportTest* parent;
+
+    public:
 
-    virtual void run() {
-        try{
-            long long deadline = Date().getTime() + 100;
-            LockSupport::parkUntil( deadline );
-        } catch(...) {
-            CPPUNIT_FAIL("Caught an unexpected exception");
+        ParkUntilTestThread(LockSupportTest* parent) : Thread(), parent(parent) {}
+        virtual ~ParkUntilTestThread() {}
+
+        virtual void run() {
+            try{
+                long long deadline = Date().getTime() + 100;
+                LockSupport::parkUntil( deadline );
+            } catch(...) {
+                parent->threadUnexpectedException();
+            }
         }
-    }
-};
+    };
+}
 
 ////////////////////////////////////////////////////////////////////////////////
 void LockSupportTest::testParkUntil() {
 
-    ParkUntilTestThread t;
+    ParkUntilTestThread t(this);
 
     try {
         t.start();

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/locks/LockSupportTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/locks/LockSupportTest.h?rev=1103205&r1=1103204&r2=1103205&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/locks/LockSupportTest.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/locks/LockSupportTest.h Sat May 14 20:17:22 2011
@@ -20,17 +20,20 @@
 
 #include <cppunit/TestFixture.h>
 #include <cppunit/extensions/HelperMacros.h>
+#include <decaf/util/concurrent/ExecutorsTestSupport.h>
 
 namespace decaf {
 namespace util {
 namespace concurrent {
 namespace locks {
 
-    class LockSupportTest : public CppUnit::TestFixture {
+    class LockSupportTest : public ExecutorsTestSupport {
 
         CPPUNIT_TEST_SUITE( LockSupportTest );
-        CPPUNIT_TEST( testPark );
+        CPPUNIT_TEST( testPark1 );
         CPPUNIT_TEST( testPark2 );
+        CPPUNIT_TEST( testPark3 );
+        CPPUNIT_TEST( testPark4 );
         CPPUNIT_TEST( testParkNanos );
         CPPUNIT_TEST( testParkUntil );
         CPPUNIT_TEST_SUITE_END();
@@ -41,8 +44,10 @@ namespace locks {
 
         virtual ~LockSupportTest();
 
-        void testPark();
+        void testPark1();
         void testPark2();
+        void testPark3();
+        void testPark4();
         void testParkNanos();
         void testParkUntil();