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