1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: osl_Thread.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sal.hxx"
33 //------------------------------------------------------------------------
35 //------------------------------------------------------------------------
36 #include <sal/types.h>
38 #ifndef _RTL_USTRING_HXX_
39 #include <rtl/string.hxx>
42 #ifndef _RTL_USTRING_HXX_
43 #include <rtl/strbuf.hxx>
46 #ifndef _OSL_THREAD_HXX
47 #include <osl/thread.hxx>
50 #ifndef _OSL_MUTEX_HXX
51 #include <osl/mutex.hxx>
55 #include <cppunit/simpleheader.hxx>
64 // -----------------------------------------------------------------------------
67 TimeValue t1
,t2
; // Start und Stopzeit
73 bool m_bIsValid
; // TRUE, wenn gestartet und gestoppt
74 bool m_bIsRunning
; // TRUE, wenn gestartet.
80 void start(); // Startet Timer
81 void stop(); // Stoppt Timer
83 double getSeconds() const;
84 double getTenthSec() const;
87 // ================================= Stop Watch =================================
89 // Eine kleine Stop-Uhr fuer den internen Gebrauch.
90 // (c) Lars Langhans 29.12.1996 22:10
92 StopWatch::StopWatch():m_bIsValid(false),m_bIsRunning(false) {}
94 void StopWatch::start()
101 osl_getSystemTime( &t1
);
102 t_print("# %d %d nsecs\n", t1
.Seconds
, t1
.Nanosec
);
103 // gettimeofday(&t1, 0);
106 void StopWatch::stop()
108 // pre: Timer should be started
109 // post: Timer will stopped
111 // gettimeofday(&t2, 0); // Timer ausfragen
112 osl_getSystemTime( &t2
);
113 t_print("# %d %d nsecs\n", t2
.Seconds
, t2
.Nanosec
);
116 { // check ob gestartet.
117 // LLA: old m_nNanoSec = static_cast<sal_Int32>(t2.Nanosec) - static_cast<sal_Int32>(t1.Nanosec);
118 // LLA: old m_nSeconds = static_cast<sal_Int32>(t2.Seconds) - static_cast<sal_Int32>(t1.Seconds);
119 // LLA: old if (m_nNanoSec < 0)
121 // LLA: old m_nNanoSec += 1000000000;
122 // LLA: old m_nSeconds -= 1;
124 //m_nNanoSec = t2.Nanosec - t1.Nanosec;
125 m_nSeconds
= static_cast<sal_Int32
>(t2
.Seconds
) - static_cast<sal_Int32
>(t1
.Seconds
);
126 if ( t2
.Nanosec
> t1
.Nanosec
)
127 m_nNanoSec
= static_cast<sal_Int32
>(t2
.Nanosec
) - static_cast<sal_Int32
>(t1
.Nanosec
);
130 m_nNanoSec
= 1000000000 + static_cast<sal_Int32
>(t2
.Nanosec
) - static_cast<sal_Int32
>(t1
.Nanosec
);
133 t_print("# %d %d nsecs\n", m_nSeconds
, m_nNanoSec
);
134 //if (m_nNanoSec < 0)
136 //m_nNanoSec += 1000000000;
140 m_bIsRunning
= false;
144 double StopWatch::getSeconds() const
146 // pre: gueltig = TRUE
147 // BACK: Zeit in Sekunden.
152 nValue
= double(m_nNanoSec
) / 1000000000.0 + m_nSeconds
; // milli micro nano
157 double StopWatch::getTenthSec() const
162 nValue
= double(m_nNanoSec
) / 100000000.0 + m_nSeconds
* 10;
167 // -----------------------------------------------------------------------------
169 class ThreadSafeValue
174 ThreadSafeValue(T n
= 0): m_nFlag(n
) {}
177 //block if already acquired by another thread.
178 osl::MutexGuard
g(m_aMutex
);
183 //only one thread operate on the flag.
184 osl::MutexGuard
g(m_aMutex
);
187 void acquire() {m_aMutex
.acquire();}
188 void release() {m_aMutex
.release();}
191 // -----------------------------------------------------------------------------
192 namespace ThreadHelper
197 // } eSleepVerboseMode;
199 void thread_sleep_tenth_sec(sal_Int32 _nTenthSec
/*, eSleepVerboseMode nVerbose = VERBOSE*/)
201 // if (nVerbose == VERBOSE)
203 // t_print("wait %d tenth seconds. ", _nTenthSec );
207 Sleep(_nTenthSec
* 100 );
209 #if ( defined UNX ) || ( defined OS2 ) //Unix
211 nTV
.Seconds
= static_cast<sal_uInt32
>( _nTenthSec
/10 );
212 nTV
.Nanosec
= ( (_nTenthSec
%10 ) * 100000000 );
213 osl_waitThread(&nTV
);
215 // if (nVerbose == VERBOSE)
217 // t_print("done\n");
221 void outputPriority(oslThreadPriority
const& _aPriority
)
223 // LLA: output the priority
224 if (_aPriority
== osl_Thread_PriorityHighest
)
226 t_print("Prio is High\n");
228 else if (_aPriority
== osl_Thread_PriorityAboveNormal
)
230 t_print("Prio is above normal\n");
232 else if (_aPriority
== osl_Thread_PriorityNormal
)
234 t_print("Prio is normal\n");
236 else if (_aPriority
== osl_Thread_PriorityBelowNormal
)
238 t_print("Prio is below normal\n");
240 else if (_aPriority
== osl_Thread_PriorityLowest
)
242 t_print("Prio is lowest\n");
246 t_print("Prio is unknown\n");
251 /** Simple thread for testing Thread-create.
253 Just add 1 of value 0, and after running, result is 1.
255 class myThread
: public Thread
257 ThreadSafeValue
<sal_Int32
> m_aFlag
;
259 sal_Int32
getValue() { return m_aFlag
.getValue(); }
261 /** guarded value which initialized 0
270 ThreadHelper::thread_sleep_tenth_sec(1);
276 virtual void SAL_CALL
suspend()
279 ::osl::Thread::suspend();
287 t_print("error: not terminated.\n");
293 // -----------------------------------------------------------------------------
294 /** Thread which has a flag add 1 every second until 20
296 class OCountThread
: public Thread
298 ThreadSafeValue
<sal_Int32
> m_aFlag
;
303 t_print("new OCountThread thread %d!\n", getIdentifier());
305 sal_Int32
getValue() { return m_aFlag
.getValue(); }
307 void setWait(sal_Int32 nSec
)
310 //m_bWait = sal_True;
313 virtual void SAL_CALL
suspend()
316 ::osl::Thread::suspend();
322 sal_Int32 m_nWaitSec
;
326 /// if the thread should terminate, schedule return false
327 while (m_aFlag
.getValue() < 20 && schedule() == sal_True
)
330 ThreadHelper::thread_sleep_tenth_sec(1);
338 //ThreadHelper::thread_sleep_tenth_sec(m_nWaitSec * 10);
340 nTV
.Seconds
= m_nWaitSec
/ 10 ;
341 nTV
.Nanosec
= ( m_nWaitSec
%10 ) * 100000000 ;
347 void SAL_CALL
onTerminated()
349 t_print("normally terminate this thread %d!\n", getIdentifier());
357 t_print("error: not terminated.\n");
363 /** call suspend in the run method
365 class OSuspendThread
: public Thread
367 ThreadSafeValue
<sal_Int32
> m_aFlag
;
369 OSuspendThread(){ m_bSuspend
= sal_False
; }
370 sal_Int32
getValue() { return m_aFlag
.getValue(); }
373 m_bSuspend
= sal_True
;
375 virtual void SAL_CALL
suspend()
378 ::osl::Thread::suspend();
385 //if the thread should terminate, schedule return false
386 while (schedule() == sal_True
)
390 ThreadHelper::thread_sleep_tenth_sec(1);
391 // m_bWait = sal_False;
396 if (m_bSuspend
== sal_True
)
399 m_bSuspend
= sal_False
;
409 t_print("error: not terminated.\n");
415 /** no call schedule in the run method
417 class ONoScheduleThread
: public Thread
419 ThreadSafeValue
<sal_Int32
> m_aFlag
;
421 sal_Int32
getValue() { return m_aFlag
.getValue(); }
423 virtual void SAL_CALL
suspend()
426 ::osl::Thread::suspend();
432 while (m_aFlag
.getValue() < 10)
435 ThreadHelper::thread_sleep_tenth_sec(1);
442 void SAL_CALL
onTerminated()
444 t_print("normally terminate this thread %d!\n", getIdentifier());
449 t_print("new thread id %d!\n", getIdentifier());
455 t_print("error: not terminated.\n");
463 class OAddThread
: public Thread
465 ThreadSafeValue
<sal_Int32
> m_aFlag
;
467 //oslThreadIdentifier m_id, m_CurId;
469 sal_Int32
getValue() { return m_aFlag
.getValue(); }
471 virtual void SAL_CALL
suspend()
474 ::osl::Thread::suspend();
480 //if the thread should terminate, schedule return false
481 while (schedule() == sal_True
)
486 void SAL_CALL
onTerminated()
488 // t_print("normally terminate this thread %d!\n", getIdentifier());
496 // t_print("error: not terminated.\n");
505 void resumeAndWaitThread(Thread
* _pThread
)
507 // This functions starts a thread, wait a second and suspends the thread
508 // Due to the fact, that a suspend and never run thread never really exists.
510 // Note: on UNX, after createSuspended, and then terminate the thread, it performs well;
511 // while on Windows, after createSuspended, the thread can not terminate, wait endlessly,
512 // so here call resume at first, then call terminate.
514 t_print("resumeAndWaitThread\n");
516 ThreadHelper::thread_sleep_tenth_sec(1);
520 // ThreadHelper::thread_sleep_tenth_sec(1);
521 // _pThread->suspend();
522 // ThreadHelper::thread_sleep_tenth_sec(1);
525 // kill a running thread and join it, if it has terminated, do nothing
526 void termAndJoinThread(Thread
* _pThread
)
528 _pThread
->terminate();
530 // LLA: Windows feature???, a suspended thread can not terminated, so we have to weak it up
533 ThreadHelper::thread_sleep_tenth_sec(1);
535 t_print("#wait for join.\n");
538 /** Test of the osl::Thread::create method
541 class create
: public CppUnit::TestFixture
545 // initialise your test code values here.
554 /** Simple create a thread.
556 Create a simple thread, it just does add 1 to value(which initialized 0),
557 if the thread run, the value should be 1.
561 myThread
* newthread
= new myThread();
562 sal_Bool bRes
= newthread
->create();
563 CPPUNIT_ASSERT_MESSAGE("Can not creates a new thread!\n", bRes
== sal_True
);
565 ThreadHelper::thread_sleep_tenth_sec(1); // wait short
566 sal_Bool isRunning
= newthread
->isRunning(); // check if thread is running
567 /// wait for the new thread to assure it has run
568 ThreadHelper::thread_sleep_tenth_sec(3);
569 sal_Int32 nValue
= newthread
->getValue();
570 /// to assure the new thread has terminated
571 termAndJoinThread(newthread
);
574 t_print(" nValue = %d\n", nValue
);
575 t_print("isRunning = %d\n", isRunning
);
577 CPPUNIT_ASSERT_MESSAGE(
578 "Creates a new thread",
579 nValue
>= 1 && isRunning
== sal_True
584 /** only one running thread per instance, return false if create secondly
588 myThread
* newthread
= new myThread();
589 sal_Bool res1
= newthread
->create();
590 sal_Bool res2
= newthread
->create();
591 t_print("In non pro, an assertion should occured. This behaviour is right.\n");
592 termAndJoinThread(newthread
);
595 CPPUNIT_ASSERT_MESSAGE(
596 "Creates a new thread: can not create two threads per instance",
602 CPPUNIT_TEST_SUITE(create
);
603 CPPUNIT_TEST(create_001
);
604 CPPUNIT_TEST(create_002
);
605 CPPUNIT_TEST_SUITE_END();
610 /** Test of the osl::Thread::createSuspended method
612 class createSuspended
: public CppUnit::TestFixture
615 // initialise your test code values here.
624 /** Create a suspended thread, use the same class as create_001
626 after create, wait enough time, check the value, if it's still the initial value, pass
628 void createSuspended_001()
630 myThread
* newthread
= new myThread();
631 sal_Bool bRes
= newthread
->createSuspended();
632 CPPUNIT_ASSERT_MESSAGE("Can not creates a new thread!", bRes
== sal_True
);
634 ThreadHelper::thread_sleep_tenth_sec(1);
635 sal_Bool isRunning
= newthread
->isRunning();
636 ThreadHelper::thread_sleep_tenth_sec(3);
637 sal_Int32 nValue
= newthread
->getValue();
639 resumeAndWaitThread(newthread
);
641 termAndJoinThread(newthread
);
644 CPPUNIT_ASSERT_MESSAGE(
645 "Creates a new suspended thread",
646 nValue
== 0 && isRunning
650 void createSuspended_002()
652 myThread
* newthread
= new myThread();
653 sal_Bool res1
= newthread
->createSuspended();
654 sal_Bool res2
= newthread
->createSuspended();
656 resumeAndWaitThread(newthread
);
658 termAndJoinThread(newthread
);
662 CPPUNIT_ASSERT_MESSAGE(
663 "Creates a new thread: can not create two threads per instance",
668 CPPUNIT_TEST_SUITE(createSuspended
);
669 CPPUNIT_TEST(createSuspended_001
);
670 // LLA: Deadlocked!!!
671 CPPUNIT_TEST(createSuspended_002
);
672 CPPUNIT_TEST_SUITE_END();
673 }; // class createSuspended
675 /** when the count value equal to or more than 3, suspend the thread.
677 void suspendCountThread(OCountThread
* _pCountThread
)
679 sal_Int32 nValue
= 0;
682 nValue
= _pCountThread
->getValue();
685 _pCountThread
->suspend();
691 /** Test of the osl::Thread::suspend method
693 class suspend
: public CppUnit::TestFixture
696 // initialise your test code values here.
705 /** Use a thread which has a flag added 1 every second
708 create the thread, after running special time, record value of flag, then suspend it,
709 wait a long time, check the flag, if it remains unchanged during suspending
713 OCountThread
* aCountThread
= new OCountThread();
714 sal_Bool bRes
= aCountThread
->create();
715 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
716 // the thread run for some seconds, but not terminate
717 suspendCountThread( aCountThread
);
719 // the value just after calling suspend
720 sal_Int32 nValue
= aCountThread
->getValue(); // (2)
722 ThreadHelper::thread_sleep_tenth_sec(3);
724 // the value after waiting 3 seconds
725 sal_Int32 nLaterValue
= aCountThread
->getValue(); // (3)
727 resumeAndWaitThread(aCountThread
);
728 termAndJoinThread(aCountThread
);
731 CPPUNIT_ASSERT_MESSAGE(
732 "Suspend the thread",
733 bRes
== sal_True
&& nValue
== nLaterValue
737 /** suspend a thread in it's worker-function, the ALGORITHM is same as suspend_001
738 reason of deadlocked I think: no schedule can schedule other threads to go on excuting
742 OSuspendThread
* aThread
= new OSuspendThread();
743 sal_Bool bRes
= aThread
->create();
744 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
745 // first the thread run for some seconds, but not terminate
746 sal_Int32 nValue
= 0;
749 ThreadHelper::thread_sleep_tenth_sec(3);
750 nValue
= aThread
->getValue(); // (1)
751 t_print(" getValue is %d !", nValue
);
754 aThread
->setSuspend();
758 t_print(" after while!");
759 // the value just after calling suspend
760 nValue
= aThread
->getValue(); // (2)
762 ThreadHelper::thread_sleep_tenth_sec(3);
763 t_print(" after sleep!");
764 // the value after waiting 3 seconds
765 sal_Int32 nLaterValue
= aThread
->getValue(); // (3)
767 //resumeAndWaitThread(aThread);
769 termAndJoinThread(aThread
);
772 CPPUNIT_ASSERT_MESSAGE(
773 "Suspend the thread",
774 bRes
== sal_True
&& nValue
== nLaterValue
778 CPPUNIT_TEST_SUITE(suspend
);
779 CPPUNIT_TEST(suspend_001
);
780 // LLA: Deadlocked!!!
781 // CPPUNIT_TEST(createSuspended_002);
782 CPPUNIT_TEST_SUITE_END();
785 /** Test of the osl::Thread::resume method
787 class resume
: public CppUnit::TestFixture
790 // initialise your test code values here.
799 /** check if the thread run samely as usual after suspend and resume
802 compare the values before and after suspend, they should be same,
803 then compare values before and after resume, the difference should be same as the sleep seconds number
807 OCountThread
* pCountThread
= new OCountThread();
808 sal_Bool bRes
= pCountThread
->create();
809 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
811 suspendCountThread(pCountThread
);
813 sal_Int32 nSuspendValue
= pCountThread
->getValue(); // (2)
814 // suspend for 3 seconds
815 ThreadHelper::thread_sleep_tenth_sec(3);
816 pCountThread
->resume();
818 ThreadHelper::thread_sleep_tenth_sec(3);
819 sal_Int32 nResumeValue
= pCountThread
->getValue();
821 ThreadHelper::thread_sleep_tenth_sec(3);
822 sal_Int32 nLaterValue
= pCountThread
->getValue();
824 termAndJoinThread(pCountThread
);
827 t_print("SuspendValue: %d\n", nSuspendValue
);
828 t_print("ResumeValue: %d\n", nResumeValue
);
829 t_print("LaterValue: %d\n", nLaterValue
);
831 /* LLA: this assumption is no longer relevant: nResumeValue == nSuspendValue && */
832 CPPUNIT_ASSERT_MESSAGE(
833 "Suspend then resume the thread",
835 nResumeValue
> nSuspendValue
&&
836 nLaterValue
> nResumeValue
841 /** Create a suspended thread then resume, check if the thread has run
845 myThread
* newthread
= new myThread();
846 sal_Bool bRes
= newthread
->createSuspended();
847 CPPUNIT_ASSERT_MESSAGE ( "Can't create thread!", bRes
== sal_True
);
850 ThreadHelper::thread_sleep_tenth_sec(2);
851 sal_Int32 nValue
= newthread
->getValue();
853 termAndJoinThread(newthread
);
856 t_print(" nValue = %d\n", nValue
);
858 CPPUNIT_ASSERT_MESSAGE(
859 "Creates a suspended thread, then resume",
864 CPPUNIT_TEST_SUITE(resume
);
865 CPPUNIT_TEST(resume_001
);
866 CPPUNIT_TEST(resume_002
);
867 CPPUNIT_TEST_SUITE_END();
870 /** Test of the osl::Thread::terminate method
872 class terminate
: public CppUnit::TestFixture
875 // initialise your test code values here.
884 /** Check after call terminate if the running thread running go on executing
887 before and after call terminate, the values should be the same
891 OCountThread
* aCountThread
= new OCountThread();
892 sal_Bool bRes
= aCountThread
->create();
893 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
895 ThreadHelper::thread_sleep_tenth_sec(2);
896 sal_Int32 nValue
= aCountThread
->getValue();
897 aCountThread
->terminate();
898 ThreadHelper::thread_sleep_tenth_sec(2);
899 sal_Int32 nLaterValue
= aCountThread
->getValue();
901 // isRunning should be false after terminate
902 sal_Bool isRunning
= aCountThread
->isRunning();
903 aCountThread
->join();
906 t_print(" nValue = %d\n", nValue
);
907 t_print("nLaterValue = %d\n", nLaterValue
);
909 CPPUNIT_ASSERT_MESSAGE(
910 "Terminate the thread",
911 isRunning
== sal_False
&& nLaterValue
>= nValue
914 /** Check if a suspended thread will terminate after call terminate, different on w32 and on UNX
918 OCountThread
* aCountThread
= new OCountThread();
919 sal_Bool bRes
= aCountThread
->create();
920 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
922 ThreadHelper::thread_sleep_tenth_sec(1);
923 suspendCountThread(aCountThread
);
924 sal_Int32 nValue
= aCountThread
->getValue();
926 // seems a suspended thread can not be terminated on W32, while on Solaris can
927 resumeAndWaitThread(aCountThread
);
929 ThreadHelper::thread_sleep_tenth_sec(2);
931 termAndJoinThread(aCountThread
);
932 sal_Int32 nLaterValue
= aCountThread
->getValue();
935 t_print(" nValue = %d\n", nValue
);
936 t_print("nLaterValue = %d\n", nLaterValue
);
938 CPPUNIT_ASSERT_MESSAGE(
939 "Suspend then resume the thread",
940 nLaterValue
> nValue
);
943 CPPUNIT_TEST_SUITE(terminate
);
944 CPPUNIT_TEST(terminate_001
);
945 CPPUNIT_TEST(terminate_002
);
946 CPPUNIT_TEST_SUITE_END();
947 }; // class terminate
949 /** Test of the osl::Thread::join method
951 class join
: public CppUnit::TestFixture
954 // initialise your test code values here.
963 /** Check after call terminate if the thread running function will not go on executing
965 the next statement after join will not exec before the thread terminate
967 recode system time at the beginning of the thread run, call join, then record system time again,
968 the difference of the two time should be equal or more than 20 seconds, the CountThead normally terminate
972 OCountThread
*aCountThread
= new OCountThread();
973 sal_Bool bRes
= aCountThread
->create();
974 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
976 StopWatch aStopWatch
;
978 // TimeValue aTimeVal_befor;
979 // osl_getSystemTime( &aTimeVal_befor );
980 //t_print("#join:the system time is %d,%d\n", pTimeVal_befor->Seconds,pTimeVal_befor->Nanosec);
982 aCountThread
->join();
984 //the below line will be executed after aCountThread terminate
985 // TimeValue aTimeVal_after;
986 // osl_getSystemTime( &aTimeVal_after );
988 // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
989 double nSec
= aStopWatch
.getSeconds();
990 t_print("join_001 nSec=%f\n", nSec
);
993 CPPUNIT_ASSERT_MESSAGE(
994 "Join the thread: after the thread terminate",
999 /** after terminated by another thread, join exited immediately
1002 terminate the thread when value>=3, call join, check the beginning time and time after join,
1003 the difference should be 3 seconds, join costs little time
1007 OCountThread
*aCountThread
= new OCountThread();
1008 sal_Bool bRes
= aCountThread
->create();
1009 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
1011 //record the time when the running begin
1012 // TimeValue aTimeVal_befor;
1013 // osl_getSystemTime( &aTimeVal_befor );
1014 StopWatch aStopWatch
;
1017 ThreadHelper::thread_sleep_tenth_sec(10);
1018 termAndJoinThread(aCountThread
);
1020 //the below line will be executed after aCountThread terminate
1021 // TimeValue aTimeVal_after;
1022 // osl_getSystemTime( &aTimeVal_after );
1023 // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
1025 double nSec
= aStopWatch
.getSeconds();
1026 t_print("join_002 nSec=%f\n", nSec
);
1028 delete aCountThread
;
1029 CPPUNIT_ASSERT_MESSAGE(
1030 "Join the thread: after thread terminate by another thread",
1035 CPPUNIT_TEST_SUITE(join
);
1036 CPPUNIT_TEST(join_001
);
1037 CPPUNIT_TEST(join_002
);
1038 CPPUNIT_TEST_SUITE_END();
1041 /** Test of the osl::Thread::isRunning method
1043 class isRunning
: public CppUnit::TestFixture
1046 // initialise your test code values here.
1057 void isRunning_001()
1059 OCountThread
*aCountThread
= new OCountThread();
1060 sal_Bool bRes
= aCountThread
->create();
1061 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
1063 sal_Bool bRun
= aCountThread
->isRunning();
1065 ThreadHelper::thread_sleep_tenth_sec(2);
1066 termAndJoinThread(aCountThread
);
1067 sal_Bool bTer
= aCountThread
->isRunning();
1068 delete aCountThread
;
1070 CPPUNIT_ASSERT_MESSAGE(
1072 bRun
== sal_True
&& bTer
== sal_False
1075 /** check the value of isRunning when suspending and after resume
1077 void isRunning_002()
1079 OCountThread
*aCountThread
= new OCountThread();
1080 sal_Bool bRes
= aCountThread
->create();
1081 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
1083 // sal_Bool bRunning = aCountThread->isRunning();
1084 // sal_Int32 nValue = 0;
1085 suspendCountThread(aCountThread
);
1087 sal_Bool bRunning_sup
= aCountThread
->isRunning();
1088 ThreadHelper::thread_sleep_tenth_sec(2);
1089 aCountThread
->resume();
1090 ThreadHelper::thread_sleep_tenth_sec(2);
1091 sal_Bool bRunning_res
= aCountThread
->isRunning();
1092 termAndJoinThread(aCountThread
);
1093 sal_Bool bRunning_ter
= aCountThread
->isRunning();
1094 delete aCountThread
;
1096 CPPUNIT_ASSERT_MESSAGE(
1099 bRunning_sup
== sal_True
&&
1100 bRunning_res
== sal_True
&&
1101 bRunning_ter
== sal_False
1106 CPPUNIT_TEST_SUITE(isRunning
);
1107 CPPUNIT_TEST(isRunning_001
);
1108 CPPUNIT_TEST(isRunning_002
);
1109 CPPUNIT_TEST_SUITE_END();
1110 }; // class isRunning
1113 /// check osl::Thread::setPriority
1114 class setPriority
: public CppUnit::TestFixture
1117 // initialise your test code values here.
1126 // insert your test code here.
1127 rtl::OString
getPrioName(oslThreadPriority _aPriority
)
1129 rtl::OString sPrioStr
;
1132 case osl_Thread_PriorityHighest
:
1133 sPrioStr
= "Highest";
1136 case osl_Thread_PriorityAboveNormal
:
1137 sPrioStr
= "AboveNormal";
1139 case osl_Thread_PriorityNormal
:
1140 sPrioStr
= "Normal";
1142 case osl_Thread_PriorityBelowNormal
:
1143 sPrioStr
= "BelowNormal";
1146 case osl_Thread_PriorityLowest
:
1147 sPrioStr
= "Lowest";
1150 sPrioStr
= "unknown";
1156 /** check 2 threads.
1159 Here the function should show, that 2 different threads,
1160 which only increase a value, should run at the same time with same prio.
1161 The test fails, if the difference between the two values is more than 5%
1162 but IMHO this isn't a failure, it's only a feature of the OS.
1165 void check2Threads(oslThreadPriority _aPriority
)
1167 // initial 5 threads with different priorities
1168 OAddThread
* pThread
= new OAddThread();
1169 OAddThread
* p2Thread
= new OAddThread();
1171 //Create them and start running at the same time
1173 pThread
->setPriority(_aPriority
);
1175 p2Thread
->setPriority(_aPriority
);
1177 ThreadHelper::thread_sleep_tenth_sec(5);
1179 pThread
->terminate();
1180 p2Thread
->terminate();
1182 sal_Int32 nValueNormal
= 0;
1183 nValueNormal
= pThread
->getValue();
1185 sal_Int32 nValueNormal2
= 0;
1186 nValueNormal2
= p2Thread
->getValue();
1188 rtl::OString sPrio
= getPrioName(_aPriority
);
1189 t_print("After 10 tenth seconds\n");
1191 t_print("nValue in %s Prio Thread is %d\n",sPrio
.getStr(), nValueNormal
);
1192 t_print("nValue in %s Prio Thread is %d\n", sPrio
.getStr(), nValueNormal2
);
1194 // ThreadHelper::thread_sleep_tenth_sec(1);
1201 sal_Int32 nDelta
= abs(nValueNormal
- nValueNormal2
);
1202 double nQuotient
= std::max(nValueNormal
, nValueNormal2
);
1203 CPPUNIT_ASSERT_MESSAGE(
1204 "Quotient is zero, which means, there exist no right values.",
1207 double nDeltaPercent
= nDelta
/ nQuotient
* 100;
1209 t_print("Delta value %d, percent %f\n",nDelta
, nDeltaPercent
);
1211 // LLA: it's not a bug if the current OS is not able to handle thread scheduling right and good.
1213 // LLA: CPPUNIT_ASSERT_MESSAGE(
1214 // LLA: "Run 2 normal threads, the count diff more than 5 percent.",
1215 // LLA: nDeltaPercent <= 5
1219 void setPriority_001_1()
1221 check2Threads(osl_Thread_PriorityHighest
);
1223 void setPriority_001_2()
1225 check2Threads(osl_Thread_PriorityAboveNormal
);
1227 void setPriority_001_3()
1229 check2Threads(osl_Thread_PriorityNormal
);
1231 void setPriority_001_4()
1233 check2Threads(osl_Thread_PriorityBelowNormal
);
1235 void setPriority_001_5()
1237 check2Threads(osl_Thread_PriorityLowest
);
1240 void setPriority_002()
1242 // initial 5 threads with different priorities
1244 OAddThread aHighestThread
;
1245 OAddThread aAboveNormalThread
;
1246 OAddThread aNormalThread
;
1247 //OAddThread *aBelowNormalThread = new OAddThread();
1248 //OAddThread *aLowestThread = new OAddThread();
1250 //Create them and start running at the same time
1251 aHighestThread
.createSuspended();
1252 aHighestThread
.setPriority(osl_Thread_PriorityHighest
);
1254 aAboveNormalThread
.createSuspended();
1255 aAboveNormalThread
.setPriority(osl_Thread_PriorityAboveNormal
);
1257 aNormalThread
.createSuspended();
1258 aNormalThread
.setPriority(osl_Thread_PriorityNormal
);
1259 /*aBelowNormalThread->create();
1260 aBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
1261 aLowestThread->create();
1262 aLowestThread->setPriority(osl_Thread_PriorityLowest);
1265 aHighestThread
.resume();
1266 aAboveNormalThread
.resume();
1267 aNormalThread
.resume();
1269 ThreadHelper::thread_sleep_tenth_sec(5);
1271 aHighestThread
.suspend();
1272 aAboveNormalThread
.suspend();
1273 aNormalThread
.suspend();
1275 termAndJoinThread(&aNormalThread
);
1276 termAndJoinThread(&aAboveNormalThread
);
1277 termAndJoinThread(&aHighestThread
);
1278 //aBelowNormalThread->terminate();
1279 //aLowestThread->terminate();
1281 sal_Int32 nValueHighest
= 0;
1282 nValueHighest
= aHighestThread
.getValue();
1284 sal_Int32 nValueAboveNormal
= 0;
1285 nValueAboveNormal
= aAboveNormalThread
.getValue();
1287 sal_Int32 nValueNormal
= 0;
1288 nValueNormal
= aNormalThread
.getValue();
1290 // sal_Int32 nValueBelowNormal = 0;
1291 //nValueBelowNormal = aBelowNormalThread->getValue();
1292 // sal_Int32 nValueLowest = 0;
1293 //nValueLowest = aLowestThread->getValue();
1294 t_print("After 10 tenth seconds\n");
1295 t_print("nValue in Highest Prio Thread is %d\n",nValueHighest
);
1296 t_print("nValue in AboveNormal Prio Thread is %d\n",nValueAboveNormal
);
1297 t_print("nValue in Normal Prio Thread is %d\n",nValueNormal
);
1299 // LLA: this is not a save test, so we only check if all values not zero
1300 // LLA: CPPUNIT_ASSERT_MESSAGE(
1301 // LLA: "SetPriority",
1302 // LLA: nValueHighest >= nValueAboveNormal &&
1303 // LLA: nValueAboveNormal >= nValueNormal &&
1304 // LLA: nValueNormal > 0
1307 // LLA: windows let starve threads with lower priority
1309 CPPUNIT_ASSERT_MESSAGE(
1311 nValueHighest
> 0 &&
1312 nValueAboveNormal
> 0 &&
1318 void setPriority_003()
1320 // initial 5 threads with different priorities
1321 OAddThread
*pHighestThread
= new OAddThread();
1322 OAddThread
*pAboveNormalThread
= new OAddThread();
1323 OAddThread
*pNormalThread
= new OAddThread();
1324 OAddThread
*pBelowNormalThread
= new OAddThread();
1325 OAddThread
*pLowestThread
= new OAddThread();
1327 //Create them and start running at the same time
1328 pHighestThread
->createSuspended();
1329 pHighestThread
->setPriority(osl_Thread_PriorityHighest
);
1331 pAboveNormalThread
->createSuspended();
1332 pAboveNormalThread
->setPriority(osl_Thread_PriorityAboveNormal
);
1334 pNormalThread
->createSuspended();
1335 pNormalThread
->setPriority(osl_Thread_PriorityNormal
);
1337 pBelowNormalThread
->createSuspended();
1338 pBelowNormalThread
->setPriority(osl_Thread_PriorityBelowNormal
);
1340 pLowestThread
->createSuspended();
1341 pLowestThread
->setPriority(osl_Thread_PriorityLowest
);
1343 pHighestThread
->resume();
1344 pAboveNormalThread
->resume();
1345 pNormalThread
->resume();
1346 pBelowNormalThread
->resume();
1347 pLowestThread
->resume();
1349 ThreadHelper::thread_sleep_tenth_sec(5);
1351 pHighestThread
->suspend();
1352 pAboveNormalThread
->suspend();
1353 pNormalThread
->suspend();
1354 pBelowNormalThread
->suspend();
1355 pLowestThread
->suspend();
1357 termAndJoinThread(pHighestThread
);
1358 termAndJoinThread(pAboveNormalThread
);
1359 termAndJoinThread(pNormalThread
);
1360 termAndJoinThread(pBelowNormalThread
);
1361 termAndJoinThread(pLowestThread
);
1363 sal_Int32 nValueHighest
= 0;
1364 nValueHighest
= pHighestThread
->getValue();
1366 sal_Int32 nValueAboveNormal
= 0;
1367 nValueAboveNormal
= pAboveNormalThread
->getValue();
1369 sal_Int32 nValueNormal
= 0;
1370 nValueNormal
= pNormalThread
->getValue();
1372 sal_Int32 nValueBelowNormal
= 0;
1373 nValueBelowNormal
= pBelowNormalThread
->getValue();
1375 sal_Int32 nValueLowest
= 0;
1376 nValueLowest
= pLowestThread
->getValue();
1378 t_print("After 10 tenth seconds\n");
1379 t_print("nValue in Highest Prio Thread is %d\n",nValueHighest
);
1380 t_print("nValue in AboveNormal Prio Thread is %d\n",nValueAboveNormal
);
1381 t_print("nValue in Normal Prio Thread is %d\n",nValueNormal
);
1382 t_print("nValue in BelowNormal Prio Thread is %d\n",nValueBelowNormal
);
1383 t_print("nValue in Lowest Prio Thread is %d\n",nValueLowest
);
1385 delete pHighestThread
;
1386 delete pAboveNormalThread
;
1387 delete pNormalThread
;
1388 delete pBelowNormalThread
;
1389 delete pLowestThread
;
1391 // LLA: this is not a save test, so we only check if all values not zero
1392 // LLA: CPPUNIT_ASSERT_MESSAGE(
1393 // LLA: "SetPriority",
1394 // LLA: nValueHighest > nValueAboveNormal &&
1395 // LLA: nValueAboveNormal > nValueNormal &&
1396 // LLA: nValueNormal > nValueBelowNormal &&
1397 // LLA: nValueBelowNormal > nValueLowest &&
1398 // LLA: nValueLowest > 0
1401 // LLA: windows let starve threads with lower priority
1403 CPPUNIT_ASSERT_MESSAGE(
1405 nValueHighest
> 0 &&
1406 nValueAboveNormal
> 0 &&
1408 nValueBelowNormal
> 0 &&
1414 void setPriority_004()
1416 // initial 5 threads with different priorities
1417 // OAddThread *pHighestThread = new OAddThread();
1418 OAddThread
*pAboveNormalThread
= new OAddThread();
1419 OAddThread
*pNormalThread
= new OAddThread();
1420 OAddThread
*pBelowNormalThread
= new OAddThread();
1421 OAddThread
*pLowestThread
= new OAddThread();
1423 //Create them and start running at the same time
1424 // pHighestThread->createSuspended();
1425 // pHighestThread->setPriority(osl_Thread_PriorityHighest);
1427 pAboveNormalThread
->createSuspended();
1428 pAboveNormalThread
->setPriority(osl_Thread_PriorityAboveNormal
);
1430 pNormalThread
->createSuspended();
1431 pNormalThread
->setPriority(osl_Thread_PriorityNormal
);
1433 pBelowNormalThread
->createSuspended();
1434 pBelowNormalThread
->setPriority(osl_Thread_PriorityBelowNormal
);
1436 pLowestThread
->createSuspended();
1437 pLowestThread
->setPriority(osl_Thread_PriorityLowest
);
1439 // pHighestThread->resume();
1440 pAboveNormalThread
->resume();
1441 pNormalThread
->resume();
1442 pBelowNormalThread
->resume();
1443 pLowestThread
->resume();
1445 ThreadHelper::thread_sleep_tenth_sec(5);
1447 // pHighestThread->suspend();
1448 pAboveNormalThread
->suspend();
1449 pNormalThread
->suspend();
1450 pBelowNormalThread
->suspend();
1451 pLowestThread
->suspend();
1453 // termAndJoinThread(pHighestThread);
1454 termAndJoinThread(pAboveNormalThread
);
1455 termAndJoinThread(pNormalThread
);
1456 termAndJoinThread(pBelowNormalThread
);
1457 termAndJoinThread(pLowestThread
);
1459 // sal_Int32 nValueHighest = 0;
1460 // nValueHighest = pHighestThread->getValue();
1462 sal_Int32 nValueAboveNormal
= 0;
1463 nValueAboveNormal
= pAboveNormalThread
->getValue();
1465 sal_Int32 nValueNormal
= 0;
1466 nValueNormal
= pNormalThread
->getValue();
1468 sal_Int32 nValueBelowNormal
= 0;
1469 nValueBelowNormal
= pBelowNormalThread
->getValue();
1471 sal_Int32 nValueLowest
= 0;
1472 nValueLowest
= pLowestThread
->getValue();
1474 t_print("After 5 tenth seconds\n");
1475 // t_print("nValue in Highest Prio Thread is %d\n",nValueHighest);
1476 t_print("nValue in AboveNormal Prio Thread is %d\n",nValueAboveNormal
);
1477 t_print("nValue in Normal Prio Thread is %d\n",nValueNormal
);
1478 t_print("nValue in BelowNormal Prio Thread is %d\n",nValueBelowNormal
);
1479 t_print("nValue in Lowest Prio Thread is %d\n",nValueLowest
);
1481 // delete pHighestThread;
1482 delete pAboveNormalThread
;
1483 delete pNormalThread
;
1484 delete pBelowNormalThread
;
1485 delete pLowestThread
;
1487 // LLA: this is not a save test, so we only check if all values not zero
1488 // LLA: CPPUNIT_ASSERT_MESSAGE(
1489 // LLA: "SetPriority",
1490 // LLA: nValueHighest > nValueAboveNormal &&
1491 // LLA: nValueAboveNormal > nValueNormal &&
1492 // LLA: nValueNormal > nValueBelowNormal &&
1493 // LLA: nValueBelowNormal > nValueLowest &&
1494 // LLA: nValueLowest > 0
1497 // LLA: windows let starve threads with lower priority
1499 CPPUNIT_ASSERT_MESSAGE(
1501 /* nValueHighest > 0 && */
1502 nValueAboveNormal
> 0 &&
1504 nValueBelowNormal
> 0 &&
1509 void setPriority_005()
1511 // initial 5 threads with different priorities
1512 // OAddThread *pHighestThread = new OAddThread();
1513 // OAddThread *pAboveNormalThread = new OAddThread();
1514 OAddThread
*pNormalThread
= new OAddThread();
1515 OAddThread
*pBelowNormalThread
= new OAddThread();
1516 OAddThread
*pLowestThread
= new OAddThread();
1518 //Create them and start running at the same time
1519 // pHighestThread->createSuspended();
1520 // pHighestThread->setPriority(osl_Thread_PriorityHighest);
1522 // pAboveNormalThread->createSuspended();
1523 // pAboveNormalThread->setPriority(osl_Thread_PriorityAboveNormal);
1525 pNormalThread
->createSuspended();
1526 pNormalThread
->setPriority(osl_Thread_PriorityNormal
);
1528 pBelowNormalThread
->createSuspended();
1529 pBelowNormalThread
->setPriority(osl_Thread_PriorityBelowNormal
);
1531 pLowestThread
->createSuspended();
1532 pLowestThread
->setPriority(osl_Thread_PriorityLowest
);
1534 // pHighestThread->resume();
1535 // pAboveNormalThread->resume();
1536 pNormalThread
->resume();
1537 pBelowNormalThread
->resume();
1538 pLowestThread
->resume();
1540 ThreadHelper::thread_sleep_tenth_sec(5);
1542 // pHighestThread->suspend();
1543 // pAboveNormalThread->suspend();
1544 pNormalThread
->suspend();
1545 pBelowNormalThread
->suspend();
1546 pLowestThread
->suspend();
1548 // termAndJoinThread(pHighestThread);
1549 // termAndJoinThread(pAboveNormalThread);
1550 termAndJoinThread(pNormalThread
);
1551 termAndJoinThread(pBelowNormalThread
);
1552 termAndJoinThread(pLowestThread
);
1554 // sal_Int32 nValueHighest = 0;
1555 // nValueHighest = pHighestThread->getValue();
1557 // sal_Int32 nValueAboveNormal = 0;
1558 // nValueAboveNormal = pAboveNormalThread->getValue();
1560 sal_Int32 nValueNormal
= 0;
1561 nValueNormal
= pNormalThread
->getValue();
1563 sal_Int32 nValueBelowNormal
= 0;
1564 nValueBelowNormal
= pBelowNormalThread
->getValue();
1566 sal_Int32 nValueLowest
= 0;
1567 nValueLowest
= pLowestThread
->getValue();
1569 t_print("After 5 tenth seconds\n");
1570 // t_print("nValue in Highest Prio Thread is %d\n",nValueHighest);
1571 // t_print("nValue in AboveNormal Prio Thread is %d\n",nValueAboveNormal);
1572 t_print("nValue in Normal Prio Thread is %d\n",nValueNormal
);
1573 t_print("nValue in BelowNormal Prio Thread is %d\n",nValueBelowNormal
);
1574 t_print("nValue in Lowest Prio Thread is %d\n",nValueLowest
);
1576 // delete pHighestThread;
1577 // delete pAboveNormalThread;
1578 delete pNormalThread
;
1579 delete pBelowNormalThread
;
1580 delete pLowestThread
;
1582 // LLA: this is not a save test, so we only check if all values not zero
1583 // LLA: CPPUNIT_ASSERT_MESSAGE(
1584 // LLA: "SetPriority",
1585 // LLA: nValueHighest > nValueAboveNormal &&
1586 // LLA: nValueAboveNormal > nValueNormal &&
1587 // LLA: nValueNormal > nValueBelowNormal &&
1588 // LLA: nValueBelowNormal > nValueLowest &&
1589 // LLA: nValueLowest > 0
1592 // LLA: windows let starve threads with lower priority
1594 CPPUNIT_ASSERT_MESSAGE(
1596 /* nValueHighest > 0 && */
1597 /* nValueAboveNormal > 0 && */
1599 nValueBelowNormal
> 0 &&
1606 CPPUNIT_TEST_SUITE(setPriority
);
1608 CPPUNIT_TEST(setPriority_002
);
1609 CPPUNIT_TEST(setPriority_003
);
1610 CPPUNIT_TEST(setPriority_004
);
1611 CPPUNIT_TEST(setPriority_005
);
1613 CPPUNIT_TEST(setPriority_001_1
);
1614 CPPUNIT_TEST(setPriority_001_2
);
1615 CPPUNIT_TEST(setPriority_001_3
);
1616 CPPUNIT_TEST(setPriority_001_4
);
1617 CPPUNIT_TEST(setPriority_001_5
);
1618 CPPUNIT_TEST_SUITE_END();
1619 }; // class setPriority
1621 /** Test of the osl::Thread::getPriority method
1623 class getPriority
: public CppUnit::TestFixture
1626 // initialise your test code values here.
1635 // insert your test code here.
1636 void getPriority_001()
1638 OAddThread
*pHighestThread
= new OAddThread();
1640 //Create them and start running at the same time
1641 pHighestThread
->create();
1642 pHighestThread
->setPriority(osl_Thread_PriorityHighest
);
1644 oslThreadPriority aPriority
= pHighestThread
->getPriority();
1645 termAndJoinThread(pHighestThread
);
1646 delete pHighestThread
;
1648 ThreadHelper::outputPriority(aPriority
);
1650 // LLA: Priority settings may not work within some OS versions.
1651 #if ( defined WNT ) || ( defined SOLARIS )
1652 CPPUNIT_ASSERT_MESSAGE(
1654 aPriority
== osl_Thread_PriorityHighest
1658 // NO_PTHREAD_PRIORITY ???
1659 CPPUNIT_ASSERT_MESSAGE(
1661 aPriority
== osl_Thread_PriorityNormal
1666 void getPriority_002()
1671 CPPUNIT_TEST_SUITE(getPriority
);
1672 CPPUNIT_TEST(getPriority_001
);
1673 CPPUNIT_TEST(getPriority_002
);
1674 CPPUNIT_TEST_SUITE_END();
1675 }; // class getPriority
1678 class getIdentifier
: public CppUnit::TestFixture
1681 // initialise your test code values here.
1690 // insert your test code here.
1691 void getIdentifier_001()
1696 void getIdentifier_002()
1701 CPPUNIT_TEST_SUITE(getIdentifier
);
1702 CPPUNIT_TEST(getIdentifier_001
);
1703 CPPUNIT_TEST(getIdentifier_002
);
1704 CPPUNIT_TEST_SUITE_END();
1705 }; // class getIdentifier
1707 /** Test of the osl::Thread::getCurrentIdentifier method
1709 class getCurrentIdentifier
: public CppUnit::TestFixture
1712 // initialise your test code values here.
1721 // insert your test code here.
1722 void getCurrentIdentifier_001()
1724 oslThreadIdentifier oId
;
1725 OCountThread
* pCountThread
= new OCountThread
;
1726 //OCountThread* pCountThread2 = new OCountThread;
1727 pCountThread
->create();
1728 //pCountThread2->create();
1729 pCountThread
->setWait(3);
1730 oId
= Thread::getCurrentIdentifier();
1731 oslThreadIdentifier oIdChild
= pCountThread
->getIdentifier();
1732 //t_print("CurrentId is %ld, Child1 id is %ld, Child2 id is %ld\n",oId, oIdChild,pCountThread2->m_id );
1733 termAndJoinThread(pCountThread
);
1734 delete pCountThread
;
1735 //termAndJoinThread(pCountThread2);
1736 //delete pCountThread2;
1738 CPPUNIT_ASSERT_MESSAGE(
1739 "Get the identifier for the current active thread.",
1745 void getCurrentIdentifier_002()
1749 CPPUNIT_TEST_SUITE(getCurrentIdentifier
);
1750 CPPUNIT_TEST(getCurrentIdentifier_001
);
1751 //CPPUNIT_TEST(getCurrentIdentifier_002);
1752 CPPUNIT_TEST_SUITE_END();
1753 }; // class getCurrentIdentifier
1755 /** Test of the osl::Thread::wait method
1757 class wait
: public CppUnit::TestFixture
1760 // initialise your test code values here.
1769 /** call wait in the run method
1772 tested thread wait nWaitSec seconds, main thread sleep (2) seconds,
1773 then terminate the tested thread, due to the fact that the thread do a sleep(1) + wait(5)
1774 it's finish after 6 seconds.
1778 OCountThread
*aCountThread
= new OCountThread();
1779 sal_Int32 nWaitSec
= 5;
1780 aCountThread
->setWait(nWaitSec
);
1781 // thread runs at least 5 seconds.
1782 sal_Bool bRes
= aCountThread
->create();
1783 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
1785 //record the time when the running begin
1786 StopWatch aStopWatch
;
1789 // wait a little bit, to let the thread the time, to start
1790 ThreadHelper::thread_sleep_tenth_sec( 4 );
1793 // this function returns, after 4 sec. later
1794 termAndJoinThread(aCountThread
);
1796 // value should be one.
1797 sal_Int32 nValue
= aCountThread
->getValue();
1801 // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
1802 double nTenthSec
= aStopWatch
.getTenthSec();
1803 double nSec
= aStopWatch
.getSeconds();
1804 delete aCountThread
;
1805 t_print("nTenthSec = %f \n", nTenthSec
);
1806 t_print("nSec = %f \n", nSec
);
1807 t_print("nValue = %d \n", nValue
);
1809 CPPUNIT_ASSERT_MESSAGE(
1810 "Wait: Blocks the calling thread for the given number of time.",
1811 nTenthSec
>= 5 && nValue
== 1
1815 // LLA: wait_001 does the same.
1816 // LLA: /** wait then terminate the thread
1819 // LLA: wait nWaitSec seconds, and terminate when the wait does not finish
1820 // LLA: Windows & UNX: thread terminates immediatlly
1822 // LLA: void wait_002()
1824 // LLA: OCountThread aThread;
1826 // LLA: sal_Int32 nWaitSec = 3;
1827 // LLA: aThread.setWait(nWaitSec);
1829 // LLA: sal_Bool bRes = aThread.create();
1830 // LLA: CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
1832 // LLA: StopWatch aStopWatch;
1833 // LLA: // TimeValue aTimeVal_befor;
1834 // LLA: // osl_getSystemTime( &aTimeVal_befor );
1835 // LLA: aStopWatch.start();
1837 // LLA: termAndJoinThread(&aThread);
1838 // LLA: sal_Int32 nValue = aThread.getValue();
1840 // LLA: // TimeValue aTimeVal_after;
1841 // LLA: // osl_getSystemTime( &aTimeVal_after );
1842 // LLA: aStopWatch.stop();
1843 // LLA: // sal_Int32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
1844 // LLA: double nSec = aStopWatch.getSeconds();
1845 // LLA: t_print("sec=%f\n", nSec);
1846 // LLA: t_print("nValue = %d\n", nValue);
1848 // LLA: CPPUNIT_ASSERT_MESSAGE(
1849 // LLA: "Wait: Blocks the calling thread for the given number of time.",
1850 // LLA: nSec < 1 && nValue == 0
1854 CPPUNIT_TEST_SUITE(wait
);
1855 CPPUNIT_TEST(wait_001
);
1856 // LLA: CPPUNIT_TEST(wait_002);
1857 CPPUNIT_TEST_SUITE_END();
1860 /** osl::Thread::yield method: can not design good test scenario to test up to now
1862 class yield
: public CppUnit::TestFixture
1873 // insert your test code here.
1879 CPPUNIT_TEST_SUITE(yield
);
1880 CPPUNIT_TEST(yield_001
);
1881 CPPUNIT_TEST_SUITE_END();
1884 /** Test of the osl::Thread::schedule method
1886 class schedule
: public CppUnit::TestFixture
1889 // initialise your test code values here.
1898 /** The requested thread will get terminate the next time schedule() is called.
1900 Note: on UNX, if call suspend thread is not the to be suspended thread, the to be
1901 suspended thread will get suspended the next time schedule() is called,
1902 while on w32, it's nothing with schedule.
1904 check if suspend and terminate work well via schedule
1908 OAddThread
* aThread
= new OAddThread();
1909 sal_Bool bRes
= aThread
->create();
1910 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
1912 ThreadHelper::thread_sleep_tenth_sec(2);
1914 ThreadHelper::thread_sleep_tenth_sec(1);
1915 sal_Int32 nValue
= aThread
->getValue();
1916 ThreadHelper::thread_sleep_tenth_sec(3);
1917 sal_Int32 nLaterValue
= aThread
->getValue();
1918 // resumeAndWaitThread(aThread);
1919 t_print(" value = %d\n", nValue
);
1920 t_print("later value = %d\n", nLaterValue
);
1921 // if value and latervalue not equal, than the thread would not suspended
1923 CPPUNIT_ASSERT_MESSAGE(
1924 "Schedule: suspend works.",
1925 nLaterValue
== nValue
1929 ThreadHelper::thread_sleep_tenth_sec(2);
1931 aThread
->terminate();
1932 sal_Int32 nValue_term
= aThread
->getValue();
1935 sal_Int32 nValue_join
= aThread
->getValue();
1937 t_print("value after term = %d\n", nValue_term
);
1938 t_print("value after join = %d\n", nValue_join
);
1940 // nValue_term and nValue_join should be the same
1941 // but should be differ from nValue
1944 //check if thread really terminate after call terminate, if join immediatlly return
1945 CPPUNIT_ASSERT_MESSAGE(
1946 "Schedule: Returns False if the thread should terminate.",
1947 nValue_join
- nValue_term
<= 1 && nValue_join
- nValue_term
>= 0
1952 /** design a thread that has not call schedule in the workfunction--run method
1956 ONoScheduleThread aThread
; // this thread runs 10 sec. (no schedule() used)
1957 sal_Bool bRes
= aThread
.create();
1958 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
1960 ThreadHelper::thread_sleep_tenth_sec(2);
1962 sal_Int32 nValue
= aThread
.getValue();
1964 ThreadHelper::thread_sleep_tenth_sec(3);
1965 sal_Int32 nLaterValue
= aThread
.getValue();
1966 ThreadHelper::thread_sleep_tenth_sec(5);
1968 resumeAndWaitThread(&aThread
);
1970 t_print(" value = %d\n", nValue
);
1971 t_print("later value = %d\n", nLaterValue
);
1973 //On windows, suspend works, so the values are same
1975 CPPUNIT_ASSERT_MESSAGE(
1976 "Schedule: don't schedule in thread run method, suspend works.",
1977 nLaterValue
== nValue
1981 //On UNX, suspend does not work, so the difference of the values equals to sleep seconds number
1984 CPPUNIT_ASSERT_MESSAGE(
1985 "Schedule: don't schedule in thread run method, suspend does not work too.",
1986 nLaterValue
> nValue
1990 // terminate will not work if no schedule in thread's work function
1991 termAndJoinThread(&aThread
);
1992 sal_Int32 nValue_term
= aThread
.getValue();
1994 t_print(" value term = %d\n", nValue_term
);
1996 CPPUNIT_ASSERT_MESSAGE(
1997 "Schedule: don't schedule in thread run method, terminate failed.",
2002 CPPUNIT_TEST_SUITE(schedule
);
2003 CPPUNIT_TEST(schedule_001
);
2004 CPPUNIT_TEST(schedule_002
);
2005 CPPUNIT_TEST_SUITE_END();
2006 }; // class schedule
2008 // -----------------------------------------------------------------------------
2009 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::create
, "osl_Thread");
2010 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::createSuspended
, "osl_Thread");
2011 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::suspend
, "osl_Thread");
2012 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::resume
, "osl_Thread");
2013 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::terminate
, "osl_Thread");
2014 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::join
, "osl_Thread");
2015 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::isRunning
, "osl_Thread");
2016 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::setPriority
, "osl_Thread");
2017 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getPriority
, "osl_Thread");
2018 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getIdentifier
, "osl_Thread");
2019 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getCurrentIdentifier
, "osl_Thread");
2020 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::wait
, "osl_Thread");
2021 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::yield
, "osl_Thread");
2022 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::schedule
, "osl_Thread");
2023 } // namespace osl_Thread
2026 // -----------------------------------------------------------------------------
2027 // destroy function when the binding thread terminate
2028 void SAL_CALL
destroyCallback(void * data
)
2030 t_print("destroying local data %s\n", (char *) data
);
2031 delete[] (char *) data
;
2034 static ThreadData
myThreadData(destroyCallback
);
2038 class myKeyThread
: public Thread
2041 // a public char member for test result checking
2043 // for pass thread-special data to thread
2044 myKeyThread(const char cData
)
2053 char * pc
= new char[2];
2054 // strcpy(pc, &m_nData);
2055 memcpy(pc
, &m_nData
, 1);
2058 myThreadData
.setData(pc
);
2059 char* pData
= (char*)myThreadData
.getData();
2060 m_Char_Test
= *pData
;
2061 // wait for long time to check the data value in main thread
2062 ThreadHelper::thread_sleep_tenth_sec(3);
2069 t_print("error: not terminated.\n");
2074 static ThreadData idData
;
2076 class idThread
: public Thread
2079 oslThreadIdentifier m_Id
;
2083 oslThreadIdentifier
* pId
= new oslThreadIdentifier
;
2084 *pId
= getIdentifier();
2085 idData
.setData(pId
);
2086 oslThreadIdentifier
* pIdData
= (oslThreadIdentifier
*)idData
.getData();
2087 //t_print("Thread %d has Data %d\n", getIdentifier(), *pIdData);
2097 t_print("error: not terminated.\n");
2102 namespace osl_ThreadData
2105 class ctors
: public CppUnit::TestFixture
2108 // initialise your test code values here.
2117 // insert your test code here.
2123 CPPUNIT_TEST_SUITE(ctors
);
2124 CPPUNIT_TEST(ctor_001
);
2125 CPPUNIT_TEST_SUITE_END();
2129 class setData
: public CppUnit::TestFixture
2132 // initialise your test code values here.
2141 /** the same instance of the class can have different values in different threads
2153 oslThreadIdentifier aThreadId1
= aThread1
.getIdentifier();
2154 oslThreadIdentifier aThreadId2
= aThread2
.getIdentifier();
2156 CPPUNIT_ASSERT_MESSAGE(
2157 "ThreadData setData: ",
2158 aThread1
.m_Id
== aThreadId1
&& aThread2
.m_Id
== aThreadId2
2165 // at first, set the data a value
2166 char* pc
= new char[2];
2168 // LLA: this is a copy functions only and really only for \0 terminated strings
2169 // m_nData is not a string, it's a character
2170 // strcpy(pc, &m_nData);
2171 memcpy(pc
, &m_nData
, 1);
2174 myThreadData
.setData(pc
);
2176 myKeyThread
aThread1('a');
2178 myKeyThread
aThread2('b');
2180 // aThread1 and aThread2 should have not terminated yet, check current data, not 'a' 'b'
2181 char* pChar
= (char*)myThreadData
.getData();
2182 char aChar
= *pChar
;
2187 // the saved thread data of aThread1 & aThread2, different
2188 char cData1
= aThread1
.m_Char_Test
;
2189 char cData2
= aThread2
.m_Char_Test
;
2191 CPPUNIT_ASSERT_MESSAGE(
2192 "ThreadData setData: ",
2193 cData1
== 'a' && cData2
== 'b' && aChar
== 'm'
2197 /** setData the second time, and then getData
2201 // at first, set the data a value
2202 char* pc
= new char[2];
2204 // strcpy(pc, &m_nData);
2205 memcpy(pc
, &m_nData
, 1);
2207 myThreadData
.setData(pc
);
2209 myKeyThread
aThread1('a');
2211 myKeyThread
aThread2('b');
2213 // aThread1 and aThread2 should have not terminated yet
2214 // setData the second time
2215 char* pc2
= new char[2];
2217 // strcpy(pc2, &m_nData);
2218 memcpy(pc2
, &m_nData
, 1);
2221 myThreadData
.setData(pc2
);
2222 char* pChar
= (char*)myThreadData
.getData();
2223 char aChar
= *pChar
;
2228 // the saved thread data of aThread1 & aThread2, different
2229 char cData1
= aThread1
.m_Char_Test
;
2230 char cData2
= aThread2
.m_Char_Test
;
2232 CPPUNIT_ASSERT_MESSAGE(
2233 "ThreadData setData: ",
2234 cData1
== 'a' && cData2
== 'b' && aChar
== 'o'
2239 CPPUNIT_TEST_SUITE(setData
);
2240 CPPUNIT_TEST(setData_001
);
2241 CPPUNIT_TEST(setData_002
);
2242 CPPUNIT_TEST(setData_003
);
2243 CPPUNIT_TEST_SUITE_END();
2246 //sal_Bool buildTwoThreads(char)
2248 class getData
: public CppUnit::TestFixture
2251 // initialise your test code values here.
2260 // After setData in child threads, get Data in the main thread, should be independent
2263 char* pc
= new char[2];
2264 char m_nData
[] = "i";
2265 strcpy(pc
, m_nData
);
2266 t_print("pc %s\n", pc
);
2267 myThreadData
.setData(pc
);
2269 myKeyThread
aThread1('c');
2271 myKeyThread
aThread2('d');
2277 char cData1
= aThread1
.m_Char_Test
;
2278 char cData2
= aThread2
.m_Char_Test
;
2280 char* pChar
= (char*)myThreadData
.getData();
2281 char aChar
= *pChar
;
2283 CPPUNIT_ASSERT_MESSAGE(
2284 "ThreadData setData: ",
2285 cData1
== 'c' && cData2
== 'd' && aChar
== 'i'
2290 // setData then change the value in the address data pointer points,
2291 // and then getData, should get the new value
2294 char* pc
= new char[2];
2296 // strcpy(pc, &m_nData);
2297 memcpy(pc
, &m_nData
, 1);
2299 // strncpy(pc, &m_nData, sizeof(char);
2301 t_print("pc %s\n", pc
);
2302 myThreadData
.setData(pc
);
2304 myKeyThread
aThread1('a');
2306 myKeyThread
aThread2('b');
2309 // change the value which pc points
2310 char m_nData2
= 'j';
2311 // strcpy(pc, &m_nData2);
2312 memcpy(pc
, &m_nData2
, 1);
2315 //t_print("pc %s\n", pc);
2316 void* pChar
= myThreadData
.getData();
2317 char aChar
= *(char*)pChar
;
2322 char cData1
= aThread1
.m_Char_Test
;
2323 char cData2
= aThread2
.m_Char_Test
;
2325 CPPUNIT_ASSERT_MESSAGE(
2326 "ThreadData setData: ",
2327 cData1
== 'a' && cData2
== 'b' && aChar
== 'j'
2332 CPPUNIT_TEST_SUITE(getData
);
2333 CPPUNIT_TEST(getData_001
);
2334 CPPUNIT_TEST(getData_002
);
2335 CPPUNIT_TEST_SUITE_END();
2338 // -----------------------------------------------------------------------------
2339 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_ThreadData::ctors
, "osl_ThreadData");
2340 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_ThreadData::setData
, "osl_ThreadData");
2341 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_ThreadData::getData
, "osl_ThreadData");
2342 } // namespace osl_ThreadData
2344 // this macro creates an empty function, which will called by the RegisterAllFunctions()
2345 // to let the user the possibility to also register some functions by hand.