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