Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sal / qa / osl / mutex / osl_Mutex.cxx
blob3424bca07b3c1ff21e4c5476e1032053391e8626
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 static void thread_sleep_tenth_sec(sal_uInt32 _nTenthSec)
37 osl::Thread::wait(std::chrono::milliseconds(_nTenthSec * 100));
39 static void thread_sleep( sal_uInt32 _nSec )
41 /// print statement in thread process must use fflush() to force display.
42 // t_print("# wait %d seconds. ", _nSec );
43 fflush(stdout);
45 thread_sleep_tenth_sec( _nSec * 10 );
46 // printf("# done\n" );
50 // Beginning of the test cases for osl_Mutex class
52 namespace {
54 /** mutually exclusive data
56 struct resource {
57 sal_Int32 data1;
58 sal_Int32 data2;
59 Mutex lock;
62 /** IncreaseThread provide data.
64 class IncreaseThread : public Thread
66 public:
67 explicit IncreaseThread( struct resource *pData ): pResource( pData ) { }
69 virtual ~IncreaseThread( ) override
71 CPPUNIT_ASSERT_MESSAGE( "#IncreaseThread does not shutdown properly.\n", !isRunning( ) );
73 protected:
74 struct resource *pResource;
76 void SAL_CALL run( ) override
78 pResource->lock.acquire( );
79 for( sal_Int8 i = 0; i < 3; i++ )
81 pResource->data1++;
82 yield( ); //yield() give CPU time to other thread, other thread if not block, they will change the data;
84 if ( pResource->data2 == 0 )
85 pResource->data2 = ( pResource->data1 > 0 ? pResource->data1 : 0 - pResource->data1 );
86 pResource->lock.release();
90 /** DecreaseThread consume data.
92 class DecreaseThread : public Thread
94 public:
95 explicit DecreaseThread( struct resource *pData ): pResource( pData ) { }
97 virtual ~DecreaseThread( ) override
99 CPPUNIT_ASSERT_MESSAGE( "#DecreaseThread does not shutdown properly.\n", !isRunning( ) );
101 protected:
102 struct resource *pResource;
104 void SAL_CALL run( ) override
106 pResource->lock.acquire( );
107 for( sal_Int8 i = 0; i < 3; i++ )
109 pResource->data1--;
110 yield( ); //yield() give CPU time to other thread, other thread if not block, they will change the data;
112 if ( pResource->data2 == 0 )
113 pResource->data2 = ( pResource->data1 > 0 ? pResource->data1 : 0 - pResource->data1 );
114 pResource->lock.release();
118 /** chain structure used in Threads as critical resource
120 struct chain {
121 sal_Int32 buffer[ BUFFER_SIZE ];
122 Mutex lock;
123 sal_Int8 pos;
126 /** PutThread write to the chain structure in a mutex manner.
128 class PutThread : public Thread
130 public:
131 //get the struct pointer to write data to buffer
132 explicit PutThread( struct chain* pData ): pChain( pData ) { }
134 virtual ~PutThread( ) override
136 CPPUNIT_ASSERT_MESSAGE( "#PutThread does not shutdown properly.\n", !isRunning( ) );
138 protected:
139 struct chain* pChain;
141 void SAL_CALL run( ) override
143 //block here if the mutex has been acquired
144 pChain->lock.acquire( );
146 //current position in buffer to write
147 sal_Int8 nPos = pChain->pos;
148 oslThreadIdentifier oId = getIdentifier( );
149 //write data
150 sal_Int8 i;
151 for ( i = 0; i < 5; i++ )
153 pChain->buffer[ nPos + i ] = oId;
154 yield( );
156 //revise the position
157 pChain->pos = nPos + i;
159 //finish writing, release the mutex
160 pChain->lock.release();
164 /** thread for testing Mutex acquire.
166 class HoldThread : public Thread
168 public:
169 //get the Mutex pointer to operate
170 explicit HoldThread( Mutex* pMutex ): pMyMutex( pMutex ) { }
172 virtual ~HoldThread( ) override
174 CPPUNIT_ASSERT_MESSAGE( "#HoldThread does not shutdown properly.\n", !isRunning( ) );
176 protected:
177 Mutex* pMyMutex;
179 void SAL_CALL run() override
181 // block here if the mutex has been acquired
182 pMyMutex->acquire( );
183 printf("# Mutex acquired. \n" );
184 pMyMutex->release( );
188 class WaitThread : public Thread
190 public:
191 //get the Mutex pointer to operate
192 explicit WaitThread( Mutex* pMutex ): pMyMutex( pMutex ) { }
194 virtual ~WaitThread( ) override
196 CPPUNIT_ASSERT_MESSAGE( "#WaitThread does not shutdown properly.\n", !isRunning( ) );
198 protected:
199 Mutex* pMyMutex;
201 void SAL_CALL run( ) override
203 // block here if the mutex has been acquired
204 pMyMutex->acquire( );
205 ThreadHelper::thread_sleep_tenth_sec( 2 );
206 pMyMutex->release( );
210 /** thread for testing getGlobalMutex.
212 class GlobalMutexThread : public Thread
214 public:
215 //get the Mutex pointer to operate
216 GlobalMutexThread( ){ }
218 virtual ~GlobalMutexThread( ) override
220 CPPUNIT_ASSERT_MESSAGE( "#GlobalMutexThread does not shutdown properly.\n", !isRunning( ) );
222 protected:
223 void SAL_CALL run( ) override
225 // block here if the mutex has been acquired
226 Mutex* pGlobalMutex;
227 pGlobalMutex = Mutex::getGlobalMutex( );
228 pGlobalMutex->acquire( );
229 printf("# Global Mutex acquired. \n" );
230 pGlobalMutex->release( );
236 namespace osl_Mutex
239 /** Test of the osl::Mutex::constructor
241 class ctor : public CppUnit::TestFixture
243 public:
244 // initialise your test code values here.
245 struct chain m_Data;
246 struct resource m_Res;
248 void setUp( ) override
250 for ( sal_Int8 i=0; i < BUFFER_SIZE; i++ )
251 m_Data.buffer[i] = 0;
252 m_Data.pos = 0;
254 m_Res.data1 = 0;
255 m_Res.data2 = 0;
258 /** Create two threads to write data to the same buffer, use Mutex to assure
259 during one thread write data five times, the other thread should not begin writing.
260 the two threads wrote two different data: their thread ID, so we can check the data
261 in buffer to know the order of the two threads writing
263 void ctor_001()
265 PutThread myThread1( &m_Data );
266 PutThread myThread2( &m_Data );
268 myThread1.create( );
269 myThread2.create( );
271 //wait until the two threads terminate
272 myThread1.join( );
273 myThread2.join( );
275 bool bRes = false;
277 // every 5 data should the same
278 // LLA: this is not a good check, it's too fix
279 if (m_Data.buffer[0] == m_Data.buffer[1] &&
280 m_Data.buffer[1] == m_Data.buffer[2] &&
281 m_Data.buffer[2] == m_Data.buffer[3] &&
282 m_Data.buffer[3] == m_Data.buffer[4] &&
283 m_Data.buffer[5] == m_Data.buffer[6] &&
284 m_Data.buffer[6] == m_Data.buffer[7] &&
285 m_Data.buffer[7] == m_Data.buffer[8] &&
286 m_Data.buffer[8] == m_Data.buffer[9])
287 bRes = true;
289 /*for (sal_Int8 i=0; i<BUFFER_SIZE; i++)
290 printf("#data in buffer is %d\n", m_Data.buffer[i]);
293 CPPUNIT_ASSERT_MESSAGE("Mutex ctor", bRes);
297 /** Create two threads to write data to operate on the same number , use Mutex to assure,
298 one thread increase data 3 times, the other thread decrease 3 times, store the operate
299 result when the first thread complete, if it is interrupt by the other thread, the stored
300 number will not be 3.
302 void ctor_002()
304 IncreaseThread myThread1( &m_Res );
305 DecreaseThread myThread2( &m_Res );
307 myThread1.create( );
308 myThread2.create( );
310 //wait until the two threads terminate
311 myThread1.join( );
312 myThread2.join( );
314 bool bRes = false;
316 // every 5 data should the same
317 if ( ( m_Res.data1 == 0 ) && ( m_Res.data2 == 3 ) )
318 bRes = true;
320 CPPUNIT_ASSERT_MESSAGE( "test Mutex ctor function: increase and decrease a number 3 times without interrupt.", bRes );
323 CPPUNIT_TEST_SUITE( ctor );
324 CPPUNIT_TEST( ctor_001 );
325 CPPUNIT_TEST( ctor_002 );
326 CPPUNIT_TEST_SUITE_END( );
327 }; // class ctor
329 /** Test of the osl::Mutex::acquire method
331 class acquire : public CppUnit::TestFixture
333 public:
334 // acquire mutex in main thread, and then call acquire again in myThread,
335 // the child thread should block, wait 2 secs, it still block.
336 // Then release mutex in main thread, the child thread could return from acquire,
337 // and go to exec next statement, so could terminate quickly.
338 void acquire_001( )
340 Mutex aMutex;
341 //acquire here
342 bool bRes = aMutex.acquire( );
343 // pass the pointer of mutex to child thread
344 HoldThread myThread( &aMutex );
345 myThread.create( );
347 ThreadHelper::thread_sleep_tenth_sec( 2 );
348 // if acquire in myThread does not work, 2 secs is long enough,
349 // myThread should terminate now, and bRes1 should be sal_False
350 bool bRes1 = myThread.isRunning( );
352 aMutex.release( );
353 ThreadHelper::thread_sleep_tenth_sec( 1 );
354 // after release mutex, myThread stops blocking and will terminate immediately
355 bool bRes2 = myThread.isRunning( );
356 myThread.join( );
358 CPPUNIT_ASSERT_MESSAGE( "Mutex acquire", bRes );
359 CPPUNIT_ASSERT_MESSAGE( "Mutex acquire", bRes1 );
360 CPPUNIT_ASSERT_MESSAGE( "Mutex acquire", !bRes2 );
363 //in the same thread, acquire twice should success
364 void acquire_002()
366 Mutex aMutex;
367 //acquire here
368 bool bRes = aMutex.acquire();
369 bool bRes1 = aMutex.acquire();
371 bool bRes2 = aMutex.tryToAcquire();
373 aMutex.release();
375 CPPUNIT_ASSERT_MESSAGE("Mutex acquire", bRes);
376 CPPUNIT_ASSERT_MESSAGE("Mutex acquire", bRes1);
377 CPPUNIT_ASSERT_MESSAGE("Mutex acquire", bRes2);
381 CPPUNIT_TEST_SUITE( acquire );
382 CPPUNIT_TEST( acquire_001 );
383 CPPUNIT_TEST( acquire_002 );
384 CPPUNIT_TEST_SUITE_END( );
385 }; // class acquire
387 /** Test of the osl::Mutex::tryToAcquire method
389 class tryToAcquire : public CppUnit::TestFixture
391 public:
392 // First let child thread acquire the mutex, and wait 2 secs, during the 2 secs,
393 // in main thread, tryToAcquire mutex should return False
394 // then after the child thread terminated, tryToAcquire should return True
395 void tryToAcquire_001()
397 Mutex aMutex;
398 WaitThread myThread(&aMutex);
399 myThread.create();
401 // ensure the child thread acquire the mutex
402 ThreadHelper::thread_sleep_tenth_sec(1);
404 bool bRes1 = aMutex.tryToAcquire();
406 if (bRes1)
407 aMutex.release();
408 // wait the child thread terminate
409 myThread.join();
411 bool bRes2 = aMutex.tryToAcquire();
413 if (bRes2)
414 aMutex.release();
416 CPPUNIT_ASSERT_MESSAGE("Try to acquire Mutex", !bRes1);
417 CPPUNIT_ASSERT_MESSAGE("Try to acquire Mutex", bRes2);
420 CPPUNIT_TEST_SUITE(tryToAcquire);
421 CPPUNIT_TEST(tryToAcquire_001);
422 CPPUNIT_TEST_SUITE_END();
423 }; // class tryToAcquire
425 /** Test of the osl::Mutex::release method
427 class release : public CppUnit::TestFixture
429 public:
430 /** acquire/release are not used in pairs: after child thread acquired mutex,
431 the main thread release it, then any thread could acquire it.
433 void release_001()
435 Mutex aMutex;
436 WaitThread myThread( &aMutex );
437 myThread.create( );
439 // ensure the child thread acquire the mutex
440 ThreadHelper::thread_sleep_tenth_sec( 1 );
442 bool bRunning = myThread.isRunning( );
443 bool bRes1 = aMutex.tryToAcquire( );
444 // wait the child thread terminate
445 myThread.join( );
447 bool bRes2 = aMutex.tryToAcquire( );
449 if ( bRes2 )
450 aMutex.release( );
452 CPPUNIT_ASSERT_MESSAGE( "release Mutex: try to acquire before and after the mutex has been released",
453 !bRes1 );
454 CPPUNIT_ASSERT_MESSAGE( "release Mutex: try to acquire before and after the mutex has been released",
455 bRes2 );
456 CPPUNIT_ASSERT_MESSAGE( "release Mutex: try to acquire before and after the mutex has been released",
457 bRunning );
461 // how about release twice?
462 void release_002()
466 CPPUNIT_TEST_SUITE( release );
467 CPPUNIT_TEST( release_001 );
468 CPPUNIT_TEST( release_002 );
469 CPPUNIT_TEST_SUITE_END( );
470 }; // class release
472 /** Test of the osl::Mutex::getGlobalMutex method
474 class getGlobalMutex : public CppUnit::TestFixture
476 public:
477 // initialise your test code values here.
478 void getGlobalMutex_001()
480 Mutex* pGlobalMutex;
481 pGlobalMutex = Mutex::getGlobalMutex();
482 pGlobalMutex->acquire();
484 GlobalMutexThread myThread;
485 myThread.create();
487 ThreadHelper::thread_sleep_tenth_sec(1);
488 bool bRes1 = myThread.isRunning();
490 pGlobalMutex->release();
491 ThreadHelper::thread_sleep_tenth_sec(1);
492 // after release mutex, myThread stops blocking and will terminate immediately
493 bool bRes2 = myThread.isRunning();
495 CPPUNIT_ASSERT_MESSAGE("Global Mutex works", bRes1);
496 CPPUNIT_ASSERT_MESSAGE("Global Mutex works", !bRes2);
499 void getGlobalMutex_002( )
501 bool bRes;
503 Mutex *pGlobalMutex;
504 pGlobalMutex = Mutex::getGlobalMutex( );
505 pGlobalMutex->acquire( );
507 Mutex *pGlobalMutex1;
508 pGlobalMutex1 = Mutex::getGlobalMutex( );
509 bRes = pGlobalMutex1->release( );
512 CPPUNIT_ASSERT_MESSAGE( "Global Mutex works: if the code between {} get the different mutex as the former one, it will return false when release.",
513 bRes );
516 CPPUNIT_TEST_SUITE(getGlobalMutex);
517 CPPUNIT_TEST(getGlobalMutex_001);
518 CPPUNIT_TEST(getGlobalMutex_002);
519 CPPUNIT_TEST_SUITE_END();
520 }; // class getGlobalMutex
522 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Mutex::ctor, "osl_Mutex");
523 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Mutex::acquire, "osl_Mutex");
524 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Mutex::tryToAcquire, "osl_Mutex");
525 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Mutex::release, "osl_Mutex");
526 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Mutex::getGlobalMutex, "osl_Mutex");
527 } // namespace osl_Mutex
529 // Beginning of the test cases for osl_Guard class
531 namespace {
533 class GuardThread : public Thread
535 public:
536 //get the Mutex pointer to operate
537 explicit GuardThread( Mutex* pMutex ): pMyMutex( pMutex ) { }
539 virtual ~GuardThread( ) override
541 CPPUNIT_ASSERT_MESSAGE( "#GuardThread does not shutdown properly.\n", !isRunning( ) );
543 protected:
544 Mutex* pMyMutex;
546 void SAL_CALL run( ) override
548 // block here if the mutex has been acquired
549 MutexGuard aGuard( pMyMutex );
550 ThreadHelper::thread_sleep_tenth_sec( 2 );
556 namespace osl_Guard
558 class ctor : public CppUnit::TestFixture
560 public:
561 // insert your test code here.
562 void ctor_001()
564 Mutex aMutex;
565 GuardThread myThread(&aMutex);
566 myThread.create();
568 ThreadHelper::thread_sleep_tenth_sec(1);
569 bool bRes = aMutex.tryToAcquire();
570 // after 1 second, the mutex has been guarded, and the child thread should be running
571 bool bRes1 = myThread.isRunning();
573 myThread.join();
574 bool bRes2 = aMutex.tryToAcquire();
576 CPPUNIT_ASSERT_MESSAGE("GuardThread constructor",
577 !bRes);
578 CPPUNIT_ASSERT_MESSAGE("GuardThread constructor",
579 bRes1);
580 CPPUNIT_ASSERT_MESSAGE("GuardThread constructor",
581 bRes2);
584 void ctor_002( )
586 Mutex aMutex;
588 /// use reference constructor here
589 MutexGuard myGuard( aMutex );
591 /// the GuardThread will block here when it is initialised.
592 GuardThread myThread( &aMutex );
593 myThread.create( );
595 /// is it still blocking?
596 ThreadHelper::thread_sleep_tenth_sec( 2 );
597 bool bRes = myThread.isRunning( );
599 /// oh, release him.
600 aMutex.release( );
601 myThread.join( );
603 CPPUNIT_ASSERT_MESSAGE("GuardThread constructor: reference initialization, acquire the mutex before running the thread, then check if it is blocking.",
604 bRes);
607 CPPUNIT_TEST_SUITE(ctor);
608 CPPUNIT_TEST(ctor_001);
609 CPPUNIT_TEST(ctor_002);
610 CPPUNIT_TEST_SUITE_END();
611 }; // class ctor
613 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Guard::ctor, "osl_Guard");
614 } // namespace osl_Guard
616 // Beginning of the test cases for osl_ClearableGuard class
618 namespace {
620 /** Thread for test ClearableGuard
622 class ClearGuardThread : public Thread
624 public:
625 //get the Mutex pointer to operate
626 explicit ClearGuardThread( Mutex* pMutex ): pMyMutex( pMutex ) {}
628 virtual ~ClearGuardThread( ) override
630 CPPUNIT_ASSERT_MESSAGE( "#ClearGuardThread does not shutdown properly.\n", !isRunning( ) );
632 protected:
633 Mutex* pMyMutex;
635 void SAL_CALL run( ) override
637 // acquire the mutex
638 // printf("# ClearGuardThread" );
639 ClearableMutexGuard aGuard( pMyMutex );
640 ThreadHelper::thread_sleep( 5 );
642 // release the mutex
643 aGuard.clear( );
644 ThreadHelper::thread_sleep( 2 );
650 namespace osl_ClearableGuard
653 class ctor : public CppUnit::TestFixture
655 public:
656 void ctor_001()
658 Mutex aMutex;
660 /// now, the aMutex has been guarded.
661 ClearableMutexGuard myMutexGuard( &aMutex );
663 /// it will return sal_False if the aMutex has not been Guarded.
664 bool bRes = aMutex.release( );
666 CPPUNIT_ASSERT_MESSAGE("ClearableMutexGuard constructor, test the acquire operation when initialized.",
667 bRes);
670 void ctor_002( )
672 Mutex aMutex;
674 /// now, the aMutex has been guarded, this time, we use reference constructor.
675 ClearableMutexGuard myMutexGuard( aMutex );
677 /// it will return sal_False if the aMutex has not been Guarded.
678 bool bRes = aMutex.release( );
680 CPPUNIT_ASSERT_MESSAGE("ClearableMutexGuard constructor, test the acquire operation when initialized, we use reference constructor this time.",
681 bRes);
684 CPPUNIT_TEST_SUITE(ctor);
685 CPPUNIT_TEST(ctor_001);
686 CPPUNIT_TEST(ctor_002);
687 CPPUNIT_TEST_SUITE_END();
688 }; // class ctor
690 class clear : public CppUnit::TestFixture
692 public:
693 void clear_001()
695 Mutex aMutex;
696 ClearGuardThread myThread(&aMutex);
697 myThread.create();
699 TimeValue aTimeVal_befor;
700 osl_getSystemTime( &aTimeVal_befor );
701 // wait 1 second to assure the child thread has begun
702 ThreadHelper::thread_sleep(1);
704 while (true)
706 if (aMutex.tryToAcquire())
708 break;
710 ThreadHelper::thread_sleep(1);
712 TimeValue aTimeVal_after;
713 osl_getSystemTime( &aTimeVal_after );
714 sal_Int32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
715 printf("nSec is %" SAL_PRIdINT32 "\n", nSec);
717 myThread.join();
719 CPPUNIT_ASSERT_MESSAGE("ClearableGuard method: clear",
720 nSec < 7);
721 CPPUNIT_ASSERT_MESSAGE("ClearableGuard method: clear",
722 nSec > 1);
725 void clear_002( )
727 Mutex aMutex;
729 /// now, the aMutex has been guarded.
730 ClearableMutexGuard myMutexGuard( &aMutex );
732 /// launch the HoldThread, it will be blocked here.
733 HoldThread myThread( &aMutex );
734 myThread.create( );
736 /// is it blocking?
737 ThreadHelper::thread_sleep_tenth_sec( 4 );
738 bool bRes = myThread.isRunning( );
740 /// use clear to release.
741 myMutexGuard.clear( );
742 myThread.join( );
743 bool bRes1 = myThread.isRunning( );
745 CPPUNIT_ASSERT_MESSAGE( "ClearableGuard method: clear, control the HoldThread's running status!",
746 bRes );
747 CPPUNIT_ASSERT_MESSAGE( "ClearableGuard method: clear, control the HoldThread's running status!",
748 !bRes1 );
751 CPPUNIT_TEST_SUITE( clear );
752 CPPUNIT_TEST( clear_001 );
753 CPPUNIT_TEST( clear_002 );
754 CPPUNIT_TEST_SUITE_END( );
755 }; // class clear
757 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( osl_ClearableGuard::ctor, "osl_ClearableGuard" );
758 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( osl_ClearableGuard::clear, "osl_ClearableGuard" );
759 } // namespace osl_ClearableGuard
761 // Beginning of the test cases for osl_ResettableGuard class
763 namespace {
765 /** Thread for test ResettableGuard
767 class ResetGuardThread : public Thread
769 public:
770 //get the Mutex pointer to operate
771 explicit ResetGuardThread( Mutex* pMutex ): pMyMutex( pMutex ) {}
773 virtual ~ResetGuardThread( ) override
775 CPPUNIT_ASSERT_MESSAGE( "#ResetGuardThread does not shutdown properly.\n", !isRunning( ) );
777 protected:
778 Mutex* pMyMutex;
780 void SAL_CALL run( ) override
782 // acquire the mutex
783 printf("# ResettableGuard\n" );
784 ResettableMutexGuard aGuard( pMyMutex );
785 // release the mutex
786 aGuard.clear( );
787 ThreadHelper::thread_sleep_tenth_sec( 2 );
793 namespace osl_ResettableGuard
795 class ctor : public CppUnit::TestFixture
797 public:
798 void ctor_001()
800 Mutex aMutex;
802 /// now, the aMutex has been guarded.
803 ResettableMutexGuard myMutexGuard( &aMutex );
805 /// it will return sal_False if the aMutex has not been Guarded.
806 bool bRes = aMutex.release( );
808 CPPUNIT_ASSERT_MESSAGE("ResettableMutexGuard constructor, test the acquire operation when initialized.",
809 bRes);
811 aMutex.acquire();
814 void ctor_002( )
816 Mutex aMutex;
818 /// now, the aMutex has been guarded, this time, we use reference constructor.
819 ResettableMutexGuard myMutexGuard( aMutex );
821 /// it will return sal_False if the aMutex has not been Guarded.
822 bool bRes = aMutex.release( );
824 CPPUNIT_ASSERT_MESSAGE( "ResettableMutexGuard constructor, test the acquire operation when initialized, we use reference constructor this time.",
825 bRes);
827 aMutex.acquire();
830 CPPUNIT_TEST_SUITE(ctor);
831 CPPUNIT_TEST(ctor_001);
832 CPPUNIT_TEST(ctor_002);
833 CPPUNIT_TEST_SUITE_END();
834 }; // class ctor
836 class reset : public CppUnit::TestFixture
838 public:
839 void reset_001( )
841 Mutex aMutex;
842 ResetGuardThread myThread( &aMutex );
843 ResettableMutexGuard myMutexGuard( aMutex );
844 myThread.create( );
846 /// is it running? and clear done?
847 bool bRes = myThread.isRunning( );
848 myMutexGuard.clear( );
849 ThreadHelper::thread_sleep_tenth_sec( 1 );
851 /// if reset is not success, the release will return sal_False
852 myMutexGuard.reset( );
853 bool bRes1 = aMutex.release( );
854 myThread.join( );
856 CPPUNIT_ASSERT_MESSAGE( "ResettableMutexGuard method: reset",
857 bRes );
858 CPPUNIT_ASSERT_MESSAGE( "ResettableMutexGuard method: reset",
859 bRes1 );
861 aMutex.acquire();
864 #ifdef LINUX
865 void reset_002( )
867 Mutex aMutex;
868 ResettableMutexGuard myMutexGuard( &aMutex );
870 /// shouldn't release after clear;
871 myMutexGuard.clear( );
872 aMutex.acquire();
873 bool bRes = aMutex.release( );
875 /// can release after reset.
876 myMutexGuard.reset( );
877 bool bRes1 = aMutex.release( );
879 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",
880 bRes );
881 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",
882 bRes1 );
884 aMutex.acquire();
886 #endif
888 CPPUNIT_TEST_SUITE(reset);
889 CPPUNIT_TEST(reset_001);
890 #ifdef LINUX
891 CPPUNIT_TEST(reset_002);
892 #endif
893 CPPUNIT_TEST_SUITE_END();
894 }; // class reset
896 CPPUNIT_TEST_SUITE_REGISTRATION(osl_ResettableGuard::ctor);
897 CPPUNIT_TEST_SUITE_REGISTRATION(osl_ResettableGuard::reset);
898 } // namespace osl_ResettableGuard
900 // The following sets variables for GNU EMACS
901 // Local Variables:
902 // tab-width:4
903 // End:
905 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */