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 .
22 #if !defined WIN32_LEAN_AND_MEAN
23 # define WIN32_LEAN_AND_MEAN
33 #include <sal/types.h>
34 #include <rtl/string.hxx>
35 #include <rtl/strbuf.hxx>
36 #include <osl/thread.hxx>
37 #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
55 TimeValue t1
,t2
; // Start and stoptime
61 bool m_bIsValid
; // TRUE, when started and stopped
62 bool m_bIsRunning
; // TRUE, when started
67 void start(); // Starts time
68 void stop(); // Stops time
70 double getSeconds() const;
71 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()
93 void StopWatch::start()
100 osl_getSystemTime( &t1
);
101 t_print("# %u %u nsecs\n", static_cast<unsigned>(t1
.Seconds
), static_cast<unsigned>(t1
.Nanosec
));
102 // gettimeofday(&t1, 0);
105 void StopWatch::stop()
107 // pre: Timer should be started
108 // post: Timer will stopped
110 osl_getSystemTime( &t2
);
111 t_print("# %u %u nsecs\n", static_cast<unsigned>(t2
.Seconds
), static_cast<unsigned>(t2
.Nanosec
));
114 { // check if started.
115 m_nSeconds
= static_cast<sal_Int32
>(t2
.Seconds
) - static_cast<sal_Int32
>(t1
.Seconds
);
116 if ( t2
.Nanosec
> t1
.Nanosec
)
117 m_nNanoSec
= static_cast<sal_Int32
>(t2
.Nanosec
) - static_cast<sal_Int32
>(t1
.Nanosec
);
120 m_nNanoSec
= 1000000000 + static_cast<sal_Int32
>(t2
.Nanosec
) - static_cast<sal_Int32
>(t1
.Nanosec
);
123 t_print("# %u %u nsecs\n", static_cast<unsigned>(m_nSeconds
), static_cast<unsigned>(m_nNanoSec
) );
125 m_bIsRunning
= false;
129 double StopWatch::getSeconds() const
132 // BACK: time in seconds
137 nValue
= double(m_nNanoSec
) / 1000000000.0 + m_nSeconds
; // milli micro nano
142 double StopWatch::getTenthSec() const
147 nValue
= double(m_nNanoSec
) / 100000000.0 + m_nSeconds
* 10;
155 class ThreadSafeValue
160 explicit ThreadSafeValue(T n
= 0): m_nFlag(n
) {}
163 //block if already acquired by another thread.
164 osl::MutexGuard
g(m_aMutex
);
169 //only one thread operate on the flag.
170 osl::MutexGuard
g(m_aMutex
);
173 void acquire() {m_aMutex
.acquire();}
174 void release() {m_aMutex
.release();}
179 namespace ThreadHelper
181 static void thread_sleep_tenth_sec(sal_Int32 _nTenthSec
)
183 osl::Thread::wait(std::chrono::milliseconds(_nTenthSec
* 100));
186 static void outputPriority(oslThreadPriority
const& _aPriority
)
188 // LLA: output the priority
189 if (_aPriority
== osl_Thread_PriorityHighest
)
191 t_print("Prio is High\n");
193 else if (_aPriority
== osl_Thread_PriorityAboveNormal
)
195 t_print("Prio is above normal\n");
197 else if (_aPriority
== osl_Thread_PriorityNormal
)
199 t_print("Prio is normal\n");
201 else if (_aPriority
== osl_Thread_PriorityBelowNormal
)
203 t_print("Prio is below normal\n");
205 else if (_aPriority
== osl_Thread_PriorityLowest
)
207 t_print("Prio is lowest\n");
211 t_print("Prio is unknown\n");
218 /** Simple thread for testing Thread-create.
220 Just add 1 of value 0, and after running, result is 1.
222 class myThread
: public Thread
224 ThreadSafeValue
<sal_Int32
> m_aFlag
;
226 sal_Int32
getValue() { return m_aFlag
.getValue(); }
228 /** guarded value which initialized 0
232 void SAL_CALL
run() override
237 ThreadHelper::thread_sleep_tenth_sec(1);
243 virtual void SAL_CALL
suspend() override
246 ::osl::Thread::suspend();
250 virtual ~myThread() override
254 t_print("error: not terminated.\n");
260 /** Thread which has a flag add 1 every second until 20
262 class OCountThread
: public Thread
264 ThreadSafeValue
<sal_Int32
> m_aFlag
;
266 OCountThread() : m_nWaitSec(0)
268 t_print("new OCountThread thread %u!\n", static_cast<unsigned>(getIdentifier()));
270 sal_Int32
getValue() { return m_aFlag
.getValue(); }
272 void setWait(sal_Int32 nSec
)
275 //m_bWait = sal_True;
278 virtual void SAL_CALL
suspend() override
281 ::osl::Thread::suspend();
287 sal_Int32 m_nWaitSec
;
289 void SAL_CALL
run() override
291 /// if the thread should terminate, schedule return false
292 while (m_aFlag
.getValue() < 20 && schedule())
295 ThreadHelper::thread_sleep_tenth_sec(1);
300 nTV
.Seconds
= m_nWaitSec
/ 10 ;
301 nTV
.Nanosec
= ( m_nWaitSec
%10 ) * 100000000 ;
307 void SAL_CALL
onTerminated() override
309 t_print("normally terminate this thread %u!\n", static_cast<unsigned>(getIdentifier()));
313 virtual ~OCountThread() override
317 t_print("error: not terminated.\n");
323 /** no call schedule in the run method
325 class ONoScheduleThread
: public Thread
327 ThreadSafeValue
<sal_Int32
> m_aFlag
;
329 sal_Int32
getValue() { return m_aFlag
.getValue(); }
331 virtual void SAL_CALL
suspend() override
334 ::osl::Thread::suspend();
338 void SAL_CALL
run() override
340 while (m_aFlag
.getValue() < 10)
343 ThreadHelper::thread_sleep_tenth_sec(1);
346 void SAL_CALL
onTerminated() override
348 t_print("normally terminate this thread %u!\n", static_cast<unsigned>(getIdentifier()));
353 t_print("new thread id %u!\n", static_cast<unsigned>(getIdentifier()));
355 virtual ~ONoScheduleThread() override
359 t_print("error: not terminated.\n");
366 class OAddThread
: public Thread
368 ThreadSafeValue
<sal_Int32
> m_aFlag
;
370 //oslThreadIdentifier m_id, m_CurId;
372 sal_Int32
getValue() { return m_aFlag
.getValue(); }
374 virtual void SAL_CALL
suspend() override
377 ::osl::Thread::suspend();
381 void SAL_CALL
run() override
383 //if the thread should terminate, schedule return false
389 void SAL_CALL
onTerminated() override
391 // t_print("normally terminate this thread %d!\n", getIdentifier());
395 virtual ~OAddThread() override
399 // t_print("error: not terminated.\n");
410 static void resumeAndWaitThread(Thread
* _pThread
)
412 // This function starts a thread, wait a second and suspends the thread
413 // Due to the fact, that a suspend and never run thread never really exists.
415 // Note: on UNX, after createSuspended, and then terminate the thread, it performs well;
416 // while on Windows, after createSuspended, the thread can not terminate, wait endlessly,
417 // so here call resume at first, then call terminate.
419 t_print("resumeAndWaitThread\n");
421 ThreadHelper::thread_sleep_tenth_sec(1);
427 // kill a running thread and join it, if it has terminated, do nothing
428 static void termAndJoinThread(Thread
* _pThread
)
430 _pThread
->terminate();
432 // LLA: Windows feature???, a suspended thread can not terminated, so we have to weak it up
435 ThreadHelper::thread_sleep_tenth_sec(1);
437 t_print("#wait for join.\n");
440 /** Test of the osl::Thread::create method
443 class create
: public CppUnit::TestFixture
446 /** Simple create a thread.
448 Create a simple thread, it just does add 1 to value(which initialized 0),
449 if the thread run, the value should be 1.
453 myThread
* newthread
= new myThread
;
454 bool bRes
= newthread
->create();
455 CPPUNIT_ASSERT_MESSAGE("Can not creates a new thread!\n", bRes
);
457 ThreadHelper::thread_sleep_tenth_sec(1); // wait short
458 bool isRunning
= newthread
->isRunning(); // check if thread is running
459 /// wait for the new thread to assure it has run
460 ThreadHelper::thread_sleep_tenth_sec(3);
461 sal_Int32 nValue
= newthread
->getValue();
462 /// to assure the new thread has terminated
463 termAndJoinThread(newthread
);
466 t_print(" nValue = %d\n", static_cast<int>(nValue
));
467 t_print("isRunning = %s\n", isRunning
? "true" : "false");
469 CPPUNIT_ASSERT_MESSAGE(
470 "Creates a new thread",
473 CPPUNIT_ASSERT_MESSAGE(
474 "Creates a new thread",
480 /** only one running thread per instance, return false if create secondly
484 myThread
* newthread
= new myThread
;
485 bool res1
= newthread
->create();
486 bool res2
= newthread
->create();
487 t_print("In non pro, an assertion should occurred. This behaviour is right.\n");
488 termAndJoinThread(newthread
);
491 CPPUNIT_ASSERT_MESSAGE(
492 "Creates a new thread: can not create two threads per instance",
495 CPPUNIT_ASSERT_MESSAGE(
496 "Creates a new thread: can not create two threads per instance",
502 CPPUNIT_TEST_SUITE(create
);
503 CPPUNIT_TEST(create_001
);
504 CPPUNIT_TEST(create_002
);
505 CPPUNIT_TEST_SUITE_END();
508 /** Test of the osl::Thread::createSuspended method
510 class createSuspended
: public CppUnit::TestFixture
513 /** Create a suspended thread, use the same class as create_001
515 after create, wait enough time, check the value, if it's still the initial value, pass
517 void createSuspended_001()
519 myThread
* newthread
= new myThread
;
520 bool bRes
= newthread
->createSuspended();
521 CPPUNIT_ASSERT_MESSAGE("Can not creates a new thread!", bRes
);
523 ThreadHelper::thread_sleep_tenth_sec(1);
524 bool isRunning
= newthread
->isRunning();
525 ThreadHelper::thread_sleep_tenth_sec(3);
526 sal_Int32 nValue
= newthread
->getValue();
528 resumeAndWaitThread(newthread
);
530 termAndJoinThread(newthread
);
533 CPPUNIT_ASSERT_EQUAL_MESSAGE(
534 "Creates a new suspended thread",
537 CPPUNIT_ASSERT_MESSAGE(
538 "Creates a new suspended thread",
543 void createSuspended_002()
545 myThread
* newthread
= new myThread
;
546 bool res1
= newthread
->createSuspended();
547 bool res2
= newthread
->createSuspended();
549 resumeAndWaitThread(newthread
);
551 termAndJoinThread(newthread
);
555 CPPUNIT_ASSERT_MESSAGE(
556 "Creates a new thread: can not create two threads per instance",
559 CPPUNIT_ASSERT_MESSAGE(
560 "Creates a new thread: can not create two threads per instance",
565 CPPUNIT_TEST_SUITE(createSuspended
);
566 CPPUNIT_TEST(createSuspended_001
);
567 // LLA: Deadlocked!!!
568 CPPUNIT_TEST(createSuspended_002
);
569 CPPUNIT_TEST_SUITE_END();
570 }; // class createSuspended
572 /** when the count value equal to or more than 3, suspend the thread.
574 static void suspendCountThread(OCountThread
* _pCountThread
)
576 sal_Int32 nValue
= 0;
579 nValue
= _pCountThread
->getValue();
582 _pCountThread
->suspend();
588 /** Test of the osl::Thread::suspend method
590 class suspend
: public CppUnit::TestFixture
593 /** Use a thread which has a flag added 1 every second
596 create the thread, after running special time, record value of flag, then suspend it,
597 wait a long time, check the flag, if it remains unchanged during suspending
601 OCountThread
* aCountThread
= new OCountThread();
602 bool bRes
= aCountThread
->create();
603 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
);
604 // the thread run for some seconds, but not terminate
605 suspendCountThread( aCountThread
);
607 // the value just after calling suspend
608 sal_Int32 nValue
= aCountThread
->getValue(); // (2)
610 ThreadHelper::thread_sleep_tenth_sec(3);
612 // the value after waiting 3 seconds
613 sal_Int32 nLaterValue
= aCountThread
->getValue(); // (3)
615 resumeAndWaitThread(aCountThread
);
616 termAndJoinThread(aCountThread
);
619 CPPUNIT_ASSERT_MESSAGE(
620 "Suspend the thread",
623 CPPUNIT_ASSERT_EQUAL_MESSAGE(
624 "Suspend the thread",
630 CPPUNIT_TEST_SUITE(suspend
);
631 CPPUNIT_TEST(suspend_001
);
632 // LLA: Deadlocked!!!
633 // CPPUNIT_TEST(createSuspended_002);
634 CPPUNIT_TEST_SUITE_END();
637 /** Test of the osl::Thread::resume method
639 class resume
: public CppUnit::TestFixture
642 /** check if the thread run samely as usual after suspend and resume
645 compare the values before and after suspend, they should be same,
646 then compare values before and after resume, the difference should be same as the sleep seconds number
650 OCountThread
* pCountThread
= new OCountThread();
651 bool bRes
= pCountThread
->create();
652 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
);
654 suspendCountThread(pCountThread
);
656 sal_Int32 nSuspendValue
= pCountThread
->getValue(); // (2)
657 // suspend for 3 seconds
658 ThreadHelper::thread_sleep_tenth_sec(3);
659 pCountThread
->resume();
661 ThreadHelper::thread_sleep_tenth_sec(3);
662 sal_Int32 nResumeValue
= pCountThread
->getValue();
664 ThreadHelper::thread_sleep_tenth_sec(3);
665 sal_Int32 nLaterValue
= pCountThread
->getValue();
667 termAndJoinThread(pCountThread
);
670 t_print("SuspendValue: %d\n", static_cast<int>(nSuspendValue
));
671 t_print("ResumeValue: %d\n", static_cast<int>(nResumeValue
));
672 t_print("LaterValue: %d\n", static_cast<int>(nLaterValue
));
674 /* LLA: this assumption is no longer relevant: nResumeValue == nSuspendValue && */
675 CPPUNIT_ASSERT_MESSAGE(
676 "Suspend then resume the thread",
679 CPPUNIT_ASSERT_MESSAGE(
680 "Suspend then resume the thread",
681 nResumeValue
> nSuspendValue
683 CPPUNIT_ASSERT_MESSAGE(
684 "Suspend then resume the thread",
685 nLaterValue
> nResumeValue
690 /** Create a suspended thread then resume, check if the thread has run
694 myThread
* newthread
= new myThread
;
695 bool bRes
= newthread
->createSuspended();
696 CPPUNIT_ASSERT_MESSAGE ( "Can't create thread!", bRes
);
699 ThreadHelper::thread_sleep_tenth_sec(2);
700 sal_Int32 nValue
= newthread
->getValue();
702 termAndJoinThread(newthread
);
705 t_print(" nValue = %d\n", static_cast<int>(nValue
));
707 CPPUNIT_ASSERT_MESSAGE(
708 "Creates a suspended thread, then resume",
713 CPPUNIT_TEST_SUITE(resume
);
714 CPPUNIT_TEST(resume_001
);
715 CPPUNIT_TEST(resume_002
);
716 CPPUNIT_TEST_SUITE_END();
719 /** Test of the osl::Thread::terminate method
721 class terminate
: public CppUnit::TestFixture
724 /** Check after call terminate if the running thread running go on executing
727 before and after call terminate, the values should be the same
731 OCountThread
* aCountThread
= new OCountThread();
732 bool bRes
= aCountThread
->create();
733 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
);
735 ThreadHelper::thread_sleep_tenth_sec(2);
736 sal_Int32 nValue
= aCountThread
->getValue();
737 aCountThread
->terminate();
738 ThreadHelper::thread_sleep_tenth_sec(2);
739 sal_Int32 nLaterValue
= aCountThread
->getValue();
741 // isRunning should be false after terminate
742 bool isRunning
= aCountThread
->isRunning();
743 aCountThread
->join();
746 t_print(" nValue = %d\n", static_cast<int>(nValue
));
747 t_print("nLaterValue = %d\n", static_cast<int>(nLaterValue
));
749 CPPUNIT_ASSERT_MESSAGE(
750 "Terminate the thread",
753 CPPUNIT_ASSERT_MESSAGE(
754 "Terminate the thread",
755 nLaterValue
>= nValue
758 /** Check if a suspended thread will terminate after call terminate, different on w32 and on UNX
762 OCountThread
* aCountThread
= new OCountThread();
763 bool bRes
= aCountThread
->create();
764 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
);
766 ThreadHelper::thread_sleep_tenth_sec(1);
767 suspendCountThread(aCountThread
);
768 sal_Int32 nValue
= aCountThread
->getValue();
770 // seems a suspended thread can not be terminated on W32, while on Solaris can
771 resumeAndWaitThread(aCountThread
);
773 ThreadHelper::thread_sleep_tenth_sec(2);
775 termAndJoinThread(aCountThread
);
776 sal_Int32 nLaterValue
= aCountThread
->getValue();
779 t_print(" nValue = %d\n", static_cast<int>(nValue
));
780 t_print("nLaterValue = %d\n", static_cast<int>(nLaterValue
));
782 CPPUNIT_ASSERT_MESSAGE(
783 "Suspend then resume the thread",
784 nLaterValue
> nValue
);
787 CPPUNIT_TEST_SUITE(terminate
);
788 CPPUNIT_TEST(terminate_001
);
789 CPPUNIT_TEST(terminate_002
);
790 CPPUNIT_TEST_SUITE_END();
791 }; // class terminate
793 /** Test of the osl::Thread::join method
795 class join
: public CppUnit::TestFixture
798 /** Check after call terminate if the thread running function will not go on executing
800 the next statement after join will not exec before the thread terminates
802 recode system time at the beginning of the thread run, call join, then record system time again,
803 the difference of the two times should be equal or more than 20 seconds, the CountThread normally terminates
807 OCountThread
*aCountThread
= new OCountThread();
808 bool bRes
= aCountThread
->create();
809 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
);
811 StopWatch aStopWatch
;
813 // TimeValue aTimeVal_befor;
814 // osl_getSystemTime( &aTimeVal_befor );
815 //t_print("#join:the system time is %d,%d\n", pTimeVal_befor->Seconds,pTimeVal_befor->Nanosec);
817 aCountThread
->join();
819 //the below line will be executed after aCountThread terminate
820 // TimeValue aTimeVal_after;
821 // osl_getSystemTime( &aTimeVal_after );
823 // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
824 double nSec
= aStopWatch
.getSeconds();
825 t_print("join_001 nSec=%f\n", nSec
);
828 CPPUNIT_ASSERT_MESSAGE(
829 "Join the thread: after the thread terminate",
834 /** after terminated by another thread, join exited immediately
837 terminate the thread when value>=3, call join, check the beginning time and time after join,
838 the difference should be 3 seconds, join costs little time
842 OCountThread
*aCountThread
= new OCountThread();
843 bool bRes
= aCountThread
->create();
844 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
);
846 //record the time when the running begin
847 // TimeValue aTimeVal_befor;
848 // osl_getSystemTime( &aTimeVal_befor );
849 StopWatch aStopWatch
;
852 ThreadHelper::thread_sleep_tenth_sec(10);
853 termAndJoinThread(aCountThread
);
855 //the below line will be executed after aCountThread terminate
856 // TimeValue aTimeVal_after;
857 // osl_getSystemTime( &aTimeVal_after );
858 // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
860 double nSec
= aStopWatch
.getSeconds();
861 t_print("join_002 nSec=%f\n", nSec
);
864 CPPUNIT_ASSERT_MESSAGE(
865 "Join the thread: after thread terminate by another thread",
870 CPPUNIT_TEST_SUITE(join
);
871 CPPUNIT_TEST(join_001
);
872 CPPUNIT_TEST(join_002
);
873 CPPUNIT_TEST_SUITE_END();
876 /** Test of the osl::Thread::isRunning method
878 class isRunning
: public CppUnit::TestFixture
883 OCountThread
*aCountThread
= new OCountThread();
884 bool bRes
= aCountThread
->create();
885 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
);
887 bool bRun
= aCountThread
->isRunning();
889 ThreadHelper::thread_sleep_tenth_sec(2);
890 termAndJoinThread(aCountThread
);
891 bool bTer
= aCountThread
->isRunning();
894 CPPUNIT_ASSERT_MESSAGE(
898 CPPUNIT_ASSERT_MESSAGE(
903 /** check the value of isRunning when suspending and after resume
907 OCountThread
*aCountThread
= new OCountThread();
908 bool bRes
= aCountThread
->create();
909 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
);
911 // sal_Bool bRunning = aCountThread->isRunning();
912 // sal_Int32 nValue = 0;
913 suspendCountThread(aCountThread
);
915 bool bRunning_sup
= aCountThread
->isRunning();
916 ThreadHelper::thread_sleep_tenth_sec(2);
917 aCountThread
->resume();
918 ThreadHelper::thread_sleep_tenth_sec(2);
919 bool bRunning_res
= aCountThread
->isRunning();
920 termAndJoinThread(aCountThread
);
921 bool bRunning_ter
= aCountThread
->isRunning();
924 CPPUNIT_ASSERT_MESSAGE(
927 CPPUNIT_ASSERT_MESSAGE(
930 CPPUNIT_ASSERT_MESSAGE(
933 CPPUNIT_ASSERT_MESSAGE(
939 CPPUNIT_TEST_SUITE(isRunning
);
940 CPPUNIT_TEST(isRunning_001
);
941 CPPUNIT_TEST(isRunning_002
);
942 CPPUNIT_TEST_SUITE_END();
943 }; // class isRunning
945 /// check osl::Thread::setPriority
946 class setPriority
: public CppUnit::TestFixture
949 // insert your test code here.
950 OString
getPrioName(oslThreadPriority _aPriority
)
955 case osl_Thread_PriorityHighest
:
956 sPrioStr
= "Highest";
959 case osl_Thread_PriorityAboveNormal
:
960 sPrioStr
= "AboveNormal";
963 case osl_Thread_PriorityNormal
:
967 case osl_Thread_PriorityBelowNormal
:
968 sPrioStr
= "BelowNormal";
971 case osl_Thread_PriorityLowest
:
975 sPrioStr
= "unknown";
983 Here the function should show, that 2 different threads,
984 which only increase a value, should run at the same time with same prio.
985 The test fails, if the difference between the two values is more than 5%
986 but IMHO this isn't a failure, it's only a feature of the OS.
989 void check2Threads(oslThreadPriority _aPriority
)
991 // initial 5 threads with different priorities
992 OAddThread
* pThread
= new OAddThread();
993 OAddThread
* p2Thread
= new OAddThread();
995 //Create them and start running at the same time
997 pThread
->setPriority(_aPriority
);
999 p2Thread
->setPriority(_aPriority
);
1001 ThreadHelper::thread_sleep_tenth_sec(5);
1003 pThread
->terminate();
1004 p2Thread
->terminate();
1006 sal_Int32 nValueNormal
= pThread
->getValue();
1008 sal_Int32 nValueNormal2
= p2Thread
->getValue();
1010 OString sPrio
= getPrioName(_aPriority
);
1011 t_print("After 10 tenth seconds\n");
1013 t_print("nValue in %s Prio Thread is %d\n",sPrio
.getStr(), static_cast<int>(nValueNormal
));
1014 t_print("nValue in %s Prio Thread is %d\n", sPrio
.getStr(), static_cast<int>(nValueNormal2
));
1016 // ThreadHelper::thread_sleep_tenth_sec(1);
1023 sal_Int32 nDelta
= abs(nValueNormal
- nValueNormal2
);
1024 double nQuotient
= std::max(nValueNormal
, nValueNormal2
);
1025 CPPUNIT_ASSERT_MESSAGE(
1026 "Quotient is zero, which means, there exist no right values.",
1029 double nDeltaPercent
= nDelta
/ nQuotient
* 100;
1031 t_print("Delta value %d, percent %f\n", static_cast<int>(nDelta
), nDeltaPercent
);
1033 // LLA: it's not a bug if the current OS is not able to handle thread scheduling right and good.
1035 // LLA: CPPUNIT_ASSERT_MESSAGE(
1036 // LLA: "Run 2 normal threads, the count diff more than 5 percent.",
1037 // LLA: nDeltaPercent <= 5
1041 void setPriority_001_1()
1043 check2Threads(osl_Thread_PriorityHighest
);
1045 void setPriority_001_2()
1047 check2Threads(osl_Thread_PriorityAboveNormal
);
1049 void setPriority_001_3()
1051 check2Threads(osl_Thread_PriorityNormal
);
1053 void setPriority_001_4()
1055 check2Threads(osl_Thread_PriorityBelowNormal
);
1057 void setPriority_001_5()
1059 check2Threads(osl_Thread_PriorityLowest
);
1062 void setPriority_002()
1064 // initial 5 threads with different priorities
1066 OAddThread aHighestThread
;
1067 OAddThread aAboveNormalThread
;
1068 OAddThread aNormalThread
;
1069 //OAddThread *aBelowNormalThread = new OAddThread();
1070 //OAddThread *aLowestThread = new OAddThread();
1072 //Create them and start running at the same time
1073 aHighestThread
.createSuspended();
1074 aHighestThread
.setPriority(osl_Thread_PriorityHighest
);
1076 aAboveNormalThread
.createSuspended();
1077 aAboveNormalThread
.setPriority(osl_Thread_PriorityAboveNormal
);
1079 aNormalThread
.createSuspended();
1080 aNormalThread
.setPriority(osl_Thread_PriorityNormal
);
1081 /*aBelowNormalThread->create();
1082 aBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
1083 aLowestThread->create();
1084 aLowestThread->setPriority(osl_Thread_PriorityLowest);
1087 aHighestThread
.resume();
1088 aAboveNormalThread
.resume();
1089 aNormalThread
.resume();
1091 ThreadHelper::thread_sleep_tenth_sec(5);
1093 aHighestThread
.suspend();
1094 aAboveNormalThread
.suspend();
1095 aNormalThread
.suspend();
1097 termAndJoinThread(&aNormalThread
);
1098 termAndJoinThread(&aAboveNormalThread
);
1099 termAndJoinThread(&aHighestThread
);
1100 //aBelowNormalThread->terminate();
1101 //aLowestThread->terminate();
1103 sal_Int32 nValueHighest
= aHighestThread
.getValue();
1105 sal_Int32 nValueAboveNormal
= aAboveNormalThread
.getValue();
1107 sal_Int32 nValueNormal
= aNormalThread
.getValue();
1109 t_print("After 10 tenth seconds\n");
1110 t_print("nValue in Highest Prio Thread is %d\n", static_cast<int>(nValueHighest
));
1111 t_print("nValue in AboveNormal Prio Thread is %d\n", static_cast<int>(nValueAboveNormal
));
1112 t_print("nValue in Normal Prio Thread is %d\n", static_cast<int>(nValueNormal
));
1115 CPPUNIT_ASSERT_MESSAGE(
1119 CPPUNIT_ASSERT_MESSAGE(
1121 nValueAboveNormal
> 0
1123 CPPUNIT_ASSERT_MESSAGE(
1130 void setPriority_003()
1132 // initial 5 threads with different priorities
1133 OAddThread pHighestThread
;
1134 OAddThread pAboveNormalThread
;
1135 OAddThread pNormalThread
;
1136 OAddThread pBelowNormalThread
;
1137 OAddThread pLowestThread
;
1139 //Create them and start running at the same time
1140 pHighestThread
.createSuspended();
1141 pHighestThread
.setPriority(osl_Thread_PriorityHighest
);
1143 pAboveNormalThread
.createSuspended();
1144 pAboveNormalThread
.setPriority(osl_Thread_PriorityAboveNormal
);
1146 pNormalThread
.createSuspended();
1147 pNormalThread
.setPriority(osl_Thread_PriorityNormal
);
1149 pBelowNormalThread
.createSuspended();
1150 pBelowNormalThread
.setPriority(osl_Thread_PriorityBelowNormal
);
1152 pLowestThread
.createSuspended();
1153 pLowestThread
.setPriority(osl_Thread_PriorityLowest
);
1155 pHighestThread
.resume();
1156 pAboveNormalThread
.resume();
1157 pNormalThread
.resume();
1158 pBelowNormalThread
.resume();
1159 pLowestThread
.resume();
1161 ThreadHelper::thread_sleep_tenth_sec(5);
1163 pHighestThread
.suspend();
1164 pAboveNormalThread
.suspend();
1165 pNormalThread
.suspend();
1166 pBelowNormalThread
.suspend();
1167 pLowestThread
.suspend();
1169 termAndJoinThread(&pHighestThread
);
1170 termAndJoinThread(&pAboveNormalThread
);
1171 termAndJoinThread(&pNormalThread
);
1172 termAndJoinThread(&pBelowNormalThread
);
1173 termAndJoinThread(&pLowestThread
);
1175 sal_Int32 nValueHighest
= pHighestThread
.getValue();
1177 sal_Int32 nValueAboveNormal
= pAboveNormalThread
.getValue();
1179 sal_Int32 nValueNormal
= pNormalThread
.getValue();
1181 sal_Int32 nValueBelowNormal
= pBelowNormalThread
.getValue();
1183 sal_Int32 nValueLowest
= pLowestThread
.getValue();
1185 t_print("After 10 tenth seconds\n");
1186 t_print("nValue in Highest Prio Thread is %d\n", static_cast<int>(nValueHighest
));
1187 t_print("nValue in AboveNormal Prio Thread is %d\n", static_cast<int>(nValueAboveNormal
));
1188 t_print("nValue in Normal Prio Thread is %d\n", static_cast<int>(nValueNormal
));
1189 t_print("nValue in BelowNormal Prio Thread is %d\n", static_cast<int>(nValueBelowNormal
));
1190 t_print("nValue in Lowest Prio Thread is %d\n", static_cast<int>(nValueLowest
));
1193 CPPUNIT_ASSERT_MESSAGE(
1197 CPPUNIT_ASSERT_MESSAGE(
1199 nValueAboveNormal
> 0
1201 CPPUNIT_ASSERT_MESSAGE(
1205 CPPUNIT_ASSERT_MESSAGE(
1207 nValueBelowNormal
> 0
1209 CPPUNIT_ASSERT_MESSAGE(
1216 void setPriority_004()
1218 // initial 5 threads with different priorities
1219 // OAddThread *pHighestThread = new OAddThread();
1220 OAddThread pAboveNormalThread
;
1221 OAddThread pNormalThread
;
1222 OAddThread pBelowNormalThread
;
1223 OAddThread pLowestThread
;
1225 //Create them and start running at the same time
1226 // pHighestThread->createSuspended();
1227 // pHighestThread->setPriority(osl_Thread_PriorityHighest);
1229 pAboveNormalThread
.createSuspended();
1230 pAboveNormalThread
.setPriority(osl_Thread_PriorityAboveNormal
);
1232 pNormalThread
.createSuspended();
1233 pNormalThread
.setPriority(osl_Thread_PriorityNormal
);
1235 pBelowNormalThread
.createSuspended();
1236 pBelowNormalThread
.setPriority(osl_Thread_PriorityBelowNormal
);
1238 pLowestThread
.createSuspended();
1239 pLowestThread
.setPriority(osl_Thread_PriorityLowest
);
1241 // pHighestThread->resume();
1242 pAboveNormalThread
.resume();
1243 pNormalThread
.resume();
1244 pBelowNormalThread
.resume();
1245 pLowestThread
.resume();
1247 ThreadHelper::thread_sleep_tenth_sec(5);
1249 // pHighestThread->suspend();
1250 pAboveNormalThread
.suspend();
1251 pNormalThread
.suspend();
1252 pBelowNormalThread
.suspend();
1253 pLowestThread
.suspend();
1255 // termAndJoinThread(pHighestThread);
1256 termAndJoinThread(&pAboveNormalThread
);
1257 termAndJoinThread(&pNormalThread
);
1258 termAndJoinThread(&pBelowNormalThread
);
1259 termAndJoinThread(&pLowestThread
);
1261 // sal_Int32 nValueHighest = 0;
1262 // nValueHighest = pHighestThread->getValue();
1264 sal_Int32 nValueAboveNormal
= pAboveNormalThread
.getValue();
1266 sal_Int32 nValueNormal
= pNormalThread
.getValue();
1268 sal_Int32 nValueBelowNormal
= pBelowNormalThread
.getValue();
1270 sal_Int32 nValueLowest
= pLowestThread
.getValue();
1272 t_print("After 5 tenth seconds\n");
1273 t_print("nValue in AboveNormal Prio Thread is %d\n", static_cast<int>(nValueAboveNormal
));
1274 t_print("nValue in Normal Prio Thread is %d\n", static_cast<int>(nValueNormal
));
1275 t_print("nValue in BelowNormal Prio Thread is %d\n", static_cast<int>(nValueBelowNormal
));
1276 t_print("nValue in Lowest Prio Thread is %d\n", static_cast<int>(nValueLowest
));
1278 // delete pHighestThread;
1281 CPPUNIT_ASSERT_MESSAGE(
1283 /* nValueHighest > 0 && */
1284 nValueAboveNormal
> 0
1286 CPPUNIT_ASSERT_MESSAGE(
1290 CPPUNIT_ASSERT_MESSAGE(
1292 nValueBelowNormal
> 0
1294 CPPUNIT_ASSERT_MESSAGE(
1300 void setPriority_005()
1302 // initial 5 threads with different priorities
1303 // OAddThread *pHighestThread = new OAddThread();
1304 // OAddThread *pAboveNormalThread = new OAddThread();
1305 OAddThread pNormalThread
;
1306 OAddThread pBelowNormalThread
;
1307 OAddThread pLowestThread
;
1309 //Create them and start running at the same time
1310 // pHighestThread->createSuspended();
1311 // pHighestThread->setPriority(osl_Thread_PriorityHighest);
1313 // pAboveNormalThread->createSuspended();
1314 // pAboveNormalThread->setPriority(osl_Thread_PriorityAboveNormal);
1316 pNormalThread
.createSuspended();
1317 pNormalThread
.setPriority(osl_Thread_PriorityNormal
);
1319 pBelowNormalThread
.createSuspended();
1320 pBelowNormalThread
.setPriority(osl_Thread_PriorityBelowNormal
);
1322 pLowestThread
.createSuspended();
1323 pLowestThread
.setPriority(osl_Thread_PriorityLowest
);
1325 // pHighestThread->resume();
1326 // pAboveNormalThread->resume();
1327 pNormalThread
.resume();
1328 pBelowNormalThread
.resume();
1329 pLowestThread
.resume();
1331 ThreadHelper::thread_sleep_tenth_sec(5);
1333 // pHighestThread->suspend();
1334 // pAboveNormalThread->suspend();
1335 pNormalThread
.suspend();
1336 pBelowNormalThread
.suspend();
1337 pLowestThread
.suspend();
1339 // termAndJoinThread(pHighestThread);
1340 // termAndJoinThread(pAboveNormalThread);
1341 termAndJoinThread(&pNormalThread
);
1342 termAndJoinThread(&pBelowNormalThread
);
1343 termAndJoinThread(&pLowestThread
);
1345 // sal_Int32 nValueHighest = 0;
1346 // nValueHighest = pHighestThread->getValue();
1348 // sal_Int32 nValueAboveNormal = 0;
1349 // nValueAboveNormal = pAboveNormalThread->getValue();
1351 sal_Int32 nValueNormal
= pNormalThread
.getValue();
1353 sal_Int32 nValueBelowNormal
= pBelowNormalThread
.getValue();
1355 sal_Int32 nValueLowest
= pLowestThread
.getValue();
1357 t_print("After 5 tenth seconds\n");
1358 t_print("nValue in Normal Prio Thread is %d\n", static_cast<int>(nValueNormal
));
1359 t_print("nValue in BelowNormal Prio Thread is %d\n", static_cast<int>(nValueBelowNormal
));
1360 t_print("nValue in Lowest Prio Thread is %d\n", static_cast<int>(nValueLowest
));
1363 CPPUNIT_ASSERT_MESSAGE(
1365 /* nValueHighest > 0 && */
1366 /* nValueAboveNormal > 0 && */
1369 CPPUNIT_ASSERT_MESSAGE(
1371 nValueBelowNormal
> 0
1373 CPPUNIT_ASSERT_MESSAGE(
1380 CPPUNIT_TEST_SUITE(setPriority
);
1382 CPPUNIT_TEST(setPriority_002
);
1383 CPPUNIT_TEST(setPriority_003
);
1384 CPPUNIT_TEST(setPriority_004
);
1385 CPPUNIT_TEST(setPriority_005
);
1387 CPPUNIT_TEST(setPriority_001_1
);
1388 CPPUNIT_TEST(setPriority_001_2
);
1389 CPPUNIT_TEST(setPriority_001_3
);
1390 CPPUNIT_TEST(setPriority_001_4
);
1391 CPPUNIT_TEST(setPriority_001_5
);
1392 CPPUNIT_TEST_SUITE_END();
1393 }; // class setPriority
1395 /** Test of the osl::Thread::getPriority method
1397 class getPriority
: public CppUnit::TestFixture
1400 // insert your test code here.
1401 void getPriority_001()
1403 OAddThread
*pHighestThread
= new OAddThread();
1405 //Create them and start running at the same time
1406 pHighestThread
->create();
1407 pHighestThread
->setPriority(osl_Thread_PriorityHighest
);
1409 oslThreadPriority aPriority
= pHighestThread
->getPriority();
1410 termAndJoinThread(pHighestThread
);
1411 delete pHighestThread
;
1413 ThreadHelper::outputPriority(aPriority
);
1415 // LLA: Priority settings may not work within some OS versions.
1416 #if defined(_WIN32) || defined(__sun)
1417 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1419 osl_Thread_PriorityHighest
, aPriority
1423 // NO_PTHREAD_PRIORITY ???
1424 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1426 osl_Thread_PriorityNormal
, aPriority
1431 CPPUNIT_TEST_SUITE(getPriority
);
1432 CPPUNIT_TEST(getPriority_001
);
1433 CPPUNIT_TEST_SUITE_END();
1434 }; // class getPriority
1436 class getIdentifier
: public CppUnit::TestFixture
1439 // initialise your test code values here.
1441 void getIdentifier_001()
1443 // insert your test code here.
1446 CPPUNIT_TEST_SUITE(getIdentifier
);
1447 CPPUNIT_TEST(getIdentifier_001
);
1448 CPPUNIT_TEST_SUITE_END();
1449 }; // class getIdentifier
1451 /** Test of the osl::Thread::getCurrentIdentifier method
1453 class getCurrentIdentifier
: public CppUnit::TestFixture
1456 void getCurrentIdentifier_001()
1458 oslThreadIdentifier oId
;
1459 OCountThread
* pCountThread
= new OCountThread
;
1460 pCountThread
->create();
1461 pCountThread
->setWait(3);
1462 oId
= Thread::getCurrentIdentifier();
1463 oslThreadIdentifier oIdChild
= pCountThread
->getIdentifier();
1464 termAndJoinThread(pCountThread
);
1465 delete pCountThread
;
1467 CPPUNIT_ASSERT_MESSAGE(
1468 "Get the identifier for the current active thread.",
1472 CPPUNIT_TEST_SUITE(getCurrentIdentifier
);
1473 CPPUNIT_TEST(getCurrentIdentifier_001
);
1474 CPPUNIT_TEST_SUITE_END();
1475 }; // class getCurrentIdentifier
1477 /** Test of the osl::Thread::wait method
1479 class waittest
: public CppUnit::TestFixture
1482 /** call wait in the run method
1485 tested thread wait nWaitSec seconds, main thread sleep (2) seconds,
1486 then terminate the tested thread, due to the fact that the thread do a sleep(1) + wait(5)
1487 it's finish after 6 seconds.
1491 OCountThread
*aCountThread
= new OCountThread();
1492 sal_Int32 nWaitSec
= 5;
1493 aCountThread
->setWait(nWaitSec
);
1494 // thread runs at least 5 seconds.
1495 bool bRes
= aCountThread
->create();
1496 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
);
1498 //record the time when the running begin
1499 StopWatch aStopWatch
;
1502 // wait a little bit, to let the thread the time, to start
1503 ThreadHelper::thread_sleep_tenth_sec( 4 );
1506 // this function returns, after 4 sec. later
1507 termAndJoinThread(aCountThread
);
1509 // value should be one.
1510 sal_Int32 nValue
= aCountThread
->getValue();
1514 // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
1515 double nTenthSec
= aStopWatch
.getTenthSec();
1516 double nSec
= aStopWatch
.getSeconds();
1517 delete aCountThread
;
1518 t_print("nTenthSec = %f \n", nTenthSec
);
1519 t_print("nSec = %f \n", nSec
);
1520 t_print("nValue = %d \n", static_cast<int>(nValue
));
1522 CPPUNIT_ASSERT_MESSAGE(
1523 "Wait: Blocks the calling thread for the given number of time.",
1526 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1527 "Wait: Blocks the calling thread for the given number of time.",
1528 sal_Int32(1), nValue
1533 CPPUNIT_TEST_SUITE(waittest
);
1534 CPPUNIT_TEST(wait_001
);
1535 CPPUNIT_TEST_SUITE_END();
1536 }; // class waittest
1538 /** osl::Thread::yield method: can not design good test scenario to test up to now
1540 class yield
: public CppUnit::TestFixture
1545 // insert your test code here.
1548 CPPUNIT_TEST_SUITE(yield
);
1549 CPPUNIT_TEST(yield_001
);
1550 CPPUNIT_TEST_SUITE_END();
1553 /** Test of the osl::Thread::schedule method
1555 class schedule
: public CppUnit::TestFixture
1559 /** The requested thread will get terminate the next time schedule() is called.
1561 Note: on UNX, if call suspend thread is not the to be suspended thread, the to be
1562 suspended thread will get suspended the next time schedule() is called,
1563 while on w32, it's nothing with schedule.
1565 check if suspend and terminate work well via schedule
1569 OAddThread
* aThread
= new OAddThread();
1570 bool bRes
= aThread
->create();
1571 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
);
1573 ThreadHelper::thread_sleep_tenth_sec(2);
1575 ThreadHelper::thread_sleep_tenth_sec(1);
1576 sal_Int32 nValue
= aThread
->getValue();
1577 ThreadHelper::thread_sleep_tenth_sec(3);
1578 sal_Int32 nLaterValue
= aThread
->getValue();
1579 // resumeAndWaitThread(aThread);
1580 t_print(" value = %d\n", static_cast<int>(nValue
));
1581 t_print("later value = %d\n", static_cast<int>(nLaterValue
));
1582 // if value and latervalue not equal, then the thread would not suspended
1584 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1585 "Schedule: suspend works.",
1590 ThreadHelper::thread_sleep_tenth_sec(2);
1592 aThread
->terminate();
1593 sal_Int32 nValue_term
= aThread
->getValue();
1596 sal_Int32 nValue_join
= aThread
->getValue();
1598 t_print("value after term = %d\n", static_cast<int>(nValue_term
));
1599 t_print("value after join = %d\n", static_cast<int>(nValue_join
));
1601 // nValue_term and nValue_join should be the same
1602 // but should be differ from nValue
1605 //check if thread really terminate after call terminate, if join immediately return
1606 CPPUNIT_ASSERT_MESSAGE(
1607 "Schedule: Returns False if the thread should terminate.",
1608 nValue_join
- nValue_term
<= 1
1610 CPPUNIT_ASSERT_MESSAGE(
1611 "Schedule: Returns False if the thread should terminate.",
1612 nValue_join
- nValue_term
>= 0
1617 /** design a thread that has not call schedule in the workfunction--run method
1621 ONoScheduleThread aThread
; // this thread runs 10 sec. (no schedule() used)
1622 bool bRes
= aThread
.create();
1623 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes
);
1625 ThreadHelper::thread_sleep_tenth_sec(2);
1627 sal_Int32 nValue
= aThread
.getValue();
1629 ThreadHelper::thread_sleep_tenth_sec(3);
1630 sal_Int32 nLaterValue
= aThread
.getValue();
1631 ThreadHelper::thread_sleep_tenth_sec(5);
1633 resumeAndWaitThread(&aThread
);
1635 t_print(" value = %d\n", static_cast<int>(nValue
));
1636 t_print("later value = %d\n", static_cast<int>(nLaterValue
));
1638 //On windows, suspend works, so the values are same
1640 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1641 "Schedule: don't schedule in thread run method, suspend works.",
1646 //On UNX, suspend does not work, so the difference of the values equals to sleep seconds number
1649 CPPUNIT_ASSERT_MESSAGE(
1650 "Schedule: don't schedule in thread run method, suspend does not work too.",
1651 nLaterValue
> nValue
1655 // terminate will not work if no schedule in thread's work function
1656 termAndJoinThread(&aThread
);
1657 sal_Int32 nValue_term
= aThread
.getValue();
1659 t_print(" value term = %d\n", static_cast<int>(nValue_term
));
1661 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1662 "Schedule: don't schedule in thread run method, terminate failed.",
1663 static_cast<sal_Int32
>(10), nValue_term
1667 CPPUNIT_TEST_SUITE(schedule
);
1668 CPPUNIT_TEST(schedule_001
);
1669 CPPUNIT_TEST(schedule_002
);
1670 CPPUNIT_TEST_SUITE_END();
1671 }; // class schedule
1673 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::create
, "osl_Thread");
1674 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::createSuspended
, "osl_Thread");
1675 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::suspend
, "osl_Thread");
1676 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::resume
, "osl_Thread");
1677 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::terminate
, "osl_Thread");
1678 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::join
, "osl_Thread");
1679 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::isRunning
, "osl_Thread");
1680 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::setPriority
, "osl_Thread");
1681 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getPriority
, "osl_Thread");
1682 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getIdentifier
, "osl_Thread");
1683 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getCurrentIdentifier
, "osl_Thread");
1684 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::waittest
, "osl_Thread");
1685 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::yield
, "osl_Thread");
1686 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::schedule
, "osl_Thread");
1687 } // namespace osl_Thread
1689 // destroy function when the binding thread terminate
1690 static void destroyCallback(void * data
)
1692 delete[] static_cast<char *>(data
);
1695 static ThreadData
myThreadData(destroyCallback
);
1699 class myKeyThread
: public Thread
1702 // a public char member for test result checking
1704 // for pass thread-special data to thread
1705 explicit myKeyThread(const char cData
)
1713 void SAL_CALL
run() override
1715 char * pc
= new char[2];
1716 // strcpy(pc, &m_nData);
1717 memcpy(pc
, &m_nData
, 1);
1720 myThreadData
.setData(pc
);
1721 char* pData
= static_cast<char*>(myThreadData
.getData());
1722 m_Char_Test
= *pData
;
1723 // wait for long time to check the data value in main thread
1724 ThreadHelper::thread_sleep_tenth_sec(3);
1727 virtual ~myKeyThread() override
1731 t_print("error: not terminated.\n");
1738 static ThreadData idData
;
1742 class idThread
: public Thread
1745 oslThreadIdentifier m_Id
;
1747 void SAL_CALL
run() override
1749 std::unique_ptr
<oslThreadIdentifier
> pId( new oslThreadIdentifier
);
1750 *pId
= getIdentifier();
1751 idData
.setData(pId
.get());
1752 oslThreadIdentifier
* pIdData
= static_cast<oslThreadIdentifier
*>(idData
.getData());
1753 //t_print("Thread %d has Data %d\n", getIdentifier(), *pIdData);
1758 virtual ~idThread() override
1762 t_print("error: not terminated.\n");
1769 namespace osl_ThreadData
1772 class ctors
: public CppUnit::TestFixture
1776 // insert your test code here.
1782 CPPUNIT_TEST_SUITE(ctors
);
1783 CPPUNIT_TEST(ctor_001
);
1784 CPPUNIT_TEST_SUITE_END();
1787 class setData
: public CppUnit::TestFixture
1791 /** the same instance of the class can have different values in different threads
1803 oslThreadIdentifier aThreadId1
= aThread1
.getIdentifier();
1804 oslThreadIdentifier aThreadId2
= aThread2
.getIdentifier();
1806 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1807 "ThreadData setData: ",
1808 aThread1
.m_Id
, aThreadId1
1810 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1811 "ThreadData setData: ",
1812 aThread2
.m_Id
, aThreadId2
1819 // at first, set the data a value
1820 char* pc
= new char[2];
1825 myThreadData
.setData(pc
);
1827 myKeyThread
aThread1('a');
1829 myKeyThread
aThread2('b');
1831 // aThread1 and aThread2 should have not terminated yet, check current data, not 'a' 'b'
1832 char* pChar
= static_cast<char*>(myThreadData
.getData());
1833 char aChar
= *pChar
;
1838 // the saved thread data of aThread1 & aThread2, different
1839 char cData1
= aThread1
.m_Char_Test
;
1840 char cData2
= aThread2
.m_Char_Test
;
1842 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1843 "ThreadData setData: ",
1846 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1847 "ThreadData setData: ",
1850 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1851 "ThreadData setData: ",
1856 /** setData the second time, and then getData
1860 // at first, set the data a value
1861 char* pc
= new char[2];
1863 memcpy(pc
, &nData
, 1);
1865 myThreadData
.setData(pc
);
1867 myKeyThread
aThread1('a');
1869 myKeyThread
aThread2('b');
1871 // aThread1 and aThread2 should have not terminated yet
1872 // setData the second time
1873 char* pc2
= new char[2];
1875 memcpy(pc2
, &nData
, 1);
1878 myThreadData
.setData(pc2
);
1879 char* pChar
= static_cast<char*>(myThreadData
.getData());
1880 char aChar
= *pChar
;
1885 // the saved thread data of aThread1 & aThread2, different
1886 char cData1
= aThread1
.m_Char_Test
;
1887 char cData2
= aThread2
.m_Char_Test
;
1889 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1890 "ThreadData setData: ",
1893 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1894 "ThreadData setData: ",
1897 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1898 "ThreadData setData: ",
1903 CPPUNIT_TEST_SUITE(setData
);
1904 CPPUNIT_TEST(setData_001
);
1905 CPPUNIT_TEST(setData_002
);
1906 CPPUNIT_TEST(setData_003
);
1907 CPPUNIT_TEST_SUITE_END();
1910 class getData
: public CppUnit::TestFixture
1914 // After setData in child threads, get Data in the main thread, should be independent
1917 char* pc
= new char[2];
1919 myThreadData
.setData(pc
);
1921 myKeyThread
aThread1('c');
1923 myKeyThread
aThread2('d');
1929 char cData1
= aThread1
.m_Char_Test
;
1930 char cData2
= aThread2
.m_Char_Test
;
1932 char* pChar
= static_cast<char*>(myThreadData
.getData());
1933 char aChar
= *pChar
;
1935 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1936 "ThreadData setData: ",
1939 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1940 "ThreadData setData: ",
1943 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1944 "ThreadData setData: ",
1949 // setData then change the value in the address data pointer points,
1950 // and then getData, should get the new value
1953 char* pc
= new char[2];
1955 memcpy(pc
, &nData
, 1);
1958 myThreadData
.setData(pc
);
1960 myKeyThread
aThread1('a');
1962 myKeyThread
aThread2('b');
1965 // change the value which pc points
1967 memcpy(pc
, &nData2
, 1);
1970 void* pChar
= myThreadData
.getData();
1971 char aChar
= *static_cast<char*>(pChar
);
1976 char cData1
= aThread1
.m_Char_Test
;
1977 char cData2
= aThread2
.m_Char_Test
;
1979 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1980 "ThreadData setData: ",
1983 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1984 "ThreadData setData: ",
1987 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1988 "ThreadData setData: ",
1994 CPPUNIT_TEST_SUITE(getData
);
1995 CPPUNIT_TEST(getData_001
);
1996 CPPUNIT_TEST(getData_002
);
1997 CPPUNIT_TEST_SUITE_END();
2000 CPPUNIT_TEST_SUITE_REGISTRATION(osl_ThreadData::ctors
);
2001 CPPUNIT_TEST_SUITE_REGISTRATION(osl_ThreadData::setData
);
2002 CPPUNIT_TEST_SUITE_REGISTRATION(osl_ThreadData::getData
);
2003 } // namespace osl_ThreadData
2005 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */