LCOV - code coverage report
Current view: top level - sal/qa/osl/process - osl_Thread.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 182 977 18.6 %
Date: 2015-06-13 12:38:46 Functions: 50 282 17.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include <algorithm>
      21             : #ifdef WNT
      22             : #include <windows.h>
      23             : #else
      24             : #include <unistd.h>
      25             : #include <time.h>
      26             : #endif
      27             : 
      28             : // include files
      29             : 
      30             : #include <sal/types.h>
      31             : 
      32             : #include <rtl/string.hxx>
      33             : 
      34             : #include <rtl/strbuf.hxx>
      35             : 
      36             : #include <osl/thread.hxx>
      37             : 
      38             : #include <osl/mutex.hxx>
      39             : #include <osl/time.h>
      40             : 
      41             : #include <string.h>
      42             : 
      43             : #include <cppunit/TestFixture.h>
      44             : #include <cppunit/extensions/HelperMacros.h>
      45             : #include <cppunit/plugin/TestPlugIn.h>
      46             : 
      47             : #define t_print printf
      48             : 
      49             : using namespace osl;
      50             : 
      51             : using ::rtl::OString;
      52             : 
      53             : // Small stopwatch
      54             : class StopWatch {
      55             :     TimeValue t1,t2;                                // Start and stoptime
      56             : 
      57             : protected:
      58             :     sal_Int32 m_nNanoSec;
      59             :     sal_Int32 m_nSeconds;
      60             : 
      61             :     bool m_bIsValid;                                   // TRUE, when started and stopped
      62             :     bool m_bIsRunning;                                 // TRUE, when started
      63             : 
      64             : public:
      65             :     StopWatch();
      66           0 :     ~StopWatch() {}
      67             : 
      68             :     void start();                                 // Starts time
      69             :     void stop();                                  // Stops time
      70             : 
      71             :     double getSeconds() const;
      72             :     double getTenthSec() const;
      73             : };
      74             : 
      75             : // ================================= Stop Watch =================================
      76             : 
      77             : // A small stopwatch for internal use
      78             : // (c) Lars Langhans 29.12.1996 22:10
      79             : 
      80           0 : StopWatch::StopWatch()
      81             :     : m_nNanoSec(0)
      82             :     , m_nSeconds(0)
      83             :     , m_bIsValid(false)
      84           0 :     , m_bIsRunning(false)
      85             : {
      86           0 :     t1.Seconds = 0;
      87           0 :     t1.Nanosec = 0;
      88           0 :     t2.Seconds = 0;
      89           0 :     t2.Nanosec = 0;
      90           0 : }
      91             : 
      92           0 : void StopWatch::start()
      93             : {
      94             : // pre: %
      95             : // post: Start Timer
      96             : 
      97           0 :     m_bIsValid = false;
      98           0 :     m_bIsRunning = true;
      99           0 :     osl_getSystemTime( &t1 );
     100           0 :     t_print("# %u %u nsecs\n", (unsigned)t1.Seconds, (unsigned)t1.Nanosec);
     101             :     // gettimeofday(&t1, 0);
     102           0 : }
     103             : 
     104           0 : void StopWatch::stop()
     105             : {
     106             : // pre: Timer should be started
     107             : // post: Timer will stopped
     108             : 
     109             :     // gettimeofday(&t2, 0);                         // Ask timer
     110           0 :     osl_getSystemTime( &t2 );
     111           0 :     t_print("# %u %u nsecs\n", (unsigned) t2.Seconds, (unsigned) t2.Nanosec);
     112             : 
     113           0 :     if (m_bIsRunning)
     114             :     {                                // check if started.
     115           0 :         m_nSeconds = static_cast<sal_Int32>(t2.Seconds) - static_cast<sal_Int32>(t1.Seconds);
     116           0 :         if ( t2.Nanosec > t1.Nanosec )
     117           0 :                m_nNanoSec = static_cast<sal_Int32>(t2.Nanosec) - static_cast<sal_Int32>(t1.Nanosec);
     118             :            else
     119             :            {
     120           0 :         m_nNanoSec = 1000000000 + static_cast<sal_Int32>(t2.Nanosec) - static_cast<sal_Int32>(t1.Nanosec);
     121           0 :                 m_nSeconds -= 1;
     122             :     }
     123           0 :     t_print("# %u %u nsecs\n", (unsigned) m_nSeconds, (unsigned) m_nNanoSec );
     124             :         //if (m_nNanoSec < 0)
     125             :         //{
     126             :             //m_nNanoSec += 1000000000;
     127             :             //m_nSeconds -= 1;
     128             :         //}
     129           0 :         m_bIsValid = true;
     130           0 :         m_bIsRunning = false;
     131             :     }
     132           0 : }
     133             : 
     134           0 : double StopWatch::getSeconds() const
     135             : {
     136             : // pre: valid = TRUE
     137             : // BACK: time in seconds
     138             : 
     139           0 :     double nValue = 0.0;
     140           0 :     if (m_bIsValid)
     141             :     {
     142           0 :         nValue = double(m_nNanoSec) / 1000000000.0 + m_nSeconds; // milli micro nano
     143             :     }
     144           0 :     return nValue;
     145             : }
     146             : 
     147           0 : double StopWatch::getTenthSec() const
     148             : {
     149           0 :     double nValue = 0.0;
     150           0 :     if (m_bIsValid)
     151             :     {
     152           0 :         nValue = double(m_nNanoSec) / 100000000.0 + m_nSeconds * 10;
     153             :     }
     154           0 :     return nValue ;
     155             : }
     156             : 
     157             : template <class T>
     158           0 : class ThreadSafeValue
     159             : {
     160             :     T   m_nFlag;
     161             :     Mutex   m_aMutex;
     162             : public:
     163           0 :     ThreadSafeValue(T n = 0): m_nFlag(n) {}
     164           0 :     T getValue()
     165             :         {
     166             :             //block if already acquired by another thread.
     167           0 :             osl::MutexGuard g(m_aMutex);
     168           0 :             return m_nFlag;
     169             :         }
     170           0 :     void addValue(T n)
     171             :         {
     172             :             //only one thread operate on the flag.
     173           0 :             osl::MutexGuard g(m_aMutex);
     174           0 :             m_nFlag += n;
     175           0 :         }
     176           0 :     void acquire() {m_aMutex.acquire();}
     177           0 :     void release() {m_aMutex.release();}
     178             : };
     179             : 
     180             : namespace ThreadHelper
     181             : {
     182           8 :     void thread_sleep_tenth_sec(sal_Int32 _nTenthSec)
     183             :     {
     184             : #ifdef WNT
     185             :         Sleep(_nTenthSec * 100 );
     186             : #else
     187             :         TimeValue nTV;
     188           8 :         nTV.Seconds = static_cast<sal_uInt32>( _nTenthSec/10 );
     189           8 :         nTV.Nanosec = ( (_nTenthSec%10 ) * 100000000 );
     190           8 :         osl_waitThread(&nTV);
     191             : #endif
     192           8 :     }
     193             : 
     194           0 :     void outputPriority(oslThreadPriority const& _aPriority)
     195             :     {
     196             :         // LLA: output the priority
     197           0 :         if (_aPriority == osl_Thread_PriorityHighest)
     198             :         {
     199           0 :             t_print("Prio is High\n");
     200             :         }
     201           0 :         else if (_aPriority == osl_Thread_PriorityAboveNormal)
     202             :         {
     203           0 :             t_print("Prio is above normal\n");
     204             :         }
     205           0 :         else if (_aPriority == osl_Thread_PriorityNormal)
     206             :         {
     207           0 :             t_print("Prio is normal\n");
     208             :         }
     209           0 :         else if (_aPriority == osl_Thread_PriorityBelowNormal)
     210             :         {
     211           0 :             t_print("Prio is below normal\n");
     212             :         }
     213           0 :         else if (_aPriority == osl_Thread_PriorityLowest)
     214             :         {
     215           0 :             t_print("Prio is lowest\n");
     216             :         }
     217             :         else
     218             :         {
     219           0 :             t_print("Prio is unknown\n");
     220             :         }
     221           0 :     }
     222             : }
     223             : 
     224             : /** Simple thread for testing Thread-create.
     225             : 
     226             :     Just add 1 of value 0, and after running, result is 1.
     227             :  */
     228           0 : class myThread : public Thread
     229             : {
     230             :     ThreadSafeValue<sal_Int32> m_aFlag;
     231             : public:
     232           0 :     sal_Int32 getValue() { return m_aFlag.getValue(); }
     233             : protected:
     234             :     /** guarded value which initialized 0
     235             : 
     236             :         @see ThreadSafeValue
     237             :     */
     238           0 :     void SAL_CALL run() SAL_OVERRIDE
     239             :         {
     240           0 :             while(schedule())
     241             :             {
     242           0 :                 m_aFlag.addValue(1);
     243           0 :                 ThreadHelper::thread_sleep_tenth_sec(1);
     244             :             }
     245           0 :         }
     246             : 
     247             : public:
     248             : 
     249           0 :     virtual void SAL_CALL suspend() SAL_OVERRIDE
     250             :         {
     251           0 :             m_aFlag.acquire();
     252           0 :             ::osl::Thread::suspend();
     253           0 :             m_aFlag.release();
     254           0 :         }
     255             : 
     256           0 :     virtual ~myThread()
     257           0 :         {
     258           0 :             if (isRunning())
     259             :             {
     260           0 :                 t_print("error: not terminated.\n");
     261             :             }
     262           0 :         }
     263             : 
     264             : };
     265             : 
     266             : /** Thread which has a flag add 1 every second until 20
     267             :  */
     268             : class OCountThread : public Thread
     269             : {
     270             :     ThreadSafeValue<sal_Int32> m_aFlag;
     271             : public:
     272           0 :     OCountThread()
     273           0 :         {
     274           0 :             m_nWaitSec = 0;
     275           0 :             t_print("new OCountThread thread %u!\n", (unsigned) getIdentifier());
     276           0 :         }
     277           0 :     sal_Int32 getValue() { return m_aFlag.getValue(); }
     278             : 
     279           0 :     void setWait(sal_Int32 nSec)
     280             :         {
     281           0 :             m_nWaitSec = nSec;
     282             :             //m_bWait = sal_True;
     283           0 :         }
     284             : 
     285           0 :     virtual void SAL_CALL suspend() SAL_OVERRIDE
     286             :         {
     287           0 :             m_aFlag.acquire();
     288           0 :             ::osl::Thread::suspend();
     289           0 :             m_aFlag.release();
     290           0 :         }
     291             : 
     292             : protected:
     293             :     //sal_Bool m_bWait;
     294             :     sal_Int32 m_nWaitSec;
     295             : 
     296           0 :     void SAL_CALL run() SAL_OVERRIDE
     297             :         {
     298             :             /// if the thread should terminate, schedule return false
     299           0 :             while (m_aFlag.getValue() < 20 && schedule())
     300             :             {
     301           0 :                 m_aFlag.addValue(1);
     302           0 :                 ThreadHelper::thread_sleep_tenth_sec(1);
     303             : 
     304           0 :                 if (m_nWaitSec != 0)
     305             :                 {
     306             :                     TimeValue nTV;
     307           0 :                     nTV.Seconds = m_nWaitSec / 10 ;
     308           0 :                     nTV.Nanosec = ( m_nWaitSec%10 ) * 100000000 ;
     309           0 :                     wait( nTV );
     310           0 :                     m_nWaitSec = 0;
     311             :                 }
     312             :             }
     313           0 :         }
     314           0 :     void SAL_CALL onTerminated() SAL_OVERRIDE
     315             :         {
     316           0 :             t_print("normally terminate this thread %u!\n", (unsigned) getIdentifier());
     317           0 :         }
     318             : public:
     319             : 
     320           0 :     virtual ~OCountThread()
     321           0 :         {
     322           0 :             if (isRunning())
     323             :             {
     324           0 :                 t_print("error: not terminated.\n");
     325             :             }
     326           0 :         }
     327             : 
     328             : };
     329             : 
     330             : /** no call schedule in the run method
     331             : */
     332             : class ONoScheduleThread : public Thread
     333             : {
     334             :     ThreadSafeValue<sal_Int32> m_aFlag;
     335             : public:
     336           0 :     sal_Int32 getValue() { return m_aFlag.getValue(); }
     337             : 
     338           0 :     virtual void SAL_CALL suspend() SAL_OVERRIDE
     339             :         {
     340           0 :             m_aFlag.acquire();
     341           0 :             ::osl::Thread::suspend();
     342           0 :             m_aFlag.release();
     343           0 :         }
     344             : protected:
     345           0 :     void SAL_CALL run() SAL_OVERRIDE
     346             :         {
     347           0 :             while (m_aFlag.getValue() < 10)
     348             :             {
     349           0 :                 m_aFlag.addValue(1);
     350           0 :                 ThreadHelper::thread_sleep_tenth_sec(1);
     351             :             }
     352           0 :         }
     353           0 :     void SAL_CALL onTerminated() SAL_OVERRIDE
     354             :         {
     355           0 :             t_print("normally terminate this thread %u!\n", (unsigned) getIdentifier());
     356           0 :         }
     357             : public:
     358           0 :     ONoScheduleThread()
     359           0 :         {
     360           0 :                 t_print("new thread id %u!\n", (unsigned) getIdentifier());
     361           0 :         }
     362           0 :     virtual ~ONoScheduleThread()
     363           0 :         {
     364           0 :             if (isRunning())
     365             :             {
     366           0 :                 t_print("error: not terminated.\n");
     367             :             }
     368           0 :         }
     369             : 
     370             : };
     371             : 
     372             : /**
     373             : */
     374             : class OAddThread : public Thread
     375             : {
     376             :     ThreadSafeValue<sal_Int32> m_aFlag;
     377             : public:
     378             :     //oslThreadIdentifier m_id, m_CurId;
     379           0 :     OAddThread(){}
     380           0 :     sal_Int32 getValue() { return m_aFlag.getValue(); }
     381             : 
     382           0 :     virtual void SAL_CALL suspend() SAL_OVERRIDE
     383             :         {
     384           0 :             m_aFlag.acquire();
     385           0 :             ::osl::Thread::suspend();
     386           0 :             m_aFlag.release();
     387           0 :         }
     388             : protected:
     389           0 :     void SAL_CALL run() SAL_OVERRIDE
     390             :         {
     391             :             //if the thread should terminate, schedule return false
     392           0 :             while (schedule())
     393             :             {
     394           0 :                 m_aFlag.addValue(1);
     395             :             }
     396           0 :         }
     397           0 :     void SAL_CALL onTerminated() SAL_OVERRIDE
     398             :         {
     399             :             // t_print("normally terminate this thread %d!\n", getIdentifier());
     400           0 :         }
     401             : public:
     402             : 
     403           0 :     virtual ~OAddThread()
     404           0 :         {
     405           0 :             if (isRunning())
     406             :             {
     407             :                 // t_print("error: not terminated.\n");
     408             :             }
     409           0 :         }
     410             : 
     411             : };
     412             : 
     413             : namespace osl_Thread
     414             : {
     415             : 
     416           0 :     void resumeAndWaitThread(Thread* _pThread)
     417             :     {
     418             :         // This function starts a thread, wait a second and suspends the thread
     419             :         // Due to the fact, that a suspend and never run thread never really exists.
     420             : 
     421             :         // Note: on UNX, after createSuspended, and then terminate the thread, it performs well;
     422             :         // while on Windows, after createSuspended, the thread can not terminate, wait endlessly,
     423             :         // so here call resume at first, then call terminate.
     424             : #ifdef WNT
     425             :         t_print("resumeAndWaitThread\n");
     426             :         _pThread->resume();
     427             :         ThreadHelper::thread_sleep_tenth_sec(1);
     428             : #else
     429           0 :         _pThread->resume();
     430             : #endif
     431           0 :     }
     432             : 
     433             :     // kill a running thread and join it, if it has terminated, do nothing
     434           0 :     void termAndJoinThread(Thread* _pThread)
     435             :     {
     436           0 :         _pThread->terminate();
     437             : 
     438             : // LLA: Windows feature???, a suspended thread can not terminated, so we have to weak it up
     439             : #ifdef WNT
     440             :         _pThread->resume();
     441             :         ThreadHelper::thread_sleep_tenth_sec(1);
     442             : #endif
     443           0 :         t_print("#wait for join.\n");
     444           0 :         _pThread->join();
     445           0 :     }
     446             : /** Test of the osl::Thread::create method
     447             :  */
     448             : 
     449           0 :     class create : public CppUnit::TestFixture
     450             :     {
     451             :     public:
     452             : 
     453             :         // initialise your test code values here.
     454           0 :         void setUp() SAL_OVERRIDE
     455             :             {
     456           0 :             }
     457             : 
     458           0 :         void tearDown() SAL_OVERRIDE
     459             :             {
     460           0 :             }
     461             : 
     462             :         /** Simple create a thread.
     463             : 
     464             :             Create a simple thread, it just does add 1 to value(which initialized 0),
     465             :             if the thread run, the value should be 1.
     466             :         */
     467           0 :         void create_001()
     468             :             {
     469           0 :                 myThread* newthread = new myThread();
     470           0 :                 bool bRes = newthread->create();
     471           0 :                 CPPUNIT_ASSERT_MESSAGE("Can not creates a new thread!\n", bRes);
     472             : 
     473           0 :                 ThreadHelper::thread_sleep_tenth_sec(1);        // wait short
     474           0 :                 bool isRunning = newthread->isRunning();    // check if thread is running
     475             :                 /// wait for the new thread to assure it has run
     476           0 :                 ThreadHelper::thread_sleep_tenth_sec(3);
     477           0 :                 sal_Int32 nValue = newthread->getValue();
     478             :                 /// to assure the new thread has terminated
     479           0 :                 termAndJoinThread(newthread);
     480           0 :                 delete newthread;
     481             : 
     482           0 :                 t_print("   nValue = %d\n", (int) nValue);
     483           0 :                 t_print("isRunning = %s\n", isRunning ? "true" : "false");
     484             : 
     485           0 :                 CPPUNIT_ASSERT_MESSAGE(
     486             :                     "Creates a new thread",
     487             :                     nValue >= 1 && isRunning
     488           0 :                     );
     489             : 
     490           0 :             }
     491             : 
     492             :         /** only one running thread per instance, return false if create secondly
     493             :          */
     494           0 :         void create_002()
     495             :             {
     496           0 :                 myThread* newthread = new myThread();
     497           0 :                 bool res1 = newthread->create();
     498           0 :                 bool res2 = newthread->create();
     499           0 :                 t_print("In non pro, an assertion should occurred. This behaviour is right.\n");
     500           0 :                 termAndJoinThread(newthread);
     501           0 :                 delete newthread;
     502             : 
     503           0 :                 CPPUNIT_ASSERT_MESSAGE(
     504             :                     "Creates a new thread: can not create two threads per instance",
     505             :                     res1 && !res2
     506           0 :                     );
     507             : 
     508           0 :             }
     509             : 
     510           0 :         CPPUNIT_TEST_SUITE(create);
     511           0 :         CPPUNIT_TEST(create_001);
     512           0 :         CPPUNIT_TEST(create_002);
     513           0 :         CPPUNIT_TEST_SUITE_END();
     514             :     }; // class create
     515             : 
     516             :     /** Test of the osl::Thread::createSuspended method
     517             :     */
     518           0 :     class createSuspended : public CppUnit::TestFixture
     519             :     {
     520             :     public:
     521             :         // initialise your test code values here.
     522           0 :         void setUp() SAL_OVERRIDE
     523             :             {
     524           0 :             }
     525             : 
     526           0 :         void tearDown() SAL_OVERRIDE
     527             :             {
     528           0 :             }
     529             : 
     530             :         /** Create a suspended thread, use the same class as create_001
     531             : 
     532             :             after create, wait enough time, check the value, if it's still the initial value, pass
     533             :         */
     534           0 :         void createSuspended_001()
     535             :             {
     536           0 :                 myThread* newthread = new myThread();
     537           0 :                 bool bRes = newthread->createSuspended();
     538           0 :                 CPPUNIT_ASSERT_MESSAGE("Can not creates a new thread!", bRes);
     539             : 
     540           0 :                 ThreadHelper::thread_sleep_tenth_sec(1);
     541           0 :                 bool isRunning = newthread->isRunning();
     542           0 :                 ThreadHelper::thread_sleep_tenth_sec(3);
     543           0 :                 sal_Int32 nValue = newthread->getValue();
     544             : 
     545           0 :                 resumeAndWaitThread(newthread);
     546             : 
     547           0 :                 termAndJoinThread(newthread);
     548           0 :                 delete newthread;
     549             : 
     550           0 :                 CPPUNIT_ASSERT_MESSAGE(
     551             :                     "Creates a new suspended thread",
     552             :                     nValue == 0 && isRunning
     553           0 :                     );
     554           0 :             }
     555             : 
     556           0 :         void createSuspended_002()
     557             :             {
     558           0 :                 myThread* newthread = new myThread();
     559           0 :                 bool res1 = newthread->createSuspended();
     560           0 :                 bool res2 = newthread->createSuspended();
     561             : 
     562           0 :                 resumeAndWaitThread(newthread);
     563             : 
     564           0 :                 termAndJoinThread(newthread);
     565             : 
     566           0 :                 delete newthread;
     567             : 
     568           0 :                 CPPUNIT_ASSERT_MESSAGE(
     569             :                     "Creates a new thread: can not create two threads per instance",
     570             :                     res1 && !res2
     571           0 :                     );
     572           0 :             }
     573             : 
     574           0 :         CPPUNIT_TEST_SUITE(createSuspended);
     575           0 :         CPPUNIT_TEST(createSuspended_001);
     576             :         // LLA: Deadlocked!!!
     577           0 :         CPPUNIT_TEST(createSuspended_002);
     578           0 :         CPPUNIT_TEST_SUITE_END();
     579             :     }; // class createSuspended
     580             : 
     581             :     /** when the count value equal to or more than 3, suspend the thread.
     582             :     */
     583           0 :     void suspendCountThread(OCountThread* _pCountThread)
     584             :     {
     585           0 :         sal_Int32 nValue = 0;
     586             :         while (true)
     587             :         {
     588           0 :             nValue = _pCountThread->getValue();
     589           0 :             if (nValue >= 3)
     590             :             {
     591           0 :                 _pCountThread->suspend();
     592           0 :                 break;
     593             :             }
     594           0 :         }
     595           0 :     }
     596             : 
     597             :     /** Test of the osl::Thread::suspend method
     598             :     */
     599           0 :     class suspend : public CppUnit::TestFixture
     600             :     {
     601             :     public:
     602             :         // initialise your test code values here.
     603           0 :         void setUp() SAL_OVERRIDE
     604             :             {
     605           0 :             }
     606             : 
     607           0 :         void tearDown() SAL_OVERRIDE
     608             :             {
     609           0 :             }
     610             : 
     611             :         /** Use a thread which has a flag added 1 every second
     612             : 
     613             :             ALGORITHM:
     614             :             create the thread, after running special time, record value of flag, then suspend it,
     615             :             wait a long time, check the flag, if it remains unchanged during suspending
     616             :         */
     617           0 :         void suspend_001()
     618             :             {
     619           0 :                 OCountThread* aCountThread = new OCountThread();
     620           0 :                 bool bRes = aCountThread->create();
     621           0 :                 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
     622             :                 // the thread run for some seconds, but not terminate
     623           0 :                 suspendCountThread( aCountThread );
     624             : 
     625             :                 // the value just after calling suspend
     626           0 :                 sal_Int32 nValue = aCountThread->getValue();       // (2)
     627             : 
     628           0 :                 ThreadHelper::thread_sleep_tenth_sec(3);
     629             : 
     630             :                 // the value after waiting 3 seconds
     631           0 :                 sal_Int32 nLaterValue = aCountThread->getValue();    // (3)
     632             : 
     633           0 :                 resumeAndWaitThread(aCountThread);
     634           0 :                 termAndJoinThread(aCountThread);
     635           0 :                 delete aCountThread;
     636             : 
     637           0 :                 CPPUNIT_ASSERT_MESSAGE(
     638             :                     "Suspend the thread",
     639             :                     bRes && nValue == nLaterValue
     640           0 :                     );
     641             : 
     642           0 :             }
     643             : 
     644           0 :         CPPUNIT_TEST_SUITE(suspend);
     645           0 :         CPPUNIT_TEST(suspend_001);
     646             :         // LLA: Deadlocked!!!
     647             :         // CPPUNIT_TEST(createSuspended_002);
     648           0 :         CPPUNIT_TEST_SUITE_END();
     649             :     }; // class suspend
     650             : 
     651             :     /** Test of the osl::Thread::resume method
     652             :     */
     653           0 :     class resume : public CppUnit::TestFixture
     654             :     {
     655             :     public:
     656             :         // initialise your test code values here.
     657           0 :         void setUp() SAL_OVERRIDE
     658             :             {
     659           0 :             }
     660             : 
     661           0 :         void tearDown() SAL_OVERRIDE
     662             :             {
     663           0 :             }
     664             : 
     665             :         /** check if the thread run samely as usual after suspend and resume
     666             : 
     667             :             ALGORITHM:
     668             :             compare the values before and after suspend, they should be same,
     669             :             then compare values before and after resume, the difference should be same as the sleep seconds number
     670             :         */
     671           0 :         void resume_001()
     672             :             {
     673           0 :                 OCountThread* pCountThread = new OCountThread();
     674           0 :                 bool bRes = pCountThread->create();
     675           0 :                 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
     676             : 
     677           0 :                 suspendCountThread(pCountThread);
     678             : 
     679           0 :                 sal_Int32 nSuspendValue = pCountThread->getValue();  // (2)
     680             :                 // suspend for 3 seconds
     681           0 :                 ThreadHelper::thread_sleep_tenth_sec(3);
     682           0 :                 pCountThread->resume();
     683             : 
     684           0 :                 ThreadHelper::thread_sleep_tenth_sec(3);
     685           0 :                 sal_Int32 nResumeValue = pCountThread->getValue();
     686             : 
     687           0 :                 ThreadHelper::thread_sleep_tenth_sec(3);
     688           0 :                 sal_Int32 nLaterValue = pCountThread->getValue();
     689             : 
     690           0 :                 termAndJoinThread(pCountThread);
     691           0 :                 delete pCountThread;
     692             : 
     693           0 :                 t_print("SuspendValue: %d\n", (int) nSuspendValue);
     694           0 :                 t_print("ResumeValue:  %d\n", (int) nResumeValue);
     695           0 :                 t_print("LaterValue:   %d\n", (int) nLaterValue);
     696             : 
     697             :                 /* LLA: this assumption is no longer relevant: nResumeValue ==  nSuspendValue && */
     698           0 :                 CPPUNIT_ASSERT_MESSAGE(
     699             :                     "Suspend then resume the thread",
     700             :                     nLaterValue >= 9 &&
     701             :                     nResumeValue > nSuspendValue &&
     702             :                     nLaterValue > nResumeValue
     703           0 :                     );
     704             : 
     705           0 :             }
     706             : 
     707             :         /** Create a suspended thread then resume, check if the thread has run
     708             :          */
     709           0 :         void resume_002()
     710             :             {
     711           0 :                 myThread* newthread = new myThread();
     712           0 :                 bool bRes = newthread->createSuspended();
     713           0 :                 CPPUNIT_ASSERT_MESSAGE ( "Can't create thread!", bRes );
     714             : 
     715           0 :                 newthread->resume();
     716           0 :                 ThreadHelper::thread_sleep_tenth_sec(2);
     717           0 :                 sal_Int32 nValue = newthread->getValue();
     718             : 
     719           0 :                 termAndJoinThread(newthread);
     720           0 :                 delete newthread;
     721             : 
     722           0 :                 t_print("   nValue = %d\n", (int) nValue);
     723             : 
     724           0 :                 CPPUNIT_ASSERT_MESSAGE(
     725             :                     "Creates a suspended thread, then resume",
     726             :                     nValue >= 1
     727           0 :                     );
     728           0 :             }
     729             : 
     730           0 :         CPPUNIT_TEST_SUITE(resume);
     731           0 :         CPPUNIT_TEST(resume_001);
     732           0 :         CPPUNIT_TEST(resume_002);
     733           0 :         CPPUNIT_TEST_SUITE_END();
     734             :     }; // class resume
     735             : 
     736             :     /** Test of the osl::Thread::terminate method
     737             :     */
     738           0 :     class terminate : public CppUnit::TestFixture
     739             :     {
     740             :     public:
     741             :         // initialise your test code values here.
     742           0 :         void setUp() SAL_OVERRIDE
     743             :             {
     744           0 :             }
     745             : 
     746           0 :         void tearDown() SAL_OVERRIDE
     747             :             {
     748           0 :             }
     749             : 
     750             :         /** Check after call terminate if the running thread running go on executing
     751             : 
     752             :             ALGORITHM:
     753             :             before and after call terminate, the values should be the same
     754             :         */
     755           0 :         void terminate_001()
     756             :             {
     757           0 :                 OCountThread* aCountThread = new OCountThread();
     758           0 :                 bool bRes = aCountThread->create();
     759           0 :                 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
     760             : 
     761           0 :                 ThreadHelper::thread_sleep_tenth_sec(2);
     762           0 :                 sal_Int32 nValue = aCountThread->getValue();
     763           0 :                 aCountThread->terminate();
     764           0 :                 ThreadHelper::thread_sleep_tenth_sec(2);
     765           0 :                 sal_Int32 nLaterValue = aCountThread->getValue();
     766             : 
     767             :                 // isRunning should be false after terminate
     768           0 :                 bool isRunning = aCountThread->isRunning();
     769           0 :                 aCountThread->join();
     770           0 :                 delete aCountThread;
     771             : 
     772           0 :                 t_print("     nValue = %d\n", (int) nValue);
     773           0 :                 t_print("nLaterValue = %d\n", (int) nLaterValue);
     774             : 
     775           0 :                 CPPUNIT_ASSERT_MESSAGE(
     776             :                     "Terminate the thread",
     777             :                     !isRunning && nLaterValue >= nValue
     778           0 :                     );
     779           0 :             }
     780             :         /** Check if a suspended thread will terminate after call terminate, different on w32 and on UNX
     781             :          */
     782           0 :         void terminate_002()
     783             :             {
     784           0 :                 OCountThread* aCountThread = new OCountThread();
     785           0 :                 bool bRes = aCountThread->create();
     786           0 :                 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
     787             : 
     788           0 :                 ThreadHelper::thread_sleep_tenth_sec(1);
     789           0 :                 suspendCountThread(aCountThread);
     790           0 :                 sal_Int32 nValue = aCountThread->getValue();
     791             : 
     792             :                 // seems a suspended thread can not be terminated on W32, while on Solaris can
     793           0 :                 resumeAndWaitThread(aCountThread);
     794             : 
     795           0 :                 ThreadHelper::thread_sleep_tenth_sec(2);
     796             : 
     797           0 :                 termAndJoinThread(aCountThread);
     798           0 :                 sal_Int32 nLaterValue = aCountThread->getValue();
     799           0 :                 delete aCountThread;
     800             : 
     801           0 :                 t_print("     nValue = %d\n", (int) nValue);
     802           0 :                 t_print("nLaterValue = %d\n", (int) nLaterValue);
     803             : 
     804           0 :                 CPPUNIT_ASSERT_MESSAGE(
     805             :                     "Suspend then resume the thread",
     806           0 :                     nLaterValue > nValue );
     807           0 :             }
     808             : 
     809           0 :         CPPUNIT_TEST_SUITE(terminate);
     810           0 :         CPPUNIT_TEST(terminate_001);
     811           0 :         CPPUNIT_TEST(terminate_002);
     812           0 :         CPPUNIT_TEST_SUITE_END();
     813             :     }; // class terminate
     814             : 
     815             :     /** Test of the osl::Thread::join method
     816             :     */
     817           0 :     class join : public CppUnit::TestFixture
     818             :     {
     819             :     public:
     820             :         // initialise your test code values here.
     821           0 :         void setUp() SAL_OVERRIDE
     822             :             {
     823           0 :             }
     824             : 
     825           0 :         void tearDown() SAL_OVERRIDE
     826             :             {
     827           0 :             }
     828             : 
     829             :         /** Check after call terminate if the thread running function will not go on executing
     830             : 
     831             :             the next statement after join will not exec before the thread terminate
     832             :             ALGORITHM:
     833             :             recode system time at the beginning of the thread run, call join, then record system time again,
     834             :             the difference of the two time should be equal or more than 20 seconds, the CountThead normally terminate
     835             :         */
     836           0 :         void join_001()
     837             :             {
     838           0 :                 OCountThread *aCountThread = new OCountThread();
     839           0 :                 bool bRes = aCountThread->create();
     840           0 :                 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
     841             : 
     842           0 :                 StopWatch aStopWatch;
     843           0 :                 aStopWatch.start();
     844             :                 // TimeValue aTimeVal_befor;
     845             :                 // osl_getSystemTime( &aTimeVal_befor );
     846             :                 //t_print("#join:the system time is %d,%d\n", pTimeVal_befor->Seconds,pTimeVal_befor->Nanosec);
     847             : 
     848           0 :                 aCountThread->join();
     849             : 
     850             :                 //the below line will be executed after aCountThread terminate
     851             :                 // TimeValue aTimeVal_after;
     852             :                 // osl_getSystemTime( &aTimeVal_after );
     853           0 :                 aStopWatch.stop();
     854             :                 // sal_uInt32 nSec  = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
     855           0 :                 double nSec = aStopWatch.getSeconds();
     856           0 :                 t_print("join_001 nSec=%f\n", nSec);
     857           0 :                 delete aCountThread;
     858             : 
     859           0 :                 CPPUNIT_ASSERT_MESSAGE(
     860             :                     "Join the thread: after the thread terminate",
     861             :                     nSec >= 2
     862           0 :                     );
     863             : 
     864           0 :             }
     865             :         /** after terminated by another thread, join exited immediately
     866             : 
     867             :             ALGORITHM:
     868             :             terminate the thread when value>=3, call join, check the beginning time and time after join,
     869             :             the difference should be 3 seconds, join costs little time
     870             :         */
     871           0 :         void join_002()
     872             :             {
     873           0 :                 OCountThread *aCountThread = new OCountThread();
     874           0 :                 bool bRes = aCountThread->create();
     875           0 :                 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
     876             : 
     877             :                 //record the time when the running begin
     878             :                 // TimeValue aTimeVal_befor;
     879             :                 // osl_getSystemTime( &aTimeVal_befor );
     880           0 :                 StopWatch aStopWatch;
     881           0 :                 aStopWatch.start();
     882             : 
     883           0 :                 ThreadHelper::thread_sleep_tenth_sec(10);
     884           0 :                 termAndJoinThread(aCountThread);
     885             : 
     886             :                 //the below line will be executed after aCountThread terminate
     887             :                 // TimeValue aTimeVal_after;
     888             :                 // osl_getSystemTime( &aTimeVal_after );
     889             :                 // sal_uInt32 nSec  = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
     890           0 :                 aStopWatch.stop();
     891           0 :                 double nSec = aStopWatch.getSeconds();
     892           0 :                 t_print("join_002 nSec=%f\n", nSec);
     893             : 
     894           0 :                 delete aCountThread;
     895           0 :                 CPPUNIT_ASSERT_MESSAGE(
     896             :                     "Join the thread: after thread terminate by another thread",
     897             :                     nSec >= 1
     898           0 :                     );
     899           0 :             }
     900             : 
     901           0 :         CPPUNIT_TEST_SUITE(join);
     902           0 :         CPPUNIT_TEST(join_001);
     903           0 :         CPPUNIT_TEST(join_002);
     904           0 :         CPPUNIT_TEST_SUITE_END();
     905             :     }; // class join
     906             : 
     907             :     /** Test of the osl::Thread::isRunning method
     908             :     */
     909           0 :     class isRunning : public CppUnit::TestFixture
     910             :     {
     911             :     public:
     912             :         // initialise your test code values here.
     913           0 :         void setUp() SAL_OVERRIDE
     914             :             {
     915           0 :             }
     916             : 
     917           0 :         void tearDown() SAL_OVERRIDE
     918             :             {
     919           0 :             }
     920             : 
     921             :         /**
     922             :          */
     923           0 :         void isRunning_001()
     924             :             {
     925           0 :                 OCountThread *aCountThread = new OCountThread();
     926           0 :                 bool bRes = aCountThread->create();
     927           0 :                 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
     928             : 
     929           0 :                 bool bRun = aCountThread->isRunning();
     930             : 
     931           0 :                 ThreadHelper::thread_sleep_tenth_sec(2);
     932           0 :                 termAndJoinThread(aCountThread);
     933           0 :                 bool bTer = aCountThread->isRunning();
     934           0 :                 delete aCountThread;
     935             : 
     936           0 :                 CPPUNIT_ASSERT_MESSAGE(
     937             :                     "Test isRunning",
     938             :                     bRun && !bTer
     939           0 :                     );
     940           0 :             }
     941             :         /** check the value of isRunning when suspending and after resume
     942             :          */
     943           0 :         void isRunning_002()
     944             :             {
     945           0 :                 OCountThread *aCountThread = new OCountThread();
     946           0 :                 bool bRes = aCountThread->create();
     947           0 :                 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
     948             : 
     949             :                 // sal_Bool bRunning = aCountThread->isRunning();
     950             :                 // sal_Int32 nValue = 0;
     951           0 :                 suspendCountThread(aCountThread);
     952             : 
     953           0 :                 bool bRunning_sup = aCountThread->isRunning();
     954           0 :                 ThreadHelper::thread_sleep_tenth_sec(2);
     955           0 :                 aCountThread->resume();
     956           0 :                 ThreadHelper::thread_sleep_tenth_sec(2);
     957           0 :                 bool bRunning_res = aCountThread->isRunning();
     958           0 :                 termAndJoinThread(aCountThread);
     959           0 :                 bool bRunning_ter = aCountThread->isRunning();
     960           0 :                 delete aCountThread;
     961             : 
     962           0 :                 CPPUNIT_ASSERT_MESSAGE(
     963             :                     "Test isRunning",
     964           0 :                     bRes && bRunning_sup && bRunning_res && !bRunning_ter );
     965             : 
     966           0 :             }
     967             : 
     968           0 :         CPPUNIT_TEST_SUITE(isRunning);
     969           0 :         CPPUNIT_TEST(isRunning_001);
     970           0 :         CPPUNIT_TEST(isRunning_002);
     971           0 :         CPPUNIT_TEST_SUITE_END();
     972             :     }; // class isRunning
     973             : 
     974             :     /// check osl::Thread::setPriority
     975           0 :     class setPriority : public CppUnit::TestFixture
     976             :     {
     977             :     public:
     978             :         // initialise your test code values here.
     979           0 :         void setUp() SAL_OVERRIDE
     980             :             {
     981           0 :             }
     982             : 
     983           0 :         void tearDown() SAL_OVERRIDE
     984             :             {
     985           0 :             }
     986             : 
     987             :         // insert your test code here.
     988           0 :         rtl::OString getPrioName(oslThreadPriority _aPriority)
     989             :             {
     990           0 :                 rtl::OString sPrioStr;
     991           0 :                 switch (_aPriority)
     992             :                 {
     993             :                 case osl_Thread_PriorityHighest:
     994           0 :                     sPrioStr = "Highest";
     995           0 :                     break;
     996             : 
     997             :                 case osl_Thread_PriorityAboveNormal:
     998           0 :                     sPrioStr = "AboveNormal";
     999           0 :                     break;
    1000             : 
    1001             :                 case osl_Thread_PriorityNormal:
    1002           0 :                     sPrioStr = "Normal";
    1003           0 :                     break;
    1004             : 
    1005             :                 case osl_Thread_PriorityBelowNormal:
    1006           0 :                     sPrioStr = "BelowNormal";
    1007           0 :                     break;
    1008             : 
    1009             :                 case osl_Thread_PriorityLowest:
    1010           0 :                     sPrioStr = "Lowest";
    1011           0 :                     break;
    1012             :                 default:
    1013           0 :                     sPrioStr = "unknown";
    1014             :                 }
    1015           0 :                 return sPrioStr;
    1016             :             }
    1017             : 
    1018             :         /** check 2 threads.
    1019             : 
    1020             :             ALGORITHM:
    1021             :             Here the function should show, that 2 different threads,
    1022             :             which only increase a value, should run at the same time with same prio.
    1023             :             The test fails, if the difference between the two values is more than 5%
    1024             :             but IMHO this isn't a failure, it's only a feature of the OS.
    1025             :         */
    1026             : 
    1027           0 :         void check2Threads(oslThreadPriority _aPriority)
    1028             :             {
    1029             :                 // initial 5 threads with different priorities
    1030           0 :                 OAddThread* pThread = new OAddThread();
    1031           0 :                 OAddThread* p2Thread = new OAddThread();
    1032             : 
    1033             :                 //Create them and start running at the same time
    1034           0 :                 pThread->create();
    1035           0 :                 pThread->setPriority(_aPriority);
    1036           0 :                 p2Thread->create();
    1037           0 :                 p2Thread->setPriority(_aPriority);
    1038             : 
    1039           0 :                 ThreadHelper::thread_sleep_tenth_sec(5);
    1040             : 
    1041           0 :                 pThread->terminate();
    1042           0 :                 p2Thread->terminate();
    1043             : 
    1044           0 :                 sal_Int32 nValueNormal = 0;
    1045           0 :                 nValueNormal = pThread->getValue();
    1046             : 
    1047           0 :                 sal_Int32 nValueNormal2 = 0;
    1048           0 :                 nValueNormal2 = p2Thread->getValue();
    1049             : 
    1050           0 :                 rtl::OString sPrio = getPrioName(_aPriority);
    1051           0 :                 t_print("After 10 tenth seconds\n");
    1052             : 
    1053           0 :                 t_print("nValue in %s Prio Thread is  %d\n",sPrio.getStr(), (int) nValueNormal);
    1054           0 :                 t_print("nValue in %s Prio Thread is %d\n", sPrio.getStr(), (int) nValueNormal2);
    1055             : 
    1056             :                 // ThreadHelper::thread_sleep_tenth_sec(1);
    1057           0 :                 pThread->join();
    1058           0 :                 p2Thread->join();
    1059             : 
    1060           0 :                 delete pThread;
    1061           0 :                 delete p2Thread;
    1062             : 
    1063           0 :                 sal_Int32 nDelta = abs(nValueNormal - nValueNormal2);
    1064           0 :                 double nQuotient = std::max(nValueNormal, nValueNormal2);
    1065           0 :                 CPPUNIT_ASSERT_MESSAGE(
    1066             :                     "Quotient is zero, which means, there exist no right values.",
    1067             :                     nQuotient != 0
    1068           0 :                     );
    1069           0 :                 double nDeltaPercent = nDelta / nQuotient * 100;
    1070             : 
    1071           0 :                 t_print("Delta value %d, percent %f\n", (int) nDelta, nDeltaPercent);
    1072             : 
    1073             :                 // LLA: it's not a bug if the current OS is not able to handle thread scheduling right and good.
    1074             :                 // like Windows XP
    1075             :                 // LLA: CPPUNIT_ASSERT_MESSAGE(
    1076             :                 // LLA:     "Run 2 normal threads, the count diff more than 5 percent.",
    1077             :                 // LLA:     nDeltaPercent <= 5
    1078             :                 // LLA:     );
    1079           0 :             }
    1080             : 
    1081           0 :         void setPriority_001_1()
    1082             :             {
    1083           0 :                 check2Threads(osl_Thread_PriorityHighest);
    1084           0 :             }
    1085           0 :         void setPriority_001_2()
    1086             :             {
    1087           0 :                 check2Threads(osl_Thread_PriorityAboveNormal);
    1088           0 :             }
    1089           0 :         void setPriority_001_3()
    1090             :             {
    1091           0 :                 check2Threads(osl_Thread_PriorityNormal);
    1092           0 :             }
    1093           0 :         void setPriority_001_4()
    1094             :             {
    1095           0 :                 check2Threads(osl_Thread_PriorityBelowNormal);
    1096           0 :             }
    1097           0 :         void setPriority_001_5()
    1098             :             {
    1099           0 :                 check2Threads(osl_Thread_PriorityLowest);
    1100           0 :             }
    1101             : 
    1102           0 :         void setPriority_002()
    1103             :             {
    1104             :                 // initial 5 threads with different priorities
    1105             : 
    1106           0 :                 OAddThread aHighestThread;
    1107           0 :                 OAddThread aAboveNormalThread;
    1108           0 :                 OAddThread aNormalThread;
    1109             :                 //OAddThread *aBelowNormalThread = new OAddThread();
    1110             :                 //OAddThread *aLowestThread = new OAddThread();
    1111             : 
    1112             :                 //Create them and start running at the same time
    1113           0 :                 aHighestThread.createSuspended();
    1114           0 :                 aHighestThread.setPriority(osl_Thread_PriorityHighest);
    1115             : 
    1116           0 :                 aAboveNormalThread.createSuspended();
    1117           0 :                 aAboveNormalThread.setPriority(osl_Thread_PriorityAboveNormal);
    1118             : 
    1119           0 :                 aNormalThread.createSuspended();
    1120           0 :                 aNormalThread.setPriority(osl_Thread_PriorityNormal);
    1121             :                 /*aBelowNormalThread->create();
    1122             :                   aBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
    1123             :                   aLowestThread->create();
    1124             :                   aLowestThread->setPriority(osl_Thread_PriorityLowest);
    1125             :                 */
    1126             : 
    1127           0 :                 aHighestThread.resume();
    1128           0 :                 aAboveNormalThread.resume();
    1129           0 :                 aNormalThread.resume();
    1130             : 
    1131           0 :                 ThreadHelper::thread_sleep_tenth_sec(5);
    1132             : 
    1133           0 :                 aHighestThread.suspend();
    1134           0 :                 aAboveNormalThread.suspend();
    1135           0 :                 aNormalThread.suspend();
    1136             : 
    1137           0 :                 termAndJoinThread(&aNormalThread);
    1138           0 :                 termAndJoinThread(&aAboveNormalThread);
    1139           0 :                 termAndJoinThread(&aHighestThread);
    1140             :                 //aBelowNormalThread->terminate();
    1141             :                 //aLowestThread->terminate();
    1142             : 
    1143           0 :                 sal_Int32 nValueHighest = 0;
    1144           0 :                 nValueHighest = aHighestThread.getValue();
    1145             : 
    1146           0 :                 sal_Int32 nValueAboveNormal = 0;
    1147           0 :                 nValueAboveNormal = aAboveNormalThread.getValue();
    1148             : 
    1149           0 :                 sal_Int32 nValueNormal = 0;
    1150           0 :                 nValueNormal = aNormalThread.getValue();
    1151             : 
    1152           0 :                 t_print("After 10 tenth seconds\n");
    1153           0 :                 t_print("nValue in Highest Prio Thread is %d\n", (int) nValueHighest);
    1154           0 :                 t_print("nValue in AboveNormal Prio Thread is %d\n", (int) nValueAboveNormal);
    1155           0 :                 t_print("nValue in Normal Prio Thread is %d\n", (int) nValueNormal);
    1156             : 
    1157             : #ifndef WNT
    1158           0 :                 CPPUNIT_ASSERT_MESSAGE(
    1159             :                     "SetPriority",
    1160             :                     nValueHighest     > 0 &&
    1161             :                     nValueAboveNormal > 0 &&
    1162             :                     nValueNormal > 0
    1163           0 :                     );
    1164             : #endif
    1165           0 :             }
    1166             : 
    1167           0 :         void setPriority_003()
    1168             :             {
    1169             :                 // initial 5 threads with different priorities
    1170           0 :                 OAddThread *pHighestThread = new OAddThread();
    1171           0 :                 OAddThread *pAboveNormalThread = new OAddThread();
    1172           0 :                 OAddThread *pNormalThread = new OAddThread();
    1173           0 :                 OAddThread *pBelowNormalThread = new OAddThread();
    1174           0 :                 OAddThread *pLowestThread = new OAddThread();
    1175             : 
    1176             :                 //Create them and start running at the same time
    1177           0 :                 pHighestThread->createSuspended();
    1178           0 :                 pHighestThread->setPriority(osl_Thread_PriorityHighest);
    1179             : 
    1180           0 :                 pAboveNormalThread->createSuspended();
    1181           0 :                 pAboveNormalThread->setPriority(osl_Thread_PriorityAboveNormal);
    1182             : 
    1183           0 :                 pNormalThread->createSuspended();
    1184           0 :                 pNormalThread->setPriority(osl_Thread_PriorityNormal);
    1185             : 
    1186           0 :                 pBelowNormalThread->createSuspended();
    1187           0 :                 pBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
    1188             : 
    1189           0 :                 pLowestThread->createSuspended();
    1190           0 :                 pLowestThread->setPriority(osl_Thread_PriorityLowest);
    1191             : 
    1192           0 :                 pHighestThread->resume();
    1193           0 :                 pAboveNormalThread->resume();
    1194           0 :                 pNormalThread->resume();
    1195           0 :                 pBelowNormalThread->resume();
    1196           0 :                 pLowestThread->resume();
    1197             : 
    1198           0 :                 ThreadHelper::thread_sleep_tenth_sec(5);
    1199             : 
    1200           0 :                 pHighestThread->suspend();
    1201           0 :                 pAboveNormalThread->suspend();
    1202           0 :                 pNormalThread->suspend();
    1203           0 :                 pBelowNormalThread->suspend();
    1204           0 :                 pLowestThread->suspend();
    1205             : 
    1206           0 :                 termAndJoinThread(pHighestThread);
    1207           0 :                 termAndJoinThread(pAboveNormalThread);
    1208           0 :                 termAndJoinThread(pNormalThread);
    1209           0 :                 termAndJoinThread(pBelowNormalThread);
    1210           0 :                 termAndJoinThread(pLowestThread);
    1211             : 
    1212           0 :                 sal_Int32 nValueHighest = 0;
    1213           0 :                 nValueHighest = pHighestThread->getValue();
    1214             : 
    1215           0 :                 sal_Int32 nValueAboveNormal = 0;
    1216           0 :                 nValueAboveNormal = pAboveNormalThread->getValue();
    1217             : 
    1218           0 :                 sal_Int32 nValueNormal = 0;
    1219           0 :                 nValueNormal = pNormalThread->getValue();
    1220             : 
    1221           0 :                 sal_Int32 nValueBelowNormal = 0;
    1222           0 :                 nValueBelowNormal = pBelowNormalThread->getValue();
    1223             : 
    1224           0 :                 sal_Int32 nValueLowest = 0;
    1225           0 :                 nValueLowest = pLowestThread->getValue();
    1226             : 
    1227           0 :                 t_print("After 10 tenth seconds\n");
    1228           0 :                 t_print("nValue in Highest Prio Thread is     %d\n", (int) nValueHighest);
    1229           0 :                 t_print("nValue in AboveNormal Prio Thread is %d\n", (int) nValueAboveNormal);
    1230           0 :                 t_print("nValue in Normal Prio Thread is      %d\n", (int) nValueNormal);
    1231           0 :                 t_print("nValue in BelowNormal Prio Thread is %d\n", (int) nValueBelowNormal);
    1232           0 :                 t_print("nValue in Lowest Prio Thread is      %d\n", (int) nValueLowest);
    1233             : 
    1234           0 :                 delete pHighestThread;
    1235           0 :                 delete pAboveNormalThread;
    1236           0 :                 delete pNormalThread;
    1237           0 :                 delete pBelowNormalThread;
    1238           0 :                 delete pLowestThread;
    1239             : 
    1240             : #ifndef WNT
    1241           0 :                 CPPUNIT_ASSERT_MESSAGE(
    1242             :                     "SetPriority",
    1243             :                     nValueHighest     > 0 &&
    1244             :                     nValueAboveNormal > 0 &&
    1245             :                     nValueNormal      > 0 &&
    1246             :                     nValueBelowNormal > 0 &&
    1247             :                     nValueLowest      > 0
    1248           0 :                     );
    1249             : #endif
    1250           0 :             }
    1251             : 
    1252           0 :         void setPriority_004()
    1253             :             {
    1254             :                 // initial 5 threads with different priorities
    1255             :                 // OAddThread *pHighestThread = new OAddThread();
    1256           0 :                 OAddThread *pAboveNormalThread = new OAddThread();
    1257           0 :                 OAddThread *pNormalThread = new OAddThread();
    1258           0 :                 OAddThread *pBelowNormalThread = new OAddThread();
    1259           0 :                 OAddThread *pLowestThread = new OAddThread();
    1260             : 
    1261             :                 //Create them and start running at the same time
    1262             :                 // pHighestThread->createSuspended();
    1263             :                 // pHighestThread->setPriority(osl_Thread_PriorityHighest);
    1264             : 
    1265           0 :                 pAboveNormalThread->createSuspended();
    1266           0 :                 pAboveNormalThread->setPriority(osl_Thread_PriorityAboveNormal);
    1267             : 
    1268           0 :                 pNormalThread->createSuspended();
    1269           0 :                 pNormalThread->setPriority(osl_Thread_PriorityNormal);
    1270             : 
    1271           0 :                 pBelowNormalThread->createSuspended();
    1272           0 :                 pBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
    1273             : 
    1274           0 :                 pLowestThread->createSuspended();
    1275           0 :                 pLowestThread->setPriority(osl_Thread_PriorityLowest);
    1276             : 
    1277             :                 // pHighestThread->resume();
    1278           0 :                 pAboveNormalThread->resume();
    1279           0 :                 pNormalThread->resume();
    1280           0 :                 pBelowNormalThread->resume();
    1281           0 :                 pLowestThread->resume();
    1282             : 
    1283           0 :                 ThreadHelper::thread_sleep_tenth_sec(5);
    1284             : 
    1285             :                 // pHighestThread->suspend();
    1286           0 :                 pAboveNormalThread->suspend();
    1287           0 :                 pNormalThread->suspend();
    1288           0 :                 pBelowNormalThread->suspend();
    1289           0 :                 pLowestThread->suspend();
    1290             : 
    1291             :                 // termAndJoinThread(pHighestThread);
    1292           0 :                 termAndJoinThread(pAboveNormalThread);
    1293           0 :                 termAndJoinThread(pNormalThread);
    1294           0 :                 termAndJoinThread(pBelowNormalThread);
    1295           0 :                 termAndJoinThread(pLowestThread);
    1296             : 
    1297             :                 // sal_Int32 nValueHighest  = 0;
    1298             :                 // nValueHighest =  pHighestThread->getValue();
    1299             : 
    1300           0 :                 sal_Int32 nValueAboveNormal = 0;
    1301           0 :                 nValueAboveNormal = pAboveNormalThread->getValue();
    1302             : 
    1303           0 :                 sal_Int32 nValueNormal = 0;
    1304           0 :                 nValueNormal = pNormalThread->getValue();
    1305             : 
    1306           0 :                 sal_Int32 nValueBelowNormal = 0;
    1307           0 :                 nValueBelowNormal = pBelowNormalThread->getValue();
    1308             : 
    1309           0 :                 sal_Int32 nValueLowest = 0;
    1310           0 :                 nValueLowest = pLowestThread->getValue();
    1311             : 
    1312           0 :                 t_print("After 5 tenth seconds\n");
    1313           0 :                 t_print("nValue in AboveNormal Prio Thread is %d\n", (int) nValueAboveNormal);
    1314           0 :                 t_print("nValue in Normal Prio Thread is      %d\n", (int) nValueNormal);
    1315           0 :                 t_print("nValue in BelowNormal Prio Thread is %d\n", (int) nValueBelowNormal);
    1316           0 :                 t_print("nValue in Lowest Prio Thread is      %d\n", (int) nValueLowest);
    1317             : 
    1318             :                 // delete pHighestThread;
    1319           0 :                 delete pAboveNormalThread;
    1320           0 :                 delete pNormalThread;
    1321           0 :                 delete pBelowNormalThread;
    1322           0 :                 delete pLowestThread;
    1323             : 
    1324             : #ifndef WNT
    1325           0 :                 CPPUNIT_ASSERT_MESSAGE(
    1326             :                     "SetPriority",
    1327             :                     /* nValueHighest     > 0 &&  */
    1328             :                     nValueAboveNormal > 0 &&
    1329             :                     nValueNormal      > 0 &&
    1330             :                     nValueBelowNormal > 0 &&
    1331             :                     nValueLowest      > 0
    1332           0 :                     );
    1333             : #endif
    1334           0 :             }
    1335           0 :         void setPriority_005()
    1336             :             {
    1337             :                 // initial 5 threads with different priorities
    1338             :                 // OAddThread *pHighestThread = new OAddThread();
    1339             :                 // OAddThread *pAboveNormalThread = new OAddThread();
    1340           0 :                 OAddThread *pNormalThread = new OAddThread();
    1341           0 :                 OAddThread *pBelowNormalThread = new OAddThread();
    1342           0 :                 OAddThread *pLowestThread = new OAddThread();
    1343             : 
    1344             :                 //Create them and start running at the same time
    1345             :                 // pHighestThread->createSuspended();
    1346             :                 // pHighestThread->setPriority(osl_Thread_PriorityHighest);
    1347             : 
    1348             :                 // pAboveNormalThread->createSuspended();
    1349             :                 // pAboveNormalThread->setPriority(osl_Thread_PriorityAboveNormal);
    1350             : 
    1351           0 :                 pNormalThread->createSuspended();
    1352           0 :                 pNormalThread->setPriority(osl_Thread_PriorityNormal);
    1353             : 
    1354           0 :                 pBelowNormalThread->createSuspended();
    1355           0 :                 pBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
    1356             : 
    1357           0 :                 pLowestThread->createSuspended();
    1358           0 :                 pLowestThread->setPriority(osl_Thread_PriorityLowest);
    1359             : 
    1360             :                 // pHighestThread->resume();
    1361             :                 // pAboveNormalThread->resume();
    1362           0 :                 pNormalThread->resume();
    1363           0 :                 pBelowNormalThread->resume();
    1364           0 :                 pLowestThread->resume();
    1365             : 
    1366           0 :                 ThreadHelper::thread_sleep_tenth_sec(5);
    1367             : 
    1368             :                 // pHighestThread->suspend();
    1369             :                 // pAboveNormalThread->suspend();
    1370           0 :                 pNormalThread->suspend();
    1371           0 :                 pBelowNormalThread->suspend();
    1372           0 :                 pLowestThread->suspend();
    1373             : 
    1374             :                 // termAndJoinThread(pHighestThread);
    1375             :                 // termAndJoinThread(pAboveNormalThread);
    1376           0 :                 termAndJoinThread(pNormalThread);
    1377           0 :                 termAndJoinThread(pBelowNormalThread);
    1378           0 :                 termAndJoinThread(pLowestThread);
    1379             : 
    1380             :                 // sal_Int32 nValueHighest  = 0;
    1381             :                 // nValueHighest =  pHighestThread->getValue();
    1382             : 
    1383             :                 // sal_Int32 nValueAboveNormal = 0;
    1384             :                 // nValueAboveNormal = pAboveNormalThread->getValue();
    1385             : 
    1386           0 :                 sal_Int32 nValueNormal = 0;
    1387           0 :                 nValueNormal = pNormalThread->getValue();
    1388             : 
    1389           0 :                 sal_Int32 nValueBelowNormal = 0;
    1390           0 :                 nValueBelowNormal = pBelowNormalThread->getValue();
    1391             : 
    1392           0 :                 sal_Int32 nValueLowest = 0;
    1393           0 :                 nValueLowest = pLowestThread->getValue();
    1394             : 
    1395           0 :                 t_print("After 5 tenth seconds\n");
    1396           0 :                 t_print("nValue in Normal Prio Thread is      %d\n", (int) nValueNormal);
    1397           0 :                 t_print("nValue in BelowNormal Prio Thread is %d\n", (int) nValueBelowNormal);
    1398           0 :                 t_print("nValue in Lowest Prio Thread is      %d\n", (int) nValueLowest);
    1399             : 
    1400           0 :                 delete pNormalThread;
    1401           0 :                 delete pBelowNormalThread;
    1402           0 :                 delete pLowestThread;
    1403             : 
    1404             : #ifndef WNT
    1405           0 :                 CPPUNIT_ASSERT_MESSAGE(
    1406             :                     "SetPriority",
    1407             :                     /* nValueHighest     > 0 &&  */
    1408             :                     /* nValueAboveNormal > 0 &&  */
    1409             :                     nValueNormal      > 0 &&
    1410             :                     nValueBelowNormal > 0 &&
    1411             :                     nValueLowest      > 0
    1412           0 :                     );
    1413             : #endif
    1414           0 :             }
    1415             : 
    1416           0 :         CPPUNIT_TEST_SUITE(setPriority);
    1417             : #ifndef SOLARIS
    1418           0 :         CPPUNIT_TEST(setPriority_002);
    1419           0 :         CPPUNIT_TEST(setPriority_003);
    1420           0 :         CPPUNIT_TEST(setPriority_004);
    1421           0 :         CPPUNIT_TEST(setPriority_005);
    1422             : #endif
    1423           0 :         CPPUNIT_TEST(setPriority_001_1);
    1424           0 :         CPPUNIT_TEST(setPriority_001_2);
    1425           0 :         CPPUNIT_TEST(setPriority_001_3);
    1426           0 :         CPPUNIT_TEST(setPriority_001_4);
    1427           0 :         CPPUNIT_TEST(setPriority_001_5);
    1428           0 :         CPPUNIT_TEST_SUITE_END();
    1429             :     }; // class setPriority
    1430             : 
    1431             :     /** Test of the osl::Thread::getPriority method
    1432             :     */
    1433           0 :     class getPriority : public CppUnit::TestFixture
    1434             :     {
    1435             :     public:
    1436             :         // initialise your test code values here.
    1437           0 :         void setUp() SAL_OVERRIDE {}
    1438             : 
    1439           0 :         void tearDown() SAL_OVERRIDE {}
    1440             : 
    1441             :         // insert your test code here.
    1442           0 :         void getPriority_001()
    1443             :             {
    1444           0 :                 OAddThread *pHighestThread = new OAddThread();
    1445             : 
    1446             :                 //Create them and start running at the same time
    1447           0 :                 pHighestThread->create();
    1448           0 :                 pHighestThread->setPriority(osl_Thread_PriorityHighest);
    1449             : 
    1450           0 :                 oslThreadPriority aPriority = pHighestThread->getPriority();
    1451           0 :                 termAndJoinThread(pHighestThread);
    1452           0 :                 delete pHighestThread;
    1453             : 
    1454           0 :                 ThreadHelper::outputPriority(aPriority);
    1455             : 
    1456             : // LLA: Priority settings may not work within some OS versions.
    1457             : #if ( defined WNT ) || ( defined SOLARIS )
    1458             :                 CPPUNIT_ASSERT_MESSAGE(
    1459             :                     "getPriority",
    1460             :                     aPriority == osl_Thread_PriorityHighest
    1461             :                     );
    1462             : #else
    1463             : // LLA: Linux
    1464             : // NO_PTHREAD_PRIORITY ???
    1465           0 :                 CPPUNIT_ASSERT_MESSAGE(
    1466             :                     "getPriority",
    1467             :                     aPriority == osl_Thread_PriorityNormal
    1468           0 :                     );
    1469             : #endif
    1470           0 :             }
    1471             : 
    1472           0 :         CPPUNIT_TEST_SUITE(getPriority);
    1473           0 :         CPPUNIT_TEST(getPriority_001);
    1474           0 :         CPPUNIT_TEST_SUITE_END();
    1475             :     }; // class getPriority
    1476             : 
    1477           0 :     class getIdentifier : public CppUnit::TestFixture
    1478             :     {
    1479             :     public:
    1480             :         // initialise your test code values here.
    1481           0 :         void setUp() SAL_OVERRIDE {}
    1482             : 
    1483           0 :         void tearDown() SAL_OVERRIDE {}
    1484             : 
    1485           0 :         void getIdentifier_001()
    1486             :         {
    1487             :             // insert your test code here.
    1488           0 :         }
    1489             : 
    1490           0 :         CPPUNIT_TEST_SUITE(getIdentifier);
    1491           0 :         CPPUNIT_TEST(getIdentifier_001);
    1492           0 :         CPPUNIT_TEST_SUITE_END();
    1493             :     }; // class getIdentifier
    1494             : 
    1495             :     /** Test of the osl::Thread::getCurrentIdentifier method
    1496             :     */
    1497           0 :     class getCurrentIdentifier : public CppUnit::TestFixture
    1498             :     {
    1499             :     public:
    1500           0 :         void setUp() SAL_OVERRIDE {}
    1501           0 :         void tearDown() SAL_OVERRIDE {}
    1502             : 
    1503           0 :         void getCurrentIdentifier_001()
    1504             :         {
    1505             :             oslThreadIdentifier oId;
    1506           0 :             OCountThread* pCountThread = new OCountThread;
    1507           0 :             pCountThread->create();
    1508           0 :             pCountThread->setWait(3);
    1509           0 :             oId = Thread::getCurrentIdentifier();
    1510           0 :             oslThreadIdentifier oIdChild = pCountThread->getIdentifier();
    1511           0 :             termAndJoinThread(pCountThread);
    1512           0 :             delete pCountThread;
    1513             : 
    1514           0 :             CPPUNIT_ASSERT_MESSAGE(
    1515             :                 "Get the identifier for the current active thread.",
    1516           0 :                 oId != oIdChild);
    1517           0 :         }
    1518             : 
    1519           0 :         CPPUNIT_TEST_SUITE(getCurrentIdentifier);
    1520           0 :         CPPUNIT_TEST(getCurrentIdentifier_001);
    1521           0 :         CPPUNIT_TEST_SUITE_END();
    1522             :     }; // class getCurrentIdentifier
    1523             : 
    1524             :     /** Test of the osl::Thread::wait method
    1525             :     */
    1526           0 :     class waittest : public CppUnit::TestFixture
    1527             :     {
    1528             :     public:
    1529           0 :         void setUp() SAL_OVERRIDE {}
    1530           0 :         void tearDown() SAL_OVERRIDE {}
    1531             : 
    1532             :         /** call wait in the run method
    1533             : 
    1534             :             ALGORITHM:
    1535             :             tested thread wait nWaitSec seconds, main thread sleep (2) seconds,
    1536             :             then terminate the tested thread, due to the fact that the thread do a sleep(1) + wait(5)
    1537             :             it's finish after 6 seconds.
    1538             :         */
    1539           0 :         void wait_001()
    1540             :             {
    1541           0 :                 OCountThread *aCountThread = new OCountThread();
    1542           0 :                 sal_Int32 nWaitSec = 5;
    1543           0 :                 aCountThread->setWait(nWaitSec);
    1544             :                 // thread runs at least 5 seconds.
    1545           0 :                 bool bRes = aCountThread->create();
    1546           0 :                 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
    1547             : 
    1548             :                 //record the time when the running begin
    1549           0 :                 StopWatch aStopWatch;
    1550           0 :                 aStopWatch.start();
    1551             : 
    1552             :                 // wait a little bit, to let the thread the time, to start
    1553           0 :                 ThreadHelper::thread_sleep_tenth_sec( 4 );
    1554             : 
    1555             :                 // if wait works,
    1556             :                 // this function returns, after 4 sec. later
    1557           0 :                 termAndJoinThread(aCountThread);
    1558             : 
    1559             :                 // value should be one.
    1560           0 :                 sal_Int32 nValue = aCountThread->getValue();
    1561             : 
    1562           0 :                 aStopWatch.stop();
    1563             : 
    1564             :                 // sal_uInt32 nSec  = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
    1565           0 :                 double nTenthSec = aStopWatch.getTenthSec();
    1566           0 :                 double nSec = aStopWatch.getSeconds();
    1567           0 :                 delete aCountThread;
    1568           0 :                 t_print("nTenthSec = %f \n", nTenthSec);
    1569           0 :                 t_print("nSec = %f \n", nSec);
    1570           0 :                 t_print("nValue = %d \n",  (int) nValue);
    1571             : 
    1572           0 :                 CPPUNIT_ASSERT_MESSAGE(
    1573             :                     "Wait: Blocks the calling thread for the given number of time.",
    1574             :                     nTenthSec >= 5 && nValue == 1
    1575           0 :                     );
    1576             : 
    1577           0 :             }
    1578             : 
    1579           0 :         CPPUNIT_TEST_SUITE(waittest);
    1580           0 :         CPPUNIT_TEST(wait_001);
    1581           0 :         CPPUNIT_TEST_SUITE_END();
    1582             :     }; // class waittest
    1583             : 
    1584             :     /** osl::Thread::yield method: can not design good test scenario to test up to now
    1585             :     */
    1586           0 :     class yield : public CppUnit::TestFixture
    1587             :     {
    1588             :     public:
    1589           0 :         void setUp() SAL_OVERRIDE {}
    1590           0 :         void tearDown() SAL_OVERRIDE {}
    1591             : 
    1592           0 :         void yield_001()
    1593             :         {
    1594             :             // insert your test code here.
    1595           0 :         }
    1596             : 
    1597           0 :         CPPUNIT_TEST_SUITE(yield);
    1598           0 :         CPPUNIT_TEST(yield_001);
    1599           0 :         CPPUNIT_TEST_SUITE_END();
    1600             :     }; // class yield
    1601             : 
    1602             :     /** Test of the osl::Thread::schedule method
    1603             :     */
    1604           0 :     class schedule : public CppUnit::TestFixture
    1605             :     {
    1606             :     public:
    1607           0 :         void setUp() SAL_OVERRIDE {}
    1608           0 :         void tearDown() SAL_OVERRIDE {}
    1609             : 
    1610             :         /** The requested thread will get terminate the next time schedule() is called.
    1611             : 
    1612             :             Note: on UNX, if call suspend thread is not the to be suspended thread, the to be
    1613             :             suspended   thread will get suspended the next time schedule() is called,
    1614             :             while on w32, it's nothing with schedule.
    1615             : 
    1616             :             check if suspend and terminate work well via schedule
    1617             :         */
    1618           0 :         void schedule_001()
    1619             :             {
    1620           0 :                 OAddThread* aThread = new OAddThread();
    1621           0 :                 bool bRes = aThread->create();
    1622           0 :                 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
    1623             : 
    1624           0 :                 ThreadHelper::thread_sleep_tenth_sec(2);
    1625           0 :                 aThread->suspend();
    1626           0 :                 ThreadHelper::thread_sleep_tenth_sec(1);
    1627           0 :                 sal_Int32 nValue = aThread->getValue();
    1628           0 :                 ThreadHelper::thread_sleep_tenth_sec(3);
    1629           0 :                 sal_Int32 nLaterValue = aThread->getValue();
    1630             :                 // resumeAndWaitThread(aThread);
    1631           0 :                 t_print("      value = %d\n", (int) nValue);
    1632           0 :                 t_print("later value = %d\n", (int) nLaterValue);
    1633             :                 // if value and latervalue not equal, than the thread would not suspended
    1634             : 
    1635           0 :                 CPPUNIT_ASSERT_MESSAGE(
    1636             :                     "Schedule: suspend works.",
    1637             :                     nLaterValue == nValue
    1638           0 :                     );
    1639             : 
    1640           0 :                 aThread->resume();
    1641           0 :                 ThreadHelper::thread_sleep_tenth_sec(2);
    1642             : 
    1643           0 :                 aThread->terminate();
    1644           0 :                 sal_Int32 nValue_term = aThread->getValue();
    1645             : 
    1646           0 :                 aThread->join();
    1647           0 :                 sal_Int32 nValue_join = aThread->getValue();
    1648             : 
    1649           0 :                 t_print("value after term = %d\n", (int) nValue_term);
    1650           0 :                 t_print("value after join = %d\n", (int) nValue_join);
    1651             : 
    1652             :                 // nValue_term and nValue_join should be the same
    1653             :                 // but should be differ from nValue
    1654             : 
    1655           0 :                 delete aThread;
    1656             :                 //check if thread really terminate after call terminate, if join immediatlly return
    1657           0 :                 CPPUNIT_ASSERT_MESSAGE(
    1658             :                     "Schedule: Returns False if the thread should terminate.",
    1659             :                     nValue_join -  nValue_term <= 1 && nValue_join -  nValue_term >= 0
    1660           0 :                     );
    1661             : 
    1662           0 :             }
    1663             : 
    1664             :         /** design a thread that has not call schedule in the workfunction--run method
    1665             :          */
    1666           0 :         void schedule_002()
    1667             :             {
    1668           0 :                 ONoScheduleThread aThread; // this thread runs 10 sec. (no schedule() used)
    1669           0 :                 bool bRes = aThread.create();
    1670           0 :                 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
    1671             : 
    1672           0 :                 ThreadHelper::thread_sleep_tenth_sec(2);
    1673           0 :                 aThread.suspend();
    1674           0 :                 sal_Int32 nValue = aThread.getValue();
    1675             : 
    1676           0 :                 ThreadHelper::thread_sleep_tenth_sec(3);
    1677           0 :                 sal_Int32 nLaterValue = aThread.getValue();
    1678           0 :                 ThreadHelper::thread_sleep_tenth_sec(5);
    1679             : 
    1680           0 :                 resumeAndWaitThread(&aThread);
    1681             : 
    1682           0 :                 t_print("      value = %d\n", (int) nValue);
    1683           0 :                 t_print("later value = %d\n", (int) nLaterValue);
    1684             : 
    1685             :                 //On windows, suspend works, so the values are same
    1686             : #ifdef WNT
    1687             :                 CPPUNIT_ASSERT_MESSAGE(
    1688             :                     "Schedule: don't schedule in thread run method, suspend works.",
    1689             :                     nLaterValue == nValue
    1690             :                     );
    1691             : #endif
    1692             : 
    1693             :                 //On UNX, suspend does not work, so the difference of the values equals to sleep seconds number
    1694             : #ifdef UNX
    1695           0 :                 aThread.resume();
    1696           0 :                 CPPUNIT_ASSERT_MESSAGE(
    1697             :                     "Schedule: don't schedule in thread run method, suspend does not work too.",
    1698             :                     nLaterValue > nValue
    1699           0 :                     );
    1700             : #endif
    1701             : 
    1702             :                 // terminate will not work if no schedule in thread's work function
    1703           0 :                 termAndJoinThread(&aThread);
    1704           0 :                 sal_Int32 nValue_term = aThread.getValue();
    1705             : 
    1706           0 :                 t_print(" value term = %d\n", (int) nValue_term);
    1707             : 
    1708           0 :                 CPPUNIT_ASSERT_MESSAGE(
    1709             :                     "Schedule: don't schedule in thread run method, terminate failed.",
    1710             :                     nValue_term == 10
    1711           0 :                     );
    1712           0 :             }
    1713             : 
    1714           0 :         CPPUNIT_TEST_SUITE(schedule);
    1715           0 :         CPPUNIT_TEST(schedule_001);
    1716           0 :         CPPUNIT_TEST(schedule_002);
    1717           0 :         CPPUNIT_TEST_SUITE_END();
    1718             :     }; // class schedule
    1719             : 
    1720           1 :     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::create, "osl_Thread");
    1721           1 :     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::createSuspended, "osl_Thread");
    1722           1 :     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::suspend, "osl_Thread");
    1723           1 :     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::resume, "osl_Thread");
    1724           1 :     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::terminate, "osl_Thread");
    1725           1 :     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::join, "osl_Thread");
    1726           1 :     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::isRunning, "osl_Thread");
    1727           1 :     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::setPriority, "osl_Thread");
    1728           1 :     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getPriority, "osl_Thread");
    1729           1 :     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getIdentifier, "osl_Thread");
    1730           1 :     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getCurrentIdentifier, "osl_Thread");
    1731           1 :     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::waittest, "osl_Thread");
    1732           1 :     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::yield, "osl_Thread");
    1733           1 :     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::schedule, "osl_Thread");
    1734             : } // namespace osl_Thread
    1735             : 
    1736             : // destroy function when the binding thread terminate
    1737          12 : void SAL_CALL destroyCallback(void * data)
    1738             : {
    1739          12 :     delete[] static_cast<char *>(data);
    1740          12 : }
    1741             : 
    1742           1 : static ThreadData myThreadData(destroyCallback);
    1743             : 
    1744             : /**
    1745             : */
    1746             : class myKeyThread : public Thread
    1747             : {
    1748             : public:
    1749             :     // a public char member for test result checking
    1750             :     char m_Char_Test;
    1751             :     // for pass thread-special data to thread
    1752           8 :     myKeyThread(const char cData)
    1753           8 :         : m_Char_Test(0)
    1754             :         {
    1755           8 :             m_nData = cData;
    1756           8 :         }
    1757             : private:
    1758             :     char m_nData;
    1759             : 
    1760           8 :     void SAL_CALL run() SAL_OVERRIDE
    1761             :         {
    1762           8 :             char * pc = new char[2];
    1763             : //      strcpy(pc, &m_nData);
    1764           8 :             memcpy(pc, &m_nData, 1);
    1765           8 :             pc[1] = '\0';
    1766             : 
    1767           8 :             myThreadData.setData(pc);
    1768           8 :             char* pData = static_cast<char*>(myThreadData.getData());
    1769           8 :             m_Char_Test = *pData;
    1770             :             // wait for long time to check the data value in main thread
    1771           8 :             ThreadHelper::thread_sleep_tenth_sec(3);
    1772           8 :         }
    1773             : public:
    1774           8 :     virtual ~myKeyThread()
    1775          16 :         {
    1776           8 :             if (isRunning())
    1777             :             {
    1778           0 :                 t_print("error: not terminated.\n");
    1779             :             }
    1780           8 :         }
    1781             : };
    1782             : 
    1783           1 : static ThreadData idData;
    1784             : 
    1785           2 : class idThread: public Thread
    1786             : {
    1787             : public:
    1788             :     oslThreadIdentifier m_Id;
    1789             : private:
    1790           2 :     void SAL_CALL run() SAL_OVERRIDE
    1791             :         {
    1792           2 :             oslThreadIdentifier* pId = new oslThreadIdentifier;
    1793           2 :             *pId = getIdentifier();
    1794           2 :             idData.setData(pId);
    1795           2 :             oslThreadIdentifier* pIdData = static_cast<oslThreadIdentifier*>(idData.getData());
    1796             :             //t_print("Thread %d has Data %d\n", getIdentifier(), *pIdData);
    1797           2 :             m_Id = *pIdData;
    1798           2 :             delete pId;
    1799           2 :         }
    1800             : 
    1801             : public:
    1802           2 :     virtual ~idThread()
    1803           4 :         {
    1804           2 :             if (isRunning())
    1805             :             {
    1806           0 :                 t_print("error: not terminated.\n");
    1807             :             }
    1808           2 :         }
    1809             : };
    1810             : 
    1811             : namespace osl_ThreadData
    1812             : {
    1813             : 
    1814           3 :     class ctors : public CppUnit::TestFixture
    1815             :     {
    1816             :     public:
    1817             :         // initialise your test code values here.
    1818           1 :         void setUp() SAL_OVERRIDE
    1819             :             {
    1820           1 :             }
    1821             : 
    1822           1 :         void tearDown() SAL_OVERRIDE
    1823             :             {
    1824           1 :             }
    1825             : 
    1826             :         // insert your test code here.
    1827           1 :         void ctor_001()
    1828             :             {
    1829             : 
    1830           1 :             }
    1831             : 
    1832           2 :         CPPUNIT_TEST_SUITE(ctors);
    1833           1 :         CPPUNIT_TEST(ctor_001);
    1834           5 :         CPPUNIT_TEST_SUITE_END();
    1835             :     }; // class ctors
    1836             : 
    1837           9 :     class setData : public CppUnit::TestFixture
    1838             :     {
    1839             :     public:
    1840             :         // initialise your test code values here.
    1841           3 :         void setUp() SAL_OVERRIDE
    1842             :             {
    1843           3 :             }
    1844             : 
    1845           3 :         void tearDown() SAL_OVERRIDE
    1846             :             {
    1847           3 :             }
    1848             : 
    1849             :         /** the same instance of the class can have different values in different threads
    1850             :          */
    1851           1 :         void setData_001()
    1852             :             {
    1853           1 :                 idThread aThread1;
    1854           1 :                 aThread1.create();
    1855           2 :                 idThread aThread2;
    1856           1 :                 aThread2.create();
    1857             : 
    1858           1 :                 aThread1.join();
    1859           1 :                 aThread2.join();
    1860             : 
    1861           1 :                 oslThreadIdentifier aThreadId1 = aThread1.getIdentifier();
    1862           1 :                 oslThreadIdentifier aThreadId2 = aThread2.getIdentifier();
    1863             : 
    1864           2 :                 CPPUNIT_ASSERT_MESSAGE(
    1865             :                     "ThreadData setData: ",
    1866             :                     aThread1.m_Id == aThreadId1 && aThread2.m_Id == aThreadId2
    1867           2 :                     );
    1868             : 
    1869           1 :             }
    1870             : 
    1871           1 :         void setData_002()
    1872             :             {
    1873             :                 // at first, set the data a value
    1874           1 :                 char* pc = new char[2];
    1875           1 :                 char m_nData = 'm';
    1876           1 :                 pc[0] = m_nData;
    1877           1 :                 pc[1] = '\0';
    1878             : 
    1879           1 :                 myThreadData.setData(pc);
    1880             : 
    1881           1 :                 myKeyThread aThread1('a');
    1882           1 :                 aThread1.create();
    1883           2 :                 myKeyThread aThread2('b');
    1884           1 :                 aThread2.create();
    1885             :                 // aThread1 and aThread2 should have not terminated yet, check current data, not 'a' 'b'
    1886           1 :                 char* pChar = static_cast<char*>(myThreadData.getData());
    1887           1 :                 char aChar = *pChar;
    1888             : 
    1889           1 :                 aThread1.join();
    1890           1 :                 aThread2.join();
    1891             : 
    1892             :                 // the saved thread data of aThread1 & aThread2, different
    1893           1 :                 char cData1 = aThread1.m_Char_Test;
    1894           1 :                 char cData2 = aThread2.m_Char_Test;
    1895             : 
    1896           2 :                 CPPUNIT_ASSERT_MESSAGE(
    1897             :                     "ThreadData setData: ",
    1898             :                     cData1 == 'a' && cData2 == 'b' && aChar == 'm'
    1899           2 :                     );
    1900             : 
    1901           1 :             }
    1902             :         /** setData the second time, and then getData
    1903             :          */
    1904           1 :         void setData_003()
    1905             :             {
    1906             :                 // at first, set the data a value
    1907           1 :                 char* pc = new char[2];
    1908           1 :                 char m_nData = 'm';
    1909           1 :                 memcpy(pc, &m_nData, 1);
    1910           1 :                 pc[1] = '\0';
    1911           1 :                 myThreadData.setData(pc);
    1912             : 
    1913           1 :                 myKeyThread aThread1('a');
    1914           1 :                 aThread1.create();
    1915           2 :                 myKeyThread aThread2('b');
    1916           1 :                 aThread2.create();
    1917             :                 // aThread1 and aThread2 should have not terminated yet
    1918             :                 // setData the second time
    1919           1 :                 char* pc2 = new char[2];
    1920           1 :                 m_nData = 'o';
    1921           1 :                 memcpy(pc2, &m_nData, 1);
    1922           1 :                 pc2[1] = '\0';
    1923             : 
    1924           1 :                 myThreadData.setData(pc2);
    1925           1 :                 char* pChar = static_cast<char*>(myThreadData.getData());
    1926           1 :                 char aChar = *pChar;
    1927             : 
    1928           1 :                 aThread1.join();
    1929           1 :                 aThread2.join();
    1930             : 
    1931             :                 // the saved thread data of aThread1 & aThread2, different
    1932           1 :                 char cData1 = aThread1.m_Char_Test;
    1933           1 :                 char cData2 = aThread2.m_Char_Test;
    1934             : 
    1935           2 :                 CPPUNIT_ASSERT_MESSAGE(
    1936             :                     "ThreadData setData: ",
    1937             :                     cData1 == 'a' && cData2 == 'b' && aChar == 'o'
    1938           2 :                     );
    1939           1 :             }
    1940             : 
    1941           2 :         CPPUNIT_TEST_SUITE(setData);
    1942           1 :         CPPUNIT_TEST(setData_001);
    1943           1 :         CPPUNIT_TEST(setData_002);
    1944           1 :         CPPUNIT_TEST(setData_003);
    1945           5 :         CPPUNIT_TEST_SUITE_END();
    1946             :     }; // class setData
    1947             : 
    1948           6 :     class getData : public CppUnit::TestFixture
    1949             :     {
    1950             :     public:
    1951             :         // initialise your test code values here.
    1952           2 :         void setUp() SAL_OVERRIDE
    1953             :             {
    1954           2 :             }
    1955             : 
    1956           2 :         void tearDown() SAL_OVERRIDE
    1957             :             {
    1958           2 :             }
    1959             : 
    1960             :         // After setData in child threads, get Data in the main thread, should be independent
    1961           1 :         void getData_001()
    1962             :             {
    1963           1 :                 char* pc = new char[2];
    1964           1 :                 char m_nData[] = "i";
    1965           1 :                 strcpy(pc, m_nData);
    1966           1 :                 myThreadData.setData(pc);
    1967             : 
    1968           1 :                 myKeyThread aThread1('c');
    1969           1 :                 aThread1.create();
    1970           2 :                 myKeyThread aThread2('d');
    1971           1 :                 aThread2.create();
    1972             : 
    1973           1 :                 aThread1.join();
    1974           1 :                 aThread2.join();
    1975             : 
    1976           1 :                 char cData1 = aThread1.m_Char_Test;
    1977           1 :                 char cData2 = aThread2.m_Char_Test;
    1978             : 
    1979           1 :                 char* pChar = static_cast<char*>(myThreadData.getData());
    1980           1 :                 char aChar = *pChar;
    1981             : 
    1982           2 :                 CPPUNIT_ASSERT_MESSAGE(
    1983             :                     "ThreadData setData: ",
    1984             :                     cData1 == 'c' && cData2 == 'd' && aChar == 'i'
    1985           2 :                     );
    1986           1 :             }
    1987             : 
    1988             :         // setData then change the value in the address data pointer points,
    1989             :         // and then getData, should get the new value
    1990           1 :         void getData_002()
    1991             :             {
    1992           1 :                 char* pc = new char[2];
    1993           1 :                 char m_nData = 'i';
    1994           1 :                 memcpy(pc, &m_nData, 1);
    1995           1 :                 pc[1] = '\0';
    1996             : 
    1997           1 :                 myThreadData.setData(pc);
    1998             : 
    1999           1 :                 myKeyThread aThread1('a');
    2000           1 :                 aThread1.create();
    2001           2 :                 myKeyThread aThread2('b');
    2002           1 :                 aThread2.create();
    2003             : 
    2004             :                 // change the value which pc points
    2005           1 :                 char m_nData2 = 'j';
    2006           1 :                 memcpy(pc, &m_nData2, 1);
    2007           1 :                 pc[1] = '\0';
    2008             : 
    2009           1 :                 void* pChar = myThreadData.getData();
    2010           1 :                 char aChar = *static_cast<char*>(pChar);
    2011             : 
    2012           1 :                 aThread1.join();
    2013           1 :                 aThread2.join();
    2014             : 
    2015           1 :                 char cData1 = aThread1.m_Char_Test;
    2016           1 :                 char cData2 = aThread2.m_Char_Test;
    2017             : 
    2018           2 :                 CPPUNIT_ASSERT_MESSAGE(
    2019             :                     "ThreadData setData: ",
    2020             :                     cData1 == 'a' && cData2 == 'b' && aChar == 'j'
    2021           2 :                     );
    2022             : 
    2023           1 :             }
    2024             : 
    2025           2 :         CPPUNIT_TEST_SUITE(getData);
    2026           1 :         CPPUNIT_TEST(getData_001);
    2027           1 :         CPPUNIT_TEST(getData_002);
    2028           5 :         CPPUNIT_TEST_SUITE_END();
    2029             :     }; // class getData
    2030             : 
    2031           1 :     CPPUNIT_TEST_SUITE_REGISTRATION(osl_ThreadData::ctors);
    2032           1 :     CPPUNIT_TEST_SUITE_REGISTRATION(osl_ThreadData::setData);
    2033           1 :     CPPUNIT_TEST_SUITE_REGISTRATION(osl_ThreadData::getData);
    2034             : } // namespace osl_ThreadData
    2035             : 
    2036             : // this macro creates an empty function, which will called by the RegisterAllFunctions()
    2037             : // to let the user the possibility to also register some functions by hand.
    2038           4 : CPPUNIT_PLUGIN_IMPLEMENT();
    2039             : 
    2040             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11