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 MUTEXTEST_H_ 00017 #define MUTEXTEST_H_ 00018 #include <cppunit/extensions/HelperMacros.h> 00019 #include <cppunit/TestAssert.h> 00020 #include "baseTest.h" 00021 #include "threadFactory.h" 00022 #include <amino/mutex.h> 00023 #include <amino/thread.h> 00024 00025 using namespace amino; 00026 namespace test{ 00027 00032 class MutexTest : public CppUnit::TestFixture { 00033 CPPUNIT_TEST_SUITE(MutexTest); 00034 CPPUNIT_TEST(testMutex); 00035 CPPUNIT_TEST(testRecursiveMutex); 00036 CPPUNIT_TEST_SUITE_END(); 00037 00038 private: 00039 mutex * mut; 00040 00041 public: 00042 void setUp() { 00043 mut = new mutex(); 00044 } 00045 00046 void reset(){ 00047 delete mut; 00048 mut = new mutex(); 00049 } 00050 00057 void testMutex(); 00058 00059 void testRecursiveMutex(); 00060 00061 void tearDown() { 00062 delete mut; 00063 } 00064 }; 00065 00066 class MutexThread : public Thread { 00067 private: 00068 mutex * fMutex; 00069 public: 00070 MutexThread(mutex * mut) { 00071 fMutex = mut; 00072 } 00073 00074 void* run() { 00075 fMutex->lock(); 00076 sleep(3); 00077 fMutex->unlock(); 00078 return NULL; 00079 } 00080 }; 00081 00082 class RecursiveMutexThread : public Thread { 00083 private: 00084 mutex * fMutex; 00085 public: 00086 RecursiveMutexThread(mutex * mut) { 00087 fMutex = mut; 00088 } 00089 00090 void* run() { 00091 fMutex->lock(); 00092 fMutex->lock(); 00093 sleep(3); 00094 fMutex->unlock(); 00095 fMutex->unlock(); 00096 00097 return NULL; 00098 } 00099 }; 00100 } 00101 #endif /*MUTEXTEST_H_*/