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