1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
27 //------------------------------------------------------------------------
29 //------------------------------------------------------------------------
30 #include <sal/types.h>
32 #include <rtl/string.hxx>
34 #include <rtl/strbuf.hxx>
36 #include <osl/thread.hxx>
38 #include <osl/mutex.hxx>
43 #include <cppunit/TestFixture.h>
44 #include <cppunit/extensions/HelperMacros.h>
45 #include <cppunit/plugin/TestPlugIn.h>
47 #define t_print printf
53 // -----------------------------------------------------------------------------
56 TimeValue t1
,t2
; // Start and stoptime
62 bool m_bIsValid
; // TRUE, when started and stopped
63 bool m_bIsRunning
; // TRUE, when started
69 void start(); // Starts time
70 void stop(); // Stops time
72 double getSeconds() const;
73 double getTenthSec() const;
76 // ================================= Stop Watch =================================
78 // A small stopwatch for internal use
79 // (c) Lars Langhans 29.12.1996 22:10
81 StopWatch::StopWatch():m_bIsValid(false),m_bIsRunning(false) {}
83 void StopWatch::start()
90 osl_getSystemTime( &t1
);
91 t_print("# %u %u nsecs\n", (unsigned)t1
.Seconds
, (unsigned)t1
.Nanosec
);
92 // gettimeofday(&t1, 0);
95 void StopWatch::stop()
97 // pre: Timer should be started
98 // post: Timer will stopped
100 // gettimeofday(&t2, 0); // Ask timer
101 osl_getSystemTime( &t2
);
102 t_print("# %u %u nsecs\n", (unsigned) t2
.Seconds
, (unsigned) t2
.Nanosec
);
105 { // check if started.
106 m_nSeconds
= static_cast<sal_Int32
>(t2
.Seconds
) - static_cast<sal_Int32
>(t1
.Seconds
);
107 if ( t2
.Nanosec
> t1
.Nanosec
)
108 m_nNanoSec
= static_cast<sal_Int32
>(t2
.Nanosec
) - static_cast<sal_Int32
>(t1
.Nanosec
);
111 m_nNanoSec
= 1000000000 + static_cast<sal_Int32
>(t2
.Nanosec
) - static_cast<sal_Int32
>(t1
.Nanosec
);
114 t_print("# %u %u nsecs\n", (unsigned) m_nSeconds
, (unsigned) m_nNanoSec
);
115 //if (m_nNanoSec < 0)
117 //m_nNanoSec += 1000000000;
121 m_bIsRunning
= false;
125 double StopWatch::getSeconds() const
128 // BACK: time in seconds
133 nValue
= double(m_nNanoSec
) / 1000000000.0 + m_nSeconds
; // milli micro nano
138 double StopWatch::getTenthSec() const
143 nValue
= double(m_nNanoSec
) / 100000000.0 + m_nSeconds
* 10;
148 // -----------------------------------------------------------------------------
150 class ThreadSafeValue
155 ThreadSafeValue(T n
= 0): m_nFlag(n
) {}
158 //block if already acquired by another thread.
159 osl::MutexGuard
g(m_aMutex
);
164 //only one thread operate on the flag.
165 osl::MutexGuard
g(m_aMutex
);
168 void acquire() {m_aMutex
.acquire();}
169 void release() {m_aMutex
.release();}
172 // -----------------------------------------------------------------------------
173 namespace ThreadHelper
175 void thread_sleep_tenth_sec(sal_Int32 _nTenthSec
)
178 Sleep(_nTenthSec
* 100 );
181 nTV
.Seconds
= static_cast<sal_uInt32
>( _nTenthSec
/10 );
182 nTV
.Nanosec
= ( (_nTenthSec
%10 ) * 100000000 );
183 osl_waitThread(&nTV
);
187 void outputPriority(oslThreadPriority
const& _aPriority
)
189 // LLA: output the priority
190 if (_aPriority
== osl_Thread_PriorityHighest
)
192 t_print("Prio is High\n");
194 else if (_aPriority
== osl_Thread_PriorityAboveNormal
)
196 t_print("Prio is above normal\n");
198 else if (_aPriority
== osl_Thread_PriorityNormal
)
200 t_print("Prio is normal\n");
202 else if (_aPriority
== osl_Thread_PriorityBelowNormal
)
204 t_print("Prio is below normal\n");
206 else if (_aPriority
== osl_Thread_PriorityLowest
)
208 t_print("Prio is lowest\n");
212 t_print("Prio is unknown\n");
217 /** Simple thread for testing Thread-create.
219 Just add 1 of value 0, and after running, result is 1.
221 class myThread
: public Thread
223 ThreadSafeValue
<sal_Int32
> m_aFlag
;
225 sal_Int32
getValue() { return m_aFlag
.getValue(); }
227 /** guarded value which initialized 0
236 ThreadHelper::thread_sleep_tenth_sec(1);
242 virtual void SAL_CALL
suspend()
245 ::osl::Thread::suspend();
253 t_print("error: not terminated.\n");
259 // -----------------------------------------------------------------------------
260 /** Thread which has a flag add 1 every second until 20
262 class OCountThread
: public Thread
264 ThreadSafeValue
<sal_Int32
> m_aFlag
;
269 t_print("new OCountThread thread %u!\n", (unsigned) getIdentifier());
271 sal_Int32
getValue() { return m_aFlag
.getValue(); }
273 void setWait(sal_Int32 nSec
)
276 //m_bWait = sal_True;
279 virtual void SAL_CALL
suspend()
282 ::osl::Thread::suspend();
288 sal_Int32 m_nWaitSec
;
292 /// if the thread should terminate, schedule return false
293 while (m_aFlag
.getValue() < 20 && schedule() == sal_True
)
296 ThreadHelper::thread_sleep_tenth_sec(1);
301 nTV
.Seconds
= m_nWaitSec
/ 10 ;
302 nTV
.Nanosec
= ( m_nWaitSec
%10 ) * 100000000 ;
308 void SAL_CALL
onTerminated()
310 t_print("normally terminate this thread %u!\n", (unsigned) getIdentifier());
318 t_print("error: not terminated.\n");
324 /** call suspend in the run method
326 class OSuspendThread
: public Thread
328 ThreadSafeValue
<sal_Int32
> m_aFlag
;
330 OSuspendThread(){ m_bSuspend
= sal_False
; }
331 sal_Int32
getValue() { return m_aFlag
.getValue(); }
334 m_bSuspend
= sal_True
;
336 virtual void SAL_CALL
suspend()
339 ::osl::Thread::suspend();
346 //if the thread should terminate, schedule return false
347 while (schedule() == sal_True
)
351 ThreadHelper::thread_sleep_tenth_sec(1);
352 if (m_bSuspend
== sal_True
)
355 m_bSuspend
= sal_False
;
365 t_print("error: not terminated.\n");
371 /** no call schedule in the run method
373 class ONoScheduleThread
: public Thread
375 ThreadSafeValue
<sal_Int32
> m_aFlag
;
377 sal_Int32
getValue() { return m_aFlag
.getValue(); }
379 virtual void SAL_CALL
suspend()
382 ::osl::Thread::suspend();
388 while (m_aFlag
.getValue() < 10)
391 ThreadHelper::thread_sleep_tenth_sec(1);
394 void SAL_CALL
onTerminated()
396 t_print("normally terminate this thread %u!\n", (unsigned) getIdentifier());
401 t_print("new thread id %u!\n", (unsigned) getIdentifier());
407 t_print("error: not terminated.\n");
415 class OAddThread
: public Thread
417 ThreadSafeValue
<sal_Int32
> m_aFlag
;
419 //oslThreadIdentifier m_id, m_CurId;
421 sal_Int32
getValue() { return m_aFlag
.getValue(); }
423 virtual void SAL_CALL
suspend()
426 ::osl::Thread::suspend();
432 //if the thread should terminate, schedule return false
433 while (schedule() == sal_True
)
438 void SAL_CALL
onTerminated()
440 // t_print("normally terminate this thread %d!\n", getIdentifier());
448 // t_print("error: not terminated.\n");
457 void resumeAndWaitThread(Thread
* _pThread
)
459 // This functions starts a thread, wait a second and suspends the thread
460 // Due to the fact, that a suspend and never run thread never really exists.
462 // Note: on UNX, after createSuspended, and then terminate the thread, it performs well;
463 // while on Windows, after createSuspended, the thread can not terminate, wait endlessly,
464 // so here call resume at first, then call terminate.
466 t_print("resumeAndWaitThread\n");
468 ThreadHelper::thread_sleep_tenth_sec(1);
474 // kill a running thread and join it, if it has terminated, do nothing
475 void termAndJoinThread(Thread
* _pThread
)
477 _pThread
->terminate();
479 // LLA: Windows feature???, a suspended thread can not terminated, so we have to weak it up
482 ThreadHelper::thread_sleep_tenth_sec(1);
484 t_print("#wait for join.\n");
487 /** Test of the osl::Thread::create method
490 class create
: public CppUnit::TestFixture
494 // initialise your test code values here.
503 /** Simple create a thread.
505 Create a simple thread, it just does add 1 to value(which initialized 0),
506 if the thread run, the value should be 1.
510 myThread
* newthread
= new myThread();
511 sal_Bool bRes
= newthread
->create();
512 CPPUNIT_ASSERT_MESSAGE("Can not creates a new thread!\n", bRes
== sal_True
);
514 ThreadHelper::thread_sleep_tenth_sec(1); // wait short
515 sal_Bool isRunning
= newthread
->isRunning(); // check if thread is running
516 /// wait for the new thread to assure it has run
517 ThreadHelper::thread_sleep_tenth_sec(3);
518 sal_Int32 nValue
= newthread
->getValue();
519 /// to assure the new thread has terminated
520 termAndJoinThread(newthread
);
523 t_print(" nValue = %d\n", (int) nValue
);
524 t_print("isRunning = %s\n", isRunning
== sal_True
? "true" : "false");
526 CPPUNIT_ASSERT_MESSAGE(
527 "Creates a new thread",
528 nValue
>= 1 && isRunning
== sal_True
533 /** only one running thread per instance, return false if create secondly
537 myThread
* newthread
= new myThread();
538 sal_Bool res1
= newthread
->create();
539 sal_Bool res2
= newthread
->create();
540 t_print("In non pro, an assertion should occurred. This behaviour is right.\n");
541 termAndJoinThread(newthread
);
544 CPPUNIT_ASSERT_MESSAGE(
545 "Creates a new thread: can not create two threads per instance",
551 CPPUNIT_TEST_SUITE(create
);
552 CPPUNIT_TEST(create_001
);
553 CPPUNIT_TEST(create_002
);
554 CPPUNIT_TEST_SUITE_END();
559 /** Test of the osl::Thread::createSuspended method
561 class createSuspended
: public CppUnit::TestFixture
564 // initialise your test code values here.
573 /** Create a suspended thread, use the same class as create_001
575 after create, wait enough time, check the value, if it's still the initial value, pass
577 void createSuspended_001()
579 myThread
* newthread
= new myThread();
580 sal_Bool bRes
= newthread
->createSuspended();
581 CPPUNIT_ASSERT_MESSAGE("Can not creates a new thread!", bRes
== sal_True
);
583 ThreadHelper::thread_sleep_tenth_sec(1);
584 sal_Bool isRunning
= newthread
->isRunning();
585 ThreadHelper::thread_sleep_tenth_sec(3);
586 sal_Int32 nValue
= newthread
->getValue();
588 resumeAndWaitThread(newthread
);
590 termAndJoinThread(newthread
);
593 CPPUNIT_ASSERT_MESSAGE(
594 "Creates a new suspended thread",
595 nValue
== 0 && isRunning
599 void createSuspended_002()
601 myThread
* newthread
= new myThread();
602 sal_Bool res1
= newthread
->createSuspended();
603 sal_Bool res2
= newthread
->createSuspended();
605 resumeAndWaitThread(newthread
);
607 termAndJoinThread(newthread
);
611 CPPUNIT_ASSERT_MESSAGE(
612 "Creates a new thread: can not create two threads per instance",
617 CPPUNIT_TEST_SUITE(createSuspended
);
618 CPPUNIT_TEST(createSuspended_001
);
619 // LLA: Deadlocked!!!
620 CPPUNIT_TEST(createSuspended_002
);
621 CPPUNIT_TEST_SUITE_END();
622 }; // class createSuspended
624 /** when the count value equal to or more than 3, suspend the thread.
626 void suspendCountThread(OCountThread
* _pCountThread
)
628 sal_Int32 nValue
= 0;
631 nValue
= _pCountThread
->getValue();
634 _pCountThread
->suspend();
640 /** Test of the osl::Thread::suspend method
642 class suspend
: public CppUnit::TestFixture
645 // initialise your test code values here.
654 /** Use a thread which has a flag added 1 every second
657 create the thread, after running special time, record value of flag, then suspend it,
658 wait a long time, check the flag, if it remains unchanged during suspending
662 OCountThread
* aCountThread
= new OCountThread();
663 sal_Bool bRes
= aCountThread
->create();
664 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
665 // the thread run for some seconds, but not terminate
666 suspendCountThread( aCountThread
);
668 // the value just after calling suspend
669 sal_Int32 nValue
= aCountThread
->getValue(); // (2)
671 ThreadHelper::thread_sleep_tenth_sec(3);
673 // the value after waiting 3 seconds
674 sal_Int32 nLaterValue
= aCountThread
->getValue(); // (3)
676 resumeAndWaitThread(aCountThread
);
677 termAndJoinThread(aCountThread
);
680 CPPUNIT_ASSERT_MESSAGE(
681 "Suspend the thread",
682 bRes
== sal_True
&& nValue
== nLaterValue
686 /** suspend a thread in it's worker-function, the ALGORITHM is same as suspend_001
687 reason of deadlocked I think: no schedule can schedule other threads to go on excuting
691 OSuspendThread
* aThread
= new OSuspendThread();
692 sal_Bool bRes
= aThread
->create();
693 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
694 // first the thread run for some seconds, but not terminate
695 sal_Int32 nValue
= 0;
698 ThreadHelper::thread_sleep_tenth_sec(3);
699 nValue
= aThread
->getValue(); // (1)
700 t_print(" getValue is %d !", (int) nValue
);
703 aThread
->setSuspend();
707 t_print(" after while!");
708 // the value just after calling suspend
709 nValue
= aThread
->getValue(); // (2)
711 ThreadHelper::thread_sleep_tenth_sec(3);
712 t_print(" after sleep!");
713 // the value after waiting 3 seconds
714 sal_Int32 nLaterValue
= aThread
->getValue(); // (3)
716 //resumeAndWaitThread(aThread);
718 termAndJoinThread(aThread
);
721 CPPUNIT_ASSERT_MESSAGE(
722 "Suspend the thread",
723 bRes
== sal_True
&& nValue
== nLaterValue
727 CPPUNIT_TEST_SUITE(suspend
);
728 CPPUNIT_TEST(suspend_001
);
729 // LLA: Deadlocked!!!
730 // CPPUNIT_TEST(createSuspended_002);
731 CPPUNIT_TEST_SUITE_END();
734 /** Test of the osl::Thread::resume method
736 class resume
: public CppUnit::TestFixture
739 // initialise your test code values here.
748 /** check if the thread run samely as usual after suspend and resume
751 compare the values before and after suspend, they should be same,
752 then compare values before and after resume, the difference should be same as the sleep seconds number
756 OCountThread
* pCountThread
= new OCountThread();
757 sal_Bool bRes
= pCountThread
->create();
758 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
760 suspendCountThread(pCountThread
);
762 sal_Int32 nSuspendValue
= pCountThread
->getValue(); // (2)
763 // suspend for 3 seconds
764 ThreadHelper::thread_sleep_tenth_sec(3);
765 pCountThread
->resume();
767 ThreadHelper::thread_sleep_tenth_sec(3);
768 sal_Int32 nResumeValue
= pCountThread
->getValue();
770 ThreadHelper::thread_sleep_tenth_sec(3);
771 sal_Int32 nLaterValue
= pCountThread
->getValue();
773 termAndJoinThread(pCountThread
);
776 t_print("SuspendValue: %d\n", (int) nSuspendValue
);
777 t_print("ResumeValue: %d\n", (int) nResumeValue
);
778 t_print("LaterValue: %d\n", (int) nLaterValue
);
780 /* LLA: this assumption is no longer relevant: nResumeValue == nSuspendValue && */
781 CPPUNIT_ASSERT_MESSAGE(
782 "Suspend then resume the thread",
784 nResumeValue
> nSuspendValue
&&
785 nLaterValue
> nResumeValue
790 /** Create a suspended thread then resume, check if the thread has run
794 myThread
* newthread
= new myThread();
795 sal_Bool bRes
= newthread
->createSuspended();
796 CPPUNIT_ASSERT_MESSAGE ( "Can't create thread!", bRes
== sal_True
);
799 ThreadHelper::thread_sleep_tenth_sec(2);
800 sal_Int32 nValue
= newthread
->getValue();
802 termAndJoinThread(newthread
);
805 t_print(" nValue = %d\n", (int) nValue
);
807 CPPUNIT_ASSERT_MESSAGE(
808 "Creates a suspended thread, then resume",
813 CPPUNIT_TEST_SUITE(resume
);
814 CPPUNIT_TEST(resume_001
);
815 CPPUNIT_TEST(resume_002
);
816 CPPUNIT_TEST_SUITE_END();
819 /** Test of the osl::Thread::terminate method
821 class terminate
: public CppUnit::TestFixture
824 // initialise your test code values here.
833 /** Check after call terminate if the running thread running go on executing
836 before and after call terminate, the values should be the same
840 OCountThread
* aCountThread
= new OCountThread();
841 sal_Bool bRes
= aCountThread
->create();
842 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
844 ThreadHelper::thread_sleep_tenth_sec(2);
845 sal_Int32 nValue
= aCountThread
->getValue();
846 aCountThread
->terminate();
847 ThreadHelper::thread_sleep_tenth_sec(2);
848 sal_Int32 nLaterValue
= aCountThread
->getValue();
850 // isRunning should be false after terminate
851 sal_Bool isRunning
= aCountThread
->isRunning();
852 aCountThread
->join();
855 t_print(" nValue = %d\n", (int) nValue
);
856 t_print("nLaterValue = %d\n", (int) nLaterValue
);
858 CPPUNIT_ASSERT_MESSAGE(
859 "Terminate the thread",
860 isRunning
== sal_False
&& nLaterValue
>= nValue
863 /** Check if a suspended thread will terminate after call terminate, different on w32 and on UNX
867 OCountThread
* aCountThread
= new OCountThread();
868 sal_Bool bRes
= aCountThread
->create();
869 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
871 ThreadHelper::thread_sleep_tenth_sec(1);
872 suspendCountThread(aCountThread
);
873 sal_Int32 nValue
= aCountThread
->getValue();
875 // seems a suspended thread can not be terminated on W32, while on Solaris can
876 resumeAndWaitThread(aCountThread
);
878 ThreadHelper::thread_sleep_tenth_sec(2);
880 termAndJoinThread(aCountThread
);
881 sal_Int32 nLaterValue
= aCountThread
->getValue();
884 t_print(" nValue = %d\n", (int) nValue
);
885 t_print("nLaterValue = %d\n", (int) nLaterValue
);
887 CPPUNIT_ASSERT_MESSAGE(
888 "Suspend then resume the thread",
889 nLaterValue
> nValue
);
892 CPPUNIT_TEST_SUITE(terminate
);
893 CPPUNIT_TEST(terminate_001
);
894 CPPUNIT_TEST(terminate_002
);
895 CPPUNIT_TEST_SUITE_END();
896 }; // class terminate
898 /** Test of the osl::Thread::join method
900 class join
: public CppUnit::TestFixture
903 // initialise your test code values here.
912 /** Check after call terminate if the thread running function will not go on executing
914 the next statement after join will not exec before the thread terminate
916 recode system time at the beginning of the thread run, call join, then record system time again,
917 the difference of the two time should be equal or more than 20 seconds, the CountThead normally terminate
921 OCountThread
*aCountThread
= new OCountThread();
922 sal_Bool bRes
= aCountThread
->create();
923 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
925 StopWatch aStopWatch
;
927 // TimeValue aTimeVal_befor;
928 // osl_getSystemTime( &aTimeVal_befor );
929 //t_print("#join:the system time is %d,%d\n", pTimeVal_befor->Seconds,pTimeVal_befor->Nanosec);
931 aCountThread
->join();
933 //the below line will be executed after aCountThread terminate
934 // TimeValue aTimeVal_after;
935 // osl_getSystemTime( &aTimeVal_after );
937 // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
938 double nSec
= aStopWatch
.getSeconds();
939 t_print("join_001 nSec=%f\n", nSec
);
942 CPPUNIT_ASSERT_MESSAGE(
943 "Join the thread: after the thread terminate",
948 /** after terminated by another thread, join exited immediately
951 terminate the thread when value>=3, call join, check the beginning time and time after join,
952 the difference should be 3 seconds, join costs little time
956 OCountThread
*aCountThread
= new OCountThread();
957 sal_Bool bRes
= aCountThread
->create();
958 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
960 //record the time when the running begin
961 // TimeValue aTimeVal_befor;
962 // osl_getSystemTime( &aTimeVal_befor );
963 StopWatch aStopWatch
;
966 ThreadHelper::thread_sleep_tenth_sec(10);
967 termAndJoinThread(aCountThread
);
969 //the below line will be executed after aCountThread terminate
970 // TimeValue aTimeVal_after;
971 // osl_getSystemTime( &aTimeVal_after );
972 // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
974 double nSec
= aStopWatch
.getSeconds();
975 t_print("join_002 nSec=%f\n", nSec
);
978 CPPUNIT_ASSERT_MESSAGE(
979 "Join the thread: after thread terminate by another thread",
984 CPPUNIT_TEST_SUITE(join
);
985 CPPUNIT_TEST(join_001
);
986 CPPUNIT_TEST(join_002
);
987 CPPUNIT_TEST_SUITE_END();
990 /** Test of the osl::Thread::isRunning method
992 class isRunning
: public CppUnit::TestFixture
995 // initialise your test code values here.
1006 void isRunning_001()
1008 OCountThread
*aCountThread
= new OCountThread();
1009 sal_Bool bRes
= aCountThread
->create();
1010 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
1012 sal_Bool bRun
= aCountThread
->isRunning();
1014 ThreadHelper::thread_sleep_tenth_sec(2);
1015 termAndJoinThread(aCountThread
);
1016 sal_Bool bTer
= aCountThread
->isRunning();
1017 delete aCountThread
;
1019 CPPUNIT_ASSERT_MESSAGE(
1021 bRun
== sal_True
&& bTer
== sal_False
1024 /** check the value of isRunning when suspending and after resume
1026 void isRunning_002()
1028 OCountThread
*aCountThread
= new OCountThread();
1029 sal_Bool bRes
= aCountThread
->create();
1030 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
1032 // sal_Bool bRunning = aCountThread->isRunning();
1033 // sal_Int32 nValue = 0;
1034 suspendCountThread(aCountThread
);
1036 sal_Bool bRunning_sup
= aCountThread
->isRunning();
1037 ThreadHelper::thread_sleep_tenth_sec(2);
1038 aCountThread
->resume();
1039 ThreadHelper::thread_sleep_tenth_sec(2);
1040 sal_Bool bRunning_res
= aCountThread
->isRunning();
1041 termAndJoinThread(aCountThread
);
1042 sal_Bool bRunning_ter
= aCountThread
->isRunning();
1043 delete aCountThread
;
1045 CPPUNIT_ASSERT_MESSAGE(
1048 bRunning_sup
== sal_True
&&
1049 bRunning_res
== sal_True
&&
1050 bRunning_ter
== sal_False
1055 CPPUNIT_TEST_SUITE(isRunning
);
1056 CPPUNIT_TEST(isRunning_001
);
1057 CPPUNIT_TEST(isRunning_002
);
1058 CPPUNIT_TEST_SUITE_END();
1059 }; // class isRunning
1062 /// check osl::Thread::setPriority
1063 class setPriority
: public CppUnit::TestFixture
1066 // initialise your test code values here.
1075 // insert your test code here.
1076 rtl::OString
getPrioName(oslThreadPriority _aPriority
)
1078 rtl::OString sPrioStr
;
1081 case osl_Thread_PriorityHighest
:
1082 sPrioStr
= "Highest";
1085 case osl_Thread_PriorityAboveNormal
:
1086 sPrioStr
= "AboveNormal";
1089 case osl_Thread_PriorityNormal
:
1090 sPrioStr
= "Normal";
1093 case osl_Thread_PriorityBelowNormal
:
1094 sPrioStr
= "BelowNormal";
1097 case osl_Thread_PriorityLowest
:
1098 sPrioStr
= "Lowest";
1101 sPrioStr
= "unknown";
1107 /** check 2 threads.
1110 Here the function should show, that 2 different threads,
1111 which only increase a value, should run at the same time with same prio.
1112 The test fails, if the difference between the two values is more than 5%
1113 but IMHO this isn't a failure, it's only a feature of the OS.
1116 void check2Threads(oslThreadPriority _aPriority
)
1118 // initial 5 threads with different priorities
1119 OAddThread
* pThread
= new OAddThread();
1120 OAddThread
* p2Thread
= new OAddThread();
1122 //Create them and start running at the same time
1124 pThread
->setPriority(_aPriority
);
1126 p2Thread
->setPriority(_aPriority
);
1128 ThreadHelper::thread_sleep_tenth_sec(5);
1130 pThread
->terminate();
1131 p2Thread
->terminate();
1133 sal_Int32 nValueNormal
= 0;
1134 nValueNormal
= pThread
->getValue();
1136 sal_Int32 nValueNormal2
= 0;
1137 nValueNormal2
= p2Thread
->getValue();
1139 rtl::OString sPrio
= getPrioName(_aPriority
);
1140 t_print("After 10 tenth seconds\n");
1142 t_print("nValue in %s Prio Thread is %d\n",sPrio
.getStr(), (int) nValueNormal
);
1143 t_print("nValue in %s Prio Thread is %d\n", sPrio
.getStr(), (int) nValueNormal2
);
1145 // ThreadHelper::thread_sleep_tenth_sec(1);
1152 sal_Int32 nDelta
= abs(nValueNormal
- nValueNormal2
);
1153 double nQuotient
= std::max(nValueNormal
, nValueNormal2
);
1154 CPPUNIT_ASSERT_MESSAGE(
1155 "Quotient is zero, which means, there exist no right values.",
1158 double nDeltaPercent
= nDelta
/ nQuotient
* 100;
1160 t_print("Delta value %d, percent %f\n", (int) nDelta
, nDeltaPercent
);
1162 // LLA: it's not a bug if the current OS is not able to handle thread scheduling right and good.
1164 // LLA: CPPUNIT_ASSERT_MESSAGE(
1165 // LLA: "Run 2 normal threads, the count diff more than 5 percent.",
1166 // LLA: nDeltaPercent <= 5
1170 void setPriority_001_1()
1172 check2Threads(osl_Thread_PriorityHighest
);
1174 void setPriority_001_2()
1176 check2Threads(osl_Thread_PriorityAboveNormal
);
1178 void setPriority_001_3()
1180 check2Threads(osl_Thread_PriorityNormal
);
1182 void setPriority_001_4()
1184 check2Threads(osl_Thread_PriorityBelowNormal
);
1186 void setPriority_001_5()
1188 check2Threads(osl_Thread_PriorityLowest
);
1191 void setPriority_002()
1193 // initial 5 threads with different priorities
1195 OAddThread aHighestThread
;
1196 OAddThread aAboveNormalThread
;
1197 OAddThread aNormalThread
;
1198 //OAddThread *aBelowNormalThread = new OAddThread();
1199 //OAddThread *aLowestThread = new OAddThread();
1201 //Create them and start running at the same time
1202 aHighestThread
.createSuspended();
1203 aHighestThread
.setPriority(osl_Thread_PriorityHighest
);
1205 aAboveNormalThread
.createSuspended();
1206 aAboveNormalThread
.setPriority(osl_Thread_PriorityAboveNormal
);
1208 aNormalThread
.createSuspended();
1209 aNormalThread
.setPriority(osl_Thread_PriorityNormal
);
1210 /*aBelowNormalThread->create();
1211 aBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
1212 aLowestThread->create();
1213 aLowestThread->setPriority(osl_Thread_PriorityLowest);
1216 aHighestThread
.resume();
1217 aAboveNormalThread
.resume();
1218 aNormalThread
.resume();
1220 ThreadHelper::thread_sleep_tenth_sec(5);
1222 aHighestThread
.suspend();
1223 aAboveNormalThread
.suspend();
1224 aNormalThread
.suspend();
1226 termAndJoinThread(&aNormalThread
);
1227 termAndJoinThread(&aAboveNormalThread
);
1228 termAndJoinThread(&aHighestThread
);
1229 //aBelowNormalThread->terminate();
1230 //aLowestThread->terminate();
1232 sal_Int32 nValueHighest
= 0;
1233 nValueHighest
= aHighestThread
.getValue();
1235 sal_Int32 nValueAboveNormal
= 0;
1236 nValueAboveNormal
= aAboveNormalThread
.getValue();
1238 sal_Int32 nValueNormal
= 0;
1239 nValueNormal
= aNormalThread
.getValue();
1241 t_print("After 10 tenth seconds\n");
1242 t_print("nValue in Highest Prio Thread is %d\n", (int) nValueHighest
);
1243 t_print("nValue in AboveNormal Prio Thread is %d\n", (int) nValueAboveNormal
);
1244 t_print("nValue in Normal Prio Thread is %d\n", (int) nValueNormal
);
1247 CPPUNIT_ASSERT_MESSAGE(
1249 nValueHighest
> 0 &&
1250 nValueAboveNormal
> 0 &&
1256 void setPriority_003()
1258 // initial 5 threads with different priorities
1259 OAddThread
*pHighestThread
= new OAddThread();
1260 OAddThread
*pAboveNormalThread
= new OAddThread();
1261 OAddThread
*pNormalThread
= new OAddThread();
1262 OAddThread
*pBelowNormalThread
= new OAddThread();
1263 OAddThread
*pLowestThread
= new OAddThread();
1265 //Create them and start running at the same time
1266 pHighestThread
->createSuspended();
1267 pHighestThread
->setPriority(osl_Thread_PriorityHighest
);
1269 pAboveNormalThread
->createSuspended();
1270 pAboveNormalThread
->setPriority(osl_Thread_PriorityAboveNormal
);
1272 pNormalThread
->createSuspended();
1273 pNormalThread
->setPriority(osl_Thread_PriorityNormal
);
1275 pBelowNormalThread
->createSuspended();
1276 pBelowNormalThread
->setPriority(osl_Thread_PriorityBelowNormal
);
1278 pLowestThread
->createSuspended();
1279 pLowestThread
->setPriority(osl_Thread_PriorityLowest
);
1281 pHighestThread
->resume();
1282 pAboveNormalThread
->resume();
1283 pNormalThread
->resume();
1284 pBelowNormalThread
->resume();
1285 pLowestThread
->resume();
1287 ThreadHelper::thread_sleep_tenth_sec(5);
1289 pHighestThread
->suspend();
1290 pAboveNormalThread
->suspend();
1291 pNormalThread
->suspend();
1292 pBelowNormalThread
->suspend();
1293 pLowestThread
->suspend();
1295 termAndJoinThread(pHighestThread
);
1296 termAndJoinThread(pAboveNormalThread
);
1297 termAndJoinThread(pNormalThread
);
1298 termAndJoinThread(pBelowNormalThread
);
1299 termAndJoinThread(pLowestThread
);
1301 sal_Int32 nValueHighest
= 0;
1302 nValueHighest
= pHighestThread
->getValue();
1304 sal_Int32 nValueAboveNormal
= 0;
1305 nValueAboveNormal
= pAboveNormalThread
->getValue();
1307 sal_Int32 nValueNormal
= 0;
1308 nValueNormal
= pNormalThread
->getValue();
1310 sal_Int32 nValueBelowNormal
= 0;
1311 nValueBelowNormal
= pBelowNormalThread
->getValue();
1313 sal_Int32 nValueLowest
= 0;
1314 nValueLowest
= pLowestThread
->getValue();
1316 t_print("After 10 tenth seconds\n");
1317 t_print("nValue in Highest Prio Thread is %d\n", (int) nValueHighest
);
1318 t_print("nValue in AboveNormal Prio Thread is %d\n", (int) nValueAboveNormal
);
1319 t_print("nValue in Normal Prio Thread is %d\n", (int) nValueNormal
);
1320 t_print("nValue in BelowNormal Prio Thread is %d\n", (int) nValueBelowNormal
);
1321 t_print("nValue in Lowest Prio Thread is %d\n", (int) nValueLowest
);
1323 delete pHighestThread
;
1324 delete pAboveNormalThread
;
1325 delete pNormalThread
;
1326 delete pBelowNormalThread
;
1327 delete pLowestThread
;
1330 CPPUNIT_ASSERT_MESSAGE(
1332 nValueHighest
> 0 &&
1333 nValueAboveNormal
> 0 &&
1335 nValueBelowNormal
> 0 &&
1341 void setPriority_004()
1343 // initial 5 threads with different priorities
1344 // OAddThread *pHighestThread = new OAddThread();
1345 OAddThread
*pAboveNormalThread
= new OAddThread();
1346 OAddThread
*pNormalThread
= new OAddThread();
1347 OAddThread
*pBelowNormalThread
= new OAddThread();
1348 OAddThread
*pLowestThread
= new OAddThread();
1350 //Create them and start running at the same time
1351 // pHighestThread->createSuspended();
1352 // pHighestThread->setPriority(osl_Thread_PriorityHighest);
1354 pAboveNormalThread
->createSuspended();
1355 pAboveNormalThread
->setPriority(osl_Thread_PriorityAboveNormal
);
1357 pNormalThread
->createSuspended();
1358 pNormalThread
->setPriority(osl_Thread_PriorityNormal
);
1360 pBelowNormalThread
->createSuspended();
1361 pBelowNormalThread
->setPriority(osl_Thread_PriorityBelowNormal
);
1363 pLowestThread
->createSuspended();
1364 pLowestThread
->setPriority(osl_Thread_PriorityLowest
);
1366 // pHighestThread->resume();
1367 pAboveNormalThread
->resume();
1368 pNormalThread
->resume();
1369 pBelowNormalThread
->resume();
1370 pLowestThread
->resume();
1372 ThreadHelper::thread_sleep_tenth_sec(5);
1374 // pHighestThread->suspend();
1375 pAboveNormalThread
->suspend();
1376 pNormalThread
->suspend();
1377 pBelowNormalThread
->suspend();
1378 pLowestThread
->suspend();
1380 // termAndJoinThread(pHighestThread);
1381 termAndJoinThread(pAboveNormalThread
);
1382 termAndJoinThread(pNormalThread
);
1383 termAndJoinThread(pBelowNormalThread
);
1384 termAndJoinThread(pLowestThread
);
1386 // sal_Int32 nValueHighest = 0;
1387 // nValueHighest = pHighestThread->getValue();
1389 sal_Int32 nValueAboveNormal
= 0;
1390 nValueAboveNormal
= pAboveNormalThread
->getValue();
1392 sal_Int32 nValueNormal
= 0;
1393 nValueNormal
= pNormalThread
->getValue();
1395 sal_Int32 nValueBelowNormal
= 0;
1396 nValueBelowNormal
= pBelowNormalThread
->getValue();
1398 sal_Int32 nValueLowest
= 0;
1399 nValueLowest
= pLowestThread
->getValue();
1401 t_print("After 5 tenth seconds\n");
1402 t_print("nValue in AboveNormal Prio Thread is %d\n", (int) nValueAboveNormal
);
1403 t_print("nValue in Normal Prio Thread is %d\n", (int) nValueNormal
);
1404 t_print("nValue in BelowNormal Prio Thread is %d\n", (int) nValueBelowNormal
);
1405 t_print("nValue in Lowest Prio Thread is %d\n", (int) nValueLowest
);
1407 // delete pHighestThread;
1408 delete pAboveNormalThread
;
1409 delete pNormalThread
;
1410 delete pBelowNormalThread
;
1411 delete pLowestThread
;
1414 CPPUNIT_ASSERT_MESSAGE(
1416 /* nValueHighest > 0 && */
1417 nValueAboveNormal
> 0 &&
1419 nValueBelowNormal
> 0 &&
1424 void setPriority_005()
1426 // initial 5 threads with different priorities
1427 // OAddThread *pHighestThread = new OAddThread();
1428 // OAddThread *pAboveNormalThread = new OAddThread();
1429 OAddThread
*pNormalThread
= new OAddThread();
1430 OAddThread
*pBelowNormalThread
= new OAddThread();
1431 OAddThread
*pLowestThread
= new OAddThread();
1433 //Create them and start running at the same time
1434 // pHighestThread->createSuspended();
1435 // pHighestThread->setPriority(osl_Thread_PriorityHighest);
1437 // pAboveNormalThread->createSuspended();
1438 // pAboveNormalThread->setPriority(osl_Thread_PriorityAboveNormal);
1440 pNormalThread
->createSuspended();
1441 pNormalThread
->setPriority(osl_Thread_PriorityNormal
);
1443 pBelowNormalThread
->createSuspended();
1444 pBelowNormalThread
->setPriority(osl_Thread_PriorityBelowNormal
);
1446 pLowestThread
->createSuspended();
1447 pLowestThread
->setPriority(osl_Thread_PriorityLowest
);
1449 // pHighestThread->resume();
1450 // pAboveNormalThread->resume();
1451 pNormalThread
->resume();
1452 pBelowNormalThread
->resume();
1453 pLowestThread
->resume();
1455 ThreadHelper::thread_sleep_tenth_sec(5);
1457 // pHighestThread->suspend();
1458 // pAboveNormalThread->suspend();
1459 pNormalThread
->suspend();
1460 pBelowNormalThread
->suspend();
1461 pLowestThread
->suspend();
1463 // termAndJoinThread(pHighestThread);
1464 // termAndJoinThread(pAboveNormalThread);
1465 termAndJoinThread(pNormalThread
);
1466 termAndJoinThread(pBelowNormalThread
);
1467 termAndJoinThread(pLowestThread
);
1469 // sal_Int32 nValueHighest = 0;
1470 // nValueHighest = pHighestThread->getValue();
1472 // sal_Int32 nValueAboveNormal = 0;
1473 // nValueAboveNormal = pAboveNormalThread->getValue();
1475 sal_Int32 nValueNormal
= 0;
1476 nValueNormal
= pNormalThread
->getValue();
1478 sal_Int32 nValueBelowNormal
= 0;
1479 nValueBelowNormal
= pBelowNormalThread
->getValue();
1481 sal_Int32 nValueLowest
= 0;
1482 nValueLowest
= pLowestThread
->getValue();
1484 t_print("After 5 tenth seconds\n");
1485 t_print("nValue in Normal Prio Thread is %d\n", (int) nValueNormal
);
1486 t_print("nValue in BelowNormal Prio Thread is %d\n", (int) nValueBelowNormal
);
1487 t_print("nValue in Lowest Prio Thread is %d\n", (int) nValueLowest
);
1489 delete pNormalThread
;
1490 delete pBelowNormalThread
;
1491 delete pLowestThread
;
1494 CPPUNIT_ASSERT_MESSAGE(
1496 /* nValueHighest > 0 && */
1497 /* nValueAboveNormal > 0 && */
1499 nValueBelowNormal
> 0 &&
1506 CPPUNIT_TEST_SUITE(setPriority
);
1508 CPPUNIT_TEST(setPriority_002
);
1509 CPPUNIT_TEST(setPriority_003
);
1510 CPPUNIT_TEST(setPriority_004
);
1511 CPPUNIT_TEST(setPriority_005
);
1513 CPPUNIT_TEST(setPriority_001_1
);
1514 CPPUNIT_TEST(setPriority_001_2
);
1515 CPPUNIT_TEST(setPriority_001_3
);
1516 CPPUNIT_TEST(setPriority_001_4
);
1517 CPPUNIT_TEST(setPriority_001_5
);
1518 CPPUNIT_TEST_SUITE_END();
1519 }; // class setPriority
1521 /** Test of the osl::Thread::getPriority method
1523 class getPriority
: public CppUnit::TestFixture
1526 // initialise your test code values here.
1535 // insert your test code here.
1536 void getPriority_001()
1538 OAddThread
*pHighestThread
= new OAddThread();
1540 //Create them and start running at the same time
1541 pHighestThread
->create();
1542 pHighestThread
->setPriority(osl_Thread_PriorityHighest
);
1544 oslThreadPriority aPriority
= pHighestThread
->getPriority();
1545 termAndJoinThread(pHighestThread
);
1546 delete pHighestThread
;
1548 ThreadHelper::outputPriority(aPriority
);
1550 // LLA: Priority settings may not work within some OS versions.
1551 #if ( defined WNT ) || ( defined SOLARIS )
1552 CPPUNIT_ASSERT_MESSAGE(
1554 aPriority
== osl_Thread_PriorityHighest
1558 // NO_PTHREAD_PRIORITY ???
1559 CPPUNIT_ASSERT_MESSAGE(
1561 aPriority
== osl_Thread_PriorityNormal
1566 void getPriority_002()
1571 CPPUNIT_TEST_SUITE(getPriority
);
1572 CPPUNIT_TEST(getPriority_001
);
1573 CPPUNIT_TEST(getPriority_002
);
1574 CPPUNIT_TEST_SUITE_END();
1575 }; // class getPriority
1578 class getIdentifier
: public CppUnit::TestFixture
1581 // initialise your test code values here.
1590 // insert your test code here.
1591 void getIdentifier_001()
1596 void getIdentifier_002()
1601 CPPUNIT_TEST_SUITE(getIdentifier
);
1602 CPPUNIT_TEST(getIdentifier_001
);
1603 CPPUNIT_TEST(getIdentifier_002
);
1604 CPPUNIT_TEST_SUITE_END();
1605 }; // class getIdentifier
1607 /** Test of the osl::Thread::getCurrentIdentifier method
1609 class getCurrentIdentifier
: public CppUnit::TestFixture
1612 // initialise your test code values here.
1621 // insert your test code here.
1622 void getCurrentIdentifier_001()
1624 oslThreadIdentifier oId
;
1625 OCountThread
* pCountThread
= new OCountThread
;
1626 pCountThread
->create();
1627 pCountThread
->setWait(3);
1628 oId
= Thread::getCurrentIdentifier();
1629 oslThreadIdentifier oIdChild
= pCountThread
->getIdentifier();
1630 termAndJoinThread(pCountThread
);
1631 delete pCountThread
;
1633 CPPUNIT_ASSERT_MESSAGE(
1634 "Get the identifier for the current active thread.",
1640 void getCurrentIdentifier_002()
1644 CPPUNIT_TEST_SUITE(getCurrentIdentifier
);
1645 CPPUNIT_TEST(getCurrentIdentifier_001
);
1646 //CPPUNIT_TEST(getCurrentIdentifier_002);
1647 CPPUNIT_TEST_SUITE_END();
1648 }; // class getCurrentIdentifier
1650 /** Test of the osl::Thread::wait method
1652 class wait
: public CppUnit::TestFixture
1655 // initialise your test code values here.
1664 /** call wait in the run method
1667 tested thread wait nWaitSec seconds, main thread sleep (2) seconds,
1668 then terminate the tested thread, due to the fact that the thread do a sleep(1) + wait(5)
1669 it's finish after 6 seconds.
1673 OCountThread
*aCountThread
= new OCountThread();
1674 sal_Int32 nWaitSec
= 5;
1675 aCountThread
->setWait(nWaitSec
);
1676 // thread runs at least 5 seconds.
1677 sal_Bool bRes
= aCountThread
->create();
1678 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
1680 //record the time when the running begin
1681 StopWatch aStopWatch
;
1684 // wait a little bit, to let the thread the time, to start
1685 ThreadHelper::thread_sleep_tenth_sec( 4 );
1688 // this function returns, after 4 sec. later
1689 termAndJoinThread(aCountThread
);
1691 // value should be one.
1692 sal_Int32 nValue
= aCountThread
->getValue();
1696 // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
1697 double nTenthSec
= aStopWatch
.getTenthSec();
1698 double nSec
= aStopWatch
.getSeconds();
1699 delete aCountThread
;
1700 t_print("nTenthSec = %f \n", nTenthSec
);
1701 t_print("nSec = %f \n", nSec
);
1702 t_print("nValue = %d \n", (int) nValue
);
1704 CPPUNIT_ASSERT_MESSAGE(
1705 "Wait: Blocks the calling thread for the given number of time.",
1706 nTenthSec
>= 5 && nValue
== 1
1711 CPPUNIT_TEST_SUITE(wait
);
1712 CPPUNIT_TEST(wait_001
);
1713 CPPUNIT_TEST_SUITE_END();
1716 /** osl::Thread::yield method: can not design good test scenario to test up to now
1718 class yield
: public CppUnit::TestFixture
1729 // insert your test code here.
1735 CPPUNIT_TEST_SUITE(yield
);
1736 CPPUNIT_TEST(yield_001
);
1737 CPPUNIT_TEST_SUITE_END();
1740 /** Test of the osl::Thread::schedule method
1742 class schedule
: public CppUnit::TestFixture
1745 // initialise your test code values here.
1754 /** The requested thread will get terminate the next time schedule() is called.
1756 Note: on UNX, if call suspend thread is not the to be suspended thread, the to be
1757 suspended thread will get suspended the next time schedule() is called,
1758 while on w32, it's nothing with schedule.
1760 check if suspend and terminate work well via schedule
1764 OAddThread
* aThread
= new OAddThread();
1765 sal_Bool bRes
= aThread
->create();
1766 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
1768 ThreadHelper::thread_sleep_tenth_sec(2);
1770 ThreadHelper::thread_sleep_tenth_sec(1);
1771 sal_Int32 nValue
= aThread
->getValue();
1772 ThreadHelper::thread_sleep_tenth_sec(3);
1773 sal_Int32 nLaterValue
= aThread
->getValue();
1774 // resumeAndWaitThread(aThread);
1775 t_print(" value = %d\n", (int) nValue
);
1776 t_print("later value = %d\n", (int) nLaterValue
);
1777 // if value and latervalue not equal, than the thread would not suspended
1779 CPPUNIT_ASSERT_MESSAGE(
1780 "Schedule: suspend works.",
1781 nLaterValue
== nValue
1785 ThreadHelper::thread_sleep_tenth_sec(2);
1787 aThread
->terminate();
1788 sal_Int32 nValue_term
= aThread
->getValue();
1791 sal_Int32 nValue_join
= aThread
->getValue();
1793 t_print("value after term = %d\n", (int) nValue_term
);
1794 t_print("value after join = %d\n", (int) nValue_join
);
1796 // nValue_term and nValue_join should be the same
1797 // but should be differ from nValue
1800 //check if thread really terminate after call terminate, if join immediatlly return
1801 CPPUNIT_ASSERT_MESSAGE(
1802 "Schedule: Returns False if the thread should terminate.",
1803 nValue_join
- nValue_term
<= 1 && nValue_join
- nValue_term
>= 0
1808 /** design a thread that has not call schedule in the workfunction--run method
1812 ONoScheduleThread aThread
; // this thread runs 10 sec. (no schedule() used)
1813 sal_Bool bRes
= aThread
.create();
1814 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
== sal_True
);
1816 ThreadHelper::thread_sleep_tenth_sec(2);
1818 sal_Int32 nValue
= aThread
.getValue();
1820 ThreadHelper::thread_sleep_tenth_sec(3);
1821 sal_Int32 nLaterValue
= aThread
.getValue();
1822 ThreadHelper::thread_sleep_tenth_sec(5);
1824 resumeAndWaitThread(&aThread
);
1826 t_print(" value = %d\n", (int) nValue
);
1827 t_print("later value = %d\n", (int) nLaterValue
);
1829 //On windows, suspend works, so the values are same
1831 CPPUNIT_ASSERT_MESSAGE(
1832 "Schedule: don't schedule in thread run method, suspend works.",
1833 nLaterValue
== nValue
1837 //On UNX, suspend does not work, so the difference of the values equals to sleep seconds number
1840 CPPUNIT_ASSERT_MESSAGE(
1841 "Schedule: don't schedule in thread run method, suspend does not work too.",
1842 nLaterValue
> nValue
1846 // terminate will not work if no schedule in thread's work function
1847 termAndJoinThread(&aThread
);
1848 sal_Int32 nValue_term
= aThread
.getValue();
1850 t_print(" value term = %d\n", (int) nValue_term
);
1852 CPPUNIT_ASSERT_MESSAGE(
1853 "Schedule: don't schedule in thread run method, terminate failed.",
1858 CPPUNIT_TEST_SUITE(schedule
);
1859 CPPUNIT_TEST(schedule_001
);
1860 CPPUNIT_TEST(schedule_002
);
1861 CPPUNIT_TEST_SUITE_END();
1862 }; // class schedule
1864 // -----------------------------------------------------------------------------
1865 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::create
, "osl_Thread");
1866 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::createSuspended
, "osl_Thread");
1867 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::suspend
, "osl_Thread");
1868 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::resume
, "osl_Thread");
1869 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::terminate
, "osl_Thread");
1870 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::join
, "osl_Thread");
1871 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::isRunning
, "osl_Thread");
1872 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::setPriority
, "osl_Thread");
1873 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getPriority
, "osl_Thread");
1874 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getIdentifier
, "osl_Thread");
1875 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getCurrentIdentifier
, "osl_Thread");
1876 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::wait
, "osl_Thread");
1877 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::yield
, "osl_Thread");
1878 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::schedule
, "osl_Thread");
1879 } // namespace osl_Thread
1882 // -----------------------------------------------------------------------------
1883 // destroy function when the binding thread terminate
1884 void SAL_CALL
destroyCallback(void * data
)
1886 delete[] (char *) data
;
1889 static ThreadData
myThreadData(destroyCallback
);
1893 class myKeyThread
: public Thread
1896 // a public char member for test result checking
1898 // for pass thread-special data to thread
1899 myKeyThread(const char cData
)
1908 char * pc
= new char[2];
1909 // strcpy(pc, &m_nData);
1910 memcpy(pc
, &m_nData
, 1);
1913 myThreadData
.setData(pc
);
1914 char* pData
= (char*)myThreadData
.getData();
1915 m_Char_Test
= *pData
;
1916 // wait for long time to check the data value in main thread
1917 ThreadHelper::thread_sleep_tenth_sec(3);
1924 t_print("error: not terminated.\n");
1929 static ThreadData idData
;
1931 class idThread
: public Thread
1934 oslThreadIdentifier m_Id
;
1938 oslThreadIdentifier
* pId
= new oslThreadIdentifier
;
1939 *pId
= getIdentifier();
1940 idData
.setData(pId
);
1941 oslThreadIdentifier
* pIdData
= (oslThreadIdentifier
*)idData
.getData();
1942 //t_print("Thread %d has Data %d\n", getIdentifier(), *pIdData);
1952 t_print("error: not terminated.\n");
1957 namespace osl_ThreadData
1960 class ctors
: public CppUnit::TestFixture
1963 // initialise your test code values here.
1972 // insert your test code here.
1978 CPPUNIT_TEST_SUITE(ctors
);
1979 CPPUNIT_TEST(ctor_001
);
1980 CPPUNIT_TEST_SUITE_END();
1984 class setData
: public CppUnit::TestFixture
1987 // initialise your test code values here.
1996 /** the same instance of the class can have different values in different threads
2008 oslThreadIdentifier aThreadId1
= aThread1
.getIdentifier();
2009 oslThreadIdentifier aThreadId2
= aThread2
.getIdentifier();
2011 CPPUNIT_ASSERT_MESSAGE(
2012 "ThreadData setData: ",
2013 aThread1
.m_Id
== aThreadId1
&& aThread2
.m_Id
== aThreadId2
2020 // at first, set the data a value
2021 char* pc
= new char[2];
2023 // LLA: this is a copy functions only and really only for \0 terminated strings
2024 // m_nData is not a string, it's a character
2025 // strcpy(pc, &m_nData);
2026 memcpy(pc
, &m_nData
, 1);
2029 myThreadData
.setData(pc
);
2031 myKeyThread
aThread1('a');
2033 myKeyThread
aThread2('b');
2035 // aThread1 and aThread2 should have not terminated yet, check current data, not 'a' 'b'
2036 char* pChar
= (char*)myThreadData
.getData();
2037 char aChar
= *pChar
;
2042 // the saved thread data of aThread1 & aThread2, different
2043 char cData1
= aThread1
.m_Char_Test
;
2044 char cData2
= aThread2
.m_Char_Test
;
2046 CPPUNIT_ASSERT_MESSAGE(
2047 "ThreadData setData: ",
2048 cData1
== 'a' && cData2
== 'b' && aChar
== 'm'
2052 /** setData the second time, and then getData
2056 // at first, set the data a value
2057 char* pc
= new char[2];
2059 memcpy(pc
, &m_nData
, 1);
2061 myThreadData
.setData(pc
);
2063 myKeyThread
aThread1('a');
2065 myKeyThread
aThread2('b');
2067 // aThread1 and aThread2 should have not terminated yet
2068 // setData the second time
2069 char* pc2
= new char[2];
2071 memcpy(pc2
, &m_nData
, 1);
2074 myThreadData
.setData(pc2
);
2075 char* pChar
= (char*)myThreadData
.getData();
2076 char aChar
= *pChar
;
2081 // the saved thread data of aThread1 & aThread2, different
2082 char cData1
= aThread1
.m_Char_Test
;
2083 char cData2
= aThread2
.m_Char_Test
;
2085 CPPUNIT_ASSERT_MESSAGE(
2086 "ThreadData setData: ",
2087 cData1
== 'a' && cData2
== 'b' && aChar
== 'o'
2091 CPPUNIT_TEST_SUITE(setData
);
2092 CPPUNIT_TEST(setData_001
);
2093 CPPUNIT_TEST(setData_002
);
2094 CPPUNIT_TEST(setData_003
);
2095 CPPUNIT_TEST_SUITE_END();
2098 class getData
: public CppUnit::TestFixture
2101 // initialise your test code values here.
2110 // After setData in child threads, get Data in the main thread, should be independent
2113 char* pc
= new char[2];
2114 char m_nData
[] = "i";
2115 strcpy(pc
, m_nData
);
2116 myThreadData
.setData(pc
);
2118 myKeyThread
aThread1('c');
2120 myKeyThread
aThread2('d');
2126 char cData1
= aThread1
.m_Char_Test
;
2127 char cData2
= aThread2
.m_Char_Test
;
2129 char* pChar
= (char*)myThreadData
.getData();
2130 char aChar
= *pChar
;
2132 CPPUNIT_ASSERT_MESSAGE(
2133 "ThreadData setData: ",
2134 cData1
== 'c' && cData2
== 'd' && aChar
== 'i'
2138 // setData then change the value in the address data pointer points,
2139 // and then getData, should get the new value
2142 char* pc
= new char[2];
2144 memcpy(pc
, &m_nData
, 1);
2147 myThreadData
.setData(pc
);
2149 myKeyThread
aThread1('a');
2151 myKeyThread
aThread2('b');
2154 // change the value which pc points
2155 char m_nData2
= 'j';
2156 memcpy(pc
, &m_nData2
, 1);
2159 void* pChar
= myThreadData
.getData();
2160 char aChar
= *(char*)pChar
;
2165 char cData1
= aThread1
.m_Char_Test
;
2166 char cData2
= aThread2
.m_Char_Test
;
2168 CPPUNIT_ASSERT_MESSAGE(
2169 "ThreadData setData: ",
2170 cData1
== 'a' && cData2
== 'b' && aChar
== 'j'
2175 CPPUNIT_TEST_SUITE(getData
);
2176 CPPUNIT_TEST(getData_001
);
2177 CPPUNIT_TEST(getData_002
);
2178 CPPUNIT_TEST_SUITE_END();
2181 // -----------------------------------------------------------------------------
2182 CPPUNIT_TEST_SUITE_REGISTRATION(osl_ThreadData::ctors
);
2183 CPPUNIT_TEST_SUITE_REGISTRATION(osl_ThreadData::setData
);
2184 CPPUNIT_TEST_SUITE_REGISTRATION(osl_ThreadData::getData
);
2185 } // namespace osl_ThreadData
2187 // this macro creates an empty function, which will called by the RegisterAllFunctions()
2188 // to let the user the possibility to also register some functions by hand.
2189 CPPUNIT_PLUGIN_IMPLEMENT();
2191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */