00001 /* 00002 * (c) Copyright 2008, IBM Corporation. 00003 * Licensed under the Apache License, Version 2.0 (the "License"); 00004 * you may not use this file except in compliance with the License. 00005 * You may obtain a copy of the License at 00006 * 00007 * http://www.apache.org/licenses/LICENSE-2.0 00008 * 00009 * Unless required by applicable law or agreed to in writing, software 00010 * distributed under the License is distributed on an "AS IS" BASIS, 00011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00012 * See the License for the specific language governing permissions and 00013 * limitations under the License. 00014 * 00015 */ 00016 #ifndef LOCKTEST_H_ 00017 #define LOCKTEST_H_ 00018 #include <cppunit/extensions/HelperMacros.h> 00019 #include <cppunit/TestAssert.h> 00020 #include "baseTest.h" 00021 #include "threadFactory.h" 00022 #include <amino/lock.h> 00023 #include <amino/mutex.h> 00024 #include <amino/thread.h> 00025 00026 using namespace amino; 00027 00028 namespace test{ 00029 00034 class LockTest : public CppUnit::TestFixture{ 00035 CPPUNIT_TEST_SUITE(LockTest); 00036 CPPUNIT_TEST(testLock); 00037 CPPUNIT_TEST(testRecursiveLock); 00038 CPPUNIT_TEST_SUITE_END(); 00039 00040 private: 00041 unique_lock<mutex> * fLock; 00042 00043 public: 00044 void setUp() { 00045 fLock = new unique_lock<mutex>(); 00046 } 00047 00048 void reset(){ 00049 delete fLock; 00050 fLock = new unique_lock<mutex>(); 00051 } 00052 00059 void testLock(); 00060 void testRecursiveLock(); 00061 00062 void tearDown() { 00063 delete fLock; 00064 } 00065 }; 00066 00067 class LockThread : public Thread { 00068 private: 00069 unique_lock<mutex> * fLock; 00070 public: 00071 LockThread(unique_lock<mutex> * lock) { 00072 fLock = lock; 00073 } 00074 00075 void* run() { 00076 fLock->lock(); 00077 sleep(3); 00078 fLock->unlock(); 00079 return NULL; 00080 } 00081 }; 00082 00083 class RecursiveLockThread : public Thread { 00084 private: 00085 unique_lock<recursive_mutex> * fLock; 00086 public: 00087 RecursiveLockThread(unique_lock<recursive_mutex> * lock) { 00088 fLock = lock; 00089 } 00090 00091 void* run() { 00092 fLock->lock(); 00093 fLock->lock(); 00094 sleep(3); 00095 fLock->unlock(); 00096 fLock->unlock(); 00097 return NULL; 00098 } 00099 }; 00100 } 00101 #endif /*MUTEXTEST_H_*/