xref: /trunk/main/sal/qa/osl/mutex/osl_Mutex.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
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
10cdf0e10cSrcweir  *
1187d2adbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
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.
19cdf0e10cSrcweir  *
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 
33a03c9fa9SDamjan Jovanovic #ifdef WNT
34a03c9fa9SDamjan Jovanovic #define WIN32_LEAN_AND_MEAN
35a03c9fa9SDamjan Jovanovic #include <windows.h>
36a03c9fa9SDamjan Jovanovic #endif
37a03c9fa9SDamjan Jovanovic 
38cdf0e10cSrcweir using namespace osl;
39cdf0e10cSrcweir using namespace rtl;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir //------------------------------------------------------------------------
42cdf0e10cSrcweir // helper functions
43cdf0e10cSrcweir //------------------------------------------------------------------------
44cdf0e10cSrcweir 
45cdf0e10cSrcweir /** print a UNI_CODE String.
46cdf0e10cSrcweir */
printUString(const::rtl::OUString & str)47cdf0e10cSrcweir inline void printUString( const ::rtl::OUString & str )
48cdf0e10cSrcweir {
49cdf0e10cSrcweir     rtl::OString aString;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir     printf("#printUString_u# " );
52cdf0e10cSrcweir     aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
53cdf0e10cSrcweir     printf("%s\n", aString.getStr( ) );
54cdf0e10cSrcweir }
55cdf0e10cSrcweir 
56cdf0e10cSrcweir /** print Boolean value.
57cdf0e10cSrcweir */
printBool(sal_Bool bOk)58cdf0e10cSrcweir inline void printBool( sal_Bool bOk )
59cdf0e10cSrcweir {
60cdf0e10cSrcweir     printf("#printBool# " );
61cdf0e10cSrcweir     ( sal_True == bOk ) ? printf("YES!\n" ): printf("NO!\n" );
62cdf0e10cSrcweir }
63cdf0e10cSrcweir 
64cdf0e10cSrcweir /** pause nSec seconds helper function.
65cdf0e10cSrcweir */
66cdf0e10cSrcweir namespace ThreadHelper
67cdf0e10cSrcweir {
thread_sleep(sal_Int32 _nSec)68cdf0e10cSrcweir     void thread_sleep( sal_Int32 _nSec )
69cdf0e10cSrcweir     {
70cdf0e10cSrcweir         /// print statement in thread process must use fflush() to force display.
71cdf0e10cSrcweir         // t_print("# wait %d seconds. ", _nSec );
72cdf0e10cSrcweir         fflush(stdout);
73cdf0e10cSrcweir 
74cdf0e10cSrcweir #ifdef WNT                               //Windows
75cdf0e10cSrcweir         Sleep( _nSec * 1000 );
76cdf0e10cSrcweir #endif
77cdf0e10cSrcweir #if ( defined UNX ) || ( defined OS2 )   //Unix
78cdf0e10cSrcweir         sleep( _nSec );
79cdf0e10cSrcweir #endif
80cdf0e10cSrcweir         // printf("# done\n" );
81cdf0e10cSrcweir     }
thread_sleep_tenth_sec(sal_Int32 _nTenthSec)82cdf0e10cSrcweir     void thread_sleep_tenth_sec(sal_Int32 _nTenthSec)
83cdf0e10cSrcweir     {
84cdf0e10cSrcweir #ifdef WNT      //Windows
85cdf0e10cSrcweir             Sleep(_nTenthSec * 100 );
86cdf0e10cSrcweir #endif
87cdf0e10cSrcweir #if ( defined UNX ) || ( defined OS2 )  //Unix
88cdf0e10cSrcweir             TimeValue nTV;
89cdf0e10cSrcweir             nTV.Seconds = static_cast<sal_uInt32>( _nTenthSec/10 );
90cdf0e10cSrcweir             nTV.Nanosec = ( (_nTenthSec%10 ) * 100000000 );
91cdf0e10cSrcweir             osl_waitThread(&nTV);
92cdf0e10cSrcweir #endif
93cdf0e10cSrcweir     }
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 
97cdf0e10cSrcweir //------------------------------------------------------------------------
98cdf0e10cSrcweir // Beginning of the test cases for osl_Mutex class
99cdf0e10cSrcweir //------------------------------------------------------------------------
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 
102cdf0e10cSrcweir /** mutually exclusive data
103cdf0e10cSrcweir */
104cdf0e10cSrcweir struct resource {
105cdf0e10cSrcweir     sal_Int32   data1;
106cdf0e10cSrcweir     sal_Int32   data2;
107cdf0e10cSrcweir     Mutex       lock;
108cdf0e10cSrcweir };
109cdf0e10cSrcweir 
110cdf0e10cSrcweir /** IncreaseThread provide data.
111cdf0e10cSrcweir */
112cdf0e10cSrcweir class IncreaseThread : public Thread
113cdf0e10cSrcweir {
114cdf0e10cSrcweir public:
IncreaseThread(struct resource * pData)115cdf0e10cSrcweir     IncreaseThread( struct resource *pData ): pResource( pData ) { }
116cdf0e10cSrcweir 
~IncreaseThread()117cdf0e10cSrcweir     ~IncreaseThread( )
118cdf0e10cSrcweir     {
119a03c9fa9SDamjan Jovanovic         EXPECT_TRUE(sal_False == this -> isRunning( )) << "#IncreaseThread does not shutdown properly.\n";
120cdf0e10cSrcweir     }
121cdf0e10cSrcweir protected:
122cdf0e10cSrcweir     struct resource *pResource;
123cdf0e10cSrcweir 
run()124cdf0e10cSrcweir     void SAL_CALL run( )
125cdf0e10cSrcweir     {
126cdf0e10cSrcweir         pResource->lock.acquire( );
127cdf0e10cSrcweir         for( sal_Int8 i = 0; i < 3; i++ )
128cdf0e10cSrcweir         {
129cdf0e10cSrcweir             pResource->data1++;
130cdf0e10cSrcweir             yield( );  //yield() give CPU time to other thread, other thread if not block, they will change the data;
131cdf0e10cSrcweir         }
132cdf0e10cSrcweir         if ( pResource->data2 == 0 )
133cdf0e10cSrcweir             pResource->data2 = ( pResource->data1 > 0 ? pResource->data1 : 0 - pResource->data1 );
134cdf0e10cSrcweir         pResource->lock.release();
135cdf0e10cSrcweir     }
136cdf0e10cSrcweir };
137cdf0e10cSrcweir 
138cdf0e10cSrcweir /** DecreaseThread consume data.
139cdf0e10cSrcweir */
140cdf0e10cSrcweir class DecreaseThread : public Thread
141cdf0e10cSrcweir {
142cdf0e10cSrcweir public:
DecreaseThread(struct resource * pData)143cdf0e10cSrcweir     DecreaseThread( struct resource *pData ): pResource( pData ) { }
144cdf0e10cSrcweir 
~DecreaseThread()145cdf0e10cSrcweir     ~DecreaseThread( )
146cdf0e10cSrcweir     {
147a03c9fa9SDamjan Jovanovic         EXPECT_TRUE(sal_False == this -> isRunning( )) << "#DecreaseThread does not shutdown properly.\n";
148cdf0e10cSrcweir     }
149cdf0e10cSrcweir protected:
150cdf0e10cSrcweir     struct resource *pResource;
151cdf0e10cSrcweir 
run()152cdf0e10cSrcweir     void SAL_CALL run( )
153cdf0e10cSrcweir     {
154cdf0e10cSrcweir         pResource->lock.acquire( );
155cdf0e10cSrcweir         for( sal_Int8 i = 0; i < 3; i++ )
156cdf0e10cSrcweir         {
157cdf0e10cSrcweir             pResource->data1--;
158cdf0e10cSrcweir             yield( );  //yield() give CPU time to other thread, other thread if not block, they will change the data;
159cdf0e10cSrcweir         }
160cdf0e10cSrcweir         if ( pResource->data2 == 0 )
161cdf0e10cSrcweir             pResource->data2 = ( pResource->data1 > 0 ? pResource->data1 : 0 - pResource->data1 );
162cdf0e10cSrcweir         pResource->lock.release();
163cdf0e10cSrcweir     }
164cdf0e10cSrcweir };
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 
167cdf0e10cSrcweir /** chain structure used in Threads as critical resource
168cdf0e10cSrcweir */
169cdf0e10cSrcweir struct chain {
170cdf0e10cSrcweir     sal_Int32   buffer[ BUFFER_SIZE ];
171cdf0e10cSrcweir     Mutex       lock;
172cdf0e10cSrcweir     sal_Int8    pos;
173cdf0e10cSrcweir };
174cdf0e10cSrcweir 
175cdf0e10cSrcweir /** PutThread write to the chain structure in a mutex manner.
176cdf0e10cSrcweir */
177cdf0e10cSrcweir class PutThread : public Thread
178cdf0e10cSrcweir {
179cdf0e10cSrcweir public:
180cdf0e10cSrcweir     //get the struct pointer to write data to buffer
PutThread(struct chain * pData)181cdf0e10cSrcweir     PutThread( struct chain* pData ): pChain( pData ) { }
182cdf0e10cSrcweir 
~PutThread()183cdf0e10cSrcweir     ~PutThread( )
184cdf0e10cSrcweir     {
185a03c9fa9SDamjan Jovanovic         EXPECT_TRUE(sal_False == this -> isRunning( )) << "#PutThread does not shutdown properly.\n";
186cdf0e10cSrcweir     }
187cdf0e10cSrcweir protected:
188cdf0e10cSrcweir     struct chain* pChain;
189cdf0e10cSrcweir 
run()190cdf0e10cSrcweir     void SAL_CALL run( )
191cdf0e10cSrcweir     {
192cdf0e10cSrcweir         //block here if the mutex has been acquired
193cdf0e10cSrcweir         pChain->lock.acquire( );
194cdf0e10cSrcweir 
195cdf0e10cSrcweir         //current position in buffer to write
196cdf0e10cSrcweir         sal_Int8 nPos = pChain->pos;
197cdf0e10cSrcweir         oslThreadIdentifier oId = getIdentifier( );
198cdf0e10cSrcweir         //write data
199cdf0e10cSrcweir                 sal_Int8 i;
200cdf0e10cSrcweir         for ( i = 0; i < 5; i++ )
201cdf0e10cSrcweir         {
202cdf0e10cSrcweir             pChain->buffer[ nPos + i ] = oId;
203cdf0e10cSrcweir             yield( );
204cdf0e10cSrcweir         }
205cdf0e10cSrcweir         //revise the position
206cdf0e10cSrcweir         pChain->pos = nPos + i;
207cdf0e10cSrcweir 
208cdf0e10cSrcweir         //finish writing, release the mutex
209cdf0e10cSrcweir         pChain->lock.release();
210cdf0e10cSrcweir     }
211cdf0e10cSrcweir };
212cdf0e10cSrcweir 
213cdf0e10cSrcweir /** thread for testing Mutex acquire.
214cdf0e10cSrcweir  */
215cdf0e10cSrcweir class HoldThread : public Thread
216cdf0e10cSrcweir {
217cdf0e10cSrcweir public:
218cdf0e10cSrcweir     //get the Mutex pointer to operate
HoldThread(Mutex * pMutex)219cdf0e10cSrcweir     HoldThread( Mutex* pMutex ): pMyMutex( pMutex ) { }
220cdf0e10cSrcweir 
~HoldThread()221cdf0e10cSrcweir     ~HoldThread( )
222cdf0e10cSrcweir     {
223a03c9fa9SDamjan Jovanovic         EXPECT_TRUE(sal_False == this -> isRunning( )) << "#HoldThread does not shutdown properly.\n";
224cdf0e10cSrcweir     }
225cdf0e10cSrcweir protected:
226cdf0e10cSrcweir     Mutex* pMyMutex;
227cdf0e10cSrcweir 
run()228cdf0e10cSrcweir     void SAL_CALL run()
229cdf0e10cSrcweir     {
230cdf0e10cSrcweir         // block here if the mutex has been acquired
231cdf0e10cSrcweir         pMyMutex->acquire( );
232cdf0e10cSrcweir         printf("# Mutex acquired. \n" );
233cdf0e10cSrcweir         pMyMutex->release( );
234cdf0e10cSrcweir     }
235cdf0e10cSrcweir };
236cdf0e10cSrcweir 
237cdf0e10cSrcweir class WaitThread : public Thread
238cdf0e10cSrcweir {
239cdf0e10cSrcweir public:
240cdf0e10cSrcweir     //get the Mutex pointer to operate
WaitThread(Mutex * pMutex)241cdf0e10cSrcweir     WaitThread( Mutex* pMutex ): pMyMutex( pMutex ) { }
242cdf0e10cSrcweir 
~WaitThread()243cdf0e10cSrcweir     ~WaitThread( )
244cdf0e10cSrcweir     {
245a03c9fa9SDamjan Jovanovic         EXPECT_TRUE(sal_False == this -> isRunning( )) << "#WaitThread does not shutdown properly.\n";
246cdf0e10cSrcweir     }
247cdf0e10cSrcweir protected:
248cdf0e10cSrcweir     Mutex* pMyMutex;
249cdf0e10cSrcweir 
run()250cdf0e10cSrcweir     void SAL_CALL run( )
251cdf0e10cSrcweir     {
252cdf0e10cSrcweir         // block here if the mutex has been acquired
253cdf0e10cSrcweir         pMyMutex->acquire( );
254cdf0e10cSrcweir         ThreadHelper::thread_sleep_tenth_sec( 2 );
255cdf0e10cSrcweir         pMyMutex->release( );
256cdf0e10cSrcweir     }
257cdf0e10cSrcweir };
258cdf0e10cSrcweir 
259cdf0e10cSrcweir /** thread for testing getGlobalMutex.
260cdf0e10cSrcweir  */
261cdf0e10cSrcweir class GlobalMutexThread : public Thread
262cdf0e10cSrcweir {
263cdf0e10cSrcweir public:
264cdf0e10cSrcweir     //get the Mutex pointer to operate
GlobalMutexThread()265cdf0e10cSrcweir     GlobalMutexThread( ){ }
266cdf0e10cSrcweir 
~GlobalMutexThread()267cdf0e10cSrcweir     ~GlobalMutexThread( )
268cdf0e10cSrcweir     {
269a03c9fa9SDamjan Jovanovic         EXPECT_TRUE(sal_False == this -> isRunning( )) << "#GlobalMutexThread does not shutdown properly.\n";
270cdf0e10cSrcweir     }
271cdf0e10cSrcweir protected:
run()272cdf0e10cSrcweir     void SAL_CALL run( )
273cdf0e10cSrcweir     {
274cdf0e10cSrcweir         // block here if the mutex has been acquired
275cdf0e10cSrcweir         Mutex* pGlobalMutex;
276cdf0e10cSrcweir         pGlobalMutex = pGlobalMutex->getGlobalMutex( );
277cdf0e10cSrcweir         pGlobalMutex->acquire( );
278cdf0e10cSrcweir         printf("# Global Mutex acquired. \n" );
279cdf0e10cSrcweir         pGlobalMutex->release( );
280cdf0e10cSrcweir     }
281cdf0e10cSrcweir };
282cdf0e10cSrcweir 
283cdf0e10cSrcweir 
284cdf0e10cSrcweir //--------------------------------------------------------------
285cdf0e10cSrcweir namespace osl_Mutex
286cdf0e10cSrcweir {
287cdf0e10cSrcweir 
288cdf0e10cSrcweir     /** Test of the osl::Mutex::constructor
289cdf0e10cSrcweir      */
2908748f2e1SDamjan Jovanovic     class MutexConstructor : public ::testing::Test
291cdf0e10cSrcweir     {
292cdf0e10cSrcweir     public:
293cdf0e10cSrcweir         // initialise your test code values here.
294cdf0e10cSrcweir         struct chain m_Data;
295cdf0e10cSrcweir         struct resource m_Res;
296cdf0e10cSrcweir 
SetUp()2978748f2e1SDamjan Jovanovic         void SetUp( )
298cdf0e10cSrcweir         {
299cdf0e10cSrcweir             for ( sal_Int8 i=0; i < BUFFER_SIZE; i++ )
300cdf0e10cSrcweir                 m_Data.buffer[i] = 0;
301cdf0e10cSrcweir             m_Data.pos = 0;
302cdf0e10cSrcweir 
303cdf0e10cSrcweir             m_Res.data1 = 0;
304cdf0e10cSrcweir             m_Res.data2 = 0;
305cdf0e10cSrcweir         }
306cdf0e10cSrcweir 
TearDown()3078748f2e1SDamjan Jovanovic         void TearDown()
308cdf0e10cSrcweir         {
309cdf0e10cSrcweir         }
3108748f2e1SDamjan Jovanovic     }; // class ctor
311cdf0e10cSrcweir 
312cdf0e10cSrcweir     /** Create two threads to write data to the same buffer, use Mutex to assure
313cdf0e10cSrcweir         during one thread write data five times, the other thread should not begin writing.
314cdf0e10cSrcweir         the two threads wrote two different datas: their thread ID, so we can check the datas
315cdf0e10cSrcweir         in buffer to know the order of the two threads writing
316cdf0e10cSrcweir     */
TEST_F(MutexConstructor,ctor_001)3178748f2e1SDamjan Jovanovic     TEST_F(MutexConstructor, ctor_001)
318cdf0e10cSrcweir     {
319cdf0e10cSrcweir         PutThread myThread1( &m_Data );
320cdf0e10cSrcweir         PutThread myThread2( &m_Data );
321cdf0e10cSrcweir 
322cdf0e10cSrcweir         myThread1.create( );
323cdf0e10cSrcweir         myThread2.create( );
324cdf0e10cSrcweir 
325cdf0e10cSrcweir         //wait until the two threads terminate
326cdf0e10cSrcweir         myThread1.join( );
327cdf0e10cSrcweir         myThread2.join( );
328cdf0e10cSrcweir 
329cdf0e10cSrcweir         sal_Bool bRes = sal_False;
330cdf0e10cSrcweir 
331cdf0e10cSrcweir         // every 5 datas should the same
332cdf0e10cSrcweir         // LLA: this is not a good check, it's too fix
333cdf0e10cSrcweir         if (m_Data.buffer[0] == m_Data.buffer[1] &&
334cdf0e10cSrcweir             m_Data.buffer[1] == m_Data.buffer[2] &&
335cdf0e10cSrcweir             m_Data.buffer[2] == m_Data.buffer[3] &&
336cdf0e10cSrcweir             m_Data.buffer[3] == m_Data.buffer[4] &&
337cdf0e10cSrcweir             m_Data.buffer[5] == m_Data.buffer[6] &&
338cdf0e10cSrcweir             m_Data.buffer[6] == m_Data.buffer[7] &&
339cdf0e10cSrcweir             m_Data.buffer[7] == m_Data.buffer[8] &&
340cdf0e10cSrcweir             m_Data.buffer[8] == m_Data.buffer[9])
341cdf0e10cSrcweir             bRes = sal_True;
342cdf0e10cSrcweir 
343cdf0e10cSrcweir         /*for (sal_Int8 i=0; i<BUFFER_SIZE; i++)
344cdf0e10cSrcweir             printf("#data in buffer is %d\n", m_Data.buffer[i]);
345cdf0e10cSrcweir         */
346cdf0e10cSrcweir 
3478748f2e1SDamjan Jovanovic         ASSERT_TRUE(bRes == sal_True) << "Mutex ctor";
348cdf0e10cSrcweir 
349cdf0e10cSrcweir     }
350cdf0e10cSrcweir 
351cdf0e10cSrcweir     /** Create two threads to write data to operate on the same number , use Mutex to assure,
352cdf0e10cSrcweir         one thread increase data 3 times, the other thread decrease 3 times, store the operate
353cdf0e10cSrcweir         result when the first thread complete, if it is interrupt by the other thread, the stored
354cdf0e10cSrcweir         number will not be 3.
355cdf0e10cSrcweir     */
TEST_F(MutexConstructor,ctor_002)3568748f2e1SDamjan Jovanovic     TEST_F(MutexConstructor, ctor_002)
357cdf0e10cSrcweir     {
358cdf0e10cSrcweir         IncreaseThread myThread1( &m_Res );
359cdf0e10cSrcweir         DecreaseThread myThread2( &m_Res );
360cdf0e10cSrcweir 
361cdf0e10cSrcweir         myThread1.create( );
362cdf0e10cSrcweir         myThread2.create( );
363cdf0e10cSrcweir 
364cdf0e10cSrcweir         //wait until the two threads terminate
365cdf0e10cSrcweir         myThread1.join( );
366cdf0e10cSrcweir         myThread2.join( );
367cdf0e10cSrcweir 
368cdf0e10cSrcweir         sal_Bool bRes = sal_False;
369cdf0e10cSrcweir 
370cdf0e10cSrcweir         // every 5 datas should the same
371cdf0e10cSrcweir         if ( ( m_Res.data1 == 0 ) && ( m_Res.data2 == 3 ) )
372cdf0e10cSrcweir             bRes = sal_True;
373cdf0e10cSrcweir 
3748748f2e1SDamjan Jovanovic         ASSERT_TRUE(bRes == sal_True) << "test Mutex ctor function: increase and decrease a number 3 times without interrupt.";
375cdf0e10cSrcweir     }
376cdf0e10cSrcweir 
377cdf0e10cSrcweir 
378cdf0e10cSrcweir     /** Test of the osl::Mutex::acquire method
379cdf0e10cSrcweir      */
3808748f2e1SDamjan Jovanovic     class acquire : public ::testing::Test
381cdf0e10cSrcweir     {
382cdf0e10cSrcweir     public:
3838748f2e1SDamjan Jovanovic     }; // class acquire
3848748f2e1SDamjan Jovanovic 
385cdf0e10cSrcweir     // acquire mutex in main thread, and then call acquire again in myThread,
386cdf0e10cSrcweir     // the child thread should block, wait 2 secs, it still block.
387cdf0e10cSrcweir     // Then release mutex in main thread, the child thread could return from acquire,
388cdf0e10cSrcweir     // and go to exec next statement, so could terminate quickly.
TEST_F(acquire,acquire_001)3898748f2e1SDamjan Jovanovic     TEST_F(acquire, acquire_001 )
390cdf0e10cSrcweir     {
391cdf0e10cSrcweir         Mutex aMutex;
392cdf0e10cSrcweir         //acquire here
393cdf0e10cSrcweir         sal_Bool bRes = aMutex.acquire( );
394cdf0e10cSrcweir         // pass the pointer of mutex to child thread
395cdf0e10cSrcweir         HoldThread myThread( &aMutex );
396cdf0e10cSrcweir         myThread.create( );
397cdf0e10cSrcweir 
398cdf0e10cSrcweir         ThreadHelper::thread_sleep_tenth_sec( 2 );
399cdf0e10cSrcweir         // if acquire in myThread does not work, 2 secs is long enough,
400cdf0e10cSrcweir         // myThread should terminate now, and bRes1 should be sal_False
401cdf0e10cSrcweir         sal_Bool bRes1 = myThread.isRunning( );
402cdf0e10cSrcweir 
403cdf0e10cSrcweir         aMutex.release( );
404cdf0e10cSrcweir         ThreadHelper::thread_sleep_tenth_sec( 1 );
405cdf0e10cSrcweir         // after release mutex, myThread stops blocking and will terminate immediately
406cdf0e10cSrcweir         sal_Bool bRes2 = myThread.isRunning( );
407cdf0e10cSrcweir         myThread.join( );
408cdf0e10cSrcweir 
4098748f2e1SDamjan Jovanovic         ASSERT_TRUE(bRes == sal_True && bRes1 == sal_True && bRes2 == sal_False) << "Mutex acquire";
410cdf0e10cSrcweir     }
411cdf0e10cSrcweir 
412cdf0e10cSrcweir     //in the same thread, acquire twice should success
TEST_F(acquire,acquire_002)4138748f2e1SDamjan Jovanovic     TEST_F(acquire, acquire_002)
414cdf0e10cSrcweir     {
415cdf0e10cSrcweir         Mutex aMutex;
416cdf0e10cSrcweir         //acquire here
417cdf0e10cSrcweir         sal_Bool bRes = aMutex.acquire();
418cdf0e10cSrcweir         sal_Bool bRes1 = aMutex.acquire();
419cdf0e10cSrcweir 
420cdf0e10cSrcweir         sal_Bool bRes2 = aMutex.tryToAcquire();
421cdf0e10cSrcweir 
422cdf0e10cSrcweir         aMutex.release();
423cdf0e10cSrcweir 
4248748f2e1SDamjan Jovanovic         ASSERT_TRUE(bRes == sal_True && bRes1 == sal_True && bRes2 == sal_True) << "Mutex acquire";
425cdf0e10cSrcweir 
426cdf0e10cSrcweir     }
427cdf0e10cSrcweir 
428cdf0e10cSrcweir 
429cdf0e10cSrcweir     /** Test of the osl::Mutex::tryToAcquire method
430cdf0e10cSrcweir      */
4318748f2e1SDamjan Jovanovic     class tryToAcquire : public ::testing::Test
432cdf0e10cSrcweir     {
433cdf0e10cSrcweir     public:
4348748f2e1SDamjan Jovanovic     }; // class tryToAcquire
4358748f2e1SDamjan Jovanovic 
436cdf0e10cSrcweir     // First let child thread acquire the mutex, and wait 2 secs, during the 2 secs,
437cdf0e10cSrcweir     // in main thread, tryToAcquire mutex should return False
438cdf0e10cSrcweir     // then after the child thread terminated, tryToAcquire should return True
TEST_F(tryToAcquire,tryToAcquire_001)4398748f2e1SDamjan Jovanovic     TEST_F(tryToAcquire, tryToAcquire_001)
440cdf0e10cSrcweir     {
441cdf0e10cSrcweir         Mutex aMutex;
442cdf0e10cSrcweir         WaitThread myThread(&aMutex);
443cdf0e10cSrcweir         myThread.create();
444cdf0e10cSrcweir 
445cdf0e10cSrcweir         // ensure the child thread acquire the mutex
446cdf0e10cSrcweir         ThreadHelper::thread_sleep_tenth_sec(1);
447cdf0e10cSrcweir 
448cdf0e10cSrcweir         sal_Bool bRes1 = aMutex.tryToAcquire();
449cdf0e10cSrcweir 
450cdf0e10cSrcweir         if (bRes1 == sal_True)
451cdf0e10cSrcweir             aMutex.release();
452cdf0e10cSrcweir         // wait the child thread terminate
453cdf0e10cSrcweir         myThread.join();
454cdf0e10cSrcweir 
455cdf0e10cSrcweir         sal_Bool bRes2 = aMutex.tryToAcquire();
456cdf0e10cSrcweir 
457cdf0e10cSrcweir         if (bRes2 == sal_True)
458cdf0e10cSrcweir             aMutex.release();
459cdf0e10cSrcweir 
4608748f2e1SDamjan Jovanovic     ASSERT_TRUE(bRes1 == sal_False && bRes2 == sal_True) << "Try to acquire Mutex";
461cdf0e10cSrcweir     }
462cdf0e10cSrcweir 
463cdf0e10cSrcweir 
464cdf0e10cSrcweir     /** Test of the osl::Mutex::release method
465cdf0e10cSrcweir      */
4668748f2e1SDamjan Jovanovic     class release : public ::testing::Test
467cdf0e10cSrcweir     {
468cdf0e10cSrcweir     public:
4698748f2e1SDamjan Jovanovic     }; // class release
4708748f2e1SDamjan Jovanovic 
471cdf0e10cSrcweir     /** acquire/release are not used in pairs: after child thread acquired mutex,
472cdf0e10cSrcweir         the main thread release it, then any thread could acquire it.
473cdf0e10cSrcweir     */
TEST_F(release,release_001)4748748f2e1SDamjan Jovanovic     TEST_F(release, release_001)
475cdf0e10cSrcweir     {
476cdf0e10cSrcweir         Mutex aMutex;
477cdf0e10cSrcweir         WaitThread myThread( &aMutex );
478cdf0e10cSrcweir         myThread.create( );
479cdf0e10cSrcweir 
480cdf0e10cSrcweir         // ensure the child thread acquire the mutex
481cdf0e10cSrcweir         ThreadHelper::thread_sleep_tenth_sec( 1 );
482cdf0e10cSrcweir 
483cdf0e10cSrcweir         sal_Bool bRunning = myThread.isRunning( );
484cdf0e10cSrcweir         sal_Bool bRes1 = aMutex.tryToAcquire( );
485cdf0e10cSrcweir         // wait the child thread terminate
486cdf0e10cSrcweir         myThread.join( );
487cdf0e10cSrcweir 
488cdf0e10cSrcweir         sal_Bool bRes2 = aMutex.tryToAcquire( );
489cdf0e10cSrcweir 
490cdf0e10cSrcweir         if ( bRes2 == sal_True )
491cdf0e10cSrcweir             aMutex.release( );
492cdf0e10cSrcweir 
49330acf5e8Spfg         ASSERT_TRUE(bRes1 == sal_False && bRes2 == sal_True && bRunning == sal_True) << "release Mutex: try to acquire before and after the mutex has been released";
494cdf0e10cSrcweir 
495cdf0e10cSrcweir     }
496cdf0e10cSrcweir 
497cdf0e10cSrcweir     // how about release twice?
TEST_F(release,release_002)4988748f2e1SDamjan Jovanovic     TEST_F(release, release_002)
499cdf0e10cSrcweir     {
500cdf0e10cSrcweir // LLA: is this a real test?
501cdf0e10cSrcweir #if 0
502cdf0e10cSrcweir         Mutex aMutex;
503cdf0e10cSrcweir         sal_Bool bRes1 = aMutex.release( );
504cdf0e10cSrcweir         sal_Bool bRes2 = aMutex.release( );
505cdf0e10cSrcweir 
50630acf5e8Spfg         ASSERT_TRUE(bRes1 == sal_False && bRes2 == sal_False) << "release Mutex: mutex should not be released without acquire, should not release twice. although the behaviour is still under discussion, this test is passed on (LINUX), not passed on (SOLARIS)&(WINDOWS)";
507cdf0e10cSrcweir #endif
508cdf0e10cSrcweir     }
509cdf0e10cSrcweir 
510cdf0e10cSrcweir 
511cdf0e10cSrcweir     /** Test of the osl::Mutex::getGlobalMutex method
512cdf0e10cSrcweir      */
5138748f2e1SDamjan Jovanovic     class getGlobalMutex : public ::testing::Test
514cdf0e10cSrcweir     {
515cdf0e10cSrcweir     public:
5168748f2e1SDamjan Jovanovic     }; // class getGlobalMutex
5178748f2e1SDamjan Jovanovic 
518cdf0e10cSrcweir     // initialise your test code values here.
TEST_F(getGlobalMutex,getGlobalMutex_001)5198748f2e1SDamjan Jovanovic     TEST_F(getGlobalMutex, getGlobalMutex_001)
520cdf0e10cSrcweir     {
521cdf0e10cSrcweir         Mutex* pGlobalMutex;
522cdf0e10cSrcweir         pGlobalMutex = pGlobalMutex->getGlobalMutex();
523cdf0e10cSrcweir         pGlobalMutex->acquire();
524cdf0e10cSrcweir 
525cdf0e10cSrcweir         GlobalMutexThread myThread;
526cdf0e10cSrcweir         myThread.create();
527cdf0e10cSrcweir 
528cdf0e10cSrcweir         ThreadHelper::thread_sleep_tenth_sec(1);
529cdf0e10cSrcweir         sal_Bool bRes1 = myThread.isRunning();
530cdf0e10cSrcweir 
531cdf0e10cSrcweir         pGlobalMutex->release();
532cdf0e10cSrcweir         ThreadHelper::thread_sleep_tenth_sec(1);
533cdf0e10cSrcweir         // after release mutex, myThread stops blocking and will terminate immediately
534cdf0e10cSrcweir         sal_Bool bRes2 = myThread.isRunning();
535cdf0e10cSrcweir 
5368748f2e1SDamjan Jovanovic         ASSERT_TRUE(bRes1 == sal_True && bRes2 == sal_False) << "Global Mutex works";
537cdf0e10cSrcweir     }
538cdf0e10cSrcweir 
TEST_F(getGlobalMutex,getGlobalMutex_002)5398748f2e1SDamjan Jovanovic     TEST_F(getGlobalMutex, getGlobalMutex_002 )
540cdf0e10cSrcweir     {
541cdf0e10cSrcweir         sal_Bool bRes;
542cdf0e10cSrcweir 
543cdf0e10cSrcweir         Mutex *pGlobalMutex;
544cdf0e10cSrcweir         pGlobalMutex = pGlobalMutex->getGlobalMutex( );
545cdf0e10cSrcweir         pGlobalMutex->acquire( );
546cdf0e10cSrcweir         {
547cdf0e10cSrcweir             Mutex *pGlobalMutex1;
548cdf0e10cSrcweir             pGlobalMutex1 = pGlobalMutex1->getGlobalMutex( );
549cdf0e10cSrcweir             bRes = pGlobalMutex1->release( );
550cdf0e10cSrcweir         }
551cdf0e10cSrcweir 
5528748f2e1SDamjan 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.";
553cdf0e10cSrcweir     }
554cdf0e10cSrcweir 
555cdf0e10cSrcweir } // namespace osl_Mutex
556cdf0e10cSrcweir 
557cdf0e10cSrcweir 
558cdf0e10cSrcweir //------------------------------------------------------------------------
559cdf0e10cSrcweir // Beginning of the test cases for osl_Guard class
560cdf0e10cSrcweir //------------------------------------------------------------------------
561cdf0e10cSrcweir 
562cdf0e10cSrcweir class GuardThread : public Thread
563cdf0e10cSrcweir {
564cdf0e10cSrcweir public:
565cdf0e10cSrcweir     //get the Mutex pointer to operate
GuardThread(Mutex * pMutex)566cdf0e10cSrcweir     GuardThread( Mutex* pMutex ): pMyMutex( pMutex ) { }
567cdf0e10cSrcweir 
~GuardThread()568cdf0e10cSrcweir     ~GuardThread( )
569cdf0e10cSrcweir     {
570a03c9fa9SDamjan Jovanovic         EXPECT_TRUE(sal_False == this -> isRunning( )) << "#GuardThread does not shutdown properly.\n";
571cdf0e10cSrcweir     }
572cdf0e10cSrcweir protected:
573cdf0e10cSrcweir     Mutex* pMyMutex;
574cdf0e10cSrcweir 
run()575cdf0e10cSrcweir     void SAL_CALL run( )
576cdf0e10cSrcweir     {
577cdf0e10cSrcweir         // block here if the mutex has been acquired
578cdf0e10cSrcweir         MutexGuard aGuard( pMyMutex );
579cdf0e10cSrcweir         ThreadHelper::thread_sleep_tenth_sec( 2 );
580cdf0e10cSrcweir     }
581cdf0e10cSrcweir };
582cdf0e10cSrcweir 
583cdf0e10cSrcweir 
584cdf0e10cSrcweir namespace osl_Guard
585cdf0e10cSrcweir {
5868748f2e1SDamjan Jovanovic     class GuardThreadConstructor : public ::testing::Test
587cdf0e10cSrcweir     {
588cdf0e10cSrcweir     public:
5898748f2e1SDamjan Jovanovic     }; // class ctor
5908748f2e1SDamjan Jovanovic 
591cdf0e10cSrcweir     // insert your test code here.
TEST_F(GuardThreadConstructor,ctor_001)5928748f2e1SDamjan Jovanovic     TEST_F(GuardThreadConstructor, ctor_001)
593cdf0e10cSrcweir     {
594cdf0e10cSrcweir         Mutex aMutex;
595cdf0e10cSrcweir         GuardThread myThread(&aMutex);
596cdf0e10cSrcweir         myThread.create();
597cdf0e10cSrcweir 
598cdf0e10cSrcweir         ThreadHelper::thread_sleep_tenth_sec(1);
599cdf0e10cSrcweir         sal_Bool bRes = aMutex.tryToAcquire();
600cdf0e10cSrcweir         // after 1 second, the mutex has been guarded, and the child thread should be running
601cdf0e10cSrcweir         sal_Bool bRes1 = myThread.isRunning();
602cdf0e10cSrcweir 
603cdf0e10cSrcweir         myThread.join();
604cdf0e10cSrcweir         sal_Bool bRes2 = aMutex.tryToAcquire();
605cdf0e10cSrcweir 
6068748f2e1SDamjan Jovanovic         ASSERT_TRUE(bRes == sal_False && bRes1 == sal_True && bRes2 == sal_True) << "GuardThread constructor";
607cdf0e10cSrcweir     }
608cdf0e10cSrcweir 
TEST_F(GuardThreadConstructor,ctor_002)6098748f2e1SDamjan Jovanovic     TEST_F(GuardThreadConstructor, ctor_002 )
610cdf0e10cSrcweir     {
611cdf0e10cSrcweir         Mutex aMutex;
612cdf0e10cSrcweir 
613cdf0e10cSrcweir         /// use reference constructor here
614cdf0e10cSrcweir         MutexGuard myGuard( aMutex );
615cdf0e10cSrcweir 
616cdf0e10cSrcweir         /// the GuardThread will block here when it is initialised.
617cdf0e10cSrcweir         GuardThread myThread( &aMutex );
618cdf0e10cSrcweir         myThread.create( );
619cdf0e10cSrcweir 
620cdf0e10cSrcweir         /// is it still blocking?
621cdf0e10cSrcweir         ThreadHelper::thread_sleep_tenth_sec( 2 );
622cdf0e10cSrcweir         sal_Bool bRes = myThread.isRunning( );
623cdf0e10cSrcweir 
624cdf0e10cSrcweir         /// oh, release him.
625cdf0e10cSrcweir         aMutex.release( );
626cdf0e10cSrcweir         myThread.join( );
627cdf0e10cSrcweir 
62830acf5e8Spfg         ASSERT_TRUE(bRes == sal_True) << "GuardThread constructor: reference initialization, acquire the mutex before running the thread, then check if it is blocking.";
629cdf0e10cSrcweir     }
630cdf0e10cSrcweir 
631cdf0e10cSrcweir } // namespace osl_Guard
632cdf0e10cSrcweir 
633cdf0e10cSrcweir 
634cdf0e10cSrcweir //------------------------------------------------------------------------
635cdf0e10cSrcweir // Beginning of the test cases for osl_ClearableGuard class
636cdf0e10cSrcweir //------------------------------------------------------------------------
637cdf0e10cSrcweir 
638cdf0e10cSrcweir /** Thread for test ClearableGuard
639cdf0e10cSrcweir  */
640cdf0e10cSrcweir class ClearGuardThread : public Thread
641cdf0e10cSrcweir {
642cdf0e10cSrcweir public:
643cdf0e10cSrcweir     //get the Mutex pointer to operate
ClearGuardThread(Mutex * pMutex)644cdf0e10cSrcweir     ClearGuardThread( Mutex* pMutex ): pMyMutex( pMutex ) {}
645cdf0e10cSrcweir 
~ClearGuardThread()646cdf0e10cSrcweir     ~ClearGuardThread( )
647cdf0e10cSrcweir     {
648a03c9fa9SDamjan Jovanovic         EXPECT_TRUE(sal_False == this -> isRunning( )) << "#ClearGuardThread does not shutdown properly.\n";
649cdf0e10cSrcweir     }
650cdf0e10cSrcweir protected:
651cdf0e10cSrcweir     Mutex* pMyMutex;
652cdf0e10cSrcweir 
run()653cdf0e10cSrcweir     void SAL_CALL run( )
654cdf0e10cSrcweir     {
655cdf0e10cSrcweir         // acquire the mutex
656cdf0e10cSrcweir         // printf("# ClearGuardThread" );
657cdf0e10cSrcweir         ClearableMutexGuard aGuard( pMyMutex );
658cdf0e10cSrcweir         ThreadHelper::thread_sleep( 5 );
659cdf0e10cSrcweir 
660cdf0e10cSrcweir         // release the mutex
661cdf0e10cSrcweir         aGuard.clear( );
662cdf0e10cSrcweir         ThreadHelper::thread_sleep( 2 );
663cdf0e10cSrcweir     }
664cdf0e10cSrcweir };
665cdf0e10cSrcweir 
666cdf0e10cSrcweir // -----------------------------------------------------------------------------
667cdf0e10cSrcweir namespace osl_ClearableGuard
668cdf0e10cSrcweir {
669cdf0e10cSrcweir 
6708748f2e1SDamjan Jovanovic     class ClearableGuardConstructor : public ::testing::Test
671cdf0e10cSrcweir     {
672cdf0e10cSrcweir     public:
6738748f2e1SDamjan Jovanovic     }; // class ctor
6748748f2e1SDamjan Jovanovic 
TEST_F(ClearableGuardConstructor,ctor_001)6758748f2e1SDamjan Jovanovic     TEST_F(ClearableGuardConstructor, ctor_001)
676cdf0e10cSrcweir     {
677cdf0e10cSrcweir         Mutex aMutex;
678cdf0e10cSrcweir 
679cdf0e10cSrcweir         /// now, the aMutex has been guarded.
680cdf0e10cSrcweir         ClearableMutexGuard myMutexGuard( &aMutex );
681cdf0e10cSrcweir 
682cdf0e10cSrcweir         /// it will return sal_False if the aMutex has not been Guarded.
683cdf0e10cSrcweir         sal_Bool bRes = aMutex.release( );
684cdf0e10cSrcweir 
68530acf5e8Spfg         ASSERT_TRUE(bRes == sal_True) << "ClearableMutexGuard constructor, test the acquire operation when initilized.";
686cdf0e10cSrcweir     }
687cdf0e10cSrcweir 
TEST_F(ClearableGuardConstructor,ctor_002)6888748f2e1SDamjan Jovanovic     TEST_F(ClearableGuardConstructor, ctor_002 )
689cdf0e10cSrcweir     {
690cdf0e10cSrcweir         Mutex aMutex;
691cdf0e10cSrcweir 
692cdf0e10cSrcweir         /// now, the aMutex has been guarded, this time, we use reference constructor.
693cdf0e10cSrcweir         ClearableMutexGuard myMutexGuard( aMutex );
694cdf0e10cSrcweir 
695cdf0e10cSrcweir         /// it will return sal_False if the aMutex has not been Guarded.
696cdf0e10cSrcweir         sal_Bool bRes = aMutex.release( );
697cdf0e10cSrcweir 
69830acf5e8Spfg         ASSERT_TRUE(bRes == sal_True) << "ClearableMutexGuard constructor, test the acquire operation when initilized, we use reference constructor this time.";
699cdf0e10cSrcweir     }
700cdf0e10cSrcweir 
7018748f2e1SDamjan Jovanovic     class clear : public ::testing::Test
702cdf0e10cSrcweir     {
703cdf0e10cSrcweir     public:
7048748f2e1SDamjan Jovanovic     }; // class clear
7058748f2e1SDamjan Jovanovic 
TEST_F(clear,clear_001)7068748f2e1SDamjan Jovanovic     TEST_F(clear, clear_001)
707cdf0e10cSrcweir     {
708cdf0e10cSrcweir         Mutex aMutex;
709cdf0e10cSrcweir         ClearGuardThread myThread(&aMutex);
710cdf0e10cSrcweir         myThread.create();
711cdf0e10cSrcweir 
712cdf0e10cSrcweir         TimeValue aTimeVal_befor;
713cdf0e10cSrcweir         osl_getSystemTime( &aTimeVal_befor );
714cdf0e10cSrcweir         // wait 1 second to assure the child thread has begun
715cdf0e10cSrcweir         ThreadHelper::thread_sleep(1);
716cdf0e10cSrcweir 
717cdf0e10cSrcweir         while (1)
718cdf0e10cSrcweir         {
719cdf0e10cSrcweir             if (aMutex.tryToAcquire() == sal_True)
720cdf0e10cSrcweir             {
721cdf0e10cSrcweir                 break;
722cdf0e10cSrcweir             }
723cdf0e10cSrcweir             ThreadHelper::thread_sleep(1);
724cdf0e10cSrcweir         }
725cdf0e10cSrcweir         TimeValue aTimeVal_after;
726cdf0e10cSrcweir         osl_getSystemTime( &aTimeVal_after );
727cdf0e10cSrcweir         sal_Int32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
728cdf0e10cSrcweir         printf("nSec is %" SAL_PRIdINT32"\n", nSec);
729cdf0e10cSrcweir 
730cdf0e10cSrcweir         myThread.join();
731cdf0e10cSrcweir 
732*b4039cddSDon Lewis         ASSERT_TRUE(nSec <= 7 && nSec > 1) << "ClearableGuard method: clear";
733cdf0e10cSrcweir     }
734cdf0e10cSrcweir 
TEST_F(clear,clear_002)7358748f2e1SDamjan Jovanovic     TEST_F(clear, clear_002 )
736cdf0e10cSrcweir     {
737cdf0e10cSrcweir         Mutex aMutex;
738cdf0e10cSrcweir 
739cdf0e10cSrcweir         /// now, the aMutex has been guarded.
740cdf0e10cSrcweir         ClearableMutexGuard myMutexGuard( &aMutex );
741cdf0e10cSrcweir 
742cdf0e10cSrcweir         /// launch the HoldThread, it will be blocked here.
743cdf0e10cSrcweir         HoldThread myThread( &aMutex );
744cdf0e10cSrcweir         myThread.create( );
745cdf0e10cSrcweir 
746cdf0e10cSrcweir         /// is it blocking?
747cdf0e10cSrcweir         ThreadHelper::thread_sleep_tenth_sec( 4 );
748cdf0e10cSrcweir         sal_Bool bRes = myThread.isRunning( );
749cdf0e10cSrcweir 
750cdf0e10cSrcweir         /// use clear to release.
751cdf0e10cSrcweir         myMutexGuard.clear( );
752cdf0e10cSrcweir         myThread.join( );
753cdf0e10cSrcweir         sal_Bool bRes1 = myThread.isRunning( );
754cdf0e10cSrcweir 
7558748f2e1SDamjan Jovanovic         ASSERT_TRUE(( sal_True == bRes ) && ( sal_False == bRes1 )) << "ClearableGuard method: clear, control the HoldThread's running status!";
756cdf0e10cSrcweir     }
757cdf0e10cSrcweir 
758cdf0e10cSrcweir } // namespace osl_ClearableGuard
759cdf0e10cSrcweir 
760cdf0e10cSrcweir 
761cdf0e10cSrcweir //------------------------------------------------------------------------
762cdf0e10cSrcweir // Beginning of the test cases for osl_ResettableGuard class
763cdf0e10cSrcweir //------------------------------------------------------------------------
764cdf0e10cSrcweir 
765cdf0e10cSrcweir /** Thread for test ResettableGuard
766cdf0e10cSrcweir  */
767cdf0e10cSrcweir class ResetGuardThread : public Thread
768cdf0e10cSrcweir {
769cdf0e10cSrcweir public:
770cdf0e10cSrcweir     //get the Mutex pointer to operate
ResetGuardThread(Mutex * pMutex)771cdf0e10cSrcweir     ResetGuardThread( Mutex* pMutex ): pMyMutex( pMutex ) {}
772cdf0e10cSrcweir 
~ResetGuardThread()773cdf0e10cSrcweir     ~ResetGuardThread( )
774cdf0e10cSrcweir     {
775a03c9fa9SDamjan Jovanovic         EXPECT_TRUE(sal_False == this -> isRunning( )) << "#ResetGuardThread does not shutdown properly.\n";
776cdf0e10cSrcweir     }
777cdf0e10cSrcweir protected:
778cdf0e10cSrcweir     Mutex* pMyMutex;
779cdf0e10cSrcweir 
run()780cdf0e10cSrcweir     void SAL_CALL run( )
781cdf0e10cSrcweir     {
782cdf0e10cSrcweir         // acquire the mutex
783cdf0e10cSrcweir         printf("# ResettableGuard\n" );
784cdf0e10cSrcweir         ResettableMutexGuard aGuard( pMyMutex );
785cdf0e10cSrcweir         // release the mutex
786cdf0e10cSrcweir         aGuard.clear( );
787cdf0e10cSrcweir         ThreadHelper::thread_sleep_tenth_sec( 2 );
788cdf0e10cSrcweir     }
789cdf0e10cSrcweir };
790cdf0e10cSrcweir 
791cdf0e10cSrcweir // -----------------------------------------------------------------------------
792cdf0e10cSrcweir namespace osl_ResettableGuard
793cdf0e10cSrcweir {
7948748f2e1SDamjan Jovanovic     class ctor : public ::testing::Test
795cdf0e10cSrcweir     {
796cdf0e10cSrcweir     public:
7978748f2e1SDamjan Jovanovic     }; // class ctor
7988748f2e1SDamjan Jovanovic 
TEST_F(ctor,ctor_001)7998748f2e1SDamjan Jovanovic     TEST_F(ctor, ctor_001)
800cdf0e10cSrcweir     {
801cdf0e10cSrcweir         Mutex aMutex;
802cdf0e10cSrcweir 
803cdf0e10cSrcweir         /// now, the aMutex has been guarded.
804cdf0e10cSrcweir         ResettableMutexGuard myMutexGuard( &aMutex );
805cdf0e10cSrcweir 
806cdf0e10cSrcweir         /// it will return sal_False if the aMutex has not been Guarded.
807cdf0e10cSrcweir         sal_Bool bRes = aMutex.release( );
808cdf0e10cSrcweir 
80930acf5e8Spfg         ASSERT_TRUE(bRes == sal_True) << "ResettableMutexGuard constructor, test the acquire operation when initilized.";
810cdf0e10cSrcweir     }
811cdf0e10cSrcweir 
TEST_F(ctor,ctor_002)8128748f2e1SDamjan Jovanovic     TEST_F(ctor, ctor_002 )
813cdf0e10cSrcweir     {
814cdf0e10cSrcweir         Mutex aMutex;
815cdf0e10cSrcweir 
816cdf0e10cSrcweir         /// now, the aMutex has been guarded, this time, we use reference constructor.
817cdf0e10cSrcweir         ResettableMutexGuard myMutexGuard( aMutex );
818cdf0e10cSrcweir 
819cdf0e10cSrcweir         /// it will return sal_False if the aMutex has not been Guarded.
820cdf0e10cSrcweir         sal_Bool bRes = aMutex.release( );
821cdf0e10cSrcweir 
82230acf5e8Spfg         ASSERT_TRUE(bRes == sal_True) << "ResettableMutexGuard constructor, test the acquire operation when initilized, we use reference constructor this time.";
823cdf0e10cSrcweir     }
824cdf0e10cSrcweir 
825cdf0e10cSrcweir 
8268748f2e1SDamjan Jovanovic     class reset : public ::testing::Test
827cdf0e10cSrcweir     {
828cdf0e10cSrcweir     public:
8298748f2e1SDamjan Jovanovic     }; // class reset
8308748f2e1SDamjan Jovanovic 
8318748f2e1SDamjan Jovanovic 
TEST_F(reset,reset_001)8328748f2e1SDamjan Jovanovic     TEST_F(reset, reset_001 )
833cdf0e10cSrcweir     {
834cdf0e10cSrcweir         Mutex aMutex;
835cdf0e10cSrcweir         ResetGuardThread myThread( &aMutex );
836cdf0e10cSrcweir         ResettableMutexGuard myMutexGuard( aMutex );
837cdf0e10cSrcweir         myThread.create( );
838cdf0e10cSrcweir 
839cdf0e10cSrcweir         /// is it running? and clear done?
840cdf0e10cSrcweir         sal_Bool bRes = myThread.isRunning( );
841cdf0e10cSrcweir         myMutexGuard.clear( );
842cdf0e10cSrcweir         ThreadHelper::thread_sleep_tenth_sec( 1 );
843cdf0e10cSrcweir 
844cdf0e10cSrcweir         /// if reset is not success, the release will return sal_False
845cdf0e10cSrcweir         myMutexGuard.reset( );
846cdf0e10cSrcweir         sal_Bool bRes1 = aMutex.release( );
847cdf0e10cSrcweir         myThread.join( );
848cdf0e10cSrcweir 
8498748f2e1SDamjan Jovanovic         ASSERT_TRUE(( sal_True == bRes ) && ( sal_True == bRes1 )) << "ResettableMutexGuard method: reset";
850cdf0e10cSrcweir     }
851cdf0e10cSrcweir 
TEST_F(reset,reset_002)8528748f2e1SDamjan Jovanovic     TEST_F(reset, reset_002 )
853cdf0e10cSrcweir     {
8548748f2e1SDamjan Jovanovic #ifdef LINUX
855cdf0e10cSrcweir         Mutex aMutex;
856cdf0e10cSrcweir         ResettableMutexGuard myMutexGuard( &aMutex );
857cdf0e10cSrcweir 
858cdf0e10cSrcweir         /// shouldn't release after clear;
859cdf0e10cSrcweir         myMutexGuard.clear( );
860cdf0e10cSrcweir         sal_Bool bRes = aMutex.release( );
861cdf0e10cSrcweir 
862cdf0e10cSrcweir         /// can release after reset.
863cdf0e10cSrcweir         myMutexGuard.reset( );
864cdf0e10cSrcweir         sal_Bool bRes1 = aMutex.release( );
865cdf0e10cSrcweir 
86630acf5e8Spfg         ASSERT_TRUE(( sal_False == bRes ) && ( sal_True == bRes1 )) << "ResettableMutexGuard method: reset, release after clear and reset, on Solaris, the mutex can be release without acquire, so it can not passed on (SOLARIS), but not the reason for reset_002";
8678748f2e1SDamjan Jovanovic #endif
868cdf0e10cSrcweir     }
869cdf0e10cSrcweir 
870cdf0e10cSrcweir } // namespace osl_ResettableGuard
871cdf0e10cSrcweir 
main(int argc,char ** argv)8728748f2e1SDamjan Jovanovic int main(int argc, char **argv)
8738748f2e1SDamjan Jovanovic {
8748748f2e1SDamjan Jovanovic     ::testing::InitGoogleTest(&argc, argv);
8758748f2e1SDamjan Jovanovic     return RUN_ALL_TESTS();
8768748f2e1SDamjan Jovanovic }
877cdf0e10cSrcweir 
878cdf0e10cSrcweir // The following sets variables for GNU EMACS
879cdf0e10cSrcweir // Local Variables:
880cdf0e10cSrcweir // tab-width:4
881cdf0e10cSrcweir // End:
882