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