1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
|
/*************************************************************************
*
* OpenOffice.org - a multi-platform office productivity suite
*
* $RCSfile: threadtest.cxx,v $
*
* $Revision: 1.5 $
*
* last change: $Author: obo $ $Date: 2006-09-16 14:33:01 $
*
* The Contents of this file are made available subject to
* the terms of GNU Lesser General Public License Version 2.1.
*
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2005 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_framework.hxx"
//_________________________________________________________________________________________________________________
// my own includes
//_________________________________________________________________________________________________________________
#ifndef __FRAMEWORK_THREADHELP_THREADHELPBASE_HXX_
#include <threadhelp/threadhelpbase.hxx>
#endif
#ifndef __FRAMEWORK_THREADHELP_TRANSACTIONBASE_HXX_
#include <threadhelp/transactionbase.hxx>
#endif
#ifndef __FRAMEWORK_THREADHELP_RESETABLEGUARD_HXX_
#include <threadhelp/resetableguard.hxx>
#endif
#ifndef __FRAMEWORK_THREADHELP_READGUARD_HXX_
#include <threadhelp/readguard.hxx>
#endif
#ifndef __FRAMEWORK_THREADHELP_WRITEGUARD_HXX_
#include <threadhelp/writeguard.hxx>
#endif
#ifndef __FRAMEWORK_THREADHELP_TRANSACTIONGUARD_HXX_
#include <threadhelp/transactionguard.hxx>
#endif
#ifndef __FRAMEWORK_MACROS_GENERIC_HXX_
#include <macros/generic.hxx>
#endif
#ifndef __FRAMEWORK_MACROS_DEBUG_HXX_
#include <macros/debug.hxx>
#endif
//_________________________________________________________________________________________________________________
// interface includes
//_________________________________________________________________________________________________________________
//_________________________________________________________________________________________________________________
// other includes
//_________________________________________________________________________________________________________________
#ifndef _RTL_RANDOM_H_
#include <rtl/random.h>
#endif
#ifndef _VOS_PROCESS_HXX_
#include <vos/process.hxx>
#endif
#ifndef _VOS_THREAD_HXX_
#include <vos/thread.hxx>
#endif
#ifndef _RTL_USTRING_
#include <rtl/ustring.hxx>
#endif
#ifndef _RTL_USTRBUF_HXX_
#include <rtl/ustrbuf.hxx>
#endif
#ifndef _OSL_TIME_H_
#include <osl/time.h>
#endif
#ifndef _OSL_INTERLOCK_H_
#include <osl/interlock.h>
#endif
#include <vcl/event.hxx>
#include <vcl/svapp.hxx>
#include <vcl/wrkwin.hxx>
#include <vcl/msgbox.hxx>
#include <stdio.h>
//_________________________________________________________________________________________________________________
// const
//_________________________________________________________________________________________________________________
#define LOGFILE "threadtest.log"
#define STATISTICS_FILE "threadtest_statistic.csv"
//_________________________________________________________________________________________________________________
// namespace
//_________________________________________________________________________________________________________________
using namespace ::rtl ;
using namespace ::osl ;
using namespace ::vos ;
using namespace ::framework ;
//_________________________________________________________________________________________________________________
// defines
//_________________________________________________________________________________________________________________
/*---------------- Use follow defines to enable/disable some special features of this little test program! -------*/
#define ENABLE_LOG
//#define ENABLE_THREADDELAY
#define ENABLE_REQUESTCOUNT
/*----------------------------------------------------------------------------------------------------------------*/
#ifdef ENABLE_LOG
#define LOG_SETA_START( NA, NID ) \
{ \
sal_uInt32 nTimeStamp = osl_getGlobalTimer(); \
::osl::MutexGuard aLogGuard( m_aLogMutex ); \
OStringBuffer sLog(256); \
sLog.append( (sal_Int32)nTimeStamp ); \
sLog.append( ": Thread[ " ); \
sLog.append( NID ); \
sLog.append( " ] call setA( " ); \
sLog.append( NA ); \
sLog.append( " )\n" ); \
WRITE_LOGFILE( LOGFILE, sLog.makeStringAndClear() ) \
}
#define LOG_SETA_END( NA, EREASON, NID ) \
{ \
sal_uInt32 nTimeStamp = osl_getGlobalTimer(); \
::osl::MutexGuard aLogGuard( m_aLogMutex ); \
OStringBuffer sLog(256); \
sLog.append( (sal_Int32)nTimeStamp ); \
sLog.append( ": Thread[ " ); \
sLog.append( NID ); \
if( EREASON == E_NOREASON ) \
sLog.append( " ] finish setA( " ); \
else \
sLog.append( " ] was refused at setA( "); \
sLog.append( NA ); \
sLog.append( " )\n" ); \
WRITE_LOGFILE( LOGFILE, sLog.makeStringAndClear() ) \
}
#define LOG_GETA_START( NID ) \
{ \
sal_uInt32 nTimeStamp = osl_getGlobalTimer(); \
::osl::MutexGuard aLogGuard( m_aLogMutex ); \
OStringBuffer sLog(256); \
sLog.append( (sal_Int32)nTimeStamp ); \
sLog.append( ": Thread[ " ); \
sLog.append( NID ); \
sLog.append( " ] call getA()\n" ); \
WRITE_LOGFILE( LOGFILE, sLog.makeStringAndClear() ) \
}
#define LOG_GETA_END( NRETURN, EREASON, NID ) \
{ \
sal_uInt32 nTimeStamp = osl_getGlobalTimer(); \
::osl::MutexGuard aLogGuard( m_aLogMutex ); \
OStringBuffer sLog(256); \
sLog.append( (sal_Int32)nTimeStamp ); \
sLog.append( ": Thread[ " ); \
sLog.append( NID ); \
if( EREASON == E_NOREASON ) \
sLog.append( " ] finish getA() with " ); \
else \
sLog.append( " ] was refused at getA() with " ); \
sLog.append( NRETURN ); \
sLog.append( "\n" ); \
WRITE_LOGFILE( LOGFILE, sLog.makeStringAndClear() ) \
}
#define LOG_WORKA_START( NA, NID ) \
{ \
sal_uInt32 nTimeStamp = osl_getGlobalTimer(); \
::osl::MutexGuard aLogGuard( m_aLogMutex ); \
OStringBuffer sLog(256); \
sLog.append( (sal_Int32)nTimeStamp ); \
sLog.append( ": Thread[ " ); \
sLog.append( NID ); \
sLog.append( " ] call workA( " ); \
sLog.append( NA ); \
sLog.append( " )\n" ); \
WRITE_LOGFILE( LOGFILE, sLog.makeStringAndClear() ) \
}
#define LOG_WORKA_END( NRETURN, EREASON, NID ) \
{ \
sal_uInt32 nTimeStamp = osl_getGlobalTimer(); \
::osl::MutexGuard aLogGuard( m_aLogMutex ); \
OStringBuffer sLog(256); \
sLog.append( (sal_Int32)nTimeStamp ); \
sLog.append( ": Thread[ " ); \
sLog.append( NID ); \
if( EREASON == E_NOREASON ) \
sLog.append( " ] finish workA() with " ); \
else \
sLog.append( " ] was refused at workA() with " ); \
sLog.append( NRETURN ); \
sLog.append( "\n" ); \
WRITE_LOGFILE( LOGFILE, sLog.makeStringAndClear() ) \
}
#define LOG_INITEXCEPTION( SMETHOD, NID ) \
{ \
sal_uInt32 nTimeStamp = osl_getGlobalTimer(); \
::osl::MutexGuard aLogGuard( m_aLogMutex ); \
OStringBuffer sLog(256); \
sLog.append( (sal_Int32)nTimeStamp ); \
sLog.append( ": Thread[ " ); \
sLog.append( NID ); \
sLog.append( " ] get EInitException from \"" ); \
sLog.append( SMETHOD ); \
sLog.append( "\"\n" ); \
WRITE_LOGFILE( LOGFILE, sLog.makeStringAndClear() ) \
}
#define LOG_CLOSEEXCEPTION( SMETHOD, NID ) \
{ \
sal_uInt32 nTimeStamp = osl_getGlobalTimer(); \
::osl::MutexGuard aLogGuard( m_aLogMutex ); \
OStringBuffer sLog(256); \
sLog.append( (sal_Int32)nTimeStamp ); \
sLog.append( ": Thread[ " ); \
sLog.append( NID ); \
sLog.append( " ] get ECloseException from \"" ); \
sLog.append( SMETHOD ); \
sLog.append( "\"\n" ); \
WRITE_LOGFILE( LOGFILE, sLog.makeStringAndClear() ) \
}
#define LOG_INIT( NA, NID ) \
{ \
sal_uInt32 nTimeStamp = osl_getGlobalTimer(); \
::osl::MutexGuard aLogGuard( m_aLogMutex ); \
OStringBuffer sLog(256); \
sLog.append( (sal_Int32)nTimeStamp ); \
sLog.append( ": Thread[ " ); \
sLog.append( NID ); \
sLog.append( " ] initialize me with " ); \
sLog.append( NA ); \
sLog.append( "\n" ); \
WRITE_LOGFILE( LOGFILE, sLog.makeStringAndClear() ) \
}
#define LOG_CLOSE( NID ) \
{ \
sal_uInt32 nTimeStamp = osl_getGlobalTimer(); \
::osl::MutexGuard aLogGuard( m_aLogMutex ); \
OStringBuffer sLog(256); \
sLog.append( (sal_Int32)nTimeStamp ); \
sLog.append( ": Thread[ " ); \
sLog.append( NID ); \
sLog.append( " ] close me\n" ); \
WRITE_LOGFILE( LOGFILE, sLog.makeStringAndClear() ) \
}
#else
#define LOG_SETA_START( NA, NID )
#define LOG_SETA_END( NA, EREASON, NID )
#define LOG_GETA_START( NID )
#define LOG_GETA_END( NRETURN, EREASON, NID )
#define LOG_WORKA_START( NA, NID )
#define LOG_WORKA_END( NRETURN, EREASON, NID )
#define LOG_INITEXCEPTION( SMETHOD, NID )
#define LOG_CLOSEEXCEPTION( SMETHOD, NID )
#define LOG_INIT( NA, NID )
#define LOG_CLOSE( NID )
#endif
//_________________________________________________________________________________________________________________
// declarations
//_________________________________________________________________________________________________________________
sal_uInt16 getRandomValue()
{
// Get new random value for thread-sleep!
// See run() for further informations.
// Always calculate a new random number.
sal_uInt16 nValue;
rtlRandomPool aPool = rtl_random_createPool();
rtl_random_getBytes ( aPool, &nValue, 2 );
rtl_random_destroyPool ( aPool );
return nValue;
}
/*-************************************************************************************************************//**
@descr This class is used from different threads at the same time.
We start working after calling init() first(!) ...
and finish it by calling close(). It exist two methods for reading/writing an
internal variable "A". Another function workA() do both things at the same time.
All public methods log information in a file if DO_LOG is defined.
@attention Our public base class FaiRWLockBase is a struct with a RWLock as member.
This member can be used by guards to safe access at internal variables
in interface methods.
Another baseclass is the TransactionBase. They support rejection of wrong calls at wrong time.
e.g. calls after closing object!
*//*-*************************************************************************************************************/
class ThreadSafeClass : private ThreadHelpBase
, private TransactionBase
{
public:
ThreadSafeClass ();
~ThreadSafeClass();
// This methods are used from differnt threads
// to test this class.
void init ( sal_Int32 nA ,
sal_Int32 nThreadID );
void close ( sal_Int32 nThreadID );
void setA ( sal_Int32 nA ,
sal_Int32 nThreadID );
sal_Int32 getA ( sal_Int32 nThreadID );
sal_Int32 workA ( sal_Int32 nA ,
sal_Int32 nThreadID );
#ifdef ENABLE_REQUESTCOUNT
// This methods are used for statistics only!
sal_Int32 getReadCount () { return m_nReadCount; }
sal_Int32 getWriteCount() { return m_nWriteCount; }
#endif
private:
sal_Int32 m_nA ; /// test member fro reading/writing
#ifdef ENABLE_LOG
::osl::Mutex m_aLogMutex ; /// mutex to serialize writing log file!
#endif
#ifdef ENABLE_REQUESTCOUNT
oslInterlockedCount m_nReadCount ; /// statistic variables to count read/write requests
oslInterlockedCount m_nWriteCount ;
#endif
};
//_________________________________________________________________________________________________________________
ThreadSafeClass::ThreadSafeClass()
: ThreadHelpBase ( )
, TransactionBase ( )
, m_nA ( 0 )
#ifdef ENABLE_REQUESTCOUNT
, m_nReadCount ( 0 )
, m_nWriteCount ( 0 )
#endif
{
}
//_________________________________________________________________________________________________________________
ThreadSafeClass::~ThreadSafeClass()
{
}
//_________________________________________________________________________________________________________________
void ThreadSafeClass::init( sal_Int32 nA, sal_Int32 nThreadID )
{
// Look for multiple calls of this method first!
// Use E_SOFTEXCEPTIONS to disable automaticly throwing of exceptions for some working modes.
TransactionGuard aTransaction( m_aTransactionManager, E_SOFTEXCEPTIONS );
// Set write lock for setting internal member AND
// protect changing of working mode!
WriteGuard aWriteLock( m_aLock );
LOG_INIT( nA, nThreadID )
// OK, it must be the first call and we are synchronized with all other threads by using the write lock!
// Otherwise (e.g. if working mode == E_WORK) we get a exception and follow lines are never called.
// We can set our member and change the working mode now.
m_nA = nA;
aWriteLock.unlock();
m_aTransactionManager.setWorkingMode( E_WORK );
}
//_________________________________________________________________________________________________________________
void ThreadSafeClass::close( sal_Int32 nThreadID )
{
// We must look for multiple calls of this method.
// Try to register this method as a transaction.
// In combination with E_HARDEXCEPTIONS only working mode E_WORK pass this barrier.
TransactionGuard aTransaction( m_aTransactionManager, E_HARDEXCEPTIONS );
aTransaction.stop();
// Change working mode to BEFORECLOSE to enable rejection of normal interface calls
// and enable SOFTEXCEPTION mode for some impl- or helper methods!
// Attention: We must stop successful registered transaction first ...
// because setWorkingMode() blocks and wait for all current existing ones!
m_aTransactionManager.setWorkingMode( E_BEFORECLOSE );
// Make it threadsafe.
// It must be an exclusiv access! => WriteLock!
WriteGuard aWriteLock( m_aLock );
LOG_CLOSE( nThreadID )
// Now we are alone ...
// All further calls to this object are rejected ...
// (not all ... some special ones can work by using E_SOFTEXCEPTIONS!)
// Deinitialize all member and set working mode to E_CLOSE.
m_nA = 0;
aWriteLock.unlock();
m_aTransactionManager.setWorkingMode( E_CLOSE );
}
//_________________________________________________________________________________________________________________
void ThreadSafeClass::setA( sal_Int32 nA, sal_Int32 nThreadID )
{
// Register this method as a transaction to prevent code against wrong calls
// after close() or before init()!
ERejectReason eReason;
TransactionGuard aTransaction( m_aTransactionManager, E_NOEXCEPTIONS, &eReason );
if( eReason == E_NOREASON )
{
// Make it threadsafe.
WriteGuard aWriteLock( m_aLock );
LOG_SETA_START( nA, nThreadID )
// This object is ready for working and we have full write access.
// We can work with our member.
m_nA = nA;
#ifdef ENABLE_REQUESTCOUNT
osl_incrementInterlockedCount( &m_nWriteCount );
#endif
LOG_SETA_END( nA, eReason, nThreadID )
}
}
//_________________________________________________________________________________________________________________
sal_Int32 ThreadSafeClass::getA( sal_Int32 nThreadID )
{
// Register this method as a transaction to prevent code against wrong calls
// after close() or before init()!
sal_Int32 nReturn = 0;
ERejectReason eReason;
TransactionGuard aTransaction( m_aTransactionManager, E_NOEXCEPTIONS, &eReason );
if( eReason == E_NOREASON )
{
// Make it threadsafe.
ReadGuard aReadLock( m_aLock );
LOG_GETA_START( nThreadID )
// This object is ready for working and we have a read access.
// We can work with our member.
nReturn = m_nA;
#ifdef ENABLE_REQUESTCOUNT
osl_incrementInterlockedCount( &m_nReadCount );
#endif
LOG_GETA_END( nReturn, eReason, nThreadID )
}
return nReturn;
}
//_________________________________________________________________________________________________________________
sal_Int32 ThreadSafeClass::workA( sal_Int32 nA ,
sal_Int32 nThreadID )
{
// Register this method as a transaction to prevent code against wrong calls
// after close() or before init()!
sal_Int32 nReturn = 0;
ERejectReason eReason;
TransactionGuard aTransaction( m_aTransactionManager, E_NOEXCEPTIONS, &eReason );
if( eReason == E_NOREASON )
{
// This method test the downgrade-mechanism of used lock implementation!
// Make it threadsafe.
WriteGuard aWriteLock( m_aLock );
LOG_WORKA_START( nA, nThreadID )
// We have write access to our member.
// Set new value.
m_nA = nA;
#ifdef ENABLE_REQUESTCOUNT
osl_incrementInterlockedCount( &m_nWriteCount );
#endif
// Downgrade write access to read access and read the set value again.
// This call can't be rejected - but it can fail!
aWriteLock.downgrade();
nReturn = m_nA;
#ifdef ENABLE_REQUESTCOUNT
osl_incrementInterlockedCount( &m_nReadCount );
#endif
LOG_WORKA_END( nReturn, eReason, nThreadID )
}
return nReturn;
}
/*-****************************************************************************************************//**
@descr Every thread instance of these class lopp from 0 up to "nLoops".
He sleep for a random time and work with given test class "pClass" then.
We use random values for waiting for better results!
Otherwise all threads are sychron after first 2,3...5 calls - I think!
*//*-*****************************************************************************************************/
class TestThread : public OThread
{
public:
TestThread( ThreadSafeClass* pClass ,
sal_Int32 nLoops ,
Condition* pListener ,
sal_Bool bOwner = sal_False );
private:
virtual void SAL_CALL run ();
virtual void SAL_CALL onTerminated ();
private:
ThreadSafeClass* m_pClass ;
sal_Int32 m_nLoops ;
sal_Int32 m_nThreadID ;
Condition* m_pListener ;
sal_Bool m_bOwner ;
};
//_________________________________________________________________________________________________________________
TestThread::TestThread( ThreadSafeClass* pClass ,
sal_Int32 nLoops ,
Condition* pListener ,
sal_Bool bOwner )
: m_pClass ( pClass )
, m_nLoops ( nLoops )
, m_pListener ( pListener )
, m_bOwner ( bOwner )
{
}
//_________________________________________________________________________________________________________________
void SAL_CALL TestThread::run()
{
// Get ID of this thread.
// Is used for logging information ...
m_nThreadID = getCurrentIdentifier();
// If we are the owner of given pClass
// we must initialize ... and close
// it. See at the end of this method too.
if( m_bOwner == sal_True )
{
m_pClass->init( 0, m_nThreadID );
}
#ifdef ENABLE_THREADDELAY
TimeValue nDelay ;
#endif
sal_Int32 nA ;
for( sal_Int32 nCount=0; nCount<m_nLoops; ++nCount )
{
// Work with class.
// Use random to select called method.
nA = (sal_Int32)getRandomValue();
if( nA % 5 == 0 )
{
//nA = m_pClass->workA( nA, m_nThreadID );
}
else
if( nA % 3 == 0 )
{
m_pClass->setA( nA, m_nThreadID );
}
else
{
nA = m_pClass->getA( m_nThreadID );
}
#ifdef ENABLE_THREADDELAY
// Sleep - use random value to do that too!
nDelay.Seconds = 0;
nDelay.Nanosec = getRandomValue();
sleep( nDelay );
#endif
}
// Don't forget to "close" teset object if you are the owner!
if( m_bOwner == sal_True )
{
m_pClass->close( m_nThreadID );
}
}
//_________________________________________________________________________________________________________________
void SAL_CALL TestThread::onTerminated()
{
// Destroy yourself if you finished.
// But don't forget to call listener before.
m_pListener->set();
m_pClass = NULL;
m_pListener = NULL;
delete this;
}
/*-****************************************************************************************************//**
@descr This is our test application.
We create one ThreadSafeClass object and a lot of threads
which use it at different times.
*//*-*****************************************************************************************************/
struct ThreadInfo
{
Condition* pCondition ;
TestThread* pThread ;
};
class TestApplication : public Application
{
public:
void Main ( );
sal_Int32 measureTime ( sal_Int32 nThreadCount ,
sal_Int32 nOwner ,
sal_Int32 nLoops=0 );
};
//_________________________________________________________________________________________________________________
// definition
//_________________________________________________________________________________________________________________
TestApplication aApplication;
//_________________________________________________________________________________________________________________
// This function start "nThreadCount" threads to use same test class.
// You can specify the owner thread of this test class which start/stop it by using "nOwner". [1..nThreadcount]!
// If you specify "nLoops" different from 0 we use it as loop count for every started thread.
// Otherwise we work with random values.
sal_Int32 TestApplication::measureTime( sal_Int32 nThreadCount ,
sal_Int32 nOwner ,
sal_Int32 nLoops )
{
// This is the class which should be tested.
ThreadSafeClass aClass;
// Create list of threads.
ThreadInfo* pThreads = new ThreadInfo[nThreadCount];
sal_Int32 nLoopCount = nLoops ;
sal_Bool bOwner = sal_False ;
for( sal_Int32 nI=0; nI<nThreadCount; ++nI )
{
// If nLoops==0 => we must use random value; otherwise we must use given count ...
if( nLoops == 0 )
{
nLoopCount = getRandomValue();
}
// Search owner of class.
bOwner = sal_False;
if( nOwner == nI )
{
bOwner = sal_True;
}
// initialize condition.
pThreads[nI].pCondition = new Condition;
// Initialize thread.
pThreads[nI].pThread = new TestThread( &aClass, nLoopCount, pThreads[nI].pCondition, bOwner );
}
// Start clock to get information about used time.
sal_uInt32 nStartTime ;
sal_uInt32 nEndTime ;
nStartTime = osl_getGlobalTimer();
// Start threads ...
for( nI=0; nI<nThreadCount; ++nI )
{
pThreads[nI].pThread->create();
}
// Wait for threads ...
for( nI=0; nI<nThreadCount; ++nI )
{
pThreads[nI].pCondition->wait();
delete pThreads[nI].pCondition;
pThreads[nI].pCondition = NULL;
pThreads[nI].pThread = NULL;
}
delete[] pThreads;
pThreads = NULL;
nEndTime = osl_getGlobalTimer();
// Calc used time and return it. [ms]
return( nEndTime-nStartTime );
}
//_________________________________________________________________________________________________________________
void TestApplication::Main()
{
sal_Int32 nTestCount = 0; /// count of calling "measureTime()"
sal_Int32 nThreadCount = 0; /// count of used threads by "measure..."
sal_Int32 nLoops = 0; /// loop count for every thread
sal_Int32 nOwner = 0; /// number of owner thread
// Parse command line.
// Attention: All parameter are required and must exist!
// syntax: "threadtest.exe <testcount> <threadcount> <loops> <owner>"
OStartupInfo aInfo ;
OUString sArgument ;
sal_Int32 nArgument ;
sal_Int32 nCount = aInfo.getCommandArgCount();
LOG_ASSERT2( nCount!=4 ,"TestApplication::Main()" , "Wrong argument line detected!")
for( nArgument=0; nArgument<nCount; ++nArgument )
{
aInfo.getCommandArg( nArgument, sArgument );
if( nArgument== 0 ) nTestCount =sArgument.toInt32();
if( nArgument== 1 ) nThreadCount=sArgument.toInt32();
if( nArgument== 2 ) nLoops =sArgument.toInt32();
if( nArgument== 3 ) nOwner =sArgument.toInt32();
}
LOG_ASSERT2( nTestCount==0||nThreadCount==0||nLoops==0||nOwner==0,"TestApplication::Main()", "Wrong argument value detected!" )
// Start test.
OStringBuffer sBuf(256);
sal_Int32 nTime=0;
sBuf.append( "Nr.\tTime\tThreadCount\tLoops\tOwner\n" );
for( sal_Int32 nI=1; nI<=nTestCount; ++nI )
{
nTime = measureTime( nThreadCount, nOwner, nLoops );
sBuf.append( nI );
sBuf.append( "\t" );
sBuf.append( nTime );
sBuf.append( "\t" );
sBuf.append( nThreadCount );
sBuf.append( "\t" );
sBuf.append( nLoops );
sBuf.append( "\t" );
sBuf.append( nOwner );
sBuf.append( "\n" );
}
WRITE_LOGFILE( STATISTICS_FILE, sBuf.makeStringAndClear() );
LOG_ERROR( "TApplication::Main()", "Test finish successful!" )
}
|