xref: /aoo42x/main/sal/qa/osl/mutex/osl_Mutex.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sal.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir //------------------------------------------------------------------------
32*cdf0e10cSrcweir // include files
33*cdf0e10cSrcweir //------------------------------------------------------------------------
34*cdf0e10cSrcweir #include "cppunit/TestAssert.h"
35*cdf0e10cSrcweir #include "cppunit/TestFixture.h"
36*cdf0e10cSrcweir #include "cppunit/extensions/HelperMacros.h"
37*cdf0e10cSrcweir #include "cppunit/plugin/TestPlugIn.h"
38*cdf0e10cSrcweir #include <osl_Mutex_Const.h>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir using namespace	osl;
41*cdf0e10cSrcweir using namespace	rtl;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir //------------------------------------------------------------------------
44*cdf0e10cSrcweir // helper functions
45*cdf0e10cSrcweir //------------------------------------------------------------------------
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir /** print a UNI_CODE String.
48*cdf0e10cSrcweir */
49*cdf0e10cSrcweir inline void printUString( const ::rtl::OUString & str )
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir 	rtl::OString aString;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir 	printf("#printUString_u# " );
54*cdf0e10cSrcweir 	aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
55*cdf0e10cSrcweir 	printf("%s\n", aString.getStr( ) );
56*cdf0e10cSrcweir }
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir /** print Boolean value.
59*cdf0e10cSrcweir */
60*cdf0e10cSrcweir inline void printBool( sal_Bool bOk )
61*cdf0e10cSrcweir {
62*cdf0e10cSrcweir 	printf("#printBool# " );
63*cdf0e10cSrcweir 	( sal_True == bOk ) ? printf("YES!\n" ): printf("NO!\n" );
64*cdf0e10cSrcweir }
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir /** pause nSec seconds helper function.
67*cdf0e10cSrcweir */
68*cdf0e10cSrcweir namespace ThreadHelper
69*cdf0e10cSrcweir {
70*cdf0e10cSrcweir 	void thread_sleep( sal_Int32 _nSec )
71*cdf0e10cSrcweir 	{
72*cdf0e10cSrcweir 		/// print statement in thread process must use fflush() to force display.
73*cdf0e10cSrcweir 		// t_print("# wait %d seconds. ", _nSec );
74*cdf0e10cSrcweir 		fflush(stdout);
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir #ifdef WNT                               //Windows
77*cdf0e10cSrcweir 		Sleep( _nSec * 1000 );
78*cdf0e10cSrcweir #endif
79*cdf0e10cSrcweir #if ( defined UNX ) || ( defined OS2 )   //Unix
80*cdf0e10cSrcweir 		sleep( _nSec );
81*cdf0e10cSrcweir #endif
82*cdf0e10cSrcweir 		// printf("# done\n" );
83*cdf0e10cSrcweir 	}
84*cdf0e10cSrcweir 	void thread_sleep_tenth_sec(sal_Int32 _nTenthSec)
85*cdf0e10cSrcweir  	{
86*cdf0e10cSrcweir #ifdef WNT      //Windows
87*cdf0e10cSrcweir         	Sleep(_nTenthSec * 100 );
88*cdf0e10cSrcweir #endif
89*cdf0e10cSrcweir #if ( defined UNX ) || ( defined OS2 )  //Unix
90*cdf0e10cSrcweir         	TimeValue nTV;
91*cdf0e10cSrcweir         	nTV.Seconds = static_cast<sal_uInt32>( _nTenthSec/10 );
92*cdf0e10cSrcweir         	nTV.Nanosec = ( (_nTenthSec%10 ) * 100000000 );
93*cdf0e10cSrcweir         	osl_waitThread(&nTV);
94*cdf0e10cSrcweir #endif
95*cdf0e10cSrcweir 	}
96*cdf0e10cSrcweir }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir //------------------------------------------------------------------------
100*cdf0e10cSrcweir // Beginning of the test cases for osl_Mutex class
101*cdf0e10cSrcweir //------------------------------------------------------------------------
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir /** mutually exclusive data
105*cdf0e10cSrcweir */
106*cdf0e10cSrcweir struct resource {
107*cdf0e10cSrcweir 	sal_Int32	data1;
108*cdf0e10cSrcweir 	sal_Int32	data2;
109*cdf0e10cSrcweir 	Mutex		lock;
110*cdf0e10cSrcweir };
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir /** IncreaseThread provide data.
113*cdf0e10cSrcweir */
114*cdf0e10cSrcweir class IncreaseThread : public Thread
115*cdf0e10cSrcweir {
116*cdf0e10cSrcweir public:
117*cdf0e10cSrcweir 	IncreaseThread( struct resource *pData ): pResource( pData ) { }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 	~IncreaseThread( )
120*cdf0e10cSrcweir 	{
121*cdf0e10cSrcweir 		CPPUNIT_ASSERT_MESSAGE( "#IncreaseThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
122*cdf0e10cSrcweir 	}
123*cdf0e10cSrcweir protected:
124*cdf0e10cSrcweir 	struct resource *pResource;
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 	void SAL_CALL run( )
127*cdf0e10cSrcweir 	{
128*cdf0e10cSrcweir 		pResource->lock.acquire( );
129*cdf0e10cSrcweir 		for( sal_Int8 i = 0; i < 3; i++ )
130*cdf0e10cSrcweir 		{
131*cdf0e10cSrcweir 			pResource->data1++;
132*cdf0e10cSrcweir 			yield( );  //yield() give CPU time to other thread, other thread if not block, they will change the data;
133*cdf0e10cSrcweir 		}
134*cdf0e10cSrcweir 		if ( pResource->data2 == 0 )
135*cdf0e10cSrcweir 			pResource->data2 = ( pResource->data1 > 0 ? pResource->data1 : 0 - pResource->data1 );
136*cdf0e10cSrcweir 		pResource->lock.release();
137*cdf0e10cSrcweir 	}
138*cdf0e10cSrcweir };
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir /** DecreaseThread consume data.
141*cdf0e10cSrcweir */
142*cdf0e10cSrcweir class DecreaseThread : public Thread
143*cdf0e10cSrcweir {
144*cdf0e10cSrcweir public:
145*cdf0e10cSrcweir 	DecreaseThread( struct resource *pData ): pResource( pData ) { }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 	~DecreaseThread( )
148*cdf0e10cSrcweir 	{
149*cdf0e10cSrcweir 		CPPUNIT_ASSERT_MESSAGE( "#DecreaseThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
150*cdf0e10cSrcweir 	}
151*cdf0e10cSrcweir protected:
152*cdf0e10cSrcweir 	struct resource *pResource;
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 	void SAL_CALL run( )
155*cdf0e10cSrcweir 	{
156*cdf0e10cSrcweir 		pResource->lock.acquire( );
157*cdf0e10cSrcweir 		for( sal_Int8 i = 0; i < 3; i++ )
158*cdf0e10cSrcweir 		{
159*cdf0e10cSrcweir 			pResource->data1--;
160*cdf0e10cSrcweir 			yield( );  //yield() give CPU time to other thread, other thread if not block, they will change the data;
161*cdf0e10cSrcweir 		}
162*cdf0e10cSrcweir 		if ( pResource->data2 == 0 )
163*cdf0e10cSrcweir 			pResource->data2 = ( pResource->data1 > 0 ? pResource->data1 : 0 - pResource->data1 );
164*cdf0e10cSrcweir 		pResource->lock.release();
165*cdf0e10cSrcweir 	}
166*cdf0e10cSrcweir };
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir /** chain structure used in Threads as critical resource
170*cdf0e10cSrcweir */
171*cdf0e10cSrcweir struct chain {
172*cdf0e10cSrcweir 	sal_Int32	buffer[ BUFFER_SIZE ];
173*cdf0e10cSrcweir 	Mutex		lock;
174*cdf0e10cSrcweir 	sal_Int8	pos;
175*cdf0e10cSrcweir };
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir /** PutThread write to the chain structure in a mutex manner.
178*cdf0e10cSrcweir */
179*cdf0e10cSrcweir class PutThread : public Thread
180*cdf0e10cSrcweir {
181*cdf0e10cSrcweir public:
182*cdf0e10cSrcweir 	//get the struct pointer to write data to buffer
183*cdf0e10cSrcweir 	PutThread( struct chain* pData ): pChain( pData ) { }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 	~PutThread( )
186*cdf0e10cSrcweir 	{
187*cdf0e10cSrcweir 		CPPUNIT_ASSERT_MESSAGE( "#PutThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
188*cdf0e10cSrcweir 	}
189*cdf0e10cSrcweir protected:
190*cdf0e10cSrcweir 	struct chain* pChain;
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir 	void SAL_CALL run( )
193*cdf0e10cSrcweir 	{
194*cdf0e10cSrcweir 		//block here if the mutex has been acquired
195*cdf0e10cSrcweir 		pChain->lock.acquire( );
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 		//current position in buffer to write
198*cdf0e10cSrcweir 		sal_Int8 nPos = pChain->pos;
199*cdf0e10cSrcweir 		oslThreadIdentifier oId = getIdentifier( );
200*cdf0e10cSrcweir 		//write data
201*cdf0e10cSrcweir                 sal_Int8 i;
202*cdf0e10cSrcweir 		for ( i = 0; i < 5; i++ )
203*cdf0e10cSrcweir 		{
204*cdf0e10cSrcweir 			pChain->buffer[ nPos + i ] = oId;
205*cdf0e10cSrcweir 			yield( );
206*cdf0e10cSrcweir 		}
207*cdf0e10cSrcweir 		//revise the position
208*cdf0e10cSrcweir 		pChain->pos = nPos + i;
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 		//finish writing, release the mutex
211*cdf0e10cSrcweir 		pChain->lock.release();
212*cdf0e10cSrcweir 	}
213*cdf0e10cSrcweir };
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir /** thread for testing Mutex acquire.
216*cdf0e10cSrcweir  */
217*cdf0e10cSrcweir class HoldThread : public Thread
218*cdf0e10cSrcweir {
219*cdf0e10cSrcweir public:
220*cdf0e10cSrcweir 	//get the Mutex pointer to operate
221*cdf0e10cSrcweir 	HoldThread( Mutex* pMutex ): pMyMutex( pMutex ) { }
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir 	~HoldThread( )
224*cdf0e10cSrcweir 	{
225*cdf0e10cSrcweir 		CPPUNIT_ASSERT_MESSAGE( "#HoldThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
226*cdf0e10cSrcweir 	}
227*cdf0e10cSrcweir protected:
228*cdf0e10cSrcweir 	Mutex* pMyMutex;
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir 	void SAL_CALL run()
231*cdf0e10cSrcweir 	{
232*cdf0e10cSrcweir 		// block here if the mutex has been acquired
233*cdf0e10cSrcweir 		pMyMutex->acquire( );
234*cdf0e10cSrcweir 		printf("# Mutex acquired. \n" );
235*cdf0e10cSrcweir 		pMyMutex->release( );
236*cdf0e10cSrcweir 	}
237*cdf0e10cSrcweir };
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir class WaitThread : public Thread
240*cdf0e10cSrcweir {
241*cdf0e10cSrcweir public:
242*cdf0e10cSrcweir 	//get the Mutex pointer to operate
243*cdf0e10cSrcweir 	WaitThread( Mutex* pMutex ): pMyMutex( pMutex ) { }
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir 	~WaitThread( )
246*cdf0e10cSrcweir 	{
247*cdf0e10cSrcweir 		CPPUNIT_ASSERT_MESSAGE( "#WaitThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
248*cdf0e10cSrcweir 	}
249*cdf0e10cSrcweir protected:
250*cdf0e10cSrcweir 	Mutex* pMyMutex;
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir 	void SAL_CALL run( )
253*cdf0e10cSrcweir 	{
254*cdf0e10cSrcweir 		// block here if the mutex has been acquired
255*cdf0e10cSrcweir 		pMyMutex->acquire( );
256*cdf0e10cSrcweir 		ThreadHelper::thread_sleep_tenth_sec( 2 );
257*cdf0e10cSrcweir 		pMyMutex->release( );
258*cdf0e10cSrcweir 	}
259*cdf0e10cSrcweir };
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir /** thread for testing getGlobalMutex.
262*cdf0e10cSrcweir  */
263*cdf0e10cSrcweir class GlobalMutexThread : public Thread
264*cdf0e10cSrcweir {
265*cdf0e10cSrcweir public:
266*cdf0e10cSrcweir 	//get the Mutex pointer to operate
267*cdf0e10cSrcweir 	GlobalMutexThread( ){ }
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir 	~GlobalMutexThread( )
270*cdf0e10cSrcweir 	{
271*cdf0e10cSrcweir 		CPPUNIT_ASSERT_MESSAGE( "#GlobalMutexThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
272*cdf0e10cSrcweir 	}
273*cdf0e10cSrcweir protected:
274*cdf0e10cSrcweir 	void SAL_CALL run( )
275*cdf0e10cSrcweir 	{
276*cdf0e10cSrcweir 		// block here if the mutex has been acquired
277*cdf0e10cSrcweir 		Mutex* pGlobalMutex;
278*cdf0e10cSrcweir 		pGlobalMutex = pGlobalMutex->getGlobalMutex( );
279*cdf0e10cSrcweir 		pGlobalMutex->acquire( );
280*cdf0e10cSrcweir 		printf("# Global Mutex acquired. \n" );
281*cdf0e10cSrcweir 		pGlobalMutex->release( );
282*cdf0e10cSrcweir 	}
283*cdf0e10cSrcweir };
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir //--------------------------------------------------------------
287*cdf0e10cSrcweir namespace osl_Mutex
288*cdf0e10cSrcweir {
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir 	/** Test of the	osl::Mutex::constructor
291*cdf0e10cSrcweir 	 */
292*cdf0e10cSrcweir 	class ctor : public CppUnit::TestFixture
293*cdf0e10cSrcweir 	{
294*cdf0e10cSrcweir 	public:
295*cdf0e10cSrcweir 		// initialise your test code values here.
296*cdf0e10cSrcweir 		struct chain m_Data;
297*cdf0e10cSrcweir 		struct resource m_Res;
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir 		void setUp( )
300*cdf0e10cSrcweir 		{
301*cdf0e10cSrcweir 			for ( sal_Int8 i=0; i < BUFFER_SIZE; i++ )
302*cdf0e10cSrcweir 				m_Data.buffer[i] = 0;
303*cdf0e10cSrcweir 			m_Data.pos = 0;
304*cdf0e10cSrcweir 
305*cdf0e10cSrcweir 			m_Res.data1 = 0;
306*cdf0e10cSrcweir 			m_Res.data2 = 0;
307*cdf0e10cSrcweir 		}
308*cdf0e10cSrcweir 
309*cdf0e10cSrcweir 		void tearDown()
310*cdf0e10cSrcweir 		{
311*cdf0e10cSrcweir 		}
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir 		/** Create two threads to write data to the same buffer, use Mutex to assure
314*cdf0e10cSrcweir 			during one thread write data five times, the other thread should not begin writing.
315*cdf0e10cSrcweir 			the two threads wrote two different datas: their thread ID, so we can check the datas
316*cdf0e10cSrcweir 			in buffer to know the order of the two threads writing
317*cdf0e10cSrcweir 		*/
318*cdf0e10cSrcweir 		void ctor_001()
319*cdf0e10cSrcweir 		{
320*cdf0e10cSrcweir 			PutThread myThread1( &m_Data );
321*cdf0e10cSrcweir 			PutThread myThread2( &m_Data );
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir 			myThread1.create( );
324*cdf0e10cSrcweir 			myThread2.create( );
325*cdf0e10cSrcweir 
326*cdf0e10cSrcweir 			//wait until the two threads terminate
327*cdf0e10cSrcweir 			myThread1.join( );
328*cdf0e10cSrcweir 			myThread2.join( );
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir 			sal_Bool bRes = sal_False;
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir 			// every 5 datas should the same
333*cdf0e10cSrcweir             // LLA: this is not a good check, it's too fix
334*cdf0e10cSrcweir 			if (m_Data.buffer[0] == m_Data.buffer[1] &&
335*cdf0e10cSrcweir 				m_Data.buffer[1] == m_Data.buffer[2] &&
336*cdf0e10cSrcweir 				m_Data.buffer[2] == m_Data.buffer[3] &&
337*cdf0e10cSrcweir 				m_Data.buffer[3] == m_Data.buffer[4] &&
338*cdf0e10cSrcweir 				m_Data.buffer[5] == m_Data.buffer[6] &&
339*cdf0e10cSrcweir 				m_Data.buffer[6] == m_Data.buffer[7] &&
340*cdf0e10cSrcweir 				m_Data.buffer[7] == m_Data.buffer[8] &&
341*cdf0e10cSrcweir 				m_Data.buffer[8] == m_Data.buffer[9])
342*cdf0e10cSrcweir 				bRes = sal_True;
343*cdf0e10cSrcweir 
344*cdf0e10cSrcweir 			/*for (sal_Int8 i=0; i<BUFFER_SIZE; i++)
345*cdf0e10cSrcweir 				printf("#data in buffer is %d\n", m_Data.buffer[i]);
346*cdf0e10cSrcweir 			*/
347*cdf0e10cSrcweir 
348*cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE("Mutex ctor", bRes == sal_True);
349*cdf0e10cSrcweir 
350*cdf0e10cSrcweir 		}
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir 		/** Create two threads to write data to operate on the same number , use Mutex to assure,
353*cdf0e10cSrcweir 			one thread increase data 3 times, the other thread decrease 3 times, store the operate
354*cdf0e10cSrcweir 			result when the first thread complete, if it is interrupt by the other thread, the stored
355*cdf0e10cSrcweir 			number will not be 3.
356*cdf0e10cSrcweir 		*/
357*cdf0e10cSrcweir 		void ctor_002()
358*cdf0e10cSrcweir 		{
359*cdf0e10cSrcweir 			IncreaseThread myThread1( &m_Res );
360*cdf0e10cSrcweir 			DecreaseThread myThread2( &m_Res );
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir 			myThread1.create( );
363*cdf0e10cSrcweir 			myThread2.create( );
364*cdf0e10cSrcweir 
365*cdf0e10cSrcweir 			//wait until the two threads terminate
366*cdf0e10cSrcweir 			myThread1.join( );
367*cdf0e10cSrcweir 			myThread2.join( );
368*cdf0e10cSrcweir 
369*cdf0e10cSrcweir 			sal_Bool bRes = sal_False;
370*cdf0e10cSrcweir 
371*cdf0e10cSrcweir 			// every 5 datas should the same
372*cdf0e10cSrcweir 			if ( ( m_Res.data1 == 0 ) && ( m_Res.data2 == 3 ) )
373*cdf0e10cSrcweir 				bRes = sal_True;
374*cdf0e10cSrcweir 
375*cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE( "test Mutex ctor function: increase and decrease a number 3 times without interrupt.", bRes == sal_True );
376*cdf0e10cSrcweir 		}
377*cdf0e10cSrcweir 
378*cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE( ctor );
379*cdf0e10cSrcweir 		CPPUNIT_TEST( ctor_001 );
380*cdf0e10cSrcweir 		CPPUNIT_TEST( ctor_002 );
381*cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE_END( );
382*cdf0e10cSrcweir 	}; // class ctor
383*cdf0e10cSrcweir 
384*cdf0e10cSrcweir 
385*cdf0e10cSrcweir 	/** Test of the	osl::Mutex::acquire method
386*cdf0e10cSrcweir 	 */
387*cdf0e10cSrcweir 	class acquire : public CppUnit::TestFixture
388*cdf0e10cSrcweir 	{
389*cdf0e10cSrcweir 	public:
390*cdf0e10cSrcweir 		// acquire mutex in main thread, and then call acquire again in myThread,
391*cdf0e10cSrcweir 		// the child thread should block, wait 2 secs, it still block.
392*cdf0e10cSrcweir 		// Then release mutex in main thread, the child thread could return from acquire,
393*cdf0e10cSrcweir 		// and go to exec next statement, so could terminate quickly.
394*cdf0e10cSrcweir 		void acquire_001( )
395*cdf0e10cSrcweir 		{
396*cdf0e10cSrcweir 			Mutex aMutex;
397*cdf0e10cSrcweir 			//acquire here
398*cdf0e10cSrcweir 			sal_Bool bRes = aMutex.acquire( );
399*cdf0e10cSrcweir 			// pass the pointer of mutex to child thread
400*cdf0e10cSrcweir 			HoldThread myThread( &aMutex );
401*cdf0e10cSrcweir 			myThread.create( );
402*cdf0e10cSrcweir 
403*cdf0e10cSrcweir 			ThreadHelper::thread_sleep_tenth_sec( 2 );
404*cdf0e10cSrcweir 			// if acquire in myThread does not work, 2 secs is long enough,
405*cdf0e10cSrcweir 			// myThread should terminate now, and bRes1 should be sal_False
406*cdf0e10cSrcweir 			sal_Bool bRes1 = myThread.isRunning( );
407*cdf0e10cSrcweir 
408*cdf0e10cSrcweir 			aMutex.release( );
409*cdf0e10cSrcweir 			ThreadHelper::thread_sleep_tenth_sec( 1 );
410*cdf0e10cSrcweir 			// after release mutex, myThread stops blocking and will terminate immediately
411*cdf0e10cSrcweir 			sal_Bool bRes2 = myThread.isRunning( );
412*cdf0e10cSrcweir 			myThread.join( );
413*cdf0e10cSrcweir 
414*cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE( "Mutex acquire",
415*cdf0e10cSrcweir 				bRes == sal_True && bRes1 == sal_True && bRes2 == sal_False );
416*cdf0e10cSrcweir 		}
417*cdf0e10cSrcweir 
418*cdf0e10cSrcweir 		//in the same thread, acquire twice should success
419*cdf0e10cSrcweir 		void acquire_002()
420*cdf0e10cSrcweir 		{
421*cdf0e10cSrcweir 			Mutex aMutex;
422*cdf0e10cSrcweir 			//acquire here
423*cdf0e10cSrcweir 			sal_Bool bRes = aMutex.acquire();
424*cdf0e10cSrcweir 			sal_Bool bRes1 = aMutex.acquire();
425*cdf0e10cSrcweir 
426*cdf0e10cSrcweir 			sal_Bool bRes2 = aMutex.tryToAcquire();
427*cdf0e10cSrcweir 
428*cdf0e10cSrcweir 			aMutex.release();
429*cdf0e10cSrcweir 
430*cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE("Mutex acquire",
431*cdf0e10cSrcweir 				bRes == sal_True && bRes1 == sal_True && bRes2 == sal_True);
432*cdf0e10cSrcweir 
433*cdf0e10cSrcweir 		}
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE( acquire );
436*cdf0e10cSrcweir 		CPPUNIT_TEST( acquire_001 );
437*cdf0e10cSrcweir 		CPPUNIT_TEST( acquire_002 );
438*cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE_END( );
439*cdf0e10cSrcweir 	}; // class acquire
440*cdf0e10cSrcweir 
441*cdf0e10cSrcweir 
442*cdf0e10cSrcweir 	/** Test of the	osl::Mutex::tryToAcquire method
443*cdf0e10cSrcweir 	 */
444*cdf0e10cSrcweir 	class tryToAcquire : public CppUnit::TestFixture
445*cdf0e10cSrcweir 	{
446*cdf0e10cSrcweir 	public:
447*cdf0e10cSrcweir 		// First let child thread acquire the mutex, and wait 2 secs, during the 2 secs,
448*cdf0e10cSrcweir 		// in main thread, tryToAcquire mutex should return False
449*cdf0e10cSrcweir 		// then after the child thread terminated, tryToAcquire should return True
450*cdf0e10cSrcweir 		void tryToAcquire_001()
451*cdf0e10cSrcweir 		{
452*cdf0e10cSrcweir 			Mutex aMutex;
453*cdf0e10cSrcweir 			WaitThread myThread(&aMutex);
454*cdf0e10cSrcweir 			myThread.create();
455*cdf0e10cSrcweir 
456*cdf0e10cSrcweir 			// ensure the child thread acquire the mutex
457*cdf0e10cSrcweir 			ThreadHelper::thread_sleep_tenth_sec(1);
458*cdf0e10cSrcweir 
459*cdf0e10cSrcweir 			sal_Bool bRes1 = aMutex.tryToAcquire();
460*cdf0e10cSrcweir 
461*cdf0e10cSrcweir 			if (bRes1 == sal_True)
462*cdf0e10cSrcweir 				aMutex.release();
463*cdf0e10cSrcweir 			// wait the child thread terminate
464*cdf0e10cSrcweir 			myThread.join();
465*cdf0e10cSrcweir 
466*cdf0e10cSrcweir 			sal_Bool bRes2 = aMutex.tryToAcquire();
467*cdf0e10cSrcweir 
468*cdf0e10cSrcweir 			if (bRes2 == sal_True)
469*cdf0e10cSrcweir 				aMutex.release();
470*cdf0e10cSrcweir 
471*cdf0e10cSrcweir 		CPPUNIT_ASSERT_MESSAGE("Try to acquire Mutex",
472*cdf0e10cSrcweir 				bRes1 == sal_False && bRes2 == sal_True);
473*cdf0e10cSrcweir 		}
474*cdf0e10cSrcweir 
475*cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE(tryToAcquire);
476*cdf0e10cSrcweir 		CPPUNIT_TEST(tryToAcquire_001);
477*cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE_END();
478*cdf0e10cSrcweir 	}; // class tryToAcquire
479*cdf0e10cSrcweir 
480*cdf0e10cSrcweir 	/** Test of the	osl::Mutex::release method
481*cdf0e10cSrcweir 	 */
482*cdf0e10cSrcweir 	class release : public CppUnit::TestFixture
483*cdf0e10cSrcweir 	{
484*cdf0e10cSrcweir 	public:
485*cdf0e10cSrcweir 		/** acquire/release are not used in pairs: after child thread acquired mutex,
486*cdf0e10cSrcweir 			the main thread release it, then any thread could acquire it.
487*cdf0e10cSrcweir 		*/
488*cdf0e10cSrcweir 		void release_001()
489*cdf0e10cSrcweir 		{
490*cdf0e10cSrcweir 			Mutex aMutex;
491*cdf0e10cSrcweir 			WaitThread myThread( &aMutex );
492*cdf0e10cSrcweir 			myThread.create( );
493*cdf0e10cSrcweir 
494*cdf0e10cSrcweir 			// ensure the child thread acquire the mutex
495*cdf0e10cSrcweir 			ThreadHelper::thread_sleep_tenth_sec( 1 );
496*cdf0e10cSrcweir 
497*cdf0e10cSrcweir 			sal_Bool bRunning = myThread.isRunning( );
498*cdf0e10cSrcweir 			sal_Bool bRes1 = aMutex.tryToAcquire( );
499*cdf0e10cSrcweir 			// wait the child thread terminate
500*cdf0e10cSrcweir 			myThread.join( );
501*cdf0e10cSrcweir 
502*cdf0e10cSrcweir 			sal_Bool bRes2 = aMutex.tryToAcquire( );
503*cdf0e10cSrcweir 
504*cdf0e10cSrcweir 			if ( bRes2 == sal_True )
505*cdf0e10cSrcweir 				aMutex.release( );
506*cdf0e10cSrcweir 
507*cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE( "release Mutex: try to aquire before and after the mutex has been released",
508*cdf0e10cSrcweir 				bRes1 == sal_False && bRes2 == sal_True && bRunning == sal_True );
509*cdf0e10cSrcweir 
510*cdf0e10cSrcweir 		}
511*cdf0e10cSrcweir 
512*cdf0e10cSrcweir 		// how about release twice?
513*cdf0e10cSrcweir 		void release_002()
514*cdf0e10cSrcweir 		{
515*cdf0e10cSrcweir // LLA: is this a real test?
516*cdf0e10cSrcweir #if 0
517*cdf0e10cSrcweir 			Mutex aMutex;
518*cdf0e10cSrcweir 			sal_Bool bRes1 = aMutex.release( );
519*cdf0e10cSrcweir 			sal_Bool bRes2 = aMutex.release( );
520*cdf0e10cSrcweir 
521*cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE( "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)",
522*cdf0e10cSrcweir 				bRes1 == sal_False && bRes2 == sal_False );
523*cdf0e10cSrcweir #endif
524*cdf0e10cSrcweir 		}
525*cdf0e10cSrcweir 
526*cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE( release );
527*cdf0e10cSrcweir 		CPPUNIT_TEST( release_001 );
528*cdf0e10cSrcweir 		CPPUNIT_TEST( release_002 );
529*cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE_END( );
530*cdf0e10cSrcweir 	}; // class release
531*cdf0e10cSrcweir 
532*cdf0e10cSrcweir 
533*cdf0e10cSrcweir 
534*cdf0e10cSrcweir 	/** Test of the	osl::Mutex::getGlobalMutex method
535*cdf0e10cSrcweir 	 */
536*cdf0e10cSrcweir 	class getGlobalMutex : public CppUnit::TestFixture
537*cdf0e10cSrcweir 	{
538*cdf0e10cSrcweir 	public:
539*cdf0e10cSrcweir 		// initialise your test code values here.
540*cdf0e10cSrcweir 		void getGlobalMutex_001()
541*cdf0e10cSrcweir 		{
542*cdf0e10cSrcweir 			Mutex* pGlobalMutex;
543*cdf0e10cSrcweir 			pGlobalMutex = pGlobalMutex->getGlobalMutex();
544*cdf0e10cSrcweir 			pGlobalMutex->acquire();
545*cdf0e10cSrcweir 
546*cdf0e10cSrcweir 			GlobalMutexThread myThread;
547*cdf0e10cSrcweir 			myThread.create();
548*cdf0e10cSrcweir 
549*cdf0e10cSrcweir 			ThreadHelper::thread_sleep_tenth_sec(1);
550*cdf0e10cSrcweir 			sal_Bool bRes1 = myThread.isRunning();
551*cdf0e10cSrcweir 
552*cdf0e10cSrcweir 			pGlobalMutex->release();
553*cdf0e10cSrcweir 			ThreadHelper::thread_sleep_tenth_sec(1);
554*cdf0e10cSrcweir 			// after release mutex, myThread stops blocking and will terminate immediately
555*cdf0e10cSrcweir 			sal_Bool bRes2 = myThread.isRunning();
556*cdf0e10cSrcweir 
557*cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE("Global Mutex works",
558*cdf0e10cSrcweir 				bRes1 == sal_True && bRes2 == sal_False);
559*cdf0e10cSrcweir 		}
560*cdf0e10cSrcweir 
561*cdf0e10cSrcweir 		void getGlobalMutex_002( )
562*cdf0e10cSrcweir 		{
563*cdf0e10cSrcweir 			sal_Bool bRes;
564*cdf0e10cSrcweir 
565*cdf0e10cSrcweir 			Mutex *pGlobalMutex;
566*cdf0e10cSrcweir 			pGlobalMutex = pGlobalMutex->getGlobalMutex( );
567*cdf0e10cSrcweir 			pGlobalMutex->acquire( );
568*cdf0e10cSrcweir 			{
569*cdf0e10cSrcweir 				Mutex *pGlobalMutex1;
570*cdf0e10cSrcweir 				pGlobalMutex1 = pGlobalMutex1->getGlobalMutex( );
571*cdf0e10cSrcweir 				bRes = pGlobalMutex1->release( );
572*cdf0e10cSrcweir 			}
573*cdf0e10cSrcweir 
574*cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE( "Global Mutex works: if the code between {} get the different mutex as the former one, it will return false when release.",
575*cdf0e10cSrcweir 				bRes == sal_True );
576*cdf0e10cSrcweir 		}
577*cdf0e10cSrcweir 
578*cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE(getGlobalMutex);
579*cdf0e10cSrcweir 		CPPUNIT_TEST(getGlobalMutex_001);
580*cdf0e10cSrcweir 		CPPUNIT_TEST(getGlobalMutex_002);
581*cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE_END();
582*cdf0e10cSrcweir 	}; // class getGlobalMutex
583*cdf0e10cSrcweir 
584*cdf0e10cSrcweir // -----------------------------------------------------------------------------
585*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Mutex::ctor, "osl_Mutex");
586*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Mutex::acquire, "osl_Mutex");
587*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Mutex::tryToAcquire, "osl_Mutex");
588*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Mutex::release, "osl_Mutex");
589*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Mutex::getGlobalMutex, "osl_Mutex");
590*cdf0e10cSrcweir } // namespace osl_Mutex
591*cdf0e10cSrcweir 
592*cdf0e10cSrcweir 
593*cdf0e10cSrcweir //------------------------------------------------------------------------
594*cdf0e10cSrcweir // Beginning of the test cases for osl_Guard class
595*cdf0e10cSrcweir //------------------------------------------------------------------------
596*cdf0e10cSrcweir 
597*cdf0e10cSrcweir class GuardThread : public Thread
598*cdf0e10cSrcweir {
599*cdf0e10cSrcweir public:
600*cdf0e10cSrcweir 	//get the Mutex pointer to operate
601*cdf0e10cSrcweir 	GuardThread( Mutex* pMutex ): pMyMutex( pMutex ) { }
602*cdf0e10cSrcweir 
603*cdf0e10cSrcweir 	~GuardThread( )
604*cdf0e10cSrcweir 	{
605*cdf0e10cSrcweir 		CPPUNIT_ASSERT_MESSAGE( "#GuardThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
606*cdf0e10cSrcweir 	}
607*cdf0e10cSrcweir protected:
608*cdf0e10cSrcweir 	Mutex* pMyMutex;
609*cdf0e10cSrcweir 
610*cdf0e10cSrcweir 	void SAL_CALL run( )
611*cdf0e10cSrcweir 	{
612*cdf0e10cSrcweir 		// block here if the mutex has been acquired
613*cdf0e10cSrcweir 		MutexGuard aGuard( pMyMutex );
614*cdf0e10cSrcweir 		ThreadHelper::thread_sleep_tenth_sec( 2 );
615*cdf0e10cSrcweir 	}
616*cdf0e10cSrcweir };
617*cdf0e10cSrcweir 
618*cdf0e10cSrcweir 
619*cdf0e10cSrcweir namespace osl_Guard
620*cdf0e10cSrcweir {
621*cdf0e10cSrcweir 	class ctor : public CppUnit::TestFixture
622*cdf0e10cSrcweir 	{
623*cdf0e10cSrcweir 	public:
624*cdf0e10cSrcweir 		// insert your test code here.
625*cdf0e10cSrcweir 		void ctor_001()
626*cdf0e10cSrcweir 		{
627*cdf0e10cSrcweir 			Mutex aMutex;
628*cdf0e10cSrcweir 			GuardThread myThread(&aMutex);
629*cdf0e10cSrcweir 			myThread.create();
630*cdf0e10cSrcweir 
631*cdf0e10cSrcweir 			ThreadHelper::thread_sleep_tenth_sec(1);
632*cdf0e10cSrcweir 			sal_Bool bRes = aMutex.tryToAcquire();
633*cdf0e10cSrcweir 			// after 1 second, the mutex has been guarded, and the child thread should be running
634*cdf0e10cSrcweir 			sal_Bool bRes1 = myThread.isRunning();
635*cdf0e10cSrcweir 
636*cdf0e10cSrcweir 			myThread.join();
637*cdf0e10cSrcweir 			sal_Bool bRes2 = aMutex.tryToAcquire();
638*cdf0e10cSrcweir 
639*cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE("GuardThread constructor",
640*cdf0e10cSrcweir 				bRes == sal_False && bRes1 == sal_True && bRes2 == sal_True);
641*cdf0e10cSrcweir 		}
642*cdf0e10cSrcweir 
643*cdf0e10cSrcweir 		void ctor_002( )
644*cdf0e10cSrcweir 		{
645*cdf0e10cSrcweir 			Mutex aMutex;
646*cdf0e10cSrcweir 
647*cdf0e10cSrcweir 			/// use reference constructor here
648*cdf0e10cSrcweir 			MutexGuard myGuard( aMutex );
649*cdf0e10cSrcweir 
650*cdf0e10cSrcweir 			/// the GuardThread will block here when it is initialised.
651*cdf0e10cSrcweir 			GuardThread myThread( &aMutex );
652*cdf0e10cSrcweir 			myThread.create( );
653*cdf0e10cSrcweir 
654*cdf0e10cSrcweir 			/// is it still blocking?
655*cdf0e10cSrcweir 			ThreadHelper::thread_sleep_tenth_sec( 2 );
656*cdf0e10cSrcweir 			sal_Bool bRes = myThread.isRunning( );
657*cdf0e10cSrcweir 
658*cdf0e10cSrcweir 			/// oh, release him.
659*cdf0e10cSrcweir 			aMutex.release( );
660*cdf0e10cSrcweir 			myThread.join( );
661*cdf0e10cSrcweir 
662*cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE("GuardThread constructor: reference initialization, aquire the mutex before running the thread, then check if it is blocking.",
663*cdf0e10cSrcweir 				bRes == sal_True);
664*cdf0e10cSrcweir 		}
665*cdf0e10cSrcweir 
666*cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE(ctor);
667*cdf0e10cSrcweir 		CPPUNIT_TEST(ctor_001);
668*cdf0e10cSrcweir 		CPPUNIT_TEST(ctor_002);
669*cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE_END();
670*cdf0e10cSrcweir 	}; // class ctor
671*cdf0e10cSrcweir 
672*cdf0e10cSrcweir // -----------------------------------------------------------------------------
673*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Guard::ctor, "osl_Guard");
674*cdf0e10cSrcweir } // namespace osl_Guard
675*cdf0e10cSrcweir 
676*cdf0e10cSrcweir 
677*cdf0e10cSrcweir //------------------------------------------------------------------------
678*cdf0e10cSrcweir // Beginning of the test cases for osl_ClearableGuard class
679*cdf0e10cSrcweir //------------------------------------------------------------------------
680*cdf0e10cSrcweir 
681*cdf0e10cSrcweir /** Thread for test ClearableGuard
682*cdf0e10cSrcweir  */
683*cdf0e10cSrcweir class ClearGuardThread : public Thread
684*cdf0e10cSrcweir {
685*cdf0e10cSrcweir public:
686*cdf0e10cSrcweir 	//get the Mutex pointer to operate
687*cdf0e10cSrcweir 	ClearGuardThread( Mutex* pMutex ): pMyMutex( pMutex ) {}
688*cdf0e10cSrcweir 
689*cdf0e10cSrcweir 	~ClearGuardThread( )
690*cdf0e10cSrcweir 	{
691*cdf0e10cSrcweir 		CPPUNIT_ASSERT_MESSAGE( "#ClearGuardThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
692*cdf0e10cSrcweir 	}
693*cdf0e10cSrcweir protected:
694*cdf0e10cSrcweir 	Mutex* pMyMutex;
695*cdf0e10cSrcweir 
696*cdf0e10cSrcweir 	void SAL_CALL run( )
697*cdf0e10cSrcweir 	{
698*cdf0e10cSrcweir 		// acquire the mutex
699*cdf0e10cSrcweir         // printf("# ClearGuardThread" );
700*cdf0e10cSrcweir 		ClearableMutexGuard aGuard( pMyMutex );
701*cdf0e10cSrcweir 		ThreadHelper::thread_sleep( 5 );
702*cdf0e10cSrcweir 
703*cdf0e10cSrcweir 		// release the mutex
704*cdf0e10cSrcweir 		aGuard.clear( );
705*cdf0e10cSrcweir 		ThreadHelper::thread_sleep( 2 );
706*cdf0e10cSrcweir 	}
707*cdf0e10cSrcweir };
708*cdf0e10cSrcweir 
709*cdf0e10cSrcweir // -----------------------------------------------------------------------------
710*cdf0e10cSrcweir namespace osl_ClearableGuard
711*cdf0e10cSrcweir {
712*cdf0e10cSrcweir 
713*cdf0e10cSrcweir 	class ctor : public CppUnit::TestFixture
714*cdf0e10cSrcweir 	{
715*cdf0e10cSrcweir 	public:
716*cdf0e10cSrcweir 		void ctor_001()
717*cdf0e10cSrcweir 		{
718*cdf0e10cSrcweir 			Mutex aMutex;
719*cdf0e10cSrcweir 
720*cdf0e10cSrcweir 			/// now, the aMutex has been guarded.
721*cdf0e10cSrcweir 			ClearableMutexGuard myMutexGuard( &aMutex );
722*cdf0e10cSrcweir 
723*cdf0e10cSrcweir 			/// it will return sal_False if the aMutex has not been Guarded.
724*cdf0e10cSrcweir 			sal_Bool bRes = aMutex.release( );
725*cdf0e10cSrcweir 
726*cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE("ClearableMutexGuard constructor, test the aquire operation when initilized.",
727*cdf0e10cSrcweir 				bRes == sal_True );
728*cdf0e10cSrcweir 		}
729*cdf0e10cSrcweir 
730*cdf0e10cSrcweir 		void ctor_002( )
731*cdf0e10cSrcweir 		{
732*cdf0e10cSrcweir 			Mutex aMutex;
733*cdf0e10cSrcweir 
734*cdf0e10cSrcweir 			/// now, the aMutex has been guarded, this time, we use reference constructor.
735*cdf0e10cSrcweir 			ClearableMutexGuard myMutexGuard( aMutex );
736*cdf0e10cSrcweir 
737*cdf0e10cSrcweir 			/// it will return sal_False if the aMutex has not been Guarded.
738*cdf0e10cSrcweir 			sal_Bool bRes = aMutex.release( );
739*cdf0e10cSrcweir 
740*cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE("ClearableMutexGuard constructor, test the aquire operation when initilized, we use reference constructor this time.",
741*cdf0e10cSrcweir 				bRes == sal_True );
742*cdf0e10cSrcweir 		}
743*cdf0e10cSrcweir 
744*cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE(ctor);
745*cdf0e10cSrcweir 		CPPUNIT_TEST(ctor_001);
746*cdf0e10cSrcweir 		CPPUNIT_TEST(ctor_002);
747*cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE_END();
748*cdf0e10cSrcweir 	}; // class ctor
749*cdf0e10cSrcweir 
750*cdf0e10cSrcweir 	class clear : public CppUnit::TestFixture
751*cdf0e10cSrcweir 	{
752*cdf0e10cSrcweir 	public:
753*cdf0e10cSrcweir 		void clear_001()
754*cdf0e10cSrcweir 		{
755*cdf0e10cSrcweir 			Mutex aMutex;
756*cdf0e10cSrcweir 			ClearGuardThread myThread(&aMutex);
757*cdf0e10cSrcweir 			myThread.create();
758*cdf0e10cSrcweir 
759*cdf0e10cSrcweir 			TimeValue aTimeVal_befor;
760*cdf0e10cSrcweir 			osl_getSystemTime( &aTimeVal_befor );
761*cdf0e10cSrcweir 			// wait 1 second to assure the child thread has begun
762*cdf0e10cSrcweir 			ThreadHelper::thread_sleep(1);
763*cdf0e10cSrcweir 
764*cdf0e10cSrcweir 			while (1)
765*cdf0e10cSrcweir 			{
766*cdf0e10cSrcweir 				if (aMutex.tryToAcquire() == sal_True)
767*cdf0e10cSrcweir                 {
768*cdf0e10cSrcweir                     break;
769*cdf0e10cSrcweir                 }
770*cdf0e10cSrcweir                 ThreadHelper::thread_sleep(1);
771*cdf0e10cSrcweir 			}
772*cdf0e10cSrcweir 			TimeValue aTimeVal_after;
773*cdf0e10cSrcweir 			osl_getSystemTime( &aTimeVal_after );
774*cdf0e10cSrcweir 			sal_Int32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
775*cdf0e10cSrcweir             printf("nSec is %"SAL_PRIdINT32"\n", nSec);
776*cdf0e10cSrcweir 
777*cdf0e10cSrcweir 			myThread.join();
778*cdf0e10cSrcweir 
779*cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE("ClearableGuard method: clear",
780*cdf0e10cSrcweir 				nSec < 7 && nSec > 1);
781*cdf0e10cSrcweir 		}
782*cdf0e10cSrcweir 
783*cdf0e10cSrcweir 		void clear_002( )
784*cdf0e10cSrcweir 		{
785*cdf0e10cSrcweir 			Mutex aMutex;
786*cdf0e10cSrcweir 
787*cdf0e10cSrcweir 			/// now, the aMutex has been guarded.
788*cdf0e10cSrcweir 			ClearableMutexGuard myMutexGuard( &aMutex );
789*cdf0e10cSrcweir 
790*cdf0e10cSrcweir 			/// launch the HoldThread, it will be blocked here.
791*cdf0e10cSrcweir 			HoldThread myThread( &aMutex );
792*cdf0e10cSrcweir 			myThread.create( );
793*cdf0e10cSrcweir 
794*cdf0e10cSrcweir 			/// is it blocking?
795*cdf0e10cSrcweir 			ThreadHelper::thread_sleep_tenth_sec( 4 );
796*cdf0e10cSrcweir 			sal_Bool bRes = myThread.isRunning( );
797*cdf0e10cSrcweir 
798*cdf0e10cSrcweir 			/// use clear to release.
799*cdf0e10cSrcweir 			myMutexGuard.clear( );
800*cdf0e10cSrcweir 			myThread.join( );
801*cdf0e10cSrcweir 			sal_Bool bRes1 = myThread.isRunning( );
802*cdf0e10cSrcweir 
803*cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE( "ClearableGuard method: clear, control the HoldThread's running status!",
804*cdf0e10cSrcweir 				( sal_True == bRes ) && ( sal_False == bRes1 ) );
805*cdf0e10cSrcweir 		}
806*cdf0e10cSrcweir 
807*cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE( clear );
808*cdf0e10cSrcweir 		CPPUNIT_TEST( clear_001 );
809*cdf0e10cSrcweir 		CPPUNIT_TEST( clear_002 );
810*cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE_END( );
811*cdf0e10cSrcweir 	}; // class clear
812*cdf0e10cSrcweir 
813*cdf0e10cSrcweir // -----------------------------------------------------------------------------
814*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( osl_ClearableGuard::ctor, "osl_ClearableGuard" );
815*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( osl_ClearableGuard::clear, "osl_ClearableGuard" );
816*cdf0e10cSrcweir } // namespace osl_ClearableGuard
817*cdf0e10cSrcweir 
818*cdf0e10cSrcweir 
819*cdf0e10cSrcweir //------------------------------------------------------------------------
820*cdf0e10cSrcweir // Beginning of the test cases for osl_ResettableGuard class
821*cdf0e10cSrcweir //------------------------------------------------------------------------
822*cdf0e10cSrcweir 
823*cdf0e10cSrcweir /** Thread for test ResettableGuard
824*cdf0e10cSrcweir  */
825*cdf0e10cSrcweir class ResetGuardThread : public Thread
826*cdf0e10cSrcweir {
827*cdf0e10cSrcweir public:
828*cdf0e10cSrcweir 	//get the Mutex pointer to operate
829*cdf0e10cSrcweir 	ResetGuardThread( Mutex* pMutex ): pMyMutex( pMutex ) {}
830*cdf0e10cSrcweir 
831*cdf0e10cSrcweir 	~ResetGuardThread( )
832*cdf0e10cSrcweir 	{
833*cdf0e10cSrcweir 		CPPUNIT_ASSERT_MESSAGE( "#ResetGuardThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
834*cdf0e10cSrcweir 	}
835*cdf0e10cSrcweir protected:
836*cdf0e10cSrcweir 	Mutex* pMyMutex;
837*cdf0e10cSrcweir 
838*cdf0e10cSrcweir 	void SAL_CALL run( )
839*cdf0e10cSrcweir 	{
840*cdf0e10cSrcweir 		// acquire the mutex
841*cdf0e10cSrcweir 		printf("# ResettableGuard\n" );
842*cdf0e10cSrcweir 		ResettableMutexGuard aGuard( pMyMutex );
843*cdf0e10cSrcweir 		// release the mutex
844*cdf0e10cSrcweir 		aGuard.clear( );
845*cdf0e10cSrcweir 		ThreadHelper::thread_sleep_tenth_sec( 2 );
846*cdf0e10cSrcweir 	}
847*cdf0e10cSrcweir };
848*cdf0e10cSrcweir 
849*cdf0e10cSrcweir // -----------------------------------------------------------------------------
850*cdf0e10cSrcweir namespace osl_ResettableGuard
851*cdf0e10cSrcweir {
852*cdf0e10cSrcweir 	class ctor : public CppUnit::TestFixture
853*cdf0e10cSrcweir 	{
854*cdf0e10cSrcweir 	public:
855*cdf0e10cSrcweir 		void ctor_001()
856*cdf0e10cSrcweir 		{
857*cdf0e10cSrcweir 			Mutex aMutex;
858*cdf0e10cSrcweir 
859*cdf0e10cSrcweir 			/// now, the aMutex has been guarded.
860*cdf0e10cSrcweir 			ResettableMutexGuard myMutexGuard( &aMutex );
861*cdf0e10cSrcweir 
862*cdf0e10cSrcweir 			/// it will return sal_False if the aMutex has not been Guarded.
863*cdf0e10cSrcweir 			sal_Bool bRes = aMutex.release( );
864*cdf0e10cSrcweir 
865*cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE("ResettableMutexGuard constructor, test the aquire operation when initilized.",
866*cdf0e10cSrcweir 				bRes == sal_True );
867*cdf0e10cSrcweir 		}
868*cdf0e10cSrcweir 
869*cdf0e10cSrcweir 		void ctor_002( )
870*cdf0e10cSrcweir 		{
871*cdf0e10cSrcweir 			Mutex aMutex;
872*cdf0e10cSrcweir 
873*cdf0e10cSrcweir 			/// now, the aMutex has been guarded, this time, we use reference constructor.
874*cdf0e10cSrcweir 			ResettableMutexGuard myMutexGuard( aMutex );
875*cdf0e10cSrcweir 
876*cdf0e10cSrcweir 			/// it will return sal_False if the aMutex has not been Guarded.
877*cdf0e10cSrcweir 			sal_Bool bRes = aMutex.release( );
878*cdf0e10cSrcweir 
879*cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE( "ResettableMutexGuard constructor, test the aquire operation when initilized, we use reference constructor this time.",
880*cdf0e10cSrcweir 				bRes == sal_True );
881*cdf0e10cSrcweir 		}
882*cdf0e10cSrcweir 
883*cdf0e10cSrcweir 
884*cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE(ctor);
885*cdf0e10cSrcweir 		CPPUNIT_TEST(ctor_001);
886*cdf0e10cSrcweir 		CPPUNIT_TEST(ctor_002);
887*cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE_END();
888*cdf0e10cSrcweir 	}; // class ctor
889*cdf0e10cSrcweir 
890*cdf0e10cSrcweir 	class reset : public CppUnit::TestFixture
891*cdf0e10cSrcweir 	{
892*cdf0e10cSrcweir 	public:
893*cdf0e10cSrcweir 		void reset_001( )
894*cdf0e10cSrcweir 		{
895*cdf0e10cSrcweir 			Mutex aMutex;
896*cdf0e10cSrcweir 			ResetGuardThread myThread( &aMutex );
897*cdf0e10cSrcweir 			ResettableMutexGuard myMutexGuard( aMutex );
898*cdf0e10cSrcweir 			myThread.create( );
899*cdf0e10cSrcweir 
900*cdf0e10cSrcweir 			/// is it running? and clear done?
901*cdf0e10cSrcweir 			sal_Bool bRes = myThread.isRunning( );
902*cdf0e10cSrcweir 			myMutexGuard.clear( );
903*cdf0e10cSrcweir 			ThreadHelper::thread_sleep_tenth_sec( 1 );
904*cdf0e10cSrcweir 
905*cdf0e10cSrcweir 			/// if reset is not success, the release will return sal_False
906*cdf0e10cSrcweir 			myMutexGuard.reset( );
907*cdf0e10cSrcweir 			sal_Bool bRes1 = aMutex.release( );
908*cdf0e10cSrcweir 			myThread.join( );
909*cdf0e10cSrcweir 
910*cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE( "ResettableMutexGuard method: reset",
911*cdf0e10cSrcweir 				( sal_True == bRes ) && ( sal_True == bRes1 ) );
912*cdf0e10cSrcweir 		}
913*cdf0e10cSrcweir 
914*cdf0e10cSrcweir 		void reset_002( )
915*cdf0e10cSrcweir 		{
916*cdf0e10cSrcweir 			Mutex aMutex;
917*cdf0e10cSrcweir 			ResettableMutexGuard myMutexGuard( &aMutex );
918*cdf0e10cSrcweir 
919*cdf0e10cSrcweir 			/// shouldn't release after clear;
920*cdf0e10cSrcweir 			myMutexGuard.clear( );
921*cdf0e10cSrcweir 			sal_Bool bRes = aMutex.release( );
922*cdf0e10cSrcweir 
923*cdf0e10cSrcweir 			/// can release after reset.
924*cdf0e10cSrcweir 			myMutexGuard.reset( );
925*cdf0e10cSrcweir 			sal_Bool bRes1 = aMutex.release( );
926*cdf0e10cSrcweir 
927*cdf0e10cSrcweir 			CPPUNIT_ASSERT_MESSAGE( "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",
928*cdf0e10cSrcweir 				( sal_False == bRes ) && ( sal_True == bRes1 ) );
929*cdf0e10cSrcweir 		}
930*cdf0e10cSrcweir 
931*cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE(reset);
932*cdf0e10cSrcweir 		CPPUNIT_TEST(reset_001);
933*cdf0e10cSrcweir #ifdef LINUX
934*cdf0e10cSrcweir 		CPPUNIT_TEST(reset_002);
935*cdf0e10cSrcweir #endif
936*cdf0e10cSrcweir 		CPPUNIT_TEST_SUITE_END();
937*cdf0e10cSrcweir 	}; // class reset
938*cdf0e10cSrcweir 
939*cdf0e10cSrcweir // -----------------------------------------------------------------------------
940*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_REGISTRATION(osl_ResettableGuard::ctor);
941*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_REGISTRATION(osl_ResettableGuard::reset);
942*cdf0e10cSrcweir } // namespace osl_ResettableGuard
943*cdf0e10cSrcweir 
944*cdf0e10cSrcweir CPPUNIT_PLUGIN_IMPLEMENT();
945*cdf0e10cSrcweir 
946*cdf0e10cSrcweir // The following sets variables for GNU EMACS
947*cdf0e10cSrcweir // Local Variables:
948*cdf0e10cSrcweir // tab-width:4
949*cdf0e10cSrcweir // End:
950