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