187d2adbcSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 387d2adbcSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 487d2adbcSAndrew Rist * or more contributor license agreements. See the NOTICE file 587d2adbcSAndrew Rist * distributed with this work for additional information 687d2adbcSAndrew Rist * regarding copyright ownership. The ASF licenses this file 787d2adbcSAndrew Rist * to you under the Apache License, Version 2.0 (the 887d2adbcSAndrew Rist * "License"); you may not use this file except in compliance 987d2adbcSAndrew Rist * with the License. You may obtain a copy of the License at 1087d2adbcSAndrew Rist * 1187d2adbcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 1287d2adbcSAndrew Rist * 1387d2adbcSAndrew Rist * Unless required by applicable law or agreed to in writing, 1487d2adbcSAndrew Rist * software distributed under the License is distributed on an 1587d2adbcSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1687d2adbcSAndrew Rist * KIND, either express or implied. See the License for the 1787d2adbcSAndrew Rist * specific language governing permissions and limitations 1887d2adbcSAndrew Rist * under the License. 1987d2adbcSAndrew Rist * 2087d2adbcSAndrew Rist *************************************************************/ 2187d2adbcSAndrew Rist 2287d2adbcSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_sal.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir //------------------------------------------------------------------------ 28cdf0e10cSrcweir // include files 29cdf0e10cSrcweir //------------------------------------------------------------------------ 308748f2e1SDamjan Jovanovic #include "gtest/gtest.h" 31cdf0e10cSrcweir #include <osl_Mutex_Const.h> 32cdf0e10cSrcweir 33*a03c9fa9SDamjan Jovanovic #ifdef WNT 34*a03c9fa9SDamjan Jovanovic #include <tools/prewin.h> 35*a03c9fa9SDamjan Jovanovic #define WIN32_LEAN_AND_MEAN 36*a03c9fa9SDamjan Jovanovic #include <windows.h> 37*a03c9fa9SDamjan Jovanovic #include <tools/postwin.h> 38*a03c9fa9SDamjan Jovanovic #endif 39*a03c9fa9SDamjan Jovanovic 40cdf0e10cSrcweir using namespace osl; 41cdf0e10cSrcweir using namespace rtl; 42cdf0e10cSrcweir 43cdf0e10cSrcweir //------------------------------------------------------------------------ 44cdf0e10cSrcweir // helper functions 45cdf0e10cSrcweir //------------------------------------------------------------------------ 46cdf0e10cSrcweir 47cdf0e10cSrcweir /** print a UNI_CODE String. 48cdf0e10cSrcweir */ 49cdf0e10cSrcweir inline void printUString( const ::rtl::OUString & str ) 50cdf0e10cSrcweir { 51cdf0e10cSrcweir rtl::OString aString; 52cdf0e10cSrcweir 53cdf0e10cSrcweir printf("#printUString_u# " ); 54cdf0e10cSrcweir aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US ); 55cdf0e10cSrcweir printf("%s\n", aString.getStr( ) ); 56cdf0e10cSrcweir } 57cdf0e10cSrcweir 58cdf0e10cSrcweir /** print Boolean value. 59cdf0e10cSrcweir */ 60cdf0e10cSrcweir inline void printBool( sal_Bool bOk ) 61cdf0e10cSrcweir { 62cdf0e10cSrcweir printf("#printBool# " ); 63cdf0e10cSrcweir ( sal_True == bOk ) ? printf("YES!\n" ): printf("NO!\n" ); 64cdf0e10cSrcweir } 65cdf0e10cSrcweir 66cdf0e10cSrcweir /** pause nSec seconds helper function. 67cdf0e10cSrcweir */ 68cdf0e10cSrcweir namespace ThreadHelper 69cdf0e10cSrcweir { 70cdf0e10cSrcweir void thread_sleep( sal_Int32 _nSec ) 71cdf0e10cSrcweir { 72cdf0e10cSrcweir /// print statement in thread process must use fflush() to force display. 73cdf0e10cSrcweir // t_print("# wait %d seconds. ", _nSec ); 74cdf0e10cSrcweir fflush(stdout); 75cdf0e10cSrcweir 76cdf0e10cSrcweir #ifdef WNT //Windows 77cdf0e10cSrcweir Sleep( _nSec * 1000 ); 78cdf0e10cSrcweir #endif 79cdf0e10cSrcweir #if ( defined UNX ) || ( defined OS2 ) //Unix 80cdf0e10cSrcweir sleep( _nSec ); 81cdf0e10cSrcweir #endif 82cdf0e10cSrcweir // printf("# done\n" ); 83cdf0e10cSrcweir } 84cdf0e10cSrcweir void thread_sleep_tenth_sec(sal_Int32 _nTenthSec) 85cdf0e10cSrcweir { 86cdf0e10cSrcweir #ifdef WNT //Windows 87cdf0e10cSrcweir Sleep(_nTenthSec * 100 ); 88cdf0e10cSrcweir #endif 89cdf0e10cSrcweir #if ( defined UNX ) || ( defined OS2 ) //Unix 90cdf0e10cSrcweir TimeValue nTV; 91cdf0e10cSrcweir nTV.Seconds = static_cast<sal_uInt32>( _nTenthSec/10 ); 92cdf0e10cSrcweir nTV.Nanosec = ( (_nTenthSec%10 ) * 100000000 ); 93cdf0e10cSrcweir osl_waitThread(&nTV); 94cdf0e10cSrcweir #endif 95cdf0e10cSrcweir } 96cdf0e10cSrcweir } 97cdf0e10cSrcweir 98cdf0e10cSrcweir 99cdf0e10cSrcweir //------------------------------------------------------------------------ 100cdf0e10cSrcweir // Beginning of the test cases for osl_Mutex class 101cdf0e10cSrcweir //------------------------------------------------------------------------ 102cdf0e10cSrcweir 103cdf0e10cSrcweir 104cdf0e10cSrcweir /** mutually exclusive data 105cdf0e10cSrcweir */ 106cdf0e10cSrcweir struct resource { 107cdf0e10cSrcweir sal_Int32 data1; 108cdf0e10cSrcweir sal_Int32 data2; 109cdf0e10cSrcweir Mutex lock; 110cdf0e10cSrcweir }; 111cdf0e10cSrcweir 112cdf0e10cSrcweir /** IncreaseThread provide data. 113cdf0e10cSrcweir */ 114cdf0e10cSrcweir class IncreaseThread : public Thread 115cdf0e10cSrcweir { 116cdf0e10cSrcweir public: 117cdf0e10cSrcweir IncreaseThread( struct resource *pData ): pResource( pData ) { } 118cdf0e10cSrcweir 119cdf0e10cSrcweir ~IncreaseThread( ) 120cdf0e10cSrcweir { 121*a03c9fa9SDamjan Jovanovic EXPECT_TRUE(sal_False == this -> isRunning( )) << "#IncreaseThread does not shutdown properly.\n"; 122cdf0e10cSrcweir } 123cdf0e10cSrcweir protected: 124cdf0e10cSrcweir struct resource *pResource; 125cdf0e10cSrcweir 126cdf0e10cSrcweir void SAL_CALL run( ) 127cdf0e10cSrcweir { 128cdf0e10cSrcweir pResource->lock.acquire( ); 129cdf0e10cSrcweir for( sal_Int8 i = 0; i < 3; i++ ) 130cdf0e10cSrcweir { 131cdf0e10cSrcweir pResource->data1++; 132cdf0e10cSrcweir yield( ); //yield() give CPU time to other thread, other thread if not block, they will change the data; 133cdf0e10cSrcweir } 134cdf0e10cSrcweir if ( pResource->data2 == 0 ) 135cdf0e10cSrcweir pResource->data2 = ( pResource->data1 > 0 ? pResource->data1 : 0 - pResource->data1 ); 136cdf0e10cSrcweir pResource->lock.release(); 137cdf0e10cSrcweir } 138cdf0e10cSrcweir }; 139cdf0e10cSrcweir 140cdf0e10cSrcweir /** DecreaseThread consume data. 141cdf0e10cSrcweir */ 142cdf0e10cSrcweir class DecreaseThread : public Thread 143cdf0e10cSrcweir { 144cdf0e10cSrcweir public: 145cdf0e10cSrcweir DecreaseThread( struct resource *pData ): pResource( pData ) { } 146cdf0e10cSrcweir 147cdf0e10cSrcweir ~DecreaseThread( ) 148cdf0e10cSrcweir { 149*a03c9fa9SDamjan Jovanovic EXPECT_TRUE(sal_False == this -> isRunning( )) << "#DecreaseThread does not shutdown properly.\n"; 150cdf0e10cSrcweir } 151cdf0e10cSrcweir protected: 152cdf0e10cSrcweir struct resource *pResource; 153cdf0e10cSrcweir 154cdf0e10cSrcweir void SAL_CALL run( ) 155cdf0e10cSrcweir { 156cdf0e10cSrcweir pResource->lock.acquire( ); 157cdf0e10cSrcweir for( sal_Int8 i = 0; i < 3; i++ ) 158cdf0e10cSrcweir { 159cdf0e10cSrcweir pResource->data1--; 160cdf0e10cSrcweir yield( ); //yield() give CPU time to other thread, other thread if not block, they will change the data; 161cdf0e10cSrcweir } 162cdf0e10cSrcweir if ( pResource->data2 == 0 ) 163cdf0e10cSrcweir pResource->data2 = ( pResource->data1 > 0 ? pResource->data1 : 0 - pResource->data1 ); 164cdf0e10cSrcweir pResource->lock.release(); 165cdf0e10cSrcweir } 166cdf0e10cSrcweir }; 167cdf0e10cSrcweir 168cdf0e10cSrcweir 169cdf0e10cSrcweir /** chain structure used in Threads as critical resource 170cdf0e10cSrcweir */ 171cdf0e10cSrcweir struct chain { 172cdf0e10cSrcweir sal_Int32 buffer[ BUFFER_SIZE ]; 173cdf0e10cSrcweir Mutex lock; 174cdf0e10cSrcweir sal_Int8 pos; 175cdf0e10cSrcweir }; 176cdf0e10cSrcweir 177cdf0e10cSrcweir /** PutThread write to the chain structure in a mutex manner. 178cdf0e10cSrcweir */ 179cdf0e10cSrcweir class PutThread : public Thread 180cdf0e10cSrcweir { 181cdf0e10cSrcweir public: 182cdf0e10cSrcweir //get the struct pointer to write data to buffer 183cdf0e10cSrcweir PutThread( struct chain* pData ): pChain( pData ) { } 184cdf0e10cSrcweir 185cdf0e10cSrcweir ~PutThread( ) 186cdf0e10cSrcweir { 187*a03c9fa9SDamjan Jovanovic EXPECT_TRUE(sal_False == this -> isRunning( )) << "#PutThread does not shutdown properly.\n"; 188cdf0e10cSrcweir } 189cdf0e10cSrcweir protected: 190cdf0e10cSrcweir struct chain* pChain; 191cdf0e10cSrcweir 192cdf0e10cSrcweir void SAL_CALL run( ) 193cdf0e10cSrcweir { 194cdf0e10cSrcweir //block here if the mutex has been acquired 195cdf0e10cSrcweir pChain->lock.acquire( ); 196cdf0e10cSrcweir 197cdf0e10cSrcweir //current position in buffer to write 198cdf0e10cSrcweir sal_Int8 nPos = pChain->pos; 199cdf0e10cSrcweir oslThreadIdentifier oId = getIdentifier( ); 200cdf0e10cSrcweir //write data 201cdf0e10cSrcweir sal_Int8 i; 202cdf0e10cSrcweir for ( i = 0; i < 5; i++ ) 203cdf0e10cSrcweir { 204cdf0e10cSrcweir pChain->buffer[ nPos + i ] = oId; 205cdf0e10cSrcweir yield( ); 206cdf0e10cSrcweir } 207cdf0e10cSrcweir //revise the position 208cdf0e10cSrcweir pChain->pos = nPos + i; 209cdf0e10cSrcweir 210cdf0e10cSrcweir //finish writing, release the mutex 211cdf0e10cSrcweir pChain->lock.release(); 212cdf0e10cSrcweir } 213cdf0e10cSrcweir }; 214cdf0e10cSrcweir 215cdf0e10cSrcweir /** thread for testing Mutex acquire. 216cdf0e10cSrcweir */ 217cdf0e10cSrcweir class HoldThread : public Thread 218cdf0e10cSrcweir { 219cdf0e10cSrcweir public: 220cdf0e10cSrcweir //get the Mutex pointer to operate 221cdf0e10cSrcweir HoldThread( Mutex* pMutex ): pMyMutex( pMutex ) { } 222cdf0e10cSrcweir 223cdf0e10cSrcweir ~HoldThread( ) 224cdf0e10cSrcweir { 225*a03c9fa9SDamjan Jovanovic EXPECT_TRUE(sal_False == this -> isRunning( )) << "#HoldThread does not shutdown properly.\n"; 226cdf0e10cSrcweir } 227cdf0e10cSrcweir protected: 228cdf0e10cSrcweir Mutex* pMyMutex; 229cdf0e10cSrcweir 230cdf0e10cSrcweir void SAL_CALL run() 231cdf0e10cSrcweir { 232cdf0e10cSrcweir // block here if the mutex has been acquired 233cdf0e10cSrcweir pMyMutex->acquire( ); 234cdf0e10cSrcweir printf("# Mutex acquired. \n" ); 235cdf0e10cSrcweir pMyMutex->release( ); 236cdf0e10cSrcweir } 237cdf0e10cSrcweir }; 238cdf0e10cSrcweir 239cdf0e10cSrcweir class WaitThread : public Thread 240cdf0e10cSrcweir { 241cdf0e10cSrcweir public: 242cdf0e10cSrcweir //get the Mutex pointer to operate 243cdf0e10cSrcweir WaitThread( Mutex* pMutex ): pMyMutex( pMutex ) { } 244cdf0e10cSrcweir 245cdf0e10cSrcweir ~WaitThread( ) 246cdf0e10cSrcweir { 247*a03c9fa9SDamjan Jovanovic EXPECT_TRUE(sal_False == this -> isRunning( )) << "#WaitThread does not shutdown properly.\n"; 248cdf0e10cSrcweir } 249cdf0e10cSrcweir protected: 250cdf0e10cSrcweir Mutex* pMyMutex; 251cdf0e10cSrcweir 252cdf0e10cSrcweir void SAL_CALL run( ) 253cdf0e10cSrcweir { 254cdf0e10cSrcweir // block here if the mutex has been acquired 255cdf0e10cSrcweir pMyMutex->acquire( ); 256cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec( 2 ); 257cdf0e10cSrcweir pMyMutex->release( ); 258cdf0e10cSrcweir } 259cdf0e10cSrcweir }; 260cdf0e10cSrcweir 261cdf0e10cSrcweir /** thread for testing getGlobalMutex. 262cdf0e10cSrcweir */ 263cdf0e10cSrcweir class GlobalMutexThread : public Thread 264cdf0e10cSrcweir { 265cdf0e10cSrcweir public: 266cdf0e10cSrcweir //get the Mutex pointer to operate 267cdf0e10cSrcweir GlobalMutexThread( ){ } 268cdf0e10cSrcweir 269cdf0e10cSrcweir ~GlobalMutexThread( ) 270cdf0e10cSrcweir { 271*a03c9fa9SDamjan Jovanovic EXPECT_TRUE(sal_False == this -> isRunning( )) << "#GlobalMutexThread does not shutdown properly.\n"; 272cdf0e10cSrcweir } 273cdf0e10cSrcweir protected: 274cdf0e10cSrcweir void SAL_CALL run( ) 275cdf0e10cSrcweir { 276cdf0e10cSrcweir // block here if the mutex has been acquired 277cdf0e10cSrcweir Mutex* pGlobalMutex; 278cdf0e10cSrcweir pGlobalMutex = pGlobalMutex->getGlobalMutex( ); 279cdf0e10cSrcweir pGlobalMutex->acquire( ); 280cdf0e10cSrcweir printf("# Global Mutex acquired. \n" ); 281cdf0e10cSrcweir pGlobalMutex->release( ); 282cdf0e10cSrcweir } 283cdf0e10cSrcweir }; 284cdf0e10cSrcweir 285cdf0e10cSrcweir 286cdf0e10cSrcweir //-------------------------------------------------------------- 287cdf0e10cSrcweir namespace osl_Mutex 288cdf0e10cSrcweir { 289cdf0e10cSrcweir 290cdf0e10cSrcweir /** Test of the osl::Mutex::constructor 291cdf0e10cSrcweir */ 2928748f2e1SDamjan Jovanovic class MutexConstructor : public ::testing::Test 293cdf0e10cSrcweir { 294cdf0e10cSrcweir public: 295cdf0e10cSrcweir // initialise your test code values here. 296cdf0e10cSrcweir struct chain m_Data; 297cdf0e10cSrcweir struct resource m_Res; 298cdf0e10cSrcweir 2998748f2e1SDamjan Jovanovic void SetUp( ) 300cdf0e10cSrcweir { 301cdf0e10cSrcweir for ( sal_Int8 i=0; i < BUFFER_SIZE; i++ ) 302cdf0e10cSrcweir m_Data.buffer[i] = 0; 303cdf0e10cSrcweir m_Data.pos = 0; 304cdf0e10cSrcweir 305cdf0e10cSrcweir m_Res.data1 = 0; 306cdf0e10cSrcweir m_Res.data2 = 0; 307cdf0e10cSrcweir } 308cdf0e10cSrcweir 3098748f2e1SDamjan Jovanovic void TearDown() 310cdf0e10cSrcweir { 311cdf0e10cSrcweir } 3128748f2e1SDamjan Jovanovic }; // class ctor 3138748f2e1SDamjan Jovanovic 3148748f2e1SDamjan Jovanovic /** Create two threads to write data to the same buffer, use Mutex to assure 3158748f2e1SDamjan Jovanovic during one thread write data five times, the other thread should not begin writing. 3168748f2e1SDamjan Jovanovic the two threads wrote two different datas: their thread ID, so we can check the datas 3178748f2e1SDamjan Jovanovic in buffer to know the order of the two threads writing 3188748f2e1SDamjan Jovanovic */ 3198748f2e1SDamjan Jovanovic TEST_F(MutexConstructor, ctor_001) 3208748f2e1SDamjan Jovanovic { 3218748f2e1SDamjan Jovanovic PutThread myThread1( &m_Data ); 3228748f2e1SDamjan Jovanovic PutThread myThread2( &m_Data ); 3238748f2e1SDamjan Jovanovic 3248748f2e1SDamjan Jovanovic myThread1.create( ); 3258748f2e1SDamjan Jovanovic myThread2.create( ); 3268748f2e1SDamjan Jovanovic 3278748f2e1SDamjan Jovanovic //wait until the two threads terminate 3288748f2e1SDamjan Jovanovic myThread1.join( ); 3298748f2e1SDamjan Jovanovic myThread2.join( ); 3308748f2e1SDamjan Jovanovic 3318748f2e1SDamjan Jovanovic sal_Bool bRes = sal_False; 3328748f2e1SDamjan Jovanovic 3338748f2e1SDamjan Jovanovic // every 5 datas should the same 3348748f2e1SDamjan Jovanovic // LLA: this is not a good check, it's too fix 3358748f2e1SDamjan Jovanovic if (m_Data.buffer[0] == m_Data.buffer[1] && 3368748f2e1SDamjan Jovanovic m_Data.buffer[1] == m_Data.buffer[2] && 3378748f2e1SDamjan Jovanovic m_Data.buffer[2] == m_Data.buffer[3] && 3388748f2e1SDamjan Jovanovic m_Data.buffer[3] == m_Data.buffer[4] && 3398748f2e1SDamjan Jovanovic m_Data.buffer[5] == m_Data.buffer[6] && 3408748f2e1SDamjan Jovanovic m_Data.buffer[6] == m_Data.buffer[7] && 3418748f2e1SDamjan Jovanovic m_Data.buffer[7] == m_Data.buffer[8] && 3428748f2e1SDamjan Jovanovic m_Data.buffer[8] == m_Data.buffer[9]) 3438748f2e1SDamjan Jovanovic bRes = sal_True; 3448748f2e1SDamjan Jovanovic 3458748f2e1SDamjan Jovanovic /*for (sal_Int8 i=0; i<BUFFER_SIZE; i++) 3468748f2e1SDamjan Jovanovic printf("#data in buffer is %d\n", m_Data.buffer[i]); 347cdf0e10cSrcweir */ 3488748f2e1SDamjan Jovanovic 3498748f2e1SDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "Mutex ctor"; 3508748f2e1SDamjan Jovanovic 3518748f2e1SDamjan Jovanovic } 3528748f2e1SDamjan Jovanovic 3538748f2e1SDamjan Jovanovic /** Create two threads to write data to operate on the same number , use Mutex to assure, 3548748f2e1SDamjan Jovanovic one thread increase data 3 times, the other thread decrease 3 times, store the operate 3558748f2e1SDamjan Jovanovic result when the first thread complete, if it is interrupt by the other thread, the stored 3568748f2e1SDamjan Jovanovic number will not be 3. 3578748f2e1SDamjan Jovanovic */ 3588748f2e1SDamjan Jovanovic TEST_F(MutexConstructor, ctor_002) 3598748f2e1SDamjan Jovanovic { 3608748f2e1SDamjan Jovanovic IncreaseThread myThread1( &m_Res ); 3618748f2e1SDamjan Jovanovic DecreaseThread myThread2( &m_Res ); 3628748f2e1SDamjan Jovanovic 3638748f2e1SDamjan Jovanovic myThread1.create( ); 3648748f2e1SDamjan Jovanovic myThread2.create( ); 3658748f2e1SDamjan Jovanovic 3668748f2e1SDamjan Jovanovic //wait until the two threads terminate 3678748f2e1SDamjan Jovanovic myThread1.join( ); 3688748f2e1SDamjan Jovanovic myThread2.join( ); 3698748f2e1SDamjan Jovanovic 3708748f2e1SDamjan Jovanovic sal_Bool bRes = sal_False; 371cdf0e10cSrcweir 3728748f2e1SDamjan Jovanovic // every 5 datas should the same 3738748f2e1SDamjan Jovanovic if ( ( m_Res.data1 == 0 ) && ( m_Res.data2 == 3 ) ) 3748748f2e1SDamjan Jovanovic bRes = sal_True; 3758748f2e1SDamjan Jovanovic 3768748f2e1SDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "test Mutex ctor function: increase and decrease a number 3 times without interrupt."; 3778748f2e1SDamjan Jovanovic } 378cdf0e10cSrcweir 379cdf0e10cSrcweir 380cdf0e10cSrcweir /** Test of the osl::Mutex::acquire method 381cdf0e10cSrcweir */ 3828748f2e1SDamjan Jovanovic class acquire : public ::testing::Test 383cdf0e10cSrcweir { 384cdf0e10cSrcweir public: 3858748f2e1SDamjan Jovanovic }; // class acquire 386cdf0e10cSrcweir 3878748f2e1SDamjan Jovanovic // acquire mutex in main thread, and then call acquire again in myThread, 3888748f2e1SDamjan Jovanovic // the child thread should block, wait 2 secs, it still block. 3898748f2e1SDamjan Jovanovic // Then release mutex in main thread, the child thread could return from acquire, 3908748f2e1SDamjan Jovanovic // and go to exec next statement, so could terminate quickly. 3918748f2e1SDamjan Jovanovic TEST_F(acquire, acquire_001 ) 3928748f2e1SDamjan Jovanovic { 3938748f2e1SDamjan Jovanovic Mutex aMutex; 3948748f2e1SDamjan Jovanovic //acquire here 3958748f2e1SDamjan Jovanovic sal_Bool bRes = aMutex.acquire( ); 3968748f2e1SDamjan Jovanovic // pass the pointer of mutex to child thread 3978748f2e1SDamjan Jovanovic HoldThread myThread( &aMutex ); 3988748f2e1SDamjan Jovanovic myThread.create( ); 3998748f2e1SDamjan Jovanovic 4008748f2e1SDamjan Jovanovic ThreadHelper::thread_sleep_tenth_sec( 2 ); 4018748f2e1SDamjan Jovanovic // if acquire in myThread does not work, 2 secs is long enough, 4028748f2e1SDamjan Jovanovic // myThread should terminate now, and bRes1 should be sal_False 4038748f2e1SDamjan Jovanovic sal_Bool bRes1 = myThread.isRunning( ); 404cdf0e10cSrcweir 4058748f2e1SDamjan Jovanovic aMutex.release( ); 4068748f2e1SDamjan Jovanovic ThreadHelper::thread_sleep_tenth_sec( 1 ); 4078748f2e1SDamjan Jovanovic // after release mutex, myThread stops blocking and will terminate immediately 4088748f2e1SDamjan Jovanovic sal_Bool bRes2 = myThread.isRunning( ); 4098748f2e1SDamjan Jovanovic myThread.join( ); 4108748f2e1SDamjan Jovanovic 4118748f2e1SDamjan Jovanovic ASSERT_TRUE(bRes == sal_True && bRes1 == sal_True && bRes2 == sal_False) << "Mutex acquire"; 4128748f2e1SDamjan Jovanovic } 4138748f2e1SDamjan Jovanovic 4148748f2e1SDamjan Jovanovic //in the same thread, acquire twice should success 4158748f2e1SDamjan Jovanovic TEST_F(acquire, acquire_002) 4168748f2e1SDamjan Jovanovic { 4178748f2e1SDamjan Jovanovic Mutex aMutex; 4188748f2e1SDamjan Jovanovic //acquire here 4198748f2e1SDamjan Jovanovic sal_Bool bRes = aMutex.acquire(); 4208748f2e1SDamjan Jovanovic sal_Bool bRes1 = aMutex.acquire(); 4218748f2e1SDamjan Jovanovic 4228748f2e1SDamjan Jovanovic sal_Bool bRes2 = aMutex.tryToAcquire(); 4238748f2e1SDamjan Jovanovic 4248748f2e1SDamjan Jovanovic aMutex.release(); 4258748f2e1SDamjan Jovanovic 4268748f2e1SDamjan Jovanovic ASSERT_TRUE(bRes == sal_True && bRes1 == sal_True && bRes2 == sal_True) << "Mutex acquire"; 427cdf0e10cSrcweir 4288748f2e1SDamjan Jovanovic } 4298748f2e1SDamjan Jovanovic 430cdf0e10cSrcweir 431cdf0e10cSrcweir /** Test of the osl::Mutex::tryToAcquire method 432cdf0e10cSrcweir */ 4338748f2e1SDamjan Jovanovic class tryToAcquire : public ::testing::Test 434cdf0e10cSrcweir { 435cdf0e10cSrcweir public: 436cdf0e10cSrcweir }; // class tryToAcquire 4378748f2e1SDamjan Jovanovic 4388748f2e1SDamjan Jovanovic // First let child thread acquire the mutex, and wait 2 secs, during the 2 secs, 4398748f2e1SDamjan Jovanovic // in main thread, tryToAcquire mutex should return False 4408748f2e1SDamjan Jovanovic // then after the child thread terminated, tryToAcquire should return True 4418748f2e1SDamjan Jovanovic TEST_F(tryToAcquire, tryToAcquire_001) 4428748f2e1SDamjan Jovanovic { 4438748f2e1SDamjan Jovanovic Mutex aMutex; 4448748f2e1SDamjan Jovanovic WaitThread myThread(&aMutex); 4458748f2e1SDamjan Jovanovic myThread.create(); 4468748f2e1SDamjan Jovanovic 4478748f2e1SDamjan Jovanovic // ensure the child thread acquire the mutex 4488748f2e1SDamjan Jovanovic ThreadHelper::thread_sleep_tenth_sec(1); 4498748f2e1SDamjan Jovanovic 4508748f2e1SDamjan Jovanovic sal_Bool bRes1 = aMutex.tryToAcquire(); 4518748f2e1SDamjan Jovanovic 4528748f2e1SDamjan Jovanovic if (bRes1 == sal_True) 4538748f2e1SDamjan Jovanovic aMutex.release(); 4548748f2e1SDamjan Jovanovic // wait the child thread terminate 4558748f2e1SDamjan Jovanovic myThread.join(); 4568748f2e1SDamjan Jovanovic 4578748f2e1SDamjan Jovanovic sal_Bool bRes2 = aMutex.tryToAcquire(); 4588748f2e1SDamjan Jovanovic 4598748f2e1SDamjan Jovanovic if (bRes2 == sal_True) 4608748f2e1SDamjan Jovanovic aMutex.release(); 4618748f2e1SDamjan Jovanovic 4628748f2e1SDamjan Jovanovic ASSERT_TRUE(bRes1 == sal_False && bRes2 == sal_True) << "Try to acquire Mutex"; 4638748f2e1SDamjan Jovanovic } 4648748f2e1SDamjan Jovanovic 465cdf0e10cSrcweir 466cdf0e10cSrcweir /** Test of the osl::Mutex::release method 467cdf0e10cSrcweir */ 4688748f2e1SDamjan Jovanovic class release : public ::testing::Test 469cdf0e10cSrcweir { 470cdf0e10cSrcweir public: 4718748f2e1SDamjan Jovanovic }; // class release 472cdf0e10cSrcweir 4738748f2e1SDamjan Jovanovic /** acquire/release are not used in pairs: after child thread acquired mutex, 4748748f2e1SDamjan Jovanovic the main thread release it, then any thread could acquire it. 4758748f2e1SDamjan Jovanovic */ 4768748f2e1SDamjan Jovanovic TEST_F(release, release_001) 4778748f2e1SDamjan Jovanovic { 4788748f2e1SDamjan Jovanovic Mutex aMutex; 4798748f2e1SDamjan Jovanovic WaitThread myThread( &aMutex ); 4808748f2e1SDamjan Jovanovic myThread.create( ); 4818748f2e1SDamjan Jovanovic 4828748f2e1SDamjan Jovanovic // ensure the child thread acquire the mutex 4838748f2e1SDamjan Jovanovic ThreadHelper::thread_sleep_tenth_sec( 1 ); 4848748f2e1SDamjan Jovanovic 4858748f2e1SDamjan Jovanovic sal_Bool bRunning = myThread.isRunning( ); 4868748f2e1SDamjan Jovanovic sal_Bool bRes1 = aMutex.tryToAcquire( ); 4878748f2e1SDamjan Jovanovic // wait the child thread terminate 4888748f2e1SDamjan Jovanovic myThread.join( ); 4898748f2e1SDamjan Jovanovic 4908748f2e1SDamjan Jovanovic sal_Bool bRes2 = aMutex.tryToAcquire( ); 4918748f2e1SDamjan Jovanovic 4928748f2e1SDamjan Jovanovic if ( bRes2 == sal_True ) 4938748f2e1SDamjan Jovanovic aMutex.release( ); 4948748f2e1SDamjan Jovanovic 4958748f2e1SDamjan Jovanovic ASSERT_TRUE(bRes1 == sal_False && bRes2 == sal_True && bRunning == sal_True) << "release Mutex: try to aquire before and after the mutex has been released"; 4968748f2e1SDamjan Jovanovic 4978748f2e1SDamjan Jovanovic } 4988748f2e1SDamjan Jovanovic 4998748f2e1SDamjan Jovanovic // how about release twice? 5008748f2e1SDamjan Jovanovic TEST_F(release, release_002) 5018748f2e1SDamjan Jovanovic { 502cdf0e10cSrcweir // LLA: is this a real test? 503cdf0e10cSrcweir #if 0 5048748f2e1SDamjan Jovanovic Mutex aMutex; 5058748f2e1SDamjan Jovanovic sal_Bool bRes1 = aMutex.release( ); 5068748f2e1SDamjan Jovanovic sal_Bool bRes2 = aMutex.release( ); 507cdf0e10cSrcweir 5088748f2e1SDamjan Jovanovic ASSERT_TRUE(bRes1 == sal_False && bRes2 == sal_False) << "release Mutex: mutex should not be released without aquire, should not release twice. although the behaviour is still under discussion, this test is passed on (LINUX), not passed on (SOLARIS)&(WINDOWS)"; 5098748f2e1SDamjan Jovanovic #endif 5108748f2e1SDamjan Jovanovic } 511cdf0e10cSrcweir 512cdf0e10cSrcweir 513cdf0e10cSrcweir /** Test of the osl::Mutex::getGlobalMutex method 514cdf0e10cSrcweir */ 5158748f2e1SDamjan Jovanovic class getGlobalMutex : public ::testing::Test 516cdf0e10cSrcweir { 517cdf0e10cSrcweir public: 5188748f2e1SDamjan Jovanovic }; // class getGlobalMutex 5198748f2e1SDamjan Jovanovic 5208748f2e1SDamjan Jovanovic // initialise your test code values here. 5218748f2e1SDamjan Jovanovic TEST_F(getGlobalMutex, getGlobalMutex_001) 5228748f2e1SDamjan Jovanovic { 5238748f2e1SDamjan Jovanovic Mutex* pGlobalMutex; 5248748f2e1SDamjan Jovanovic pGlobalMutex = pGlobalMutex->getGlobalMutex(); 5258748f2e1SDamjan Jovanovic pGlobalMutex->acquire(); 5268748f2e1SDamjan Jovanovic 5278748f2e1SDamjan Jovanovic GlobalMutexThread myThread; 5288748f2e1SDamjan Jovanovic myThread.create(); 5298748f2e1SDamjan Jovanovic 5308748f2e1SDamjan Jovanovic ThreadHelper::thread_sleep_tenth_sec(1); 5318748f2e1SDamjan Jovanovic sal_Bool bRes1 = myThread.isRunning(); 5328748f2e1SDamjan Jovanovic 5338748f2e1SDamjan Jovanovic pGlobalMutex->release(); 5348748f2e1SDamjan Jovanovic ThreadHelper::thread_sleep_tenth_sec(1); 5358748f2e1SDamjan Jovanovic // after release mutex, myThread stops blocking and will terminate immediately 5368748f2e1SDamjan Jovanovic sal_Bool bRes2 = myThread.isRunning(); 5378748f2e1SDamjan Jovanovic 5388748f2e1SDamjan Jovanovic ASSERT_TRUE(bRes1 == sal_True && bRes2 == sal_False) << "Global Mutex works"; 5398748f2e1SDamjan Jovanovic } 5408748f2e1SDamjan Jovanovic 5418748f2e1SDamjan Jovanovic TEST_F(getGlobalMutex, getGlobalMutex_002 ) 5428748f2e1SDamjan Jovanovic { 5438748f2e1SDamjan Jovanovic sal_Bool bRes; 5448748f2e1SDamjan Jovanovic 5458748f2e1SDamjan Jovanovic Mutex *pGlobalMutex; 5468748f2e1SDamjan Jovanovic pGlobalMutex = pGlobalMutex->getGlobalMutex( ); 5478748f2e1SDamjan Jovanovic pGlobalMutex->acquire( ); 548cdf0e10cSrcweir { 5498748f2e1SDamjan Jovanovic Mutex *pGlobalMutex1; 5508748f2e1SDamjan Jovanovic pGlobalMutex1 = pGlobalMutex1->getGlobalMutex( ); 5518748f2e1SDamjan Jovanovic bRes = pGlobalMutex1->release( ); 552cdf0e10cSrcweir } 5538748f2e1SDamjan Jovanovic 5548748f2e1SDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "Global Mutex works: if the code between {} get the different mutex as the former one, it will return false when release."; 5558748f2e1SDamjan Jovanovic } 5568748f2e1SDamjan Jovanovic 557cdf0e10cSrcweir } // namespace osl_Mutex 558cdf0e10cSrcweir 559cdf0e10cSrcweir 560cdf0e10cSrcweir //------------------------------------------------------------------------ 561cdf0e10cSrcweir // Beginning of the test cases for osl_Guard class 562cdf0e10cSrcweir //------------------------------------------------------------------------ 563cdf0e10cSrcweir 564cdf0e10cSrcweir class GuardThread : public Thread 565cdf0e10cSrcweir { 566cdf0e10cSrcweir public: 567cdf0e10cSrcweir //get the Mutex pointer to operate 568cdf0e10cSrcweir GuardThread( Mutex* pMutex ): pMyMutex( pMutex ) { } 569cdf0e10cSrcweir 570cdf0e10cSrcweir ~GuardThread( ) 571cdf0e10cSrcweir { 572*a03c9fa9SDamjan Jovanovic EXPECT_TRUE(sal_False == this -> isRunning( )) << "#GuardThread does not shutdown properly.\n"; 573cdf0e10cSrcweir } 574cdf0e10cSrcweir protected: 575cdf0e10cSrcweir Mutex* pMyMutex; 576cdf0e10cSrcweir 577cdf0e10cSrcweir void SAL_CALL run( ) 578cdf0e10cSrcweir { 579cdf0e10cSrcweir // block here if the mutex has been acquired 580cdf0e10cSrcweir MutexGuard aGuard( pMyMutex ); 581cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec( 2 ); 582cdf0e10cSrcweir } 583cdf0e10cSrcweir }; 584cdf0e10cSrcweir 585cdf0e10cSrcweir 586cdf0e10cSrcweir namespace osl_Guard 587cdf0e10cSrcweir { 5888748f2e1SDamjan Jovanovic class GuardThreadConstructor : public ::testing::Test 589cdf0e10cSrcweir { 590cdf0e10cSrcweir public: 591cdf0e10cSrcweir }; // class ctor 5928748f2e1SDamjan Jovanovic 5938748f2e1SDamjan Jovanovic // insert your test code here. 5948748f2e1SDamjan Jovanovic TEST_F(GuardThreadConstructor, ctor_001) 5958748f2e1SDamjan Jovanovic { 5968748f2e1SDamjan Jovanovic Mutex aMutex; 5978748f2e1SDamjan Jovanovic GuardThread myThread(&aMutex); 5988748f2e1SDamjan Jovanovic myThread.create(); 5998748f2e1SDamjan Jovanovic 6008748f2e1SDamjan Jovanovic ThreadHelper::thread_sleep_tenth_sec(1); 6018748f2e1SDamjan Jovanovic sal_Bool bRes = aMutex.tryToAcquire(); 6028748f2e1SDamjan Jovanovic // after 1 second, the mutex has been guarded, and the child thread should be running 6038748f2e1SDamjan Jovanovic sal_Bool bRes1 = myThread.isRunning(); 6048748f2e1SDamjan Jovanovic 6058748f2e1SDamjan Jovanovic myThread.join(); 6068748f2e1SDamjan Jovanovic sal_Bool bRes2 = aMutex.tryToAcquire(); 6078748f2e1SDamjan Jovanovic 6088748f2e1SDamjan Jovanovic ASSERT_TRUE(bRes == sal_False && bRes1 == sal_True && bRes2 == sal_True) << "GuardThread constructor"; 6098748f2e1SDamjan Jovanovic } 6108748f2e1SDamjan Jovanovic 6118748f2e1SDamjan Jovanovic TEST_F(GuardThreadConstructor, ctor_002 ) 6128748f2e1SDamjan Jovanovic { 6138748f2e1SDamjan Jovanovic Mutex aMutex; 6148748f2e1SDamjan Jovanovic 6158748f2e1SDamjan Jovanovic /// use reference constructor here 6168748f2e1SDamjan Jovanovic MutexGuard myGuard( aMutex ); 6178748f2e1SDamjan Jovanovic 6188748f2e1SDamjan Jovanovic /// the GuardThread will block here when it is initialised. 6198748f2e1SDamjan Jovanovic GuardThread myThread( &aMutex ); 6208748f2e1SDamjan Jovanovic myThread.create( ); 6218748f2e1SDamjan Jovanovic 6228748f2e1SDamjan Jovanovic /// is it still blocking? 6238748f2e1SDamjan Jovanovic ThreadHelper::thread_sleep_tenth_sec( 2 ); 6248748f2e1SDamjan Jovanovic sal_Bool bRes = myThread.isRunning( ); 6258748f2e1SDamjan Jovanovic 6268748f2e1SDamjan Jovanovic /// oh, release him. 6278748f2e1SDamjan Jovanovic aMutex.release( ); 6288748f2e1SDamjan Jovanovic myThread.join( ); 6298748f2e1SDamjan Jovanovic 6308748f2e1SDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "GuardThread constructor: reference initialization, aquire the mutex before running the thread, then check if it is blocking."; 6318748f2e1SDamjan Jovanovic } 632cdf0e10cSrcweir 633cdf0e10cSrcweir } // namespace osl_Guard 634cdf0e10cSrcweir 635cdf0e10cSrcweir 636cdf0e10cSrcweir //------------------------------------------------------------------------ 637cdf0e10cSrcweir // Beginning of the test cases for osl_ClearableGuard class 638cdf0e10cSrcweir //------------------------------------------------------------------------ 639cdf0e10cSrcweir 640cdf0e10cSrcweir /** Thread for test ClearableGuard 641cdf0e10cSrcweir */ 642cdf0e10cSrcweir class ClearGuardThread : public Thread 643cdf0e10cSrcweir { 644cdf0e10cSrcweir public: 645cdf0e10cSrcweir //get the Mutex pointer to operate 646cdf0e10cSrcweir ClearGuardThread( Mutex* pMutex ): pMyMutex( pMutex ) {} 647cdf0e10cSrcweir 648cdf0e10cSrcweir ~ClearGuardThread( ) 649cdf0e10cSrcweir { 650*a03c9fa9SDamjan Jovanovic EXPECT_TRUE(sal_False == this -> isRunning( )) << "#ClearGuardThread does not shutdown properly.\n"; 651cdf0e10cSrcweir } 652cdf0e10cSrcweir protected: 653cdf0e10cSrcweir Mutex* pMyMutex; 654cdf0e10cSrcweir 655cdf0e10cSrcweir void SAL_CALL run( ) 656cdf0e10cSrcweir { 657cdf0e10cSrcweir // acquire the mutex 658cdf0e10cSrcweir // printf("# ClearGuardThread" ); 659cdf0e10cSrcweir ClearableMutexGuard aGuard( pMyMutex ); 660cdf0e10cSrcweir ThreadHelper::thread_sleep( 5 ); 661cdf0e10cSrcweir 662cdf0e10cSrcweir // release the mutex 663cdf0e10cSrcweir aGuard.clear( ); 664cdf0e10cSrcweir ThreadHelper::thread_sleep( 2 ); 665cdf0e10cSrcweir } 666cdf0e10cSrcweir }; 667cdf0e10cSrcweir 668cdf0e10cSrcweir // ----------------------------------------------------------------------------- 669cdf0e10cSrcweir namespace osl_ClearableGuard 670cdf0e10cSrcweir { 671cdf0e10cSrcweir 6728748f2e1SDamjan Jovanovic class ClearableGuardConstructor : public ::testing::Test 673cdf0e10cSrcweir { 674cdf0e10cSrcweir public: 675cdf0e10cSrcweir }; // class ctor 6768748f2e1SDamjan Jovanovic 6778748f2e1SDamjan Jovanovic TEST_F(ClearableGuardConstructor, ctor_001) 6788748f2e1SDamjan Jovanovic { 6798748f2e1SDamjan Jovanovic Mutex aMutex; 6808748f2e1SDamjan Jovanovic 6818748f2e1SDamjan Jovanovic /// now, the aMutex has been guarded. 6828748f2e1SDamjan Jovanovic ClearableMutexGuard myMutexGuard( &aMutex ); 6838748f2e1SDamjan Jovanovic 6848748f2e1SDamjan Jovanovic /// it will return sal_False if the aMutex has not been Guarded. 6858748f2e1SDamjan Jovanovic sal_Bool bRes = aMutex.release( ); 6868748f2e1SDamjan Jovanovic 6878748f2e1SDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "ClearableMutexGuard constructor, test the aquire operation when initilized."; 6888748f2e1SDamjan Jovanovic } 6898748f2e1SDamjan Jovanovic 6908748f2e1SDamjan Jovanovic TEST_F(ClearableGuardConstructor, ctor_002 ) 6918748f2e1SDamjan Jovanovic { 6928748f2e1SDamjan Jovanovic Mutex aMutex; 6938748f2e1SDamjan Jovanovic 6948748f2e1SDamjan Jovanovic /// now, the aMutex has been guarded, this time, we use reference constructor. 6958748f2e1SDamjan Jovanovic ClearableMutexGuard myMutexGuard( aMutex ); 6968748f2e1SDamjan Jovanovic 6978748f2e1SDamjan Jovanovic /// it will return sal_False if the aMutex has not been Guarded. 6988748f2e1SDamjan Jovanovic sal_Bool bRes = aMutex.release( ); 6998748f2e1SDamjan Jovanovic 7008748f2e1SDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "ClearableMutexGuard constructor, test the aquire operation when initilized, we use reference constructor this time."; 7018748f2e1SDamjan Jovanovic } 702cdf0e10cSrcweir 7038748f2e1SDamjan Jovanovic class clear : public ::testing::Test 704cdf0e10cSrcweir { 705cdf0e10cSrcweir public: 7068748f2e1SDamjan Jovanovic }; // class clear 7078748f2e1SDamjan Jovanovic 7088748f2e1SDamjan Jovanovic TEST_F(clear, clear_001) 7098748f2e1SDamjan Jovanovic { 7108748f2e1SDamjan Jovanovic Mutex aMutex; 7118748f2e1SDamjan Jovanovic ClearGuardThread myThread(&aMutex); 7128748f2e1SDamjan Jovanovic myThread.create(); 7138748f2e1SDamjan Jovanovic 7148748f2e1SDamjan Jovanovic TimeValue aTimeVal_befor; 7158748f2e1SDamjan Jovanovic osl_getSystemTime( &aTimeVal_befor ); 7168748f2e1SDamjan Jovanovic // wait 1 second to assure the child thread has begun 7178748f2e1SDamjan Jovanovic ThreadHelper::thread_sleep(1); 7188748f2e1SDamjan Jovanovic 7198748f2e1SDamjan Jovanovic while (1) 720cdf0e10cSrcweir { 7218748f2e1SDamjan Jovanovic if (aMutex.tryToAcquire() == sal_True) 7228748f2e1SDamjan Jovanovic { 7238748f2e1SDamjan Jovanovic break; 7248748f2e1SDamjan Jovanovic } 7258748f2e1SDamjan Jovanovic ThreadHelper::thread_sleep(1); 726cdf0e10cSrcweir } 7278748f2e1SDamjan Jovanovic TimeValue aTimeVal_after; 7288748f2e1SDamjan Jovanovic osl_getSystemTime( &aTimeVal_after ); 7298748f2e1SDamjan Jovanovic sal_Int32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds; 7308748f2e1SDamjan Jovanovic printf("nSec is %"SAL_PRIdINT32"\n", nSec); 7318748f2e1SDamjan Jovanovic 7328748f2e1SDamjan Jovanovic myThread.join(); 733cdf0e10cSrcweir 7348748f2e1SDamjan Jovanovic ASSERT_TRUE(nSec < 7 && nSec > 1) << "ClearableGuard method: clear"; 7358748f2e1SDamjan Jovanovic } 7368748f2e1SDamjan Jovanovic 7378748f2e1SDamjan Jovanovic TEST_F(clear, clear_002 ) 7388748f2e1SDamjan Jovanovic { 7398748f2e1SDamjan Jovanovic Mutex aMutex; 7408748f2e1SDamjan Jovanovic 7418748f2e1SDamjan Jovanovic /// now, the aMutex has been guarded. 7428748f2e1SDamjan Jovanovic ClearableMutexGuard myMutexGuard( &aMutex ); 743cdf0e10cSrcweir 7448748f2e1SDamjan Jovanovic /// launch the HoldThread, it will be blocked here. 7458748f2e1SDamjan Jovanovic HoldThread myThread( &aMutex ); 7468748f2e1SDamjan Jovanovic myThread.create( ); 747cdf0e10cSrcweir 7488748f2e1SDamjan Jovanovic /// is it blocking? 7498748f2e1SDamjan Jovanovic ThreadHelper::thread_sleep_tenth_sec( 4 ); 7508748f2e1SDamjan Jovanovic sal_Bool bRes = myThread.isRunning( ); 7518748f2e1SDamjan Jovanovic 7528748f2e1SDamjan Jovanovic /// use clear to release. 7538748f2e1SDamjan Jovanovic myMutexGuard.clear( ); 7548748f2e1SDamjan Jovanovic myThread.join( ); 7558748f2e1SDamjan Jovanovic sal_Bool bRes1 = myThread.isRunning( ); 7568748f2e1SDamjan Jovanovic 7578748f2e1SDamjan Jovanovic ASSERT_TRUE(( sal_True == bRes ) && ( sal_False == bRes1 )) << "ClearableGuard method: clear, control the HoldThread's running status!"; 7588748f2e1SDamjan Jovanovic } 759cdf0e10cSrcweir 760cdf0e10cSrcweir } // namespace osl_ClearableGuard 761cdf0e10cSrcweir 762cdf0e10cSrcweir 763cdf0e10cSrcweir //------------------------------------------------------------------------ 764cdf0e10cSrcweir // Beginning of the test cases for osl_ResettableGuard class 765cdf0e10cSrcweir //------------------------------------------------------------------------ 766cdf0e10cSrcweir 767cdf0e10cSrcweir /** Thread for test ResettableGuard 768cdf0e10cSrcweir */ 769cdf0e10cSrcweir class ResetGuardThread : public Thread 770cdf0e10cSrcweir { 771cdf0e10cSrcweir public: 772cdf0e10cSrcweir //get the Mutex pointer to operate 773cdf0e10cSrcweir ResetGuardThread( Mutex* pMutex ): pMyMutex( pMutex ) {} 774cdf0e10cSrcweir 775cdf0e10cSrcweir ~ResetGuardThread( ) 776cdf0e10cSrcweir { 777*a03c9fa9SDamjan Jovanovic EXPECT_TRUE(sal_False == this -> isRunning( )) << "#ResetGuardThread does not shutdown properly.\n"; 778cdf0e10cSrcweir } 779cdf0e10cSrcweir protected: 780cdf0e10cSrcweir Mutex* pMyMutex; 781cdf0e10cSrcweir 782cdf0e10cSrcweir void SAL_CALL run( ) 783cdf0e10cSrcweir { 784cdf0e10cSrcweir // acquire the mutex 785cdf0e10cSrcweir printf("# ResettableGuard\n" ); 786cdf0e10cSrcweir ResettableMutexGuard aGuard( pMyMutex ); 787cdf0e10cSrcweir // release the mutex 788cdf0e10cSrcweir aGuard.clear( ); 789cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec( 2 ); 790cdf0e10cSrcweir } 791cdf0e10cSrcweir }; 792cdf0e10cSrcweir 793cdf0e10cSrcweir // ----------------------------------------------------------------------------- 794cdf0e10cSrcweir namespace osl_ResettableGuard 795cdf0e10cSrcweir { 7968748f2e1SDamjan Jovanovic class ctor : public ::testing::Test 797cdf0e10cSrcweir { 798cdf0e10cSrcweir public: 799cdf0e10cSrcweir }; // class ctor 8008748f2e1SDamjan Jovanovic 8018748f2e1SDamjan Jovanovic TEST_F(ctor, ctor_001) 8028748f2e1SDamjan Jovanovic { 8038748f2e1SDamjan Jovanovic Mutex aMutex; 8048748f2e1SDamjan Jovanovic 8058748f2e1SDamjan Jovanovic /// now, the aMutex has been guarded. 8068748f2e1SDamjan Jovanovic ResettableMutexGuard myMutexGuard( &aMutex ); 8078748f2e1SDamjan Jovanovic 8088748f2e1SDamjan Jovanovic /// it will return sal_False if the aMutex has not been Guarded. 8098748f2e1SDamjan Jovanovic sal_Bool bRes = aMutex.release( ); 8108748f2e1SDamjan Jovanovic 8118748f2e1SDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "ResettableMutexGuard constructor, test the aquire operation when initilized."; 8128748f2e1SDamjan Jovanovic } 8138748f2e1SDamjan Jovanovic 8148748f2e1SDamjan Jovanovic TEST_F(ctor, ctor_002 ) 8158748f2e1SDamjan Jovanovic { 8168748f2e1SDamjan Jovanovic Mutex aMutex; 8178748f2e1SDamjan Jovanovic 8188748f2e1SDamjan Jovanovic /// now, the aMutex has been guarded, this time, we use reference constructor. 8198748f2e1SDamjan Jovanovic ResettableMutexGuard myMutexGuard( aMutex ); 8208748f2e1SDamjan Jovanovic 8218748f2e1SDamjan Jovanovic /// it will return sal_False if the aMutex has not been Guarded. 8228748f2e1SDamjan Jovanovic sal_Bool bRes = aMutex.release( ); 8238748f2e1SDamjan Jovanovic 8248748f2e1SDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "ResettableMutexGuard constructor, test the aquire operation when initilized, we use reference constructor this time."; 8258748f2e1SDamjan Jovanovic } 8268748f2e1SDamjan Jovanovic 827cdf0e10cSrcweir 8288748f2e1SDamjan Jovanovic class reset : public ::testing::Test 829cdf0e10cSrcweir { 830cdf0e10cSrcweir public: 8318748f2e1SDamjan Jovanovic }; // class reset 8328748f2e1SDamjan Jovanovic 8338748f2e1SDamjan Jovanovic 8348748f2e1SDamjan Jovanovic TEST_F(reset, reset_001 ) 8358748f2e1SDamjan Jovanovic { 8368748f2e1SDamjan Jovanovic Mutex aMutex; 8378748f2e1SDamjan Jovanovic ResetGuardThread myThread( &aMutex ); 8388748f2e1SDamjan Jovanovic ResettableMutexGuard myMutexGuard( aMutex ); 8398748f2e1SDamjan Jovanovic myThread.create( ); 840cdf0e10cSrcweir 8418748f2e1SDamjan Jovanovic /// is it running? and clear done? 8428748f2e1SDamjan Jovanovic sal_Bool bRes = myThread.isRunning( ); 8438748f2e1SDamjan Jovanovic myMutexGuard.clear( ); 8448748f2e1SDamjan Jovanovic ThreadHelper::thread_sleep_tenth_sec( 1 ); 8458748f2e1SDamjan Jovanovic 8468748f2e1SDamjan Jovanovic /// if reset is not success, the release will return sal_False 8478748f2e1SDamjan Jovanovic myMutexGuard.reset( ); 8488748f2e1SDamjan Jovanovic sal_Bool bRes1 = aMutex.release( ); 8498748f2e1SDamjan Jovanovic myThread.join( ); 8508748f2e1SDamjan Jovanovic 8518748f2e1SDamjan Jovanovic ASSERT_TRUE(( sal_True == bRes ) && ( sal_True == bRes1 )) << "ResettableMutexGuard method: reset"; 8528748f2e1SDamjan Jovanovic } 8538748f2e1SDamjan Jovanovic 8548748f2e1SDamjan Jovanovic TEST_F(reset, reset_002 ) 8558748f2e1SDamjan Jovanovic { 8568748f2e1SDamjan Jovanovic #ifdef LINUX 8578748f2e1SDamjan Jovanovic Mutex aMutex; 8588748f2e1SDamjan Jovanovic ResettableMutexGuard myMutexGuard( &aMutex ); 8598748f2e1SDamjan Jovanovic 8608748f2e1SDamjan Jovanovic /// shouldn't release after clear; 8618748f2e1SDamjan Jovanovic myMutexGuard.clear( ); 8628748f2e1SDamjan Jovanovic sal_Bool bRes = aMutex.release( ); 863cdf0e10cSrcweir 8648748f2e1SDamjan Jovanovic /// can release after reset. 8658748f2e1SDamjan Jovanovic myMutexGuard.reset( ); 8668748f2e1SDamjan Jovanovic sal_Bool bRes1 = aMutex.release( ); 8678748f2e1SDamjan Jovanovic 8688748f2e1SDamjan Jovanovic ASSERT_TRUE(( sal_False == bRes ) && ( sal_True == bRes1 )) << "ResettableMutexGuard method: reset, release after clear and reset, on Solaris, the mutex can be release without aquire, so it can not passed on (SOLARIS), but not the reason for reset_002"; 869cdf0e10cSrcweir #endif 8708748f2e1SDamjan Jovanovic } 871cdf0e10cSrcweir 872cdf0e10cSrcweir } // namespace osl_ResettableGuard 873cdf0e10cSrcweir 8748748f2e1SDamjan Jovanovic int main(int argc, char **argv) 8758748f2e1SDamjan Jovanovic { 8768748f2e1SDamjan Jovanovic ::testing::InitGoogleTest(&argc, argv); 8778748f2e1SDamjan Jovanovic return RUN_ALL_TESTS(); 8788748f2e1SDamjan Jovanovic } 879cdf0e10cSrcweir 880cdf0e10cSrcweir // The following sets variables for GNU EMACS 881cdf0e10cSrcweir // Local Variables: 882cdf0e10cSrcweir // tab-width:4 883cdf0e10cSrcweir // End: 884