update dev300-m57
[ooovba.git] / sal / qa / osl / mutex / osl_Mutex.cxx
blobf419e0130baf3a68aaa57041b2a200d7f8a8a8cc
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: osl_Mutex.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sal.hxx"
34 //------------------------------------------------------------------------
35 // include files
36 //------------------------------------------------------------------------
37 #include <osl_Mutex_Const.h>
39 using namespace osl;
40 using namespace rtl;
42 //------------------------------------------------------------------------
43 // helper functions
44 //------------------------------------------------------------------------
46 /** print a UNI_CODE String.
48 inline void printUString( const ::rtl::OUString & str )
50 rtl::OString aString;
52 t_print("#printUString_u# " );
53 aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
54 t_print("%s\n", aString.getStr( ) );
57 /** print Boolean value.
59 inline void printBool( sal_Bool bOk )
61 t_print("#printBool# " );
62 ( sal_True == bOk ) ? t_print("YES!\n" ): t_print("NO!\n" );
65 /** pause nSec seconds helper function.
67 namespace ThreadHelper
69 void thread_sleep( sal_Int32 _nSec )
71 /// print statement in thread process must use fflush() to force display.
72 // t_print("# wait %d seconds. ", _nSec );
73 fflush(stdout);
75 #ifdef WNT //Windows
76 Sleep( _nSec * 1000 );
77 #endif
78 #if ( defined UNX ) || ( defined OS2 ) //Unix
79 sleep( _nSec );
80 #endif
81 // t_print("# done\n" );
83 void thread_sleep_tenth_sec(sal_Int32 _nTenthSec)
85 #ifdef WNT //Windows
86 Sleep(_nTenthSec * 100 );
87 #endif
88 #if ( defined UNX ) || ( defined OS2 ) //Unix
89 TimeValue nTV;
90 nTV.Seconds = static_cast<sal_uInt32>( _nTenthSec/10 );
91 nTV.Nanosec = ( (_nTenthSec%10 ) * 100000000 );
92 osl_waitThread(&nTV);
93 #endif
98 //------------------------------------------------------------------------
99 // Beginning of the test cases for osl_Mutex class
100 //------------------------------------------------------------------------
103 /** mutually exclusive data
105 struct resource {
106 sal_Int32 data1;
107 sal_Int32 data2;
108 Mutex lock;
111 /** IncreaseThread provide data.
113 class IncreaseThread : public Thread
115 public:
116 IncreaseThread( struct resource *pData ): pResource( pData ) { }
118 ~IncreaseThread( )
120 CPPUNIT_ASSERT_MESSAGE( "#IncreaseThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
122 protected:
123 struct resource *pResource;
125 void SAL_CALL run( )
127 pResource->lock.acquire( );
128 for( sal_Int8 i = 0; i < 3; i++ )
130 pResource->data1++;
131 yield( ); //yield() give CPU time to other thread, other thread if not block, they will change the data;
133 if ( pResource->data2 == 0 )
134 pResource->data2 = ( pResource->data1 > 0 ? pResource->data1 : 0 - pResource->data1 );
135 pResource->lock.release();
139 /** DecreaseThread consume data.
141 class DecreaseThread : public Thread
143 public:
144 DecreaseThread( struct resource *pData ): pResource( pData ) { }
146 ~DecreaseThread( )
148 CPPUNIT_ASSERT_MESSAGE( "#DecreaseThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
150 protected:
151 struct resource *pResource;
153 void SAL_CALL run( )
155 pResource->lock.acquire( );
156 for( sal_Int8 i = 0; i < 3; i++ )
158 pResource->data1--;
159 yield( ); //yield() give CPU time to other thread, other thread if not block, they will change the data;
161 if ( pResource->data2 == 0 )
162 pResource->data2 = ( pResource->data1 > 0 ? pResource->data1 : 0 - pResource->data1 );
163 pResource->lock.release();
168 /** chain structure used in Threads as critical resource
170 struct chain {
171 sal_Int32 buffer[ BUFFER_SIZE ];
172 Mutex lock;
173 sal_Int8 pos;
176 /** PutThread write to the chain structure in a mutex manner.
178 class PutThread : public Thread
180 public:
181 //get the struct pointer to write data to buffer
182 PutThread( struct chain* pData ): pChain( pData ) { }
184 ~PutThread( )
186 CPPUNIT_ASSERT_MESSAGE( "#PutThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
188 protected:
189 struct chain* pChain;
191 void SAL_CALL run( )
193 //block here if the mutex has been acquired
194 pChain->lock.acquire( );
196 //current position in buffer to write
197 sal_Int8 nPos = pChain->pos;
198 oslThreadIdentifier oId = getIdentifier( );
199 //write data
200 sal_Int8 i;
201 for ( i = 0; i < 5; i++ )
203 pChain->buffer[ nPos + i ] = oId;
204 yield( );
206 //revise the position
207 pChain->pos = nPos + i;
209 //finish writing, release the mutex
210 pChain->lock.release();
214 /** thread for testing Mutex acquire.
216 class HoldThread : public Thread
218 public:
219 //get the Mutex pointer to operate
220 HoldThread( Mutex* pMutex ): pMyMutex( pMutex ) { }
222 ~HoldThread( )
224 CPPUNIT_ASSERT_MESSAGE( "#HoldThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
226 protected:
227 Mutex* pMyMutex;
229 void SAL_CALL run()
231 // block here if the mutex has been acquired
232 pMyMutex->acquire( );
233 t_print("# Mutex acquired. \n" );
234 pMyMutex->release( );
238 class WaitThread : public Thread
240 public:
241 //get the Mutex pointer to operate
242 WaitThread( Mutex* pMutex ): pMyMutex( pMutex ) { }
244 ~WaitThread( )
246 CPPUNIT_ASSERT_MESSAGE( "#WaitThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
248 protected:
249 Mutex* pMyMutex;
251 void SAL_CALL run( )
253 // block here if the mutex has been acquired
254 pMyMutex->acquire( );
255 ThreadHelper::thread_sleep_tenth_sec( 2 );
256 pMyMutex->release( );
260 /** thread for testing getGlobalMutex.
262 class GlobalMutexThread : public Thread
264 public:
265 //get the Mutex pointer to operate
266 GlobalMutexThread( ){ }
268 ~GlobalMutexThread( )
270 CPPUNIT_ASSERT_MESSAGE( "#GlobalMutexThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
272 protected:
273 void SAL_CALL run( )
275 // block here if the mutex has been acquired
276 Mutex* pGlobalMutex;
277 pGlobalMutex = pGlobalMutex->getGlobalMutex( );
278 pGlobalMutex->acquire( );
279 t_print("# Global Mutex acquired. \n" );
280 pGlobalMutex->release( );
285 //--------------------------------------------------------------
286 namespace osl_Mutex
289 /** Test of the osl::Mutex::constructor
291 class ctor : public CppUnit::TestFixture
293 public:
294 // initialise your test code values here.
295 struct chain m_Data;
296 struct resource m_Res;
298 void setUp( )
300 for ( sal_Int8 i=0; i < BUFFER_SIZE; i++ )
301 m_Data.buffer[i] = 0;
302 m_Data.pos = 0;
304 m_Res.data1 = 0;
305 m_Res.data2 = 0;
308 void tearDown()
312 /** Create two threads to write data to the same buffer, use Mutex to assure
313 during one thread write data five times, the other thread should not begin writing.
314 the two threads wrote two different datas: their thread ID, so we can check the datas
315 in buffer to know the order of the two threads writing
317 void ctor_001()
319 PutThread myThread1( &m_Data );
320 PutThread myThread2( &m_Data );
322 myThread1.create( );
323 myThread2.create( );
325 //wait until the two threads terminate
326 myThread1.join( );
327 myThread2.join( );
329 sal_Bool bRes = sal_False;
331 // every 5 datas should the same
332 // LLA: this is not a good check, it's too fix
333 if (m_Data.buffer[0] == m_Data.buffer[1] &&
334 m_Data.buffer[1] == m_Data.buffer[2] &&
335 m_Data.buffer[2] == m_Data.buffer[3] &&
336 m_Data.buffer[3] == m_Data.buffer[4] &&
337 m_Data.buffer[5] == m_Data.buffer[6] &&
338 m_Data.buffer[6] == m_Data.buffer[7] &&
339 m_Data.buffer[7] == m_Data.buffer[8] &&
340 m_Data.buffer[8] == m_Data.buffer[9])
341 bRes = sal_True;
343 /*for (sal_Int8 i=0; i<BUFFER_SIZE; i++)
344 t_print("#data in buffer is %d\n", m_Data.buffer[i]);
347 CPPUNIT_ASSERT_MESSAGE("Mutex ctor", bRes == sal_True);
351 /** Create two threads to write data to operate on the same number , use Mutex to assure,
352 one thread increase data 3 times, the other thread decrease 3 times, store the operate
353 result when the first thread complete, if it is interrupt by the other thread, the stored
354 number will not be 3.
356 void ctor_002()
358 IncreaseThread myThread1( &m_Res );
359 DecreaseThread myThread2( &m_Res );
361 myThread1.create( );
362 myThread2.create( );
364 //wait until the two threads terminate
365 myThread1.join( );
366 myThread2.join( );
368 sal_Bool bRes = sal_False;
370 // every 5 datas should the same
371 if ( ( m_Res.data1 == 0 ) && ( m_Res.data2 == 3 ) )
372 bRes = sal_True;
374 CPPUNIT_ASSERT_MESSAGE( "test Mutex ctor function: increase and decrease a number 3 times without interrupt.", bRes == sal_True );
377 CPPUNIT_TEST_SUITE( ctor );
378 CPPUNIT_TEST( ctor_001 );
379 CPPUNIT_TEST( ctor_002 );
380 CPPUNIT_TEST_SUITE_END( );
381 }; // class ctor
384 /** Test of the osl::Mutex::acquire method
386 class acquire : public CppUnit::TestFixture
388 public:
389 // acquire mutex in main thread, and then call acquire again in myThread,
390 // the child thread should block, wait 2 secs, it still block.
391 // Then release mutex in main thread, the child thread could return from acquire,
392 // and go to exec next statement, so could terminate quickly.
393 void acquire_001( )
395 Mutex aMutex;
396 //acquire here
397 sal_Bool bRes = aMutex.acquire( );
398 // pass the pointer of mutex to child thread
399 HoldThread myThread( &aMutex );
400 myThread.create( );
402 ThreadHelper::thread_sleep_tenth_sec( 2 );
403 // if acquire in myThread does not work, 2 secs is long enough,
404 // myThread should terminate now, and bRes1 should be sal_False
405 sal_Bool bRes1 = myThread.isRunning( );
407 aMutex.release( );
408 ThreadHelper::thread_sleep_tenth_sec( 1 );
409 // after release mutex, myThread stops blocking and will terminate immediately
410 sal_Bool bRes2 = myThread.isRunning( );
411 myThread.join( );
413 CPPUNIT_ASSERT_MESSAGE( "Mutex acquire",
414 bRes == sal_True && bRes1 == sal_True && bRes2 == sal_False );
417 //in the same thread, acquire twice should success
418 void acquire_002()
420 Mutex aMutex;
421 //acquire here
422 sal_Bool bRes = aMutex.acquire();
423 sal_Bool bRes1 = aMutex.acquire();
425 sal_Bool bRes2 = aMutex.tryToAcquire();
427 aMutex.release();
429 CPPUNIT_ASSERT_MESSAGE("Mutex acquire",
430 bRes == sal_True && bRes1 == sal_True && bRes2 == sal_True);
434 CPPUNIT_TEST_SUITE( acquire );
435 CPPUNIT_TEST( acquire_001 );
436 CPPUNIT_TEST( acquire_002 );
437 CPPUNIT_TEST_SUITE_END( );
438 }; // class acquire
441 /** Test of the osl::Mutex::tryToAcquire method
443 class tryToAcquire : public CppUnit::TestFixture
445 public:
446 // First let child thread acquire the mutex, and wait 2 secs, during the 2 secs,
447 // in main thread, tryToAcquire mutex should return False
448 // then after the child thread terminated, tryToAcquire should return True
449 void tryToAcquire_001()
451 Mutex aMutex;
452 WaitThread myThread(&aMutex);
453 myThread.create();
455 // ensure the child thread acquire the mutex
456 ThreadHelper::thread_sleep_tenth_sec(1);
458 sal_Bool bRes1 = aMutex.tryToAcquire();
460 if (bRes1 == sal_True)
461 aMutex.release();
462 // wait the child thread terminate
463 myThread.join();
465 sal_Bool bRes2 = aMutex.tryToAcquire();
467 if (bRes2 == sal_True)
468 aMutex.release();
470 CPPUNIT_ASSERT_MESSAGE("Try to acquire Mutex",
471 bRes1 == sal_False && bRes2 == sal_True);
474 CPPUNIT_TEST_SUITE(tryToAcquire);
475 CPPUNIT_TEST(tryToAcquire_001);
476 CPPUNIT_TEST_SUITE_END();
477 }; // class tryToAcquire
479 /** Test of the osl::Mutex::release method
481 class release : public CppUnit::TestFixture
483 public:
484 /** acquire/release are not used in pairs: after child thread acquired mutex,
485 the main thread release it, then any thread could acquire it.
487 void release_001()
489 Mutex aMutex;
490 WaitThread myThread( &aMutex );
491 myThread.create( );
493 // ensure the child thread acquire the mutex
494 ThreadHelper::thread_sleep_tenth_sec( 1 );
496 sal_Bool bRunning = myThread.isRunning( );
497 sal_Bool bRes1 = aMutex.tryToAcquire( );
498 // wait the child thread terminate
499 myThread.join( );
501 sal_Bool bRes2 = aMutex.tryToAcquire( );
503 if ( bRes2 == sal_True )
504 aMutex.release( );
506 CPPUNIT_ASSERT_MESSAGE( "release Mutex: try to aquire before and after the mutex has been released",
507 bRes1 == sal_False && bRes2 == sal_True && bRunning == sal_True );
511 // how about release twice?
512 void release_002()
514 // LLA: is this a real test?
515 #if 0
516 Mutex aMutex;
517 sal_Bool bRes1 = aMutex.release( );
518 sal_Bool bRes2 = aMutex.release( );
520 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)",
521 bRes1 == sal_False && bRes2 == sal_False );
522 #endif
525 CPPUNIT_TEST_SUITE( release );
526 CPPUNIT_TEST( release_001 );
527 CPPUNIT_TEST( release_002 );
528 CPPUNIT_TEST_SUITE_END( );
529 }; // class release
533 /** Test of the osl::Mutex::getGlobalMutex method
535 class getGlobalMutex : public CppUnit::TestFixture
537 public:
538 // initialise your test code values here.
539 void getGlobalMutex_001()
541 Mutex* pGlobalMutex;
542 pGlobalMutex = pGlobalMutex->getGlobalMutex();
543 pGlobalMutex->acquire();
545 GlobalMutexThread myThread;
546 myThread.create();
548 ThreadHelper::thread_sleep_tenth_sec(1);
549 sal_Bool bRes1 = myThread.isRunning();
551 pGlobalMutex->release();
552 ThreadHelper::thread_sleep_tenth_sec(1);
553 // after release mutex, myThread stops blocking and will terminate immediately
554 sal_Bool bRes2 = myThread.isRunning();
556 CPPUNIT_ASSERT_MESSAGE("Global Mutex works",
557 bRes1 == sal_True && bRes2 == sal_False);
560 void getGlobalMutex_002( )
562 sal_Bool bRes;
564 Mutex *pGlobalMutex;
565 pGlobalMutex = pGlobalMutex->getGlobalMutex( );
566 pGlobalMutex->acquire( );
568 Mutex *pGlobalMutex1;
569 pGlobalMutex1 = pGlobalMutex1->getGlobalMutex( );
570 bRes = pGlobalMutex1->release( );
573 CPPUNIT_ASSERT_MESSAGE( "Global Mutex works: if the code between {} get the different mutex as the former one, it will return false when release.",
574 bRes == sal_True );
577 CPPUNIT_TEST_SUITE(getGlobalMutex);
578 CPPUNIT_TEST(getGlobalMutex_001);
579 CPPUNIT_TEST(getGlobalMutex_002);
580 CPPUNIT_TEST_SUITE_END();
581 }; // class getGlobalMutex
583 // -----------------------------------------------------------------------------
584 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Mutex::ctor, "osl_Mutex");
585 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Mutex::acquire, "osl_Mutex");
586 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Mutex::tryToAcquire, "osl_Mutex");
587 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Mutex::release, "osl_Mutex");
588 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Mutex::getGlobalMutex, "osl_Mutex");
589 } // namespace osl_Mutex
592 //------------------------------------------------------------------------
593 // Beginning of the test cases for osl_Guard class
594 //------------------------------------------------------------------------
596 class GuardThread : public Thread
598 public:
599 //get the Mutex pointer to operate
600 GuardThread( Mutex* pMutex ): pMyMutex( pMutex ) { }
602 ~GuardThread( )
604 CPPUNIT_ASSERT_MESSAGE( "#GuardThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
606 protected:
607 Mutex* pMyMutex;
609 void SAL_CALL run( )
611 // block here if the mutex has been acquired
612 MutexGuard aGuard( pMyMutex );
613 ThreadHelper::thread_sleep_tenth_sec( 2 );
618 namespace osl_Guard
620 class ctor : public CppUnit::TestFixture
622 public:
623 // insert your test code here.
624 void ctor_001()
626 Mutex aMutex;
627 GuardThread myThread(&aMutex);
628 myThread.create();
630 ThreadHelper::thread_sleep_tenth_sec(1);
631 sal_Bool bRes = aMutex.tryToAcquire();
632 // after 1 second, the mutex has been guarded, and the child thread should be running
633 sal_Bool bRes1 = myThread.isRunning();
635 myThread.join();
636 sal_Bool bRes2 = aMutex.tryToAcquire();
638 CPPUNIT_ASSERT_MESSAGE("GuardThread constructor",
639 bRes == sal_False && bRes1 == sal_True && bRes2 == sal_True);
642 void ctor_002( )
644 Mutex aMutex;
646 /// use reference constructor here
647 MutexGuard myGuard( aMutex );
649 /// the GuardThread will block here when it is initialised.
650 GuardThread myThread( &aMutex );
651 myThread.create( );
653 /// is it still blocking?
654 ThreadHelper::thread_sleep_tenth_sec( 2 );
655 sal_Bool bRes = myThread.isRunning( );
657 /// oh, release him.
658 aMutex.release( );
659 myThread.join( );
661 CPPUNIT_ASSERT_MESSAGE("GuardThread constructor: reference initialization, aquire the mutex before running the thread, then check if it is blocking.",
662 bRes == sal_True);
665 CPPUNIT_TEST_SUITE(ctor);
666 CPPUNIT_TEST(ctor_001);
667 CPPUNIT_TEST(ctor_002);
668 CPPUNIT_TEST_SUITE_END();
669 }; // class ctor
671 // -----------------------------------------------------------------------------
672 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Guard::ctor, "osl_Guard");
673 } // namespace osl_Guard
676 //------------------------------------------------------------------------
677 // Beginning of the test cases for osl_ClearableGuard class
678 //------------------------------------------------------------------------
680 /** Thread for test ClearableGuard
682 class ClearGuardThread : public Thread
684 public:
685 //get the Mutex pointer to operate
686 ClearGuardThread( Mutex* pMutex ): pMyMutex( pMutex ) {}
688 ~ClearGuardThread( )
690 CPPUNIT_ASSERT_MESSAGE( "#ClearGuardThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
692 protected:
693 Mutex* pMyMutex;
695 void SAL_CALL run( )
697 // acquire the mutex
698 // t_print("# ClearGuardThread" );
699 ClearableMutexGuard aGuard( pMyMutex );
700 ThreadHelper::thread_sleep( 5 );
702 // release the mutex
703 aGuard.clear( );
704 ThreadHelper::thread_sleep( 2 );
708 // -----------------------------------------------------------------------------
709 namespace osl_ClearableGuard
712 class ctor : public CppUnit::TestFixture
714 public:
715 void ctor_001()
717 Mutex aMutex;
719 /// now, the aMutex has been guarded.
720 ClearableMutexGuard myMutexGuard( &aMutex );
722 /// it will return sal_False if the aMutex has not been Guarded.
723 sal_Bool bRes = aMutex.release( );
725 CPPUNIT_ASSERT_MESSAGE("ClearableMutexGuard constructor, test the aquire operation when initilized.",
726 bRes == sal_True );
729 void ctor_002( )
731 Mutex aMutex;
733 /// now, the aMutex has been guarded, this time, we use reference constructor.
734 ClearableMutexGuard myMutexGuard( aMutex );
736 /// it will return sal_False if the aMutex has not been Guarded.
737 sal_Bool bRes = aMutex.release( );
739 CPPUNIT_ASSERT_MESSAGE("ClearableMutexGuard constructor, test the aquire operation when initilized, we use reference constructor this time.",
740 bRes == sal_True );
743 CPPUNIT_TEST_SUITE(ctor);
744 CPPUNIT_TEST(ctor_001);
745 CPPUNIT_TEST(ctor_002);
746 CPPUNIT_TEST_SUITE_END();
747 }; // class ctor
749 class clear : public CppUnit::TestFixture
751 public:
752 void clear_001()
754 Mutex aMutex;
755 ClearGuardThread myThread(&aMutex);
756 myThread.create();
758 TimeValue aTimeVal_befor;
759 osl_getSystemTime( &aTimeVal_befor );
760 // wait 1 second to assure the child thread has begun
761 ThreadHelper::thread_sleep(1);
763 while (1)
765 if (aMutex.tryToAcquire() == sal_True)
767 break;
769 ThreadHelper::thread_sleep(1);
771 TimeValue aTimeVal_after;
772 osl_getSystemTime( &aTimeVal_after );
773 sal_Int32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
774 t_print("nSec is %d\n", nSec);
776 myThread.join();
778 CPPUNIT_ASSERT_MESSAGE("ClearableGuard method: clear",
779 nSec < 7 && nSec > 1);
782 void clear_002( )
784 Mutex aMutex;
786 /// now, the aMutex has been guarded.
787 ClearableMutexGuard myMutexGuard( &aMutex );
789 /// launch the HoldThread, it will be blocked here.
790 HoldThread myThread( &aMutex );
791 myThread.create( );
793 /// is it blocking?
794 ThreadHelper::thread_sleep_tenth_sec( 4 );
795 sal_Bool bRes = myThread.isRunning( );
797 /// use clear to release.
798 myMutexGuard.clear( );
799 myThread.join( );
800 sal_Bool bRes1 = myThread.isRunning( );
802 CPPUNIT_ASSERT_MESSAGE( "ClearableGuard method: clear, control the HoldThread's running status!",
803 ( sal_True == bRes ) && ( sal_False == bRes1 ) );
806 CPPUNIT_TEST_SUITE( clear );
807 CPPUNIT_TEST( clear_001 );
808 CPPUNIT_TEST( clear_002 );
809 CPPUNIT_TEST_SUITE_END( );
810 }; // class clear
812 // -----------------------------------------------------------------------------
813 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( osl_ClearableGuard::ctor, "osl_ClearableGuard" );
814 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( osl_ClearableGuard::clear, "osl_ClearableGuard" );
815 } // namespace osl_ClearableGuard
818 //------------------------------------------------------------------------
819 // Beginning of the test cases for osl_ResettableGuard class
820 //------------------------------------------------------------------------
822 /** Thread for test ResettableGuard
824 class ResetGuardThread : public Thread
826 public:
827 //get the Mutex pointer to operate
828 ResetGuardThread( Mutex* pMutex ): pMyMutex( pMutex ) {}
830 ~ResetGuardThread( )
832 CPPUNIT_ASSERT_MESSAGE( "#ResetGuardThread does not shutdown properly.\n", sal_False == this -> isRunning( ) );
834 protected:
835 Mutex* pMyMutex;
837 void SAL_CALL run( )
839 // acquire the mutex
840 t_print("# ResettableGuard" );
841 ResettableMutexGuard aGuard( pMyMutex );
842 // release the mutex
843 aGuard.clear( );
844 ThreadHelper::thread_sleep_tenth_sec( 2 );
848 // -----------------------------------------------------------------------------
849 namespace osl_ResettableGuard
851 class ctor : public CppUnit::TestFixture
853 public:
854 void ctor_001()
856 Mutex aMutex;
858 /// now, the aMutex has been guarded.
859 ResettableMutexGuard myMutexGuard( &aMutex );
861 /// it will return sal_False if the aMutex has not been Guarded.
862 sal_Bool bRes = aMutex.release( );
864 CPPUNIT_ASSERT_MESSAGE("ResettableMutexGuard constructor, test the aquire operation when initilized.",
865 bRes == sal_True );
868 void ctor_002( )
870 Mutex aMutex;
872 /// now, the aMutex has been guarded, this time, we use reference constructor.
873 ResettableMutexGuard myMutexGuard( aMutex );
875 /// it will return sal_False if the aMutex has not been Guarded.
876 sal_Bool bRes = aMutex.release( );
878 CPPUNIT_ASSERT_MESSAGE( "ResettableMutexGuard constructor, test the aquire operation when initilized, we use reference constructor this time.",
879 bRes == sal_True );
883 CPPUNIT_TEST_SUITE(ctor);
884 CPPUNIT_TEST(ctor_001);
885 CPPUNIT_TEST(ctor_002);
886 CPPUNIT_TEST_SUITE_END();
887 }; // class ctor
889 class reset : public CppUnit::TestFixture
891 public:
892 void reset_001( )
894 Mutex aMutex;
895 ResetGuardThread myThread( &aMutex );
896 myThread.create( );
897 ResettableMutexGuard myMutexGuard( aMutex );
899 /// is it running? and clear done?
900 myMutexGuard.clear( );
901 ThreadHelper::thread_sleep_tenth_sec( 1 );
902 sal_Bool bRes = myThread.isRunning( );
904 /// if reset is not success, the release will return sal_False
905 myMutexGuard.reset( );
906 sal_Bool bRes1 = aMutex.release( );
907 myThread.join( );
909 CPPUNIT_ASSERT_MESSAGE( "ResettableMutexGuard method: reset",
910 ( sal_True == bRes ) && ( sal_True == bRes1 ) );
913 void reset_002( )
915 Mutex aMutex;
916 ResettableMutexGuard myMutexGuard( &aMutex );
918 /// shouldn't release after clear;
919 myMutexGuard.clear( );
920 sal_Bool bRes = aMutex.release( );
922 /// can release after reset.
923 myMutexGuard.reset( );
924 sal_Bool bRes1 = aMutex.release( );
926 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",
927 ( sal_False == bRes ) && ( sal_True == bRes1 ) );
930 CPPUNIT_TEST_SUITE(reset);
931 CPPUNIT_TEST(reset_001);
932 #ifdef LINUX
933 CPPUNIT_TEST(reset_002);
934 #endif
935 CPPUNIT_TEST_SUITE_END();
936 }; // class reset
938 // -----------------------------------------------------------------------------
939 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_ResettableGuard::ctor, "osl_ResettableGuard");
940 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_ResettableGuard::reset, "osl_ResettableGuard");
941 } // namespace osl_ResettableGuard
943 // this macro creates an empty function, which will called by the RegisterAllFunctions()
944 // to let the user the possibility to also register some functions by hand.
945 NOADDITIONAL;
947 // The following sets variables for GNU EMACS
948 // Local Variables:
949 // tab-width:4
950 // End: