bump product version to 5.0.4.1
[LibreOffice.git] / sal / qa / osl / mutex / osl_Mutex.cxx
blob4c613b6bd22d36a3654bc4e051185373589e5dcc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 // include files
22 #include <sal/types.h>
23 #include "cppunit/TestAssert.h"
24 #include "cppunit/TestFixture.h"
25 #include "cppunit/extensions/HelperMacros.h"
26 #include "cppunit/plugin/TestPlugIn.h"
27 #include <osl_Mutex_Const.h>
29 using namespace osl;
31 /** pause nSec seconds helper function.
33 namespace ThreadHelper
35 void thread_sleep_tenth_sec(sal_uInt32 _nTenthSec)
37 TimeValue nTV;
38 nTV.Seconds = _nTenthSec/10;
39 nTV.Nanosec = ( (_nTenthSec%10 ) * 100000000 );
40 osl_waitThread(&nTV);
42 void thread_sleep( sal_uInt32 _nSec )
44 /// print statement in thread process must use fflush() to force display.
45 // t_print("# wait %d seconds. ", _nSec );
46 fflush(stdout);
48 thread_sleep_tenth_sec( _nSec * 10 );
49 // printf("# done\n" );
53 // Beginning of the test cases for osl_Mutex class
55 /** mutually exclusive data
57 struct resource {
58 sal_Int32 data1;
59 sal_Int32 data2;
60 Mutex lock;
63 /** IncreaseThread provide data.
65 class IncreaseThread : public Thread
67 public:
68 IncreaseThread( struct resource *pData ): pResource( pData ) { }
70 virtual ~IncreaseThread( )
72 CPPUNIT_ASSERT_MESSAGE( "#IncreaseThread does not shutdown properly.\n", !isRunning( ) );
74 protected:
75 struct resource *pResource;
77 void SAL_CALL run( ) SAL_OVERRIDE
79 pResource->lock.acquire( );
80 for( sal_Int8 i = 0; i < 3; i++ )
82 pResource->data1++;
83 yield( ); //yield() give CPU time to other thread, other thread if not block, they will change the data;
85 if ( pResource->data2 == 0 )
86 pResource->data2 = ( pResource->data1 > 0 ? pResource->data1 : 0 - pResource->data1 );
87 pResource->lock.release();
91 /** DecreaseThread consume data.
93 class DecreaseThread : public Thread
95 public:
96 DecreaseThread( struct resource *pData ): pResource( pData ) { }
98 virtual ~DecreaseThread( )
100 CPPUNIT_ASSERT_MESSAGE( "#DecreaseThread does not shutdown properly.\n", !isRunning( ) );
102 protected:
103 struct resource *pResource;
105 void SAL_CALL run( ) SAL_OVERRIDE
107 pResource->lock.acquire( );
108 for( sal_Int8 i = 0; i < 3; i++ )
110 pResource->data1--;
111 yield( ); //yield() give CPU time to other thread, other thread if not block, they will change the data;
113 if ( pResource->data2 == 0 )
114 pResource->data2 = ( pResource->data1 > 0 ? pResource->data1 : 0 - pResource->data1 );
115 pResource->lock.release();
119 /** chain structure used in Threads as critical resource
121 struct chain {
122 sal_Int32 buffer[ BUFFER_SIZE ];
123 Mutex lock;
124 sal_Int8 pos;
127 /** PutThread write to the chain structure in a mutex manner.
129 class PutThread : public Thread
131 public:
132 //get the struct pointer to write data to buffer
133 PutThread( struct chain* pData ): pChain( pData ) { }
135 virtual ~PutThread( )
137 CPPUNIT_ASSERT_MESSAGE( "#PutThread does not shutdown properly.\n", !isRunning( ) );
139 protected:
140 struct chain* pChain;
142 void SAL_CALL run( ) SAL_OVERRIDE
144 //block here if the mutex has been acquired
145 pChain->lock.acquire( );
147 //current position in buffer to write
148 sal_Int8 nPos = pChain->pos;
149 oslThreadIdentifier oId = getIdentifier( );
150 //write data
151 sal_Int8 i;
152 for ( i = 0; i < 5; i++ )
154 pChain->buffer[ nPos + i ] = oId;
155 yield( );
157 //revise the position
158 pChain->pos = nPos + i;
160 //finish writing, release the mutex
161 pChain->lock.release();
165 /** thread for testing Mutex acquire.
167 class HoldThread : public Thread
169 public:
170 //get the Mutex pointer to operate
171 HoldThread( Mutex* pMutex ): pMyMutex( pMutex ) { }
173 virtual ~HoldThread( )
175 CPPUNIT_ASSERT_MESSAGE( "#HoldThread does not shutdown properly.\n", !isRunning( ) );
177 protected:
178 Mutex* pMyMutex;
180 void SAL_CALL run() SAL_OVERRIDE
182 // block here if the mutex has been acquired
183 pMyMutex->acquire( );
184 printf("# Mutex acquired. \n" );
185 pMyMutex->release( );
189 class WaitThread : public Thread
191 public:
192 //get the Mutex pointer to operate
193 WaitThread( Mutex* pMutex ): pMyMutex( pMutex ) { }
195 virtual ~WaitThread( )
197 CPPUNIT_ASSERT_MESSAGE( "#WaitThread does not shutdown properly.\n", !isRunning( ) );
199 protected:
200 Mutex* pMyMutex;
202 void SAL_CALL run( ) SAL_OVERRIDE
204 // block here if the mutex has been acquired
205 pMyMutex->acquire( );
206 ThreadHelper::thread_sleep_tenth_sec( 2 );
207 pMyMutex->release( );
211 /** thread for testing getGlobalMutex.
213 class GlobalMutexThread : public Thread
215 public:
216 //get the Mutex pointer to operate
217 GlobalMutexThread( ){ }
219 virtual ~GlobalMutexThread( )
221 CPPUNIT_ASSERT_MESSAGE( "#GlobalMutexThread does not shutdown properly.\n", !isRunning( ) );
223 protected:
224 void SAL_CALL run( ) SAL_OVERRIDE
226 // block here if the mutex has been acquired
227 Mutex* pGlobalMutex;
228 pGlobalMutex = Mutex::getGlobalMutex( );
229 pGlobalMutex->acquire( );
230 printf("# Global Mutex acquired. \n" );
231 pGlobalMutex->release( );
235 namespace osl_Mutex
238 /** Test of the osl::Mutex::constructor
240 class ctor : public CppUnit::TestFixture
242 public:
243 // initialise your test code values here.
244 struct chain m_Data;
245 struct resource m_Res;
247 void setUp( ) SAL_OVERRIDE
249 for ( sal_Int8 i=0; i < BUFFER_SIZE; i++ )
250 m_Data.buffer[i] = 0;
251 m_Data.pos = 0;
253 m_Res.data1 = 0;
254 m_Res.data2 = 0;
257 void tearDown() SAL_OVERRIDE
261 /** Create two threads to write data to the same buffer, use Mutex to assure
262 during one thread write data five times, the other thread should not begin writing.
263 the two threads wrote two different datas: their thread ID, so we can check the datas
264 in buffer to know the order of the two threads writing
266 void ctor_001()
268 PutThread myThread1( &m_Data );
269 PutThread myThread2( &m_Data );
271 myThread1.create( );
272 myThread2.create( );
274 //wait until the two threads terminate
275 myThread1.join( );
276 myThread2.join( );
278 bool bRes = false;
280 // every 5 datas should the same
281 // LLA: this is not a good check, it's too fix
282 if (m_Data.buffer[0] == m_Data.buffer[1] &&
283 m_Data.buffer[1] == m_Data.buffer[2] &&
284 m_Data.buffer[2] == m_Data.buffer[3] &&
285 m_Data.buffer[3] == m_Data.buffer[4] &&
286 m_Data.buffer[5] == m_Data.buffer[6] &&
287 m_Data.buffer[6] == m_Data.buffer[7] &&
288 m_Data.buffer[7] == m_Data.buffer[8] &&
289 m_Data.buffer[8] == m_Data.buffer[9])
290 bRes = true;
292 /*for (sal_Int8 i=0; i<BUFFER_SIZE; i++)
293 printf("#data in buffer is %d\n", m_Data.buffer[i]);
296 CPPUNIT_ASSERT_MESSAGE("Mutex ctor", bRes);
300 /** Create two threads to write data to operate on the same number , use Mutex to assure,
301 one thread increase data 3 times, the other thread decrease 3 times, store the operate
302 result when the first thread complete, if it is interrupt by the other thread, the stored
303 number will not be 3.
305 void ctor_002()
307 IncreaseThread myThread1( &m_Res );
308 DecreaseThread myThread2( &m_Res );
310 myThread1.create( );
311 myThread2.create( );
313 //wait until the two threads terminate
314 myThread1.join( );
315 myThread2.join( );
317 bool bRes = false;
319 // every 5 datas should the same
320 if ( ( m_Res.data1 == 0 ) && ( m_Res.data2 == 3 ) )
321 bRes = true;
323 CPPUNIT_ASSERT_MESSAGE( "test Mutex ctor function: increase and decrease a number 3 times without interrupt.", bRes );
326 CPPUNIT_TEST_SUITE( ctor );
327 CPPUNIT_TEST( ctor_001 );
328 CPPUNIT_TEST( ctor_002 );
329 CPPUNIT_TEST_SUITE_END( );
330 }; // class ctor
332 /** Test of the osl::Mutex::acquire method
334 class acquire : public CppUnit::TestFixture
336 public:
337 // acquire mutex in main thread, and then call acquire again in myThread,
338 // the child thread should block, wait 2 secs, it still block.
339 // Then release mutex in main thread, the child thread could return from acquire,
340 // and go to exec next statement, so could terminate quickly.
341 void acquire_001( )
343 Mutex aMutex;
344 //acquire here
345 bool bRes = aMutex.acquire( );
346 // pass the pointer of mutex to child thread
347 HoldThread myThread( &aMutex );
348 myThread.create( );
350 ThreadHelper::thread_sleep_tenth_sec( 2 );
351 // if acquire in myThread does not work, 2 secs is long enough,
352 // myThread should terminate now, and bRes1 should be sal_False
353 bool bRes1 = myThread.isRunning( );
355 aMutex.release( );
356 ThreadHelper::thread_sleep_tenth_sec( 1 );
357 // after release mutex, myThread stops blocking and will terminate immediately
358 bool bRes2 = myThread.isRunning( );
359 myThread.join( );
361 CPPUNIT_ASSERT_MESSAGE( "Mutex acquire", bRes && bRes1 && !bRes2 );
364 //in the same thread, acquire twice should success
365 void acquire_002()
367 Mutex aMutex;
368 //acquire here
369 bool bRes = aMutex.acquire();
370 bool bRes1 = aMutex.acquire();
372 bool bRes2 = aMutex.tryToAcquire();
374 aMutex.release();
376 CPPUNIT_ASSERT_MESSAGE("Mutex acquire", bRes && bRes1 && bRes2);
380 CPPUNIT_TEST_SUITE( acquire );
381 CPPUNIT_TEST( acquire_001 );
382 CPPUNIT_TEST( acquire_002 );
383 CPPUNIT_TEST_SUITE_END( );
384 }; // class acquire
386 /** Test of the osl::Mutex::tryToAcquire method
388 class tryToAcquire : public CppUnit::TestFixture
390 public:
391 // First let child thread acquire the mutex, and wait 2 secs, during the 2 secs,
392 // in main thread, tryToAcquire mutex should return False
393 // then after the child thread terminated, tryToAcquire should return True
394 void tryToAcquire_001()
396 Mutex aMutex;
397 WaitThread myThread(&aMutex);
398 myThread.create();
400 // ensure the child thread acquire the mutex
401 ThreadHelper::thread_sleep_tenth_sec(1);
403 bool bRes1 = aMutex.tryToAcquire();
405 if (bRes1)
406 aMutex.release();
407 // wait the child thread terminate
408 myThread.join();
410 bool bRes2 = aMutex.tryToAcquire();
412 if (bRes2)
413 aMutex.release();
415 CPPUNIT_ASSERT_MESSAGE("Try to acquire Mutex", !bRes1 && bRes2);
418 CPPUNIT_TEST_SUITE(tryToAcquire);
419 CPPUNIT_TEST(tryToAcquire_001);
420 CPPUNIT_TEST_SUITE_END();
421 }; // class tryToAcquire
423 /** Test of the osl::Mutex::release method
425 class release : public CppUnit::TestFixture
427 public:
428 /** acquire/release are not used in pairs: after child thread acquired mutex,
429 the main thread release it, then any thread could acquire it.
431 void release_001()
433 Mutex aMutex;
434 WaitThread myThread( &aMutex );
435 myThread.create( );
437 // ensure the child thread acquire the mutex
438 ThreadHelper::thread_sleep_tenth_sec( 1 );
440 bool bRunning = myThread.isRunning( );
441 bool bRes1 = aMutex.tryToAcquire( );
442 // wait the child thread terminate
443 myThread.join( );
445 bool bRes2 = aMutex.tryToAcquire( );
447 if ( bRes2 )
448 aMutex.release( );
450 CPPUNIT_ASSERT_MESSAGE( "release Mutex: try to acquire before and after the mutex has been released",
451 !bRes1 && bRes2 && bRunning );
455 // how about release twice?
456 void release_002()
460 CPPUNIT_TEST_SUITE( release );
461 CPPUNIT_TEST( release_001 );
462 CPPUNIT_TEST( release_002 );
463 CPPUNIT_TEST_SUITE_END( );
464 }; // class release
466 /** Test of the osl::Mutex::getGlobalMutex method
468 class getGlobalMutex : public CppUnit::TestFixture
470 public:
471 // initialise your test code values here.
472 void getGlobalMutex_001()
474 Mutex* pGlobalMutex;
475 pGlobalMutex = Mutex::getGlobalMutex();
476 pGlobalMutex->acquire();
478 GlobalMutexThread myThread;
479 myThread.create();
481 ThreadHelper::thread_sleep_tenth_sec(1);
482 bool bRes1 = myThread.isRunning();
484 pGlobalMutex->release();
485 ThreadHelper::thread_sleep_tenth_sec(1);
486 // after release mutex, myThread stops blocking and will terminate immediately
487 bool bRes2 = myThread.isRunning();
489 CPPUNIT_ASSERT_MESSAGE("Global Mutex works", bRes1 && !bRes2);
492 void getGlobalMutex_002( )
494 bool bRes;
496 Mutex *pGlobalMutex;
497 pGlobalMutex = Mutex::getGlobalMutex( );
498 pGlobalMutex->acquire( );
500 Mutex *pGlobalMutex1;
501 pGlobalMutex1 = Mutex::getGlobalMutex( );
502 bRes = pGlobalMutex1->release( );
505 CPPUNIT_ASSERT_MESSAGE( "Global Mutex works: if the code between {} get the different mutex as the former one, it will return false when release.",
506 bRes );
509 CPPUNIT_TEST_SUITE(getGlobalMutex);
510 CPPUNIT_TEST(getGlobalMutex_001);
511 CPPUNIT_TEST(getGlobalMutex_002);
512 CPPUNIT_TEST_SUITE_END();
513 }; // class getGlobalMutex
515 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Mutex::ctor, "osl_Mutex");
516 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Mutex::acquire, "osl_Mutex");
517 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Mutex::tryToAcquire, "osl_Mutex");
518 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Mutex::release, "osl_Mutex");
519 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Mutex::getGlobalMutex, "osl_Mutex");
520 } // namespace osl_Mutex
522 // Beginning of the test cases for osl_Guard class
524 class GuardThread : public Thread
526 public:
527 //get the Mutex pointer to operate
528 GuardThread( Mutex* pMutex ): pMyMutex( pMutex ) { }
530 virtual ~GuardThread( )
532 CPPUNIT_ASSERT_MESSAGE( "#GuardThread does not shutdown properly.\n", !isRunning( ) );
534 protected:
535 Mutex* pMyMutex;
537 void SAL_CALL run( ) SAL_OVERRIDE
539 // block here if the mutex has been acquired
540 MutexGuard aGuard( pMyMutex );
541 ThreadHelper::thread_sleep_tenth_sec( 2 );
545 namespace osl_Guard
547 class ctor : public CppUnit::TestFixture
549 public:
550 // insert your test code here.
551 void ctor_001()
553 Mutex aMutex;
554 GuardThread myThread(&aMutex);
555 myThread.create();
557 ThreadHelper::thread_sleep_tenth_sec(1);
558 bool bRes = aMutex.tryToAcquire();
559 // after 1 second, the mutex has been guarded, and the child thread should be running
560 bool bRes1 = myThread.isRunning();
562 myThread.join();
563 bool bRes2 = aMutex.tryToAcquire();
565 CPPUNIT_ASSERT_MESSAGE("GuardThread constructor",
566 !bRes && bRes1 && bRes2);
569 void ctor_002( )
571 Mutex aMutex;
573 /// use reference constructor here
574 MutexGuard myGuard( aMutex );
576 /// the GuardThread will block here when it is initialised.
577 GuardThread myThread( &aMutex );
578 myThread.create( );
580 /// is it still blocking?
581 ThreadHelper::thread_sleep_tenth_sec( 2 );
582 bool bRes = myThread.isRunning( );
584 /// oh, release him.
585 aMutex.release( );
586 myThread.join( );
588 CPPUNIT_ASSERT_MESSAGE("GuardThread constructor: reference initialization, acquire the mutex before running the thread, then check if it is blocking.",
589 bRes);
592 CPPUNIT_TEST_SUITE(ctor);
593 CPPUNIT_TEST(ctor_001);
594 CPPUNIT_TEST(ctor_002);
595 CPPUNIT_TEST_SUITE_END();
596 }; // class ctor
598 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Guard::ctor, "osl_Guard");
599 } // namespace osl_Guard
601 // Beginning of the test cases for osl_ClearableGuard class
603 /** Thread for test ClearableGuard
605 class ClearGuardThread : public Thread
607 public:
608 //get the Mutex pointer to operate
609 ClearGuardThread( Mutex* pMutex ): pMyMutex( pMutex ) {}
611 virtual ~ClearGuardThread( )
613 CPPUNIT_ASSERT_MESSAGE( "#ClearGuardThread does not shutdown properly.\n", !isRunning( ) );
615 protected:
616 Mutex* pMyMutex;
618 void SAL_CALL run( ) SAL_OVERRIDE
620 // acquire the mutex
621 // printf("# ClearGuardThread" );
622 ClearableMutexGuard aGuard( pMyMutex );
623 ThreadHelper::thread_sleep( 5 );
625 // release the mutex
626 aGuard.clear( );
627 ThreadHelper::thread_sleep( 2 );
631 namespace osl_ClearableGuard
634 class ctor : public CppUnit::TestFixture
636 public:
637 void ctor_001()
639 Mutex aMutex;
641 /// now, the aMutex has been guarded.
642 ClearableMutexGuard myMutexGuard( &aMutex );
644 /// it will return sal_False if the aMutex has not been Guarded.
645 bool bRes = aMutex.release( );
647 CPPUNIT_ASSERT_MESSAGE("ClearableMutexGuard constructor, test the acquire operation when initilized.",
648 bRes);
651 void ctor_002( )
653 Mutex aMutex;
655 /// now, the aMutex has been guarded, this time, we use reference constructor.
656 ClearableMutexGuard myMutexGuard( aMutex );
658 /// it will return sal_False if the aMutex has not been Guarded.
659 bool bRes = aMutex.release( );
661 CPPUNIT_ASSERT_MESSAGE("ClearableMutexGuard constructor, test the acquire operation when initilized, we use reference constructor this time.",
662 bRes);
665 CPPUNIT_TEST_SUITE(ctor);
666 CPPUNIT_TEST(ctor_001);
667 CPPUNIT_TEST(ctor_002);
668 CPPUNIT_TEST_SUITE_END();
669 }; // class ctor
671 class clear : public CppUnit::TestFixture
673 public:
674 void clear_001()
676 Mutex aMutex;
677 ClearGuardThread myThread(&aMutex);
678 myThread.create();
680 TimeValue aTimeVal_befor;
681 osl_getSystemTime( &aTimeVal_befor );
682 // wait 1 second to assure the child thread has begun
683 ThreadHelper::thread_sleep(1);
685 while (true)
687 if (aMutex.tryToAcquire())
689 break;
691 ThreadHelper::thread_sleep(1);
693 TimeValue aTimeVal_after;
694 osl_getSystemTime( &aTimeVal_after );
695 sal_Int32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
696 printf("nSec is %" SAL_PRIdINT32 "\n", nSec);
698 myThread.join();
700 CPPUNIT_ASSERT_MESSAGE("ClearableGuard method: clear",
701 nSec < 7 && nSec > 1);
704 void clear_002( )
706 Mutex aMutex;
708 /// now, the aMutex has been guarded.
709 ClearableMutexGuard myMutexGuard( &aMutex );
711 /// launch the HoldThread, it will be blocked here.
712 HoldThread myThread( &aMutex );
713 myThread.create( );
715 /// is it blocking?
716 ThreadHelper::thread_sleep_tenth_sec( 4 );
717 bool bRes = myThread.isRunning( );
719 /// use clear to release.
720 myMutexGuard.clear( );
721 myThread.join( );
722 bool bRes1 = myThread.isRunning( );
724 CPPUNIT_ASSERT_MESSAGE( "ClearableGuard method: clear, control the HoldThread's running status!",
725 bRes && !bRes1 );
728 CPPUNIT_TEST_SUITE( clear );
729 CPPUNIT_TEST( clear_001 );
730 CPPUNIT_TEST( clear_002 );
731 CPPUNIT_TEST_SUITE_END( );
732 }; // class clear
734 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( osl_ClearableGuard::ctor, "osl_ClearableGuard" );
735 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( osl_ClearableGuard::clear, "osl_ClearableGuard" );
736 } // namespace osl_ClearableGuard
738 // Beginning of the test cases for osl_ResettableGuard class
740 /** Thread for test ResettableGuard
742 class ResetGuardThread : public Thread
744 public:
745 //get the Mutex pointer to operate
746 ResetGuardThread( Mutex* pMutex ): pMyMutex( pMutex ) {}
748 virtual ~ResetGuardThread( )
750 CPPUNIT_ASSERT_MESSAGE( "#ResetGuardThread does not shutdown properly.\n", !isRunning( ) );
752 protected:
753 Mutex* pMyMutex;
755 void SAL_CALL run( ) SAL_OVERRIDE
757 // acquire the mutex
758 printf("# ResettableGuard\n" );
759 ResettableMutexGuard aGuard( pMyMutex );
760 // release the mutex
761 aGuard.clear( );
762 ThreadHelper::thread_sleep_tenth_sec( 2 );
766 namespace osl_ResettableGuard
768 class ctor : public CppUnit::TestFixture
770 public:
771 void ctor_001()
773 Mutex aMutex;
775 /// now, the aMutex has been guarded.
776 ResettableMutexGuard myMutexGuard( &aMutex );
778 /// it will return sal_False if the aMutex has not been Guarded.
779 bool bRes = aMutex.release( );
781 CPPUNIT_ASSERT_MESSAGE("ResettableMutexGuard constructor, test the acquire operation when initilized.",
782 bRes);
785 void ctor_002( )
787 Mutex aMutex;
789 /// now, the aMutex has been guarded, this time, we use reference constructor.
790 ResettableMutexGuard myMutexGuard( aMutex );
792 /// it will return sal_False if the aMutex has not been Guarded.
793 bool bRes = aMutex.release( );
795 CPPUNIT_ASSERT_MESSAGE( "ResettableMutexGuard constructor, test the acquire operation when initilized, we use reference constructor this time.",
796 bRes);
799 CPPUNIT_TEST_SUITE(ctor);
800 CPPUNIT_TEST(ctor_001);
801 CPPUNIT_TEST(ctor_002);
802 CPPUNIT_TEST_SUITE_END();
803 }; // class ctor
805 class reset : public CppUnit::TestFixture
807 public:
808 void reset_001( )
810 Mutex aMutex;
811 ResetGuardThread myThread( &aMutex );
812 ResettableMutexGuard myMutexGuard( aMutex );
813 myThread.create( );
815 /// is it running? and clear done?
816 bool bRes = myThread.isRunning( );
817 myMutexGuard.clear( );
818 ThreadHelper::thread_sleep_tenth_sec( 1 );
820 /// if reset is not success, the release will return sal_False
821 myMutexGuard.reset( );
822 bool bRes1 = aMutex.release( );
823 myThread.join( );
825 CPPUNIT_ASSERT_MESSAGE( "ResettableMutexGuard method: reset",
826 bRes && bRes1 );
829 #ifdef LINUX
830 void reset_002( )
832 Mutex aMutex;
833 ResettableMutexGuard myMutexGuard( &aMutex );
835 /// shouldn't release after clear;
836 myMutexGuard.clear( );
837 bool bRes = aMutex.release( );
839 /// can release after reset.
840 myMutexGuard.reset( );
841 bool bRes1 = aMutex.release( );
843 CPPUNIT_ASSERT_MESSAGE( "ResettableMutexGuard method: reset, release after clear and reset, on Solaris, the mutex can be release without acquire, so it can not passed on (SOLARIS), but not the reason for reset_002",
844 !bRes && bRes1 );
846 #endif
848 CPPUNIT_TEST_SUITE(reset);
849 CPPUNIT_TEST(reset_001);
850 #ifdef LINUX
851 CPPUNIT_TEST(reset_002);
852 #endif
853 CPPUNIT_TEST_SUITE_END();
854 }; // class reset
856 CPPUNIT_TEST_SUITE_REGISTRATION(osl_ResettableGuard::ctor);
857 CPPUNIT_TEST_SUITE_REGISTRATION(osl_ResettableGuard::reset);
858 } // namespace osl_ResettableGuard
860 CPPUNIT_PLUGIN_IMPLEMENT();
862 // The following sets variables for GNU EMACS
863 // Local Variables:
864 // tab-width:4
865 // End:
867 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */