187d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
387d2adbcSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
487d2adbcSAndrew Rist * or more contributor license agreements. See the NOTICE file
587d2adbcSAndrew Rist * distributed with this work for additional information
687d2adbcSAndrew Rist * regarding copyright ownership. The ASF licenses this file
787d2adbcSAndrew Rist * to you under the Apache License, Version 2.0 (the
887d2adbcSAndrew Rist * "License"); you may not use this file except in compliance
987d2adbcSAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
1187d2adbcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
1387d2adbcSAndrew Rist * Unless required by applicable law or agreed to in writing,
1487d2adbcSAndrew Rist * software distributed under the License is distributed on an
1587d2adbcSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1687d2adbcSAndrew Rist * KIND, either express or implied. See the License for the
1787d2adbcSAndrew Rist * specific language governing permissions and limitations
1887d2adbcSAndrew Rist * under the License.
19cdf0e10cSrcweir *
2087d2adbcSAndrew Rist *************************************************************/
2187d2adbcSAndrew Rist
2287d2adbcSAndrew 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
4844e8df1fSDamjan Jovanovic #include "gtest/gtest.h"
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 );
9544e8df1fSDamjan Jovanovic printf("# %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 );
10644e8df1fSDamjan Jovanovic printf("# %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 }
12644e8df1fSDamjan Jovanovic printf("# %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 // {
19644e8df1fSDamjan Jovanovic // printf("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 // {
21044e8df1fSDamjan Jovanovic // printf("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 {
21944e8df1fSDamjan Jovanovic printf("Prio is High\n");
220cdf0e10cSrcweir }
221cdf0e10cSrcweir else if (_aPriority == osl_Thread_PriorityAboveNormal)
222cdf0e10cSrcweir {
22344e8df1fSDamjan Jovanovic printf("Prio is above normal\n");
224cdf0e10cSrcweir }
225cdf0e10cSrcweir else if (_aPriority == osl_Thread_PriorityNormal)
226cdf0e10cSrcweir {
22744e8df1fSDamjan Jovanovic printf("Prio is normal\n");
228cdf0e10cSrcweir }
229cdf0e10cSrcweir else if (_aPriority == osl_Thread_PriorityBelowNormal)
230cdf0e10cSrcweir {
23144e8df1fSDamjan Jovanovic printf("Prio is below normal\n");
232cdf0e10cSrcweir }
233cdf0e10cSrcweir else if (_aPriority == osl_Thread_PriorityLowest)
234cdf0e10cSrcweir {
23544e8df1fSDamjan Jovanovic printf("Prio is lowest\n");
236cdf0e10cSrcweir }
237cdf0e10cSrcweir else
238cdf0e10cSrcweir {
23944e8df1fSDamjan Jovanovic printf("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 {
28044e8df1fSDamjan Jovanovic printf("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;
29644e8df1fSDamjan Jovanovic printf("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 {
34244e8df1fSDamjan Jovanovic printf("normally terminate this thread %d!\n", getIdentifier());
343cdf0e10cSrcweir }
344cdf0e10cSrcweir public:
345cdf0e10cSrcweir
~OCountThread()346cdf0e10cSrcweir ~OCountThread()
347cdf0e10cSrcweir {
348cdf0e10cSrcweir if (isRunning())
349cdf0e10cSrcweir {
35044e8df1fSDamjan Jovanovic printf("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 {
40244e8df1fSDamjan Jovanovic printf("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 {
43744e8df1fSDamjan Jovanovic printf("normally terminate this thread %d!\n", getIdentifier());
438cdf0e10cSrcweir }
439cdf0e10cSrcweir public:
ONoScheduleThread()440cdf0e10cSrcweir ONoScheduleThread()
441cdf0e10cSrcweir {
44244e8df1fSDamjan Jovanovic printf("new thread id %d!\n", getIdentifier());
443cdf0e10cSrcweir }
~ONoScheduleThread()444cdf0e10cSrcweir ~ONoScheduleThread()
445cdf0e10cSrcweir {
446cdf0e10cSrcweir if (isRunning())
447cdf0e10cSrcweir {
44844e8df1fSDamjan Jovanovic printf("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 {
48144e8df1fSDamjan Jovanovic // printf("normally terminate this thread %d!\n", getIdentifier());
482cdf0e10cSrcweir }
483cdf0e10cSrcweir public:
484cdf0e10cSrcweir
~OAddThread()485cdf0e10cSrcweir ~OAddThread()
486cdf0e10cSrcweir {
487cdf0e10cSrcweir if (isRunning())
488cdf0e10cSrcweir {
48944e8df1fSDamjan Jovanovic // printf("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
50744e8df1fSDamjan Jovanovic printf("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
52844e8df1fSDamjan Jovanovic printf("#wait for join.\n");
529cdf0e10cSrcweir _pThread->join();
530cdf0e10cSrcweir }
531cdf0e10cSrcweir /** Test of the osl::Thread::create method
532cdf0e10cSrcweir */
533cdf0e10cSrcweir
53444e8df1fSDamjan Jovanovic class create : public ::testing::Test
535cdf0e10cSrcweir {
536cdf0e10cSrcweir public:
537cdf0e10cSrcweir
538cdf0e10cSrcweir // initialise your test code values here.
SetUp()53944e8df1fSDamjan Jovanovic void SetUp()
540cdf0e10cSrcweir {
541cdf0e10cSrcweir }
542cdf0e10cSrcweir
TearDown()54344e8df1fSDamjan Jovanovic void TearDown()
544cdf0e10cSrcweir {
545cdf0e10cSrcweir }
54644e8df1fSDamjan Jovanovic }; // class create
54744e8df1fSDamjan Jovanovic
548cdf0e10cSrcweir
549cdf0e10cSrcweir /** Simple create a thread.
550cdf0e10cSrcweir
551cdf0e10cSrcweir Create a simple thread, it just does add 1 to value(which initialized 0),
552cdf0e10cSrcweir if the thread run, the value should be 1.
553cdf0e10cSrcweir */
TEST_F(create,create_001)55444e8df1fSDamjan Jovanovic TEST_F(create, create_001)
555cdf0e10cSrcweir {
556cdf0e10cSrcweir myThread* newthread = new myThread();
557cdf0e10cSrcweir sal_Bool bRes = newthread->create();
55844e8df1fSDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "Can not creates a new thread!\n";
559cdf0e10cSrcweir
560cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(1); // wait short
561cdf0e10cSrcweir sal_Bool isRunning = newthread->isRunning(); // check if thread is running
562cdf0e10cSrcweir /// wait for the new thread to assure it has run
563cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(3);
564cdf0e10cSrcweir sal_Int32 nValue = newthread->getValue();
565cdf0e10cSrcweir /// to assure the new thread has terminated
566cdf0e10cSrcweir termAndJoinThread(newthread);
567cdf0e10cSrcweir delete newthread;
568cdf0e10cSrcweir
56944e8df1fSDamjan Jovanovic printf(" nValue = %d\n", nValue);
57044e8df1fSDamjan Jovanovic printf("isRunning = %d\n", isRunning);
571cdf0e10cSrcweir
57244e8df1fSDamjan Jovanovic ASSERT_TRUE(nValue >= 1 && isRunning == sal_True) << "Creates a new thread";
573cdf0e10cSrcweir
574cdf0e10cSrcweir }
575cdf0e10cSrcweir
576cdf0e10cSrcweir /** only one running thread per instance, return false if create secondly
577cdf0e10cSrcweir */
TEST_F(create,create_002)57844e8df1fSDamjan Jovanovic TEST_F(create, create_002)
579cdf0e10cSrcweir {
580cdf0e10cSrcweir myThread* newthread = new myThread();
581cdf0e10cSrcweir sal_Bool res1 = newthread->create();
582cdf0e10cSrcweir sal_Bool res2 = newthread->create();
583*30acf5e8Spfg printf("In non pro, an assertion should occurred. This behaviour is right.\n");
584cdf0e10cSrcweir termAndJoinThread(newthread);
585cdf0e10cSrcweir delete newthread;
586cdf0e10cSrcweir
58744e8df1fSDamjan Jovanovic ASSERT_TRUE(res1 && !res2) << "Creates a new thread: can not create two threads per instance";
588cdf0e10cSrcweir
589cdf0e10cSrcweir }
590cdf0e10cSrcweir
591cdf0e10cSrcweir
592cdf0e10cSrcweir /** Test of the osl::Thread::createSuspended method
593cdf0e10cSrcweir */
59444e8df1fSDamjan Jovanovic class createSuspended : public ::testing::Test
595cdf0e10cSrcweir {
596cdf0e10cSrcweir public:
597cdf0e10cSrcweir // initialise your test code values here.
SetUp()59844e8df1fSDamjan Jovanovic void SetUp()
599cdf0e10cSrcweir {
600cdf0e10cSrcweir }
601cdf0e10cSrcweir
TearDown()60244e8df1fSDamjan Jovanovic void TearDown()
603cdf0e10cSrcweir {
604cdf0e10cSrcweir }
60544e8df1fSDamjan Jovanovic }; // class createSuspended
606cdf0e10cSrcweir
607cdf0e10cSrcweir /** Create a suspended thread, use the same class as create_001
608cdf0e10cSrcweir
609cdf0e10cSrcweir after create, wait enough time, check the value, if it's still the initial value, pass
610cdf0e10cSrcweir */
TEST_F(createSuspended,createSuspended_001)61144e8df1fSDamjan Jovanovic TEST_F(createSuspended, createSuspended_001)
612cdf0e10cSrcweir {
613cdf0e10cSrcweir myThread* newthread = new myThread();
614cdf0e10cSrcweir sal_Bool bRes = newthread->createSuspended();
61544e8df1fSDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "Can not creates a new thread!";
616cdf0e10cSrcweir
617cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(1);
618cdf0e10cSrcweir sal_Bool isRunning = newthread->isRunning();
619cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(3);
620cdf0e10cSrcweir sal_Int32 nValue = newthread->getValue();
621cdf0e10cSrcweir
622cdf0e10cSrcweir resumeAndWaitThread(newthread);
623cdf0e10cSrcweir
624cdf0e10cSrcweir termAndJoinThread(newthread);
625cdf0e10cSrcweir delete newthread;
626cdf0e10cSrcweir
62744e8df1fSDamjan Jovanovic ASSERT_TRUE(nValue == 0 && isRunning) << "Creates a new suspended thread";
628cdf0e10cSrcweir }
62944e8df1fSDamjan Jovanovic // LLA: Deadlocked!!!
TEST_F(createSuspended,createSuspended_002)63044e8df1fSDamjan Jovanovic TEST_F(createSuspended, createSuspended_002)
631cdf0e10cSrcweir {
632cdf0e10cSrcweir myThread* newthread = new myThread();
633cdf0e10cSrcweir sal_Bool res1 = newthread->createSuspended();
634cdf0e10cSrcweir sal_Bool res2 = newthread->createSuspended();
635cdf0e10cSrcweir
636cdf0e10cSrcweir resumeAndWaitThread(newthread);
637cdf0e10cSrcweir
638cdf0e10cSrcweir termAndJoinThread(newthread);
639cdf0e10cSrcweir
640cdf0e10cSrcweir delete newthread;
641cdf0e10cSrcweir
64244e8df1fSDamjan Jovanovic ASSERT_TRUE(res1 && !res2) << "Creates a new thread: can not create two threads per instance";
643cdf0e10cSrcweir }
644cdf0e10cSrcweir
645cdf0e10cSrcweir
646cdf0e10cSrcweir /** when the count value equal to or more than 3, suspend the thread.
647cdf0e10cSrcweir */
suspendCountThread(OCountThread * _pCountThread)648cdf0e10cSrcweir void suspendCountThread(OCountThread* _pCountThread)
649cdf0e10cSrcweir {
650cdf0e10cSrcweir sal_Int32 nValue = 0;
651cdf0e10cSrcweir while (1)
652cdf0e10cSrcweir {
653cdf0e10cSrcweir nValue = _pCountThread->getValue();
654cdf0e10cSrcweir if (nValue >= 3)
655cdf0e10cSrcweir {
656cdf0e10cSrcweir _pCountThread->suspend();
657cdf0e10cSrcweir break;
658cdf0e10cSrcweir }
659cdf0e10cSrcweir }
660cdf0e10cSrcweir }
661cdf0e10cSrcweir
662cdf0e10cSrcweir /** Test of the osl::Thread::suspend method
663cdf0e10cSrcweir */
66444e8df1fSDamjan Jovanovic class suspend : public ::testing::Test
665cdf0e10cSrcweir {
666cdf0e10cSrcweir public:
667cdf0e10cSrcweir // initialise your test code values here.
SetUp()66844e8df1fSDamjan Jovanovic void SetUp()
669cdf0e10cSrcweir {
670cdf0e10cSrcweir }
671cdf0e10cSrcweir
TearDown()67244e8df1fSDamjan Jovanovic void TearDown()
673cdf0e10cSrcweir {
674cdf0e10cSrcweir }
67544e8df1fSDamjan Jovanovic }; // class suspend
676cdf0e10cSrcweir
677cdf0e10cSrcweir /** Use a thread which has a flag added 1 every second
678cdf0e10cSrcweir
679cdf0e10cSrcweir ALGORITHM:
680cdf0e10cSrcweir create the thread, after running special time, record value of flag, then suspend it,
681cdf0e10cSrcweir wait a long time, check the flag, if it remains unchanged during suspending
682cdf0e10cSrcweir */
TEST_F(suspend,suspend_001)68344e8df1fSDamjan Jovanovic TEST_F(suspend, suspend_001)
684cdf0e10cSrcweir {
685cdf0e10cSrcweir OCountThread* aCountThread = new OCountThread();
686cdf0e10cSrcweir sal_Bool bRes = aCountThread->create();
68744e8df1fSDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "Can't start thread!";
688cdf0e10cSrcweir // the thread run for some seconds, but not terminate
689cdf0e10cSrcweir suspendCountThread( aCountThread );
690cdf0e10cSrcweir
691cdf0e10cSrcweir // the value just after calling suspend
692cdf0e10cSrcweir sal_Int32 nValue = aCountThread->getValue(); // (2)
693cdf0e10cSrcweir
694cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(3);
695cdf0e10cSrcweir
696cdf0e10cSrcweir // the value after waiting 3 seconds
697cdf0e10cSrcweir sal_Int32 nLaterValue = aCountThread->getValue(); // (3)
698cdf0e10cSrcweir
699cdf0e10cSrcweir resumeAndWaitThread(aCountThread);
700cdf0e10cSrcweir termAndJoinThread(aCountThread);
701cdf0e10cSrcweir delete aCountThread;
702cdf0e10cSrcweir
70344e8df1fSDamjan Jovanovic ASSERT_TRUE(bRes == sal_True && nValue == nLaterValue) << "Suspend the thread";
704cdf0e10cSrcweir
705cdf0e10cSrcweir }
706cdf0e10cSrcweir /** suspend a thread in it's worker-function, the ALGORITHM is same as suspend_001
707cdf0e10cSrcweir reason of deadlocked I think: no schedule can schedule other threads to go on excuting
708cdf0e10cSrcweir */
TEST_F(suspend,suspend_002)70944e8df1fSDamjan Jovanovic TEST_F(suspend, suspend_002)
710cdf0e10cSrcweir {
71144e8df1fSDamjan Jovanovic #if 0
712cdf0e10cSrcweir OSuspendThread* aThread = new OSuspendThread();
713cdf0e10cSrcweir sal_Bool bRes = aThread->create();
71444e8df1fSDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "Can't start thread!";
715cdf0e10cSrcweir // first the thread run for some seconds, but not terminate
716cdf0e10cSrcweir sal_Int32 nValue = 0;
717cdf0e10cSrcweir //while (1)
718cdf0e10cSrcweir //{
719cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(3);
720cdf0e10cSrcweir nValue = aThread->getValue(); // (1)
72144e8df1fSDamjan Jovanovic printf(" getValue is %d !", nValue );
722cdf0e10cSrcweir if (nValue >= 2)
723cdf0e10cSrcweir {
724cdf0e10cSrcweir aThread->setSuspend();
725cdf0e10cSrcweir //break;
726cdf0e10cSrcweir }
727cdf0e10cSrcweir //}
72844e8df1fSDamjan Jovanovic printf(" after while!");
729cdf0e10cSrcweir // the value just after calling suspend
730cdf0e10cSrcweir nValue = aThread->getValue(); // (2)
731cdf0e10cSrcweir
732cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(3);
73344e8df1fSDamjan Jovanovic printf(" after sleep!");
734cdf0e10cSrcweir // the value after waiting 3 seconds
735cdf0e10cSrcweir sal_Int32 nLaterValue = aThread->getValue(); // (3)
736cdf0e10cSrcweir
737cdf0e10cSrcweir //resumeAndWaitThread(aThread);
738cdf0e10cSrcweir aThread->resume();
739cdf0e10cSrcweir termAndJoinThread(aThread);
740cdf0e10cSrcweir delete aThread;
741cdf0e10cSrcweir
74244e8df1fSDamjan Jovanovic ASSERT_TRUE(bRes == sal_True && nValue == nLaterValue) << "Suspend the thread";
74344e8df1fSDamjan Jovanovic #endif
744cdf0e10cSrcweir }
745cdf0e10cSrcweir
746cdf0e10cSrcweir
747cdf0e10cSrcweir /** Test of the osl::Thread::resume method
748cdf0e10cSrcweir */
74944e8df1fSDamjan Jovanovic class resume : public ::testing::Test
750cdf0e10cSrcweir {
751cdf0e10cSrcweir public:
752cdf0e10cSrcweir // initialise your test code values here.
SetUp()75344e8df1fSDamjan Jovanovic void SetUp()
754cdf0e10cSrcweir {
755cdf0e10cSrcweir }
756cdf0e10cSrcweir
TearDown()75744e8df1fSDamjan Jovanovic void TearDown()
758cdf0e10cSrcweir {
759cdf0e10cSrcweir }
76044e8df1fSDamjan Jovanovic }; // class resume
761cdf0e10cSrcweir
762cdf0e10cSrcweir /** check if the thread run samely as usual after suspend and resume
763cdf0e10cSrcweir
764cdf0e10cSrcweir ALGORITHM:
765cdf0e10cSrcweir compare the values before and after suspend, they should be same,
766cdf0e10cSrcweir then compare values before and after resume, the difference should be same as the sleep seconds number
767cdf0e10cSrcweir */
TEST_F(resume,resume_001)76844e8df1fSDamjan Jovanovic TEST_F(resume, resume_001)
769cdf0e10cSrcweir {
770cdf0e10cSrcweir OCountThread* pCountThread = new OCountThread();
771cdf0e10cSrcweir sal_Bool bRes = pCountThread->create();
77244e8df1fSDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "Can't start thread!";
773cdf0e10cSrcweir
774cdf0e10cSrcweir suspendCountThread(pCountThread);
775cdf0e10cSrcweir
776cdf0e10cSrcweir sal_Int32 nSuspendValue = pCountThread->getValue(); // (2)
777cdf0e10cSrcweir // suspend for 3 seconds
778cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(3);
779cdf0e10cSrcweir pCountThread->resume();
780cdf0e10cSrcweir
781cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(3);
782cdf0e10cSrcweir sal_Int32 nResumeValue = pCountThread->getValue();
783cdf0e10cSrcweir
784cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(3);
785cdf0e10cSrcweir sal_Int32 nLaterValue = pCountThread->getValue();
786cdf0e10cSrcweir
787cdf0e10cSrcweir termAndJoinThread(pCountThread);
788cdf0e10cSrcweir delete pCountThread;
789cdf0e10cSrcweir
79044e8df1fSDamjan Jovanovic printf("SuspendValue: %d\n", nSuspendValue);
79144e8df1fSDamjan Jovanovic printf("ResumeValue: %d\n", nResumeValue);
79244e8df1fSDamjan Jovanovic printf("LaterValue: %d\n", nLaterValue);
793cdf0e10cSrcweir
794cdf0e10cSrcweir /* LLA: this assumption is no longer relevant: nResumeValue == nSuspendValue && */
79544e8df1fSDamjan Jovanovic ASSERT_TRUE(nLaterValue >= 9 &&
796cdf0e10cSrcweir nResumeValue > nSuspendValue &&
79744e8df1fSDamjan Jovanovic nLaterValue > nResumeValue) << "Suspend then resume the thread";
798cdf0e10cSrcweir
799cdf0e10cSrcweir }
800cdf0e10cSrcweir
801cdf0e10cSrcweir /** Create a suspended thread then resume, check if the thread has run
802cdf0e10cSrcweir */
TEST_F(resume,resume_002)80344e8df1fSDamjan Jovanovic TEST_F(resume, resume_002)
804cdf0e10cSrcweir {
805cdf0e10cSrcweir myThread* newthread = new myThread();
806cdf0e10cSrcweir sal_Bool bRes = newthread->createSuspended();
80744e8df1fSDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "Can't create thread!";
808cdf0e10cSrcweir
809cdf0e10cSrcweir newthread->resume();
810cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(2);
811cdf0e10cSrcweir sal_Int32 nValue = newthread->getValue();
812cdf0e10cSrcweir
813cdf0e10cSrcweir termAndJoinThread(newthread);
814cdf0e10cSrcweir delete newthread;
815cdf0e10cSrcweir
81644e8df1fSDamjan Jovanovic printf(" nValue = %d\n", nValue);
817cdf0e10cSrcweir
81844e8df1fSDamjan Jovanovic ASSERT_TRUE(nValue >= 1) << "Creates a suspended thread, then resume";
819cdf0e10cSrcweir }
820cdf0e10cSrcweir
821cdf0e10cSrcweir /** Test of the osl::Thread::terminate method
822cdf0e10cSrcweir */
82344e8df1fSDamjan Jovanovic class terminate : public ::testing::Test
824cdf0e10cSrcweir {
825cdf0e10cSrcweir public:
826cdf0e10cSrcweir // initialise your test code values here.
SetUp()82744e8df1fSDamjan Jovanovic void SetUp()
828cdf0e10cSrcweir {
829cdf0e10cSrcweir }
830cdf0e10cSrcweir
TearDown()83144e8df1fSDamjan Jovanovic void TearDown()
832cdf0e10cSrcweir {
833cdf0e10cSrcweir }
83444e8df1fSDamjan Jovanovic }; // class terminate
835cdf0e10cSrcweir
836cdf0e10cSrcweir /** Check after call terminate if the running thread running go on executing
837cdf0e10cSrcweir
838cdf0e10cSrcweir ALGORITHM:
839cdf0e10cSrcweir before and after call terminate, the values should be the same
840cdf0e10cSrcweir */
TEST_F(terminate,terminate_001)84144e8df1fSDamjan Jovanovic TEST_F(terminate, terminate_001)
842cdf0e10cSrcweir {
843cdf0e10cSrcweir OCountThread* aCountThread = new OCountThread();
844cdf0e10cSrcweir sal_Bool bRes = aCountThread->create();
84544e8df1fSDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "Can't start thread!";
846cdf0e10cSrcweir
847cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(2);
848cdf0e10cSrcweir sal_Int32 nValue = aCountThread->getValue();
849cdf0e10cSrcweir aCountThread->terminate();
850cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(2);
851cdf0e10cSrcweir sal_Int32 nLaterValue = aCountThread->getValue();
852cdf0e10cSrcweir
853cdf0e10cSrcweir // isRunning should be false after terminate
854cdf0e10cSrcweir sal_Bool isRunning = aCountThread->isRunning();
855cdf0e10cSrcweir aCountThread->join();
856cdf0e10cSrcweir delete aCountThread;
857cdf0e10cSrcweir
85844e8df1fSDamjan Jovanovic printf(" nValue = %d\n", nValue);
85944e8df1fSDamjan Jovanovic printf("nLaterValue = %d\n", nLaterValue);
860cdf0e10cSrcweir
86144e8df1fSDamjan Jovanovic ASSERT_TRUE(isRunning == sal_False && nLaterValue >= nValue) << "Terminate the thread";
862cdf0e10cSrcweir }
863cdf0e10cSrcweir /** Check if a suspended thread will terminate after call terminate, different on w32 and on UNX
864cdf0e10cSrcweir */
TEST_F(terminate,terminate_002)86544e8df1fSDamjan Jovanovic TEST_F(terminate, terminate_002)
866cdf0e10cSrcweir {
867cdf0e10cSrcweir OCountThread* aCountThread = new OCountThread();
868cdf0e10cSrcweir sal_Bool bRes = aCountThread->create();
86944e8df1fSDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "Can't start thread!";
870cdf0e10cSrcweir
871cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(1);
872cdf0e10cSrcweir suspendCountThread(aCountThread);
873cdf0e10cSrcweir sal_Int32 nValue = aCountThread->getValue();
874cdf0e10cSrcweir
875cdf0e10cSrcweir // seems a suspended thread can not be terminated on W32, while on Solaris can
876cdf0e10cSrcweir resumeAndWaitThread(aCountThread);
877cdf0e10cSrcweir
878cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(2);
879cdf0e10cSrcweir
880cdf0e10cSrcweir termAndJoinThread(aCountThread);
881cdf0e10cSrcweir sal_Int32 nLaterValue = aCountThread->getValue();
882cdf0e10cSrcweir delete aCountThread;
883cdf0e10cSrcweir
88444e8df1fSDamjan Jovanovic printf(" nValue = %d\n", nValue);
88544e8df1fSDamjan Jovanovic printf("nLaterValue = %d\n", nLaterValue);
886cdf0e10cSrcweir
88744e8df1fSDamjan Jovanovic ASSERT_TRUE(nLaterValue > nValue) << "Suspend then resume the thread";
888cdf0e10cSrcweir }
889cdf0e10cSrcweir
890cdf0e10cSrcweir
891cdf0e10cSrcweir /** Test of the osl::Thread::join method
892cdf0e10cSrcweir */
89344e8df1fSDamjan Jovanovic class join : public ::testing::Test
894cdf0e10cSrcweir {
895cdf0e10cSrcweir public:
896cdf0e10cSrcweir // initialise your test code values here.
SetUp()89744e8df1fSDamjan Jovanovic void SetUp()
898cdf0e10cSrcweir {
899cdf0e10cSrcweir }
900cdf0e10cSrcweir
TearDown()90144e8df1fSDamjan Jovanovic void TearDown()
902cdf0e10cSrcweir {
903cdf0e10cSrcweir }
90444e8df1fSDamjan Jovanovic }; // class join
905cdf0e10cSrcweir
906cdf0e10cSrcweir /** Check after call terminate if the thread running function will not go on executing
907cdf0e10cSrcweir
908cdf0e10cSrcweir the next statement after join will not exec before the thread terminate
909cdf0e10cSrcweir ALGORITHM:
910cdf0e10cSrcweir recode system time at the beginning of the thread run, call join, then record system time again,
911cdf0e10cSrcweir the difference of the two time should be equal or more than 20 seconds, the CountThead normally terminate
912cdf0e10cSrcweir */
TEST_F(join,join_001)91344e8df1fSDamjan Jovanovic TEST_F(join, join_001)
914cdf0e10cSrcweir {
915cdf0e10cSrcweir OCountThread *aCountThread = new OCountThread();
916cdf0e10cSrcweir sal_Bool bRes = aCountThread->create();
91744e8df1fSDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "Can't start thread!";
918cdf0e10cSrcweir
919cdf0e10cSrcweir StopWatch aStopWatch;
920cdf0e10cSrcweir aStopWatch.start();
921cdf0e10cSrcweir // TimeValue aTimeVal_befor;
922cdf0e10cSrcweir // osl_getSystemTime( &aTimeVal_befor );
92344e8df1fSDamjan Jovanovic //printf("#join:the system time is %d,%d\n", pTimeVal_befor->Seconds,pTimeVal_befor->Nanosec);
924cdf0e10cSrcweir
925cdf0e10cSrcweir aCountThread->join();
926cdf0e10cSrcweir
927cdf0e10cSrcweir //the below line will be executed after aCountThread terminate
928cdf0e10cSrcweir // TimeValue aTimeVal_after;
929cdf0e10cSrcweir // osl_getSystemTime( &aTimeVal_after );
930cdf0e10cSrcweir aStopWatch.stop();
931cdf0e10cSrcweir // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
932cdf0e10cSrcweir double nSec = aStopWatch.getSeconds();
93344e8df1fSDamjan Jovanovic printf("join_001 nSec=%f\n", nSec);
934cdf0e10cSrcweir delete aCountThread;
935cdf0e10cSrcweir
93644e8df1fSDamjan Jovanovic ASSERT_TRUE(nSec >= 2) << "Join the thread: after the thread terminate";
937cdf0e10cSrcweir
938cdf0e10cSrcweir }
939cdf0e10cSrcweir /** after terminated by another thread, join exited immediately
940cdf0e10cSrcweir
941cdf0e10cSrcweir ALGORITHM:
942cdf0e10cSrcweir terminate the thread when value>=3, call join, check the beginning time and time after join,
943cdf0e10cSrcweir the difference should be 3 seconds, join costs little time
944cdf0e10cSrcweir */
TEST_F(join,join_002)94544e8df1fSDamjan Jovanovic TEST_F(join, join_002)
946cdf0e10cSrcweir {
947cdf0e10cSrcweir OCountThread *aCountThread = new OCountThread();
948cdf0e10cSrcweir sal_Bool bRes = aCountThread->create();
94944e8df1fSDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "Can't start thread!";
950cdf0e10cSrcweir
951cdf0e10cSrcweir //record the time when the running begin
952cdf0e10cSrcweir // TimeValue aTimeVal_befor;
953cdf0e10cSrcweir // osl_getSystemTime( &aTimeVal_befor );
954cdf0e10cSrcweir StopWatch aStopWatch;
955cdf0e10cSrcweir aStopWatch.start();
956cdf0e10cSrcweir
957cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(10);
958cdf0e10cSrcweir termAndJoinThread(aCountThread);
959cdf0e10cSrcweir
960cdf0e10cSrcweir //the below line will be executed after aCountThread terminate
961cdf0e10cSrcweir // TimeValue aTimeVal_after;
962cdf0e10cSrcweir // osl_getSystemTime( &aTimeVal_after );
963cdf0e10cSrcweir // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
964cdf0e10cSrcweir aStopWatch.stop();
965cdf0e10cSrcweir double nSec = aStopWatch.getSeconds();
96644e8df1fSDamjan Jovanovic printf("join_002 nSec=%f\n", nSec);
967cdf0e10cSrcweir
968cdf0e10cSrcweir delete aCountThread;
96944e8df1fSDamjan Jovanovic ASSERT_TRUE(nSec >= 1) << "Join the thread: after thread terminate by another thread";
970cdf0e10cSrcweir }
971cdf0e10cSrcweir
972cdf0e10cSrcweir /** Test of the osl::Thread::isRunning method
973cdf0e10cSrcweir */
97444e8df1fSDamjan Jovanovic class isRunning : public ::testing::Test
975cdf0e10cSrcweir {
976cdf0e10cSrcweir public:
977cdf0e10cSrcweir // initialise your test code values here.
SetUp()97844e8df1fSDamjan Jovanovic void SetUp()
979cdf0e10cSrcweir {
980cdf0e10cSrcweir }
981cdf0e10cSrcweir
TearDown()98244e8df1fSDamjan Jovanovic void TearDown()
983cdf0e10cSrcweir {
984cdf0e10cSrcweir }
98544e8df1fSDamjan Jovanovic }; // class isRunning
986cdf0e10cSrcweir
987cdf0e10cSrcweir /**
988cdf0e10cSrcweir */
TEST_F(isRunning,isRunning_001)98944e8df1fSDamjan Jovanovic TEST_F(isRunning, isRunning_001)
990cdf0e10cSrcweir {
991cdf0e10cSrcweir OCountThread *aCountThread = new OCountThread();
992cdf0e10cSrcweir sal_Bool bRes = aCountThread->create();
99344e8df1fSDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "Can't start thread!";
994cdf0e10cSrcweir
995cdf0e10cSrcweir sal_Bool bRun = aCountThread->isRunning();
996cdf0e10cSrcweir
997cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(2);
998cdf0e10cSrcweir termAndJoinThread(aCountThread);
999cdf0e10cSrcweir sal_Bool bTer = aCountThread->isRunning();
1000cdf0e10cSrcweir delete aCountThread;
1001cdf0e10cSrcweir
100244e8df1fSDamjan Jovanovic ASSERT_TRUE(bRun == sal_True && bTer == sal_False) << "Test isRunning";
1003cdf0e10cSrcweir }
1004cdf0e10cSrcweir /** check the value of isRunning when suspending and after resume
1005cdf0e10cSrcweir */
TEST_F(isRunning,isRunning_002)100644e8df1fSDamjan Jovanovic TEST_F(isRunning, isRunning_002)
1007cdf0e10cSrcweir {
1008cdf0e10cSrcweir OCountThread *aCountThread = new OCountThread();
1009cdf0e10cSrcweir sal_Bool bRes = aCountThread->create();
101044e8df1fSDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "Can't start thread!";
1011cdf0e10cSrcweir
1012cdf0e10cSrcweir // sal_Bool bRunning = aCountThread->isRunning();
1013cdf0e10cSrcweir // sal_Int32 nValue = 0;
1014cdf0e10cSrcweir suspendCountThread(aCountThread);
1015cdf0e10cSrcweir
1016cdf0e10cSrcweir sal_Bool bRunning_sup = aCountThread->isRunning();
1017cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(2);
1018cdf0e10cSrcweir aCountThread->resume();
1019cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(2);
1020cdf0e10cSrcweir sal_Bool bRunning_res = aCountThread->isRunning();
1021cdf0e10cSrcweir termAndJoinThread(aCountThread);
1022cdf0e10cSrcweir sal_Bool bRunning_ter = aCountThread->isRunning();
1023cdf0e10cSrcweir delete aCountThread;
1024cdf0e10cSrcweir
102544e8df1fSDamjan Jovanovic ASSERT_TRUE(bRes == sal_True &&
1026cdf0e10cSrcweir bRunning_sup == sal_True &&
1027cdf0e10cSrcweir bRunning_res == sal_True &&
102844e8df1fSDamjan Jovanovic bRunning_ter == sal_False) << "Test isRunning";
1029cdf0e10cSrcweir
1030cdf0e10cSrcweir }
1031cdf0e10cSrcweir
1032cdf0e10cSrcweir /// check osl::Thread::setPriority
103344e8df1fSDamjan Jovanovic class setPriority : public ::testing::Test
1034cdf0e10cSrcweir {
1035cdf0e10cSrcweir public:
1036cdf0e10cSrcweir // initialise your test code values here.
SetUp()103744e8df1fSDamjan Jovanovic void SetUp()
1038cdf0e10cSrcweir {
1039cdf0e10cSrcweir }
1040cdf0e10cSrcweir
TearDown()104144e8df1fSDamjan Jovanovic void TearDown()
1042cdf0e10cSrcweir {
1043cdf0e10cSrcweir }
1044cdf0e10cSrcweir
1045cdf0e10cSrcweir // insert your test code here.
getPrioName(oslThreadPriority _aPriority)1046cdf0e10cSrcweir rtl::OString getPrioName(oslThreadPriority _aPriority)
1047cdf0e10cSrcweir {
1048cdf0e10cSrcweir rtl::OString sPrioStr;
1049cdf0e10cSrcweir switch (_aPriority)
1050cdf0e10cSrcweir {
1051cdf0e10cSrcweir case osl_Thread_PriorityHighest:
1052cdf0e10cSrcweir sPrioStr = "Highest";
1053cdf0e10cSrcweir break;
1054cdf0e10cSrcweir
1055cdf0e10cSrcweir case osl_Thread_PriorityAboveNormal:
1056cdf0e10cSrcweir sPrioStr = "AboveNormal";
1057cdf0e10cSrcweir
1058cdf0e10cSrcweir case osl_Thread_PriorityNormal:
1059cdf0e10cSrcweir sPrioStr = "Normal";
1060cdf0e10cSrcweir
1061cdf0e10cSrcweir case osl_Thread_PriorityBelowNormal:
1062cdf0e10cSrcweir sPrioStr = "BelowNormal";
1063cdf0e10cSrcweir break;
1064cdf0e10cSrcweir
1065cdf0e10cSrcweir case osl_Thread_PriorityLowest:
1066cdf0e10cSrcweir sPrioStr = "Lowest";
1067cdf0e10cSrcweir break;
1068cdf0e10cSrcweir default:
1069cdf0e10cSrcweir sPrioStr = "unknown";
1070cdf0e10cSrcweir }
1071cdf0e10cSrcweir return sPrioStr;
1072cdf0e10cSrcweir }
1073cdf0e10cSrcweir
1074cdf0e10cSrcweir
1075cdf0e10cSrcweir /** check 2 threads.
1076cdf0e10cSrcweir
1077cdf0e10cSrcweir ALGORITHM:
1078cdf0e10cSrcweir Here the function should show, that 2 different threads,
1079cdf0e10cSrcweir which only increase a value, should run at the same time with same prio.
1080cdf0e10cSrcweir The test fails, if the difference between the two values is more than 5%
1081cdf0e10cSrcweir but IMHO this isn't a failure, it's only a feature of the OS.
1082cdf0e10cSrcweir */
1083cdf0e10cSrcweir
check2Threads(oslThreadPriority _aPriority)1084cdf0e10cSrcweir void check2Threads(oslThreadPriority _aPriority)
1085cdf0e10cSrcweir {
1086cdf0e10cSrcweir // initial 5 threads with different priorities
1087cdf0e10cSrcweir OAddThread* pThread = new OAddThread();
1088cdf0e10cSrcweir OAddThread* p2Thread = new OAddThread();
1089cdf0e10cSrcweir
1090cdf0e10cSrcweir //Create them and start running at the same time
1091cdf0e10cSrcweir pThread->create();
1092cdf0e10cSrcweir pThread->setPriority(_aPriority);
1093cdf0e10cSrcweir p2Thread->create();
1094cdf0e10cSrcweir p2Thread->setPriority(_aPriority);
1095cdf0e10cSrcweir
1096cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(5);
1097cdf0e10cSrcweir
1098cdf0e10cSrcweir pThread->terminate();
1099cdf0e10cSrcweir p2Thread->terminate();
1100cdf0e10cSrcweir
1101cdf0e10cSrcweir sal_Int32 nValueNormal = 0;
1102cdf0e10cSrcweir nValueNormal = pThread->getValue();
1103cdf0e10cSrcweir
1104cdf0e10cSrcweir sal_Int32 nValueNormal2 = 0;
1105cdf0e10cSrcweir nValueNormal2 = p2Thread->getValue();
1106cdf0e10cSrcweir
1107cdf0e10cSrcweir rtl::OString sPrio = getPrioName(_aPriority);
110844e8df1fSDamjan Jovanovic printf("After 10 tenth seconds\n");
1109cdf0e10cSrcweir
111044e8df1fSDamjan Jovanovic printf("nValue in %s Prio Thread is %d\n",sPrio.getStr(), nValueNormal);
111144e8df1fSDamjan Jovanovic printf("nValue in %s Prio Thread is %d\n", sPrio.getStr(), nValueNormal2);
1112cdf0e10cSrcweir
1113cdf0e10cSrcweir // ThreadHelper::thread_sleep_tenth_sec(1);
1114cdf0e10cSrcweir pThread->join();
1115cdf0e10cSrcweir p2Thread->join();
1116cdf0e10cSrcweir
1117cdf0e10cSrcweir delete pThread;
1118cdf0e10cSrcweir delete p2Thread;
1119cdf0e10cSrcweir
1120cdf0e10cSrcweir sal_Int32 nDelta = abs(nValueNormal - nValueNormal2);
1121cdf0e10cSrcweir double nQuotient = std::max(nValueNormal, nValueNormal2);
112244e8df1fSDamjan Jovanovic ASSERT_TRUE(nQuotient != 0) << "Quotient is zero, which means, there exist no right values.";
1123cdf0e10cSrcweir double nDeltaPercent = nDelta / nQuotient * 100;
1124cdf0e10cSrcweir
112544e8df1fSDamjan Jovanovic printf("Delta value %d, percent %f\n",nDelta, nDeltaPercent);
1126cdf0e10cSrcweir
1127cdf0e10cSrcweir // LLA: it's not a bug if the current OS is not able to handle thread scheduling right and good.
1128cdf0e10cSrcweir // like Windows XP
112944e8df1fSDamjan Jovanovic // LLA: ASSERT_TRUE(// LLA: nDeltaPercent <= 5
113044e8df1fSDamjan Jovanovic // LLA:) << // LLA: "Run 2 normal threads, the count diff more than 5 percent.";
1131cdf0e10cSrcweir }
113244e8df1fSDamjan Jovanovic }; // class setPriority
1133cdf0e10cSrcweir
TEST_F(setPriority,setPriority_001_1)113444e8df1fSDamjan Jovanovic TEST_F(setPriority, setPriority_001_1)
1135cdf0e10cSrcweir {
1136cdf0e10cSrcweir check2Threads(osl_Thread_PriorityHighest);
1137cdf0e10cSrcweir }
TEST_F(setPriority,setPriority_001_2)113844e8df1fSDamjan Jovanovic TEST_F(setPriority, setPriority_001_2)
1139cdf0e10cSrcweir {
1140cdf0e10cSrcweir check2Threads(osl_Thread_PriorityAboveNormal);
1141cdf0e10cSrcweir }
TEST_F(setPriority,setPriority_001_3)114244e8df1fSDamjan Jovanovic TEST_F(setPriority, setPriority_001_3)
1143cdf0e10cSrcweir {
1144cdf0e10cSrcweir check2Threads(osl_Thread_PriorityNormal);
1145cdf0e10cSrcweir }
TEST_F(setPriority,setPriority_001_4)114644e8df1fSDamjan Jovanovic TEST_F(setPriority, setPriority_001_4)
1147cdf0e10cSrcweir {
1148cdf0e10cSrcweir check2Threads(osl_Thread_PriorityBelowNormal);
1149cdf0e10cSrcweir }
TEST_F(setPriority,setPriority_001_5)115044e8df1fSDamjan Jovanovic TEST_F(setPriority, setPriority_001_5)
1151cdf0e10cSrcweir {
1152cdf0e10cSrcweir check2Threads(osl_Thread_PriorityLowest);
1153cdf0e10cSrcweir }
1154cdf0e10cSrcweir
115544e8df1fSDamjan Jovanovic #ifndef SOLARIS
TEST_F(setPriority,setPriority_002)115644e8df1fSDamjan Jovanovic TEST_F(setPriority, setPriority_002)
1157cdf0e10cSrcweir {
1158cdf0e10cSrcweir // initial 5 threads with different priorities
1159cdf0e10cSrcweir
1160cdf0e10cSrcweir OAddThread aHighestThread;
1161cdf0e10cSrcweir OAddThread aAboveNormalThread;
1162cdf0e10cSrcweir OAddThread aNormalThread;
1163cdf0e10cSrcweir //OAddThread *aBelowNormalThread = new OAddThread();
1164cdf0e10cSrcweir //OAddThread *aLowestThread = new OAddThread();
1165cdf0e10cSrcweir
1166cdf0e10cSrcweir //Create them and start running at the same time
1167cdf0e10cSrcweir aHighestThread.createSuspended();
1168cdf0e10cSrcweir aHighestThread.setPriority(osl_Thread_PriorityHighest);
1169cdf0e10cSrcweir
1170cdf0e10cSrcweir aAboveNormalThread.createSuspended();
1171cdf0e10cSrcweir aAboveNormalThread.setPriority(osl_Thread_PriorityAboveNormal);
1172cdf0e10cSrcweir
1173cdf0e10cSrcweir aNormalThread.createSuspended();
1174cdf0e10cSrcweir aNormalThread.setPriority(osl_Thread_PriorityNormal);
1175cdf0e10cSrcweir /*aBelowNormalThread->create();
1176cdf0e10cSrcweir aBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
1177cdf0e10cSrcweir aLowestThread->create();
1178cdf0e10cSrcweir aLowestThread->setPriority(osl_Thread_PriorityLowest);
1179cdf0e10cSrcweir */
1180cdf0e10cSrcweir
1181cdf0e10cSrcweir aHighestThread.resume();
1182cdf0e10cSrcweir aAboveNormalThread.resume();
1183cdf0e10cSrcweir aNormalThread.resume();
1184cdf0e10cSrcweir
1185cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(5);
1186cdf0e10cSrcweir
1187cdf0e10cSrcweir aHighestThread.suspend();
1188cdf0e10cSrcweir aAboveNormalThread.suspend();
1189cdf0e10cSrcweir aNormalThread.suspend();
1190cdf0e10cSrcweir
1191cdf0e10cSrcweir termAndJoinThread(&aNormalThread);
1192cdf0e10cSrcweir termAndJoinThread(&aAboveNormalThread);
1193cdf0e10cSrcweir termAndJoinThread(&aHighestThread);
1194cdf0e10cSrcweir //aBelowNormalThread->terminate();
1195cdf0e10cSrcweir //aLowestThread->terminate();
1196cdf0e10cSrcweir
1197cdf0e10cSrcweir sal_Int32 nValueHighest = 0;
1198cdf0e10cSrcweir nValueHighest = aHighestThread.getValue();
1199cdf0e10cSrcweir
1200cdf0e10cSrcweir sal_Int32 nValueAboveNormal = 0;
1201cdf0e10cSrcweir nValueAboveNormal = aAboveNormalThread.getValue();
1202cdf0e10cSrcweir
1203cdf0e10cSrcweir sal_Int32 nValueNormal = 0;
1204cdf0e10cSrcweir nValueNormal = aNormalThread.getValue();
1205cdf0e10cSrcweir
1206cdf0e10cSrcweir // sal_Int32 nValueBelowNormal = 0;
1207cdf0e10cSrcweir //nValueBelowNormal = aBelowNormalThread->getValue();
1208cdf0e10cSrcweir // sal_Int32 nValueLowest = 0;
1209cdf0e10cSrcweir //nValueLowest = aLowestThread->getValue();
121044e8df1fSDamjan Jovanovic printf("After 10 tenth seconds\n");
121144e8df1fSDamjan Jovanovic printf("nValue in Highest Prio Thread is %d\n",nValueHighest);
121244e8df1fSDamjan Jovanovic printf("nValue in AboveNormal Prio Thread is %d\n",nValueAboveNormal);
121344e8df1fSDamjan Jovanovic printf("nValue in Normal Prio Thread is %d\n",nValueNormal);
1214cdf0e10cSrcweir
1215cdf0e10cSrcweir // LLA: this is not a save test, so we only check if all values not zero
121644e8df1fSDamjan Jovanovic // LLA: ASSERT_TRUE(// LLA: nValueHighest >= nValueAboveNormal &&
1217cdf0e10cSrcweir // LLA: nValueAboveNormal >= nValueNormal &&
1218cdf0e10cSrcweir // LLA: nValueNormal > 0
121944e8df1fSDamjan Jovanovic // LLA:) << // LLA: "SetPriority";
1220cdf0e10cSrcweir
1221cdf0e10cSrcweir // LLA: windows let starve threads with lower priority
1222cdf0e10cSrcweir #ifndef WNT
122344e8df1fSDamjan Jovanovic ASSERT_TRUE(nValueHighest > 0 &&
1224cdf0e10cSrcweir nValueAboveNormal > 0 &&
122544e8df1fSDamjan Jovanovic nValueNormal > 0) << "SetPriority";
1226cdf0e10cSrcweir #endif
1227cdf0e10cSrcweir }
1228cdf0e10cSrcweir
TEST_F(setPriority,setPriority_003)122944e8df1fSDamjan Jovanovic TEST_F(setPriority, setPriority_003)
1230cdf0e10cSrcweir {
1231cdf0e10cSrcweir // initial 5 threads with different priorities
1232cdf0e10cSrcweir OAddThread *pHighestThread = new OAddThread();
1233cdf0e10cSrcweir OAddThread *pAboveNormalThread = new OAddThread();
1234cdf0e10cSrcweir OAddThread *pNormalThread = new OAddThread();
1235cdf0e10cSrcweir OAddThread *pBelowNormalThread = new OAddThread();
1236cdf0e10cSrcweir OAddThread *pLowestThread = new OAddThread();
1237cdf0e10cSrcweir
1238cdf0e10cSrcweir //Create them and start running at the same time
1239cdf0e10cSrcweir pHighestThread->createSuspended();
1240cdf0e10cSrcweir pHighestThread->setPriority(osl_Thread_PriorityHighest);
1241cdf0e10cSrcweir
1242cdf0e10cSrcweir pAboveNormalThread->createSuspended();
1243cdf0e10cSrcweir pAboveNormalThread->setPriority(osl_Thread_PriorityAboveNormal);
1244cdf0e10cSrcweir
1245cdf0e10cSrcweir pNormalThread->createSuspended();
1246cdf0e10cSrcweir pNormalThread->setPriority(osl_Thread_PriorityNormal);
1247cdf0e10cSrcweir
1248cdf0e10cSrcweir pBelowNormalThread->createSuspended();
1249cdf0e10cSrcweir pBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
1250cdf0e10cSrcweir
1251cdf0e10cSrcweir pLowestThread->createSuspended();
1252cdf0e10cSrcweir pLowestThread->setPriority(osl_Thread_PriorityLowest);
1253cdf0e10cSrcweir
1254cdf0e10cSrcweir pHighestThread->resume();
1255cdf0e10cSrcweir pAboveNormalThread->resume();
1256cdf0e10cSrcweir pNormalThread->resume();
1257cdf0e10cSrcweir pBelowNormalThread->resume();
1258cdf0e10cSrcweir pLowestThread->resume();
1259cdf0e10cSrcweir
1260cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(5);
1261cdf0e10cSrcweir
1262cdf0e10cSrcweir pHighestThread->suspend();
1263cdf0e10cSrcweir pAboveNormalThread->suspend();
1264cdf0e10cSrcweir pNormalThread->suspend();
1265cdf0e10cSrcweir pBelowNormalThread->suspend();
1266cdf0e10cSrcweir pLowestThread->suspend();
1267cdf0e10cSrcweir
1268cdf0e10cSrcweir termAndJoinThread(pHighestThread);
1269cdf0e10cSrcweir termAndJoinThread(pAboveNormalThread);
1270cdf0e10cSrcweir termAndJoinThread(pNormalThread);
1271cdf0e10cSrcweir termAndJoinThread(pBelowNormalThread);
1272cdf0e10cSrcweir termAndJoinThread(pLowestThread);
1273cdf0e10cSrcweir
1274cdf0e10cSrcweir sal_Int32 nValueHighest = 0;
1275cdf0e10cSrcweir nValueHighest = pHighestThread->getValue();
1276cdf0e10cSrcweir
1277cdf0e10cSrcweir sal_Int32 nValueAboveNormal = 0;
1278cdf0e10cSrcweir nValueAboveNormal = pAboveNormalThread->getValue();
1279cdf0e10cSrcweir
1280cdf0e10cSrcweir sal_Int32 nValueNormal = 0;
1281cdf0e10cSrcweir nValueNormal = pNormalThread->getValue();
1282cdf0e10cSrcweir
1283cdf0e10cSrcweir sal_Int32 nValueBelowNormal = 0;
1284cdf0e10cSrcweir nValueBelowNormal = pBelowNormalThread->getValue();
1285cdf0e10cSrcweir
1286cdf0e10cSrcweir sal_Int32 nValueLowest = 0;
1287cdf0e10cSrcweir nValueLowest = pLowestThread->getValue();
1288cdf0e10cSrcweir
128944e8df1fSDamjan Jovanovic printf("After 10 tenth seconds\n");
129044e8df1fSDamjan Jovanovic printf("nValue in Highest Prio Thread is %d\n",nValueHighest);
129144e8df1fSDamjan Jovanovic printf("nValue in AboveNormal Prio Thread is %d\n",nValueAboveNormal);
129244e8df1fSDamjan Jovanovic printf("nValue in Normal Prio Thread is %d\n",nValueNormal);
129344e8df1fSDamjan Jovanovic printf("nValue in BelowNormal Prio Thread is %d\n",nValueBelowNormal);
129444e8df1fSDamjan Jovanovic printf("nValue in Lowest Prio Thread is %d\n",nValueLowest);
1295cdf0e10cSrcweir
1296cdf0e10cSrcweir delete pHighestThread;
1297cdf0e10cSrcweir delete pAboveNormalThread;
1298cdf0e10cSrcweir delete pNormalThread;
1299cdf0e10cSrcweir delete pBelowNormalThread;
1300cdf0e10cSrcweir delete pLowestThread;
1301cdf0e10cSrcweir
1302cdf0e10cSrcweir // LLA: this is not a save test, so we only check if all values not zero
130344e8df1fSDamjan Jovanovic // LLA: ASSERT_TRUE(// LLA: nValueHighest > nValueAboveNormal &&
1304cdf0e10cSrcweir // LLA: nValueAboveNormal > nValueNormal &&
1305cdf0e10cSrcweir // LLA: nValueNormal > nValueBelowNormal &&
1306cdf0e10cSrcweir // LLA: nValueBelowNormal > nValueLowest &&
1307cdf0e10cSrcweir // LLA: nValueLowest > 0
130844e8df1fSDamjan Jovanovic // LLA:) << // LLA: "SetPriority";
1309cdf0e10cSrcweir
1310cdf0e10cSrcweir // LLA: windows let starve threads with lower priority
1311cdf0e10cSrcweir #ifndef WNT
131244e8df1fSDamjan Jovanovic ASSERT_TRUE(nValueHighest > 0 &&
1313cdf0e10cSrcweir nValueAboveNormal > 0 &&
1314cdf0e10cSrcweir nValueNormal > 0 &&
1315cdf0e10cSrcweir nValueBelowNormal > 0 &&
131644e8df1fSDamjan Jovanovic nValueLowest > 0) << "SetPriority";
1317cdf0e10cSrcweir #endif
1318cdf0e10cSrcweir }
1319cdf0e10cSrcweir
TEST_F(setPriority,setPriority_004)132044e8df1fSDamjan Jovanovic TEST_F(setPriority, setPriority_004)
1321cdf0e10cSrcweir {
1322cdf0e10cSrcweir // initial 5 threads with different priorities
1323cdf0e10cSrcweir // OAddThread *pHighestThread = new OAddThread();
1324cdf0e10cSrcweir OAddThread *pAboveNormalThread = new OAddThread();
1325cdf0e10cSrcweir OAddThread *pNormalThread = new OAddThread();
1326cdf0e10cSrcweir OAddThread *pBelowNormalThread = new OAddThread();
1327cdf0e10cSrcweir OAddThread *pLowestThread = new OAddThread();
1328cdf0e10cSrcweir
1329cdf0e10cSrcweir //Create them and start running at the same time
1330cdf0e10cSrcweir // pHighestThread->createSuspended();
1331cdf0e10cSrcweir // pHighestThread->setPriority(osl_Thread_PriorityHighest);
1332cdf0e10cSrcweir
1333cdf0e10cSrcweir pAboveNormalThread->createSuspended();
1334cdf0e10cSrcweir pAboveNormalThread->setPriority(osl_Thread_PriorityAboveNormal);
1335cdf0e10cSrcweir
1336cdf0e10cSrcweir pNormalThread->createSuspended();
1337cdf0e10cSrcweir pNormalThread->setPriority(osl_Thread_PriorityNormal);
1338cdf0e10cSrcweir
1339cdf0e10cSrcweir pBelowNormalThread->createSuspended();
1340cdf0e10cSrcweir pBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
1341cdf0e10cSrcweir
1342cdf0e10cSrcweir pLowestThread->createSuspended();
1343cdf0e10cSrcweir pLowestThread->setPriority(osl_Thread_PriorityLowest);
1344cdf0e10cSrcweir
1345cdf0e10cSrcweir // pHighestThread->resume();
1346cdf0e10cSrcweir pAboveNormalThread->resume();
1347cdf0e10cSrcweir pNormalThread->resume();
1348cdf0e10cSrcweir pBelowNormalThread->resume();
1349cdf0e10cSrcweir pLowestThread->resume();
1350cdf0e10cSrcweir
1351cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(5);
1352cdf0e10cSrcweir
1353cdf0e10cSrcweir // pHighestThread->suspend();
1354cdf0e10cSrcweir pAboveNormalThread->suspend();
1355cdf0e10cSrcweir pNormalThread->suspend();
1356cdf0e10cSrcweir pBelowNormalThread->suspend();
1357cdf0e10cSrcweir pLowestThread->suspend();
1358cdf0e10cSrcweir
1359cdf0e10cSrcweir // termAndJoinThread(pHighestThread);
1360cdf0e10cSrcweir termAndJoinThread(pAboveNormalThread);
1361cdf0e10cSrcweir termAndJoinThread(pNormalThread);
1362cdf0e10cSrcweir termAndJoinThread(pBelowNormalThread);
1363cdf0e10cSrcweir termAndJoinThread(pLowestThread);
1364cdf0e10cSrcweir
1365cdf0e10cSrcweir // sal_Int32 nValueHighest = 0;
1366cdf0e10cSrcweir // nValueHighest = pHighestThread->getValue();
1367cdf0e10cSrcweir
1368cdf0e10cSrcweir sal_Int32 nValueAboveNormal = 0;
1369cdf0e10cSrcweir nValueAboveNormal = pAboveNormalThread->getValue();
1370cdf0e10cSrcweir
1371cdf0e10cSrcweir sal_Int32 nValueNormal = 0;
1372cdf0e10cSrcweir nValueNormal = pNormalThread->getValue();
1373cdf0e10cSrcweir
1374cdf0e10cSrcweir sal_Int32 nValueBelowNormal = 0;
1375cdf0e10cSrcweir nValueBelowNormal = pBelowNormalThread->getValue();
1376cdf0e10cSrcweir
1377cdf0e10cSrcweir sal_Int32 nValueLowest = 0;
1378cdf0e10cSrcweir nValueLowest = pLowestThread->getValue();
1379cdf0e10cSrcweir
138044e8df1fSDamjan Jovanovic printf("After 5 tenth seconds\n");
138144e8df1fSDamjan Jovanovic // printf("nValue in Highest Prio Thread is %d\n",nValueHighest);
138244e8df1fSDamjan Jovanovic printf("nValue in AboveNormal Prio Thread is %d\n",nValueAboveNormal);
138344e8df1fSDamjan Jovanovic printf("nValue in Normal Prio Thread is %d\n",nValueNormal);
138444e8df1fSDamjan Jovanovic printf("nValue in BelowNormal Prio Thread is %d\n",nValueBelowNormal);
138544e8df1fSDamjan Jovanovic printf("nValue in Lowest Prio Thread is %d\n",nValueLowest);
1386cdf0e10cSrcweir
1387cdf0e10cSrcweir // delete pHighestThread;
1388cdf0e10cSrcweir delete pAboveNormalThread;
1389cdf0e10cSrcweir delete pNormalThread;
1390cdf0e10cSrcweir delete pBelowNormalThread;
1391cdf0e10cSrcweir delete pLowestThread;
1392cdf0e10cSrcweir
1393cdf0e10cSrcweir // LLA: this is not a save test, so we only check if all values not zero
139444e8df1fSDamjan Jovanovic // LLA: ASSERT_TRUE(// LLA: nValueHighest > nValueAboveNormal &&
1395cdf0e10cSrcweir // LLA: nValueAboveNormal > nValueNormal &&
1396cdf0e10cSrcweir // LLA: nValueNormal > nValueBelowNormal &&
1397cdf0e10cSrcweir // LLA: nValueBelowNormal > nValueLowest &&
1398cdf0e10cSrcweir // LLA: nValueLowest > 0
139944e8df1fSDamjan Jovanovic // LLA:) << // LLA: "SetPriority";
1400cdf0e10cSrcweir
1401cdf0e10cSrcweir // LLA: windows let starve threads with lower priority
1402cdf0e10cSrcweir #ifndef WNT
140344e8df1fSDamjan Jovanovic ASSERT_TRUE(/* nValueHighest > 0 && */
1404cdf0e10cSrcweir nValueAboveNormal > 0 &&
1405cdf0e10cSrcweir nValueNormal > 0 &&
1406cdf0e10cSrcweir nValueBelowNormal > 0 &&
140744e8df1fSDamjan Jovanovic nValueLowest > 0) << "SetPriority";
1408cdf0e10cSrcweir #endif
1409cdf0e10cSrcweir }
TEST_F(setPriority,setPriority_005)141044e8df1fSDamjan Jovanovic TEST_F(setPriority, setPriority_005)
1411cdf0e10cSrcweir {
1412cdf0e10cSrcweir // initial 5 threads with different priorities
1413cdf0e10cSrcweir // OAddThread *pHighestThread = new OAddThread();
1414cdf0e10cSrcweir // OAddThread *pAboveNormalThread = new OAddThread();
1415cdf0e10cSrcweir OAddThread *pNormalThread = new OAddThread();
1416cdf0e10cSrcweir OAddThread *pBelowNormalThread = new OAddThread();
1417cdf0e10cSrcweir OAddThread *pLowestThread = new OAddThread();
1418cdf0e10cSrcweir
1419cdf0e10cSrcweir //Create them and start running at the same time
1420cdf0e10cSrcweir // pHighestThread->createSuspended();
1421cdf0e10cSrcweir // pHighestThread->setPriority(osl_Thread_PriorityHighest);
1422cdf0e10cSrcweir
1423cdf0e10cSrcweir // pAboveNormalThread->createSuspended();
1424cdf0e10cSrcweir // pAboveNormalThread->setPriority(osl_Thread_PriorityAboveNormal);
1425cdf0e10cSrcweir
1426cdf0e10cSrcweir pNormalThread->createSuspended();
1427cdf0e10cSrcweir pNormalThread->setPriority(osl_Thread_PriorityNormal);
1428cdf0e10cSrcweir
1429cdf0e10cSrcweir pBelowNormalThread->createSuspended();
1430cdf0e10cSrcweir pBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
1431cdf0e10cSrcweir
1432cdf0e10cSrcweir pLowestThread->createSuspended();
1433cdf0e10cSrcweir pLowestThread->setPriority(osl_Thread_PriorityLowest);
1434cdf0e10cSrcweir
1435cdf0e10cSrcweir // pHighestThread->resume();
1436cdf0e10cSrcweir // pAboveNormalThread->resume();
1437cdf0e10cSrcweir pNormalThread->resume();
1438cdf0e10cSrcweir pBelowNormalThread->resume();
1439cdf0e10cSrcweir pLowestThread->resume();
1440cdf0e10cSrcweir
1441cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(5);
1442cdf0e10cSrcweir
1443cdf0e10cSrcweir // pHighestThread->suspend();
1444cdf0e10cSrcweir // pAboveNormalThread->suspend();
1445cdf0e10cSrcweir pNormalThread->suspend();
1446cdf0e10cSrcweir pBelowNormalThread->suspend();
1447cdf0e10cSrcweir pLowestThread->suspend();
1448cdf0e10cSrcweir
1449cdf0e10cSrcweir // termAndJoinThread(pHighestThread);
1450cdf0e10cSrcweir // termAndJoinThread(pAboveNormalThread);
1451cdf0e10cSrcweir termAndJoinThread(pNormalThread);
1452cdf0e10cSrcweir termAndJoinThread(pBelowNormalThread);
1453cdf0e10cSrcweir termAndJoinThread(pLowestThread);
1454cdf0e10cSrcweir
1455cdf0e10cSrcweir // sal_Int32 nValueHighest = 0;
1456cdf0e10cSrcweir // nValueHighest = pHighestThread->getValue();
1457cdf0e10cSrcweir
1458cdf0e10cSrcweir // sal_Int32 nValueAboveNormal = 0;
1459cdf0e10cSrcweir // nValueAboveNormal = pAboveNormalThread->getValue();
1460cdf0e10cSrcweir
1461cdf0e10cSrcweir sal_Int32 nValueNormal = 0;
1462cdf0e10cSrcweir nValueNormal = pNormalThread->getValue();
1463cdf0e10cSrcweir
1464cdf0e10cSrcweir sal_Int32 nValueBelowNormal = 0;
1465cdf0e10cSrcweir nValueBelowNormal = pBelowNormalThread->getValue();
1466cdf0e10cSrcweir
1467cdf0e10cSrcweir sal_Int32 nValueLowest = 0;
1468cdf0e10cSrcweir nValueLowest = pLowestThread->getValue();
1469cdf0e10cSrcweir
147044e8df1fSDamjan Jovanovic printf("After 5 tenth seconds\n");
147144e8df1fSDamjan Jovanovic // printf("nValue in Highest Prio Thread is %d\n",nValueHighest);
147244e8df1fSDamjan Jovanovic // printf("nValue in AboveNormal Prio Thread is %d\n",nValueAboveNormal);
147344e8df1fSDamjan Jovanovic printf("nValue in Normal Prio Thread is %d\n",nValueNormal);
147444e8df1fSDamjan Jovanovic printf("nValue in BelowNormal Prio Thread is %d\n",nValueBelowNormal);
147544e8df1fSDamjan Jovanovic printf("nValue in Lowest Prio Thread is %d\n",nValueLowest);
1476cdf0e10cSrcweir
1477cdf0e10cSrcweir // delete pHighestThread;
1478cdf0e10cSrcweir // delete pAboveNormalThread;
1479cdf0e10cSrcweir delete pNormalThread;
1480cdf0e10cSrcweir delete pBelowNormalThread;
1481cdf0e10cSrcweir delete pLowestThread;
1482cdf0e10cSrcweir
1483cdf0e10cSrcweir // LLA: this is not a save test, so we only check if all values not zero
148444e8df1fSDamjan Jovanovic // LLA: ASSERT_TRUE(// LLA: nValueHighest > nValueAboveNormal &&
1485cdf0e10cSrcweir // LLA: nValueAboveNormal > nValueNormal &&
1486cdf0e10cSrcweir // LLA: nValueNormal > nValueBelowNormal &&
1487cdf0e10cSrcweir // LLA: nValueBelowNormal > nValueLowest &&
1488cdf0e10cSrcweir // LLA: nValueLowest > 0
148944e8df1fSDamjan Jovanovic // LLA:) << // LLA: "SetPriority";
1490cdf0e10cSrcweir
1491cdf0e10cSrcweir // LLA: windows let starve threads with lower priority
1492cdf0e10cSrcweir #ifndef WNT
149344e8df1fSDamjan Jovanovic ASSERT_TRUE(/* nValueHighest > 0 && */
1494cdf0e10cSrcweir /* nValueAboveNormal > 0 && */
1495cdf0e10cSrcweir nValueNormal > 0 &&
1496cdf0e10cSrcweir nValueBelowNormal > 0 &&
149744e8df1fSDamjan Jovanovic nValueLowest > 0) << "SetPriority";
1498cdf0e10cSrcweir #endif
1499cdf0e10cSrcweir }
150044e8df1fSDamjan Jovanovic #endif // SOLARIS
1501cdf0e10cSrcweir
1502cdf0e10cSrcweir
1503cdf0e10cSrcweir /** Test of the osl::Thread::getPriority method
1504cdf0e10cSrcweir */
150544e8df1fSDamjan Jovanovic class getPriority : public ::testing::Test
1506cdf0e10cSrcweir {
1507cdf0e10cSrcweir public:
1508cdf0e10cSrcweir // initialise your test code values here.
SetUp()150944e8df1fSDamjan Jovanovic void SetUp()
1510cdf0e10cSrcweir {
1511cdf0e10cSrcweir }
1512cdf0e10cSrcweir
TearDown()151344e8df1fSDamjan Jovanovic void TearDown()
1514cdf0e10cSrcweir {
1515cdf0e10cSrcweir }
151644e8df1fSDamjan Jovanovic }; // class getPriority
1517cdf0e10cSrcweir
TEST_F(getPriority,getPriority_001)151844e8df1fSDamjan Jovanovic TEST_F(getPriority, getPriority_001)
1519cdf0e10cSrcweir {
1520cdf0e10cSrcweir OAddThread *pHighestThread = new OAddThread();
1521cdf0e10cSrcweir
1522cdf0e10cSrcweir //Create them and start running at the same time
1523cdf0e10cSrcweir pHighestThread->create();
1524cdf0e10cSrcweir pHighestThread->setPriority(osl_Thread_PriorityHighest);
1525cdf0e10cSrcweir
1526cdf0e10cSrcweir oslThreadPriority aPriority = pHighestThread->getPriority();
1527cdf0e10cSrcweir termAndJoinThread(pHighestThread);
1528cdf0e10cSrcweir delete pHighestThread;
1529cdf0e10cSrcweir
1530cdf0e10cSrcweir ThreadHelper::outputPriority(aPriority);
1531cdf0e10cSrcweir
1532cdf0e10cSrcweir // LLA: Priority settings may not work within some OS versions.
1533cdf0e10cSrcweir #if ( defined WNT ) || ( defined SOLARIS )
153444e8df1fSDamjan Jovanovic ASSERT_TRUE(aPriority == osl_Thread_PriorityHighest) << "getPriority";
1535cdf0e10cSrcweir #else
1536cdf0e10cSrcweir // LLA: Linux
1537cdf0e10cSrcweir // NO_PTHREAD_PRIORITY ???
153844e8df1fSDamjan Jovanovic ASSERT_TRUE(aPriority == osl_Thread_PriorityNormal) << "getPriority";
1539cdf0e10cSrcweir #endif
1540cdf0e10cSrcweir }
1541cdf0e10cSrcweir
TEST_F(getPriority,getPriority_002)154244e8df1fSDamjan Jovanovic TEST_F(getPriority, getPriority_002)
1543cdf0e10cSrcweir {
1544cdf0e10cSrcweir
1545cdf0e10cSrcweir }
1546cdf0e10cSrcweir
1547cdf0e10cSrcweir
154844e8df1fSDamjan Jovanovic class getIdentifier : public ::testing::Test
1549cdf0e10cSrcweir {
1550cdf0e10cSrcweir public:
1551cdf0e10cSrcweir // initialise your test code values here.
SetUp()155244e8df1fSDamjan Jovanovic void SetUp()
1553cdf0e10cSrcweir {
1554cdf0e10cSrcweir }
1555cdf0e10cSrcweir
TearDown()155644e8df1fSDamjan Jovanovic void TearDown()
1557cdf0e10cSrcweir {
1558cdf0e10cSrcweir }
155944e8df1fSDamjan Jovanovic }; // class getIdentifier
1560cdf0e10cSrcweir
1561cdf0e10cSrcweir // insert your test code here.
TEST_F(getIdentifier,getIdentifier_001)156244e8df1fSDamjan Jovanovic TEST_F(getIdentifier, getIdentifier_001)
1563cdf0e10cSrcweir {
1564cdf0e10cSrcweir
1565cdf0e10cSrcweir }
1566cdf0e10cSrcweir
TEST_F(getIdentifier,getIdentifier_002)156744e8df1fSDamjan Jovanovic TEST_F(getIdentifier, getIdentifier_002)
1568cdf0e10cSrcweir {
1569cdf0e10cSrcweir
1570cdf0e10cSrcweir }
1571cdf0e10cSrcweir
1572cdf0e10cSrcweir /** Test of the osl::Thread::getCurrentIdentifier method
1573cdf0e10cSrcweir */
157444e8df1fSDamjan Jovanovic class getCurrentIdentifier : public ::testing::Test
1575cdf0e10cSrcweir {
1576cdf0e10cSrcweir public:
1577cdf0e10cSrcweir // initialise your test code values here.
SetUp()157844e8df1fSDamjan Jovanovic void SetUp()
1579cdf0e10cSrcweir {
1580cdf0e10cSrcweir }
1581cdf0e10cSrcweir
TearDown()158244e8df1fSDamjan Jovanovic void TearDown()
1583cdf0e10cSrcweir {
1584cdf0e10cSrcweir }
158544e8df1fSDamjan Jovanovic }; // class getCurrentIdentifier
1586cdf0e10cSrcweir
1587cdf0e10cSrcweir // insert your test code here.
TEST_F(getCurrentIdentifier,getCurrentIdentifier_001)158844e8df1fSDamjan Jovanovic TEST_F(getCurrentIdentifier, getCurrentIdentifier_001)
1589cdf0e10cSrcweir {
1590cdf0e10cSrcweir oslThreadIdentifier oId;
1591cdf0e10cSrcweir OCountThread* pCountThread = new OCountThread;
1592cdf0e10cSrcweir //OCountThread* pCountThread2 = new OCountThread;
1593cdf0e10cSrcweir pCountThread->create();
1594cdf0e10cSrcweir //pCountThread2->create();
1595cdf0e10cSrcweir pCountThread->setWait(3);
1596cdf0e10cSrcweir oId = Thread::getCurrentIdentifier();
1597cdf0e10cSrcweir oslThreadIdentifier oIdChild = pCountThread->getIdentifier();
159844e8df1fSDamjan Jovanovic //printf("CurrentId is %ld, Child1 id is %ld, Child2 id is %ld\n",oId, oIdChild,pCountThread2->m_id );
1599cdf0e10cSrcweir termAndJoinThread(pCountThread);
1600cdf0e10cSrcweir delete pCountThread;
1601cdf0e10cSrcweir //termAndJoinThread(pCountThread2);
1602cdf0e10cSrcweir //delete pCountThread2;
1603cdf0e10cSrcweir
160444e8df1fSDamjan Jovanovic ASSERT_TRUE(oId != oIdChild) << "Get the identifier for the current active thread.";
1605cdf0e10cSrcweir
1606cdf0e10cSrcweir }
1607cdf0e10cSrcweir
TEST_F(getCurrentIdentifier,getCurrentIdentifier_002)160844e8df1fSDamjan Jovanovic TEST_F(getCurrentIdentifier, getCurrentIdentifier_002)
1609cdf0e10cSrcweir {
1610cdf0e10cSrcweir }
1611cdf0e10cSrcweir
1612cdf0e10cSrcweir
1613cdf0e10cSrcweir /** Test of the osl::Thread::wait method
1614cdf0e10cSrcweir */
161544e8df1fSDamjan Jovanovic class wait : public ::testing::Test
1616cdf0e10cSrcweir {
1617cdf0e10cSrcweir public:
1618cdf0e10cSrcweir // initialise your test code values here.
SetUp()161944e8df1fSDamjan Jovanovic void SetUp()
1620cdf0e10cSrcweir {
1621cdf0e10cSrcweir }
1622cdf0e10cSrcweir
TearDown()162344e8df1fSDamjan Jovanovic void TearDown()
1624cdf0e10cSrcweir {
1625cdf0e10cSrcweir }
162644e8df1fSDamjan Jovanovic }; // class wait
162744e8df1fSDamjan Jovanovic
1628cdf0e10cSrcweir
1629cdf0e10cSrcweir /** call wait in the run method
1630cdf0e10cSrcweir
1631cdf0e10cSrcweir ALGORITHM:
1632cdf0e10cSrcweir tested thread wait nWaitSec seconds, main thread sleep (2) seconds,
1633cdf0e10cSrcweir then terminate the tested thread, due to the fact that the thread do a sleep(1) + wait(5)
1634cdf0e10cSrcweir it's finish after 6 seconds.
1635cdf0e10cSrcweir */
TEST_F(wait,wait_001)163644e8df1fSDamjan Jovanovic TEST_F(wait, wait_001)
1637cdf0e10cSrcweir {
1638cdf0e10cSrcweir OCountThread *aCountThread = new OCountThread();
1639cdf0e10cSrcweir sal_Int32 nWaitSec = 5;
1640cdf0e10cSrcweir aCountThread->setWait(nWaitSec);
1641cdf0e10cSrcweir // thread runs at least 5 seconds.
1642cdf0e10cSrcweir sal_Bool bRes = aCountThread->create();
164344e8df1fSDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "Can't start thread!";
1644cdf0e10cSrcweir
1645cdf0e10cSrcweir //record the time when the running begin
1646cdf0e10cSrcweir StopWatch aStopWatch;
1647cdf0e10cSrcweir aStopWatch.start();
1648cdf0e10cSrcweir
1649cdf0e10cSrcweir // wait a little bit, to let the thread the time, to start
1650cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec( 4 );
1651cdf0e10cSrcweir
1652cdf0e10cSrcweir // if wait works,
1653cdf0e10cSrcweir // this function returns, after 4 sec. later
1654cdf0e10cSrcweir termAndJoinThread(aCountThread);
1655cdf0e10cSrcweir
1656cdf0e10cSrcweir // value should be one.
1657cdf0e10cSrcweir sal_Int32 nValue = aCountThread->getValue();
1658cdf0e10cSrcweir
1659cdf0e10cSrcweir aStopWatch.stop();
1660cdf0e10cSrcweir
1661cdf0e10cSrcweir // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
1662cdf0e10cSrcweir double nTenthSec = aStopWatch.getTenthSec();
1663cdf0e10cSrcweir double nSec = aStopWatch.getSeconds();
1664cdf0e10cSrcweir delete aCountThread;
166544e8df1fSDamjan Jovanovic printf("nTenthSec = %f \n", nTenthSec);
166644e8df1fSDamjan Jovanovic printf("nSec = %f \n", nSec);
166744e8df1fSDamjan Jovanovic printf("nValue = %d \n", nValue);
1668cdf0e10cSrcweir
166944e8df1fSDamjan Jovanovic ASSERT_TRUE(nTenthSec >= 5 && nValue == 1) << "Wait: Blocks the calling thread for the given number of time.";
1670cdf0e10cSrcweir
1671cdf0e10cSrcweir }
1672cdf0e10cSrcweir // LLA: wait_001 does the same.
1673cdf0e10cSrcweir // LLA: /** wait then terminate the thread
1674cdf0e10cSrcweir // LLA:
1675cdf0e10cSrcweir // LLA: ALGORITHM:
1676cdf0e10cSrcweir // LLA: wait nWaitSec seconds, and terminate when the wait does not finish
1677cdf0e10cSrcweir // LLA: Windows & UNX: thread terminates immediatlly
1678cdf0e10cSrcweir // LLA: */
167944e8df1fSDamjan Jovanovic // LLA: TEST_F(wait, wait_002)
1680cdf0e10cSrcweir // LLA: {
1681cdf0e10cSrcweir // LLA: OCountThread aThread;
1682cdf0e10cSrcweir // LLA:
1683cdf0e10cSrcweir // LLA: sal_Int32 nWaitSec = 3;
1684cdf0e10cSrcweir // LLA: aThread.setWait(nWaitSec);
1685cdf0e10cSrcweir // LLA:
1686cdf0e10cSrcweir // LLA: sal_Bool bRes = aThread.create();
168744e8df1fSDamjan Jovanovic // LLA: ASSERT_TRUE(bRes == sal_True) << "Can't start thread!";
1688cdf0e10cSrcweir // LLA:
1689cdf0e10cSrcweir // LLA: StopWatch aStopWatch;
1690cdf0e10cSrcweir // LLA: // TimeValue aTimeVal_befor;
1691cdf0e10cSrcweir // LLA: // osl_getSystemTime( &aTimeVal_befor );
1692cdf0e10cSrcweir // LLA: aStopWatch.start();
1693cdf0e10cSrcweir // LLA:
1694cdf0e10cSrcweir // LLA: termAndJoinThread(&aThread);
1695cdf0e10cSrcweir // LLA: sal_Int32 nValue = aThread.getValue();
1696cdf0e10cSrcweir // LLA:
1697cdf0e10cSrcweir // LLA: // TimeValue aTimeVal_after;
1698cdf0e10cSrcweir // LLA: // osl_getSystemTime( &aTimeVal_after );
1699cdf0e10cSrcweir // LLA: aStopWatch.stop();
1700cdf0e10cSrcweir // LLA: // sal_Int32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
1701cdf0e10cSrcweir // LLA: double nSec = aStopWatch.getSeconds();
170244e8df1fSDamjan Jovanovic // LLA: printf("sec=%f\n", nSec);
170344e8df1fSDamjan Jovanovic // LLA: printf("nValue = %d\n", nValue);
1704cdf0e10cSrcweir // LLA:
170544e8df1fSDamjan Jovanovic // LLA: ASSERT_TRUE(// LLA: nSec < 1 && nValue == 0
170644e8df1fSDamjan Jovanovic // LLA:) << // LLA: "Wait: Blocks the calling thread for the given number of time.";
1707cdf0e10cSrcweir // LLA: }
1708cdf0e10cSrcweir /** osl::Thread::yield method: can not design good test scenario to test up to now
1709cdf0e10cSrcweir */
171044e8df1fSDamjan Jovanovic class yield : public ::testing::Test
1711cdf0e10cSrcweir {
1712cdf0e10cSrcweir public:
SetUp()171344e8df1fSDamjan Jovanovic void SetUp()
1714cdf0e10cSrcweir {
1715cdf0e10cSrcweir }
1716cdf0e10cSrcweir
TearDown()171744e8df1fSDamjan Jovanovic void TearDown()
1718cdf0e10cSrcweir {
1719cdf0e10cSrcweir }
172044e8df1fSDamjan Jovanovic }; // class yield
1721cdf0e10cSrcweir
1722cdf0e10cSrcweir // insert your test code here.
TEST_F(yield,yield_001)172344e8df1fSDamjan Jovanovic TEST_F(yield, yield_001)
1724cdf0e10cSrcweir {
1725cdf0e10cSrcweir }
1726cdf0e10cSrcweir
1727cdf0e10cSrcweir /** Test of the osl::Thread::schedule method
1728cdf0e10cSrcweir */
172944e8df1fSDamjan Jovanovic class schedule : public ::testing::Test
1730cdf0e10cSrcweir {
1731cdf0e10cSrcweir public:
1732cdf0e10cSrcweir // initialise your test code values here.
SetUp()173344e8df1fSDamjan Jovanovic void SetUp()
1734cdf0e10cSrcweir {
1735cdf0e10cSrcweir }
1736cdf0e10cSrcweir
TearDown()173744e8df1fSDamjan Jovanovic void TearDown()
1738cdf0e10cSrcweir {
1739cdf0e10cSrcweir }
174044e8df1fSDamjan Jovanovic }; // class schedule
1741cdf0e10cSrcweir
1742cdf0e10cSrcweir /** The requested thread will get terminate the next time schedule() is called.
1743cdf0e10cSrcweir
1744cdf0e10cSrcweir Note: on UNX, if call suspend thread is not the to be suspended thread, the to be
1745cdf0e10cSrcweir suspended thread will get suspended the next time schedule() is called,
1746cdf0e10cSrcweir while on w32, it's nothing with schedule.
1747cdf0e10cSrcweir
1748cdf0e10cSrcweir check if suspend and terminate work well via schedule
1749cdf0e10cSrcweir */
TEST_F(schedule,schedule_001)175044e8df1fSDamjan Jovanovic TEST_F(schedule, schedule_001)
1751cdf0e10cSrcweir {
1752cdf0e10cSrcweir OAddThread* aThread = new OAddThread();
1753cdf0e10cSrcweir sal_Bool bRes = aThread->create();
175444e8df1fSDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "Can't start thread!";
1755cdf0e10cSrcweir
1756cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(2);
1757cdf0e10cSrcweir aThread->suspend();
1758cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(1);
1759cdf0e10cSrcweir sal_Int32 nValue = aThread->getValue();
1760cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(3);
1761cdf0e10cSrcweir sal_Int32 nLaterValue = aThread->getValue();
1762cdf0e10cSrcweir // resumeAndWaitThread(aThread);
176344e8df1fSDamjan Jovanovic printf(" value = %d\n", nValue);
176444e8df1fSDamjan Jovanovic printf("later value = %d\n", nLaterValue);
1765cdf0e10cSrcweir // if value and latervalue not equal, than the thread would not suspended
1766cdf0e10cSrcweir
176744e8df1fSDamjan Jovanovic ASSERT_TRUE(nLaterValue == nValue) << "Schedule: suspend works.";
1768cdf0e10cSrcweir
1769cdf0e10cSrcweir aThread->resume();
1770cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(2);
1771cdf0e10cSrcweir
1772cdf0e10cSrcweir aThread->terminate();
1773cdf0e10cSrcweir sal_Int32 nValue_term = aThread->getValue();
1774cdf0e10cSrcweir
1775cdf0e10cSrcweir aThread->join();
1776cdf0e10cSrcweir sal_Int32 nValue_join = aThread->getValue();
1777cdf0e10cSrcweir
177844e8df1fSDamjan Jovanovic printf("value after term = %d\n", nValue_term);
177944e8df1fSDamjan Jovanovic printf("value after join = %d\n", nValue_join);
1780cdf0e10cSrcweir
1781cdf0e10cSrcweir // nValue_term and nValue_join should be the same
1782cdf0e10cSrcweir // but should be differ from nValue
1783cdf0e10cSrcweir
1784cdf0e10cSrcweir delete aThread;
1785cdf0e10cSrcweir //check if thread really terminate after call terminate, if join immediatlly return
178644e8df1fSDamjan Jovanovic ASSERT_TRUE(nValue_join - nValue_term <= 1 && nValue_join - nValue_term >= 0) << "Schedule: Returns False if the thread should terminate.";
1787cdf0e10cSrcweir
1788cdf0e10cSrcweir }
1789cdf0e10cSrcweir
1790cdf0e10cSrcweir /** design a thread that has not call schedule in the workfunction--run method
1791cdf0e10cSrcweir */
TEST_F(schedule,schedule_002)179244e8df1fSDamjan Jovanovic TEST_F(schedule, schedule_002)
1793cdf0e10cSrcweir {
1794cdf0e10cSrcweir ONoScheduleThread aThread; // this thread runs 10 sec. (no schedule() used)
1795cdf0e10cSrcweir sal_Bool bRes = aThread.create();
179644e8df1fSDamjan Jovanovic ASSERT_TRUE(bRes == sal_True) << "Can't start thread!";
1797cdf0e10cSrcweir
1798cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(2);
1799cdf0e10cSrcweir aThread.suspend();
1800cdf0e10cSrcweir sal_Int32 nValue = aThread.getValue();
1801cdf0e10cSrcweir
1802cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(3);
1803cdf0e10cSrcweir sal_Int32 nLaterValue = aThread.getValue();
1804cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(5);
1805cdf0e10cSrcweir
1806cdf0e10cSrcweir resumeAndWaitThread(&aThread);
1807cdf0e10cSrcweir
180844e8df1fSDamjan Jovanovic printf(" value = %d\n", nValue);
180944e8df1fSDamjan Jovanovic printf("later value = %d\n", nLaterValue);
1810cdf0e10cSrcweir
1811cdf0e10cSrcweir //On windows, suspend works, so the values are same
1812cdf0e10cSrcweir #ifdef WNT
181344e8df1fSDamjan Jovanovic ASSERT_TRUE(nLaterValue == nValue) << "Schedule: don't schedule in thread run method, suspend works.";
1814cdf0e10cSrcweir #endif
1815cdf0e10cSrcweir
1816cdf0e10cSrcweir //On UNX, suspend does not work, so the difference of the values equals to sleep seconds number
1817cdf0e10cSrcweir #ifdef UNX
1818cdf0e10cSrcweir aThread.resume();
181944e8df1fSDamjan Jovanovic ASSERT_TRUE(nLaterValue > nValue) << "Schedule: don't schedule in thread run method, suspend does not work too.";
1820cdf0e10cSrcweir #endif
1821cdf0e10cSrcweir
1822cdf0e10cSrcweir // terminate will not work if no schedule in thread's work function
1823cdf0e10cSrcweir termAndJoinThread(&aThread);
1824cdf0e10cSrcweir sal_Int32 nValue_term = aThread.getValue();
1825cdf0e10cSrcweir
182644e8df1fSDamjan Jovanovic printf(" value term = %d\n", nValue_term);
1827cdf0e10cSrcweir
182844e8df1fSDamjan Jovanovic ASSERT_TRUE(nValue_term == 10) << "Schedule: don't schedule in thread run method, terminate failed.";
1829cdf0e10cSrcweir }
1830cdf0e10cSrcweir
1831cdf0e10cSrcweir } // namespace osl_Thread
1832cdf0e10cSrcweir
1833cdf0e10cSrcweir
1834cdf0e10cSrcweir // -----------------------------------------------------------------------------
1835cdf0e10cSrcweir // destroy function when the binding thread terminate
destroyCallback(void * data)1836cdf0e10cSrcweir void SAL_CALL destroyCallback(void * data)
1837cdf0e10cSrcweir {
183844e8df1fSDamjan Jovanovic printf("destroying local data %s\n", (char *) data);
1839cdf0e10cSrcweir delete[] (char *) data;
1840cdf0e10cSrcweir }
1841cdf0e10cSrcweir
1842cdf0e10cSrcweir static ThreadData myThreadData(destroyCallback);
1843cdf0e10cSrcweir
1844cdf0e10cSrcweir /**
1845cdf0e10cSrcweir */
1846cdf0e10cSrcweir class myKeyThread : public Thread
1847cdf0e10cSrcweir {
1848cdf0e10cSrcweir public:
1849cdf0e10cSrcweir // a public char member for test result checking
1850cdf0e10cSrcweir char m_Char_Test;
1851cdf0e10cSrcweir // for pass thread-special data to thread
myKeyThread(const char cData)1852cdf0e10cSrcweir myKeyThread(const char cData)
1853cdf0e10cSrcweir {
1854cdf0e10cSrcweir m_nData = cData;
1855cdf0e10cSrcweir }
1856cdf0e10cSrcweir private:
1857cdf0e10cSrcweir char m_nData;
1858cdf0e10cSrcweir
run()1859cdf0e10cSrcweir void SAL_CALL run()
1860cdf0e10cSrcweir {
1861cdf0e10cSrcweir char * pc = new char[2];
1862cdf0e10cSrcweir // strcpy(pc, &m_nData);
1863cdf0e10cSrcweir memcpy(pc, &m_nData, 1);
1864cdf0e10cSrcweir pc[1] = '\0';
1865cdf0e10cSrcweir
1866cdf0e10cSrcweir myThreadData.setData(pc);
1867cdf0e10cSrcweir char* pData = (char*)myThreadData.getData();
1868cdf0e10cSrcweir m_Char_Test = *pData;
1869cdf0e10cSrcweir // wait for long time to check the data value in main thread
1870cdf0e10cSrcweir ThreadHelper::thread_sleep_tenth_sec(3);
1871cdf0e10cSrcweir }
1872cdf0e10cSrcweir public:
~myKeyThread()1873cdf0e10cSrcweir ~myKeyThread()
1874cdf0e10cSrcweir {
1875cdf0e10cSrcweir if (isRunning())
1876cdf0e10cSrcweir {
187744e8df1fSDamjan Jovanovic printf("error: not terminated.\n");
1878cdf0e10cSrcweir }
1879cdf0e10cSrcweir }
1880cdf0e10cSrcweir };
1881cdf0e10cSrcweir
1882cdf0e10cSrcweir static ThreadData idData;
1883cdf0e10cSrcweir
1884cdf0e10cSrcweir class idThread: public Thread
1885cdf0e10cSrcweir {
1886cdf0e10cSrcweir public:
1887cdf0e10cSrcweir oslThreadIdentifier m_Id;
1888cdf0e10cSrcweir private:
run()1889cdf0e10cSrcweir void SAL_CALL run()
1890cdf0e10cSrcweir {
1891cdf0e10cSrcweir oslThreadIdentifier* pId = new oslThreadIdentifier;
1892cdf0e10cSrcweir *pId = getIdentifier();
1893cdf0e10cSrcweir idData.setData(pId);
1894cdf0e10cSrcweir oslThreadIdentifier* pIdData = (oslThreadIdentifier*)idData.getData();
189544e8df1fSDamjan Jovanovic //printf("Thread %d has Data %d\n", getIdentifier(), *pIdData);
1896cdf0e10cSrcweir m_Id = *pIdData;
1897cdf0e10cSrcweir delete pId;
1898cdf0e10cSrcweir }
1899cdf0e10cSrcweir
1900cdf0e10cSrcweir public:
~idThread()1901cdf0e10cSrcweir ~idThread()
1902cdf0e10cSrcweir {
1903cdf0e10cSrcweir if (isRunning())
1904cdf0e10cSrcweir {
190544e8df1fSDamjan Jovanovic printf("error: not terminated.\n");
1906cdf0e10cSrcweir }
1907cdf0e10cSrcweir }
1908cdf0e10cSrcweir };
1909cdf0e10cSrcweir
1910cdf0e10cSrcweir namespace osl_ThreadData
1911cdf0e10cSrcweir {
1912cdf0e10cSrcweir
191344e8df1fSDamjan Jovanovic class ctors : public ::testing::Test
1914cdf0e10cSrcweir {
1915cdf0e10cSrcweir public:
1916cdf0e10cSrcweir // initialise your test code values here.
SetUp()191744e8df1fSDamjan Jovanovic void SetUp()
1918cdf0e10cSrcweir {
1919cdf0e10cSrcweir }
1920cdf0e10cSrcweir
TearDown()192144e8df1fSDamjan Jovanovic void TearDown()
1922cdf0e10cSrcweir {
1923cdf0e10cSrcweir }
1924cdf0e10cSrcweir }; // class ctors
1925cdf0e10cSrcweir
1926cdf0e10cSrcweir
192744e8df1fSDamjan Jovanovic // insert your test code here.
TEST_F(ctors,ctor_001)192844e8df1fSDamjan Jovanovic TEST_F(ctors, ctor_001)
192944e8df1fSDamjan Jovanovic {
193044e8df1fSDamjan Jovanovic
193144e8df1fSDamjan Jovanovic }
193244e8df1fSDamjan Jovanovic
193344e8df1fSDamjan Jovanovic class setData : public ::testing::Test
1934cdf0e10cSrcweir {
1935cdf0e10cSrcweir public:
1936cdf0e10cSrcweir // initialise your test code values here.
SetUp()193744e8df1fSDamjan Jovanovic void SetUp()
1938cdf0e10cSrcweir {
1939cdf0e10cSrcweir }
1940cdf0e10cSrcweir
TearDown()194144e8df1fSDamjan Jovanovic void TearDown()
1942cdf0e10cSrcweir {
1943cdf0e10cSrcweir }
194444e8df1fSDamjan Jovanovic }; // class setData
1945cdf0e10cSrcweir
1946cdf0e10cSrcweir /** the same instance of the class can have different values in different threads
1947cdf0e10cSrcweir */
TEST_F(setData,setData_001)194844e8df1fSDamjan Jovanovic TEST_F(setData, setData_001)
1949cdf0e10cSrcweir {
1950cdf0e10cSrcweir idThread aThread1;
1951cdf0e10cSrcweir aThread1.create();
1952cdf0e10cSrcweir idThread aThread2;
1953cdf0e10cSrcweir aThread2.create();
1954cdf0e10cSrcweir
1955cdf0e10cSrcweir aThread1.join();
1956cdf0e10cSrcweir aThread2.join();
1957cdf0e10cSrcweir
1958cdf0e10cSrcweir oslThreadIdentifier aThreadId1 = aThread1.getIdentifier();
1959cdf0e10cSrcweir oslThreadIdentifier aThreadId2 = aThread2.getIdentifier();
1960cdf0e10cSrcweir
196144e8df1fSDamjan Jovanovic ASSERT_TRUE(aThread1.m_Id == aThreadId1 && aThread2.m_Id == aThreadId2) << "ThreadData setData: ";
1962cdf0e10cSrcweir
1963cdf0e10cSrcweir }
1964cdf0e10cSrcweir
TEST_F(setData,setData_002)196544e8df1fSDamjan Jovanovic TEST_F(setData, setData_002)
1966cdf0e10cSrcweir {
1967cdf0e10cSrcweir // at first, set the data a value
1968cdf0e10cSrcweir char* pc = new char[2];
1969cdf0e10cSrcweir char m_nData = 'm';
1970cdf0e10cSrcweir // LLA: this is a copy functions only and really only for \0 terminated strings
1971cdf0e10cSrcweir // m_nData is not a string, it's a character
1972cdf0e10cSrcweir // strcpy(pc, &m_nData);
1973cdf0e10cSrcweir memcpy(pc, &m_nData, 1);
1974cdf0e10cSrcweir pc[1] = '\0';
1975cdf0e10cSrcweir
1976cdf0e10cSrcweir myThreadData.setData(pc);
1977cdf0e10cSrcweir
1978cdf0e10cSrcweir myKeyThread aThread1('a');
1979cdf0e10cSrcweir aThread1.create();
1980cdf0e10cSrcweir myKeyThread aThread2('b');
1981cdf0e10cSrcweir aThread2.create();
1982cdf0e10cSrcweir // aThread1 and aThread2 should have not terminated yet, check current data, not 'a' 'b'
1983cdf0e10cSrcweir char* pChar = (char*)myThreadData.getData();
1984cdf0e10cSrcweir char aChar = *pChar;
1985cdf0e10cSrcweir
1986cdf0e10cSrcweir aThread1.join();
1987cdf0e10cSrcweir aThread2.join();
1988cdf0e10cSrcweir
1989cdf0e10cSrcweir // the saved thread data of aThread1 & aThread2, different
1990cdf0e10cSrcweir char cData1 = aThread1.m_Char_Test;
1991cdf0e10cSrcweir char cData2 = aThread2.m_Char_Test;
1992cdf0e10cSrcweir
199344e8df1fSDamjan Jovanovic ASSERT_TRUE(cData1 == 'a' && cData2 == 'b' && aChar == 'm') << "ThreadData setData: ";
1994cdf0e10cSrcweir
1995cdf0e10cSrcweir }
1996cdf0e10cSrcweir /** setData the second time, and then getData
1997cdf0e10cSrcweir */
TEST_F(setData,setData_003)199844e8df1fSDamjan Jovanovic TEST_F(setData, setData_003)
1999cdf0e10cSrcweir {
2000cdf0e10cSrcweir // at first, set the data a value
2001cdf0e10cSrcweir char* pc = new char[2];
2002cdf0e10cSrcweir char m_nData = 'm';
2003cdf0e10cSrcweir // strcpy(pc, &m_nData);
2004cdf0e10cSrcweir memcpy(pc, &m_nData, 1);
2005cdf0e10cSrcweir pc[1] = '\0';
2006cdf0e10cSrcweir myThreadData.setData(pc);
2007cdf0e10cSrcweir
2008cdf0e10cSrcweir myKeyThread aThread1('a');
2009cdf0e10cSrcweir aThread1.create();
2010cdf0e10cSrcweir myKeyThread aThread2('b');
2011cdf0e10cSrcweir aThread2.create();
2012cdf0e10cSrcweir // aThread1 and aThread2 should have not terminated yet
2013cdf0e10cSrcweir // setData the second time
2014cdf0e10cSrcweir char* pc2 = new char[2];
2015cdf0e10cSrcweir m_nData = 'o';
2016cdf0e10cSrcweir // strcpy(pc2, &m_nData);
2017cdf0e10cSrcweir memcpy(pc2, &m_nData, 1);
2018cdf0e10cSrcweir pc2[1] = '\0';
2019cdf0e10cSrcweir
2020cdf0e10cSrcweir myThreadData.setData(pc2);
2021cdf0e10cSrcweir char* pChar = (char*)myThreadData.getData();
2022cdf0e10cSrcweir char aChar = *pChar;
2023cdf0e10cSrcweir
2024cdf0e10cSrcweir aThread1.join();
2025cdf0e10cSrcweir aThread2.join();
2026cdf0e10cSrcweir
2027cdf0e10cSrcweir // the saved thread data of aThread1 & aThread2, different
2028cdf0e10cSrcweir char cData1 = aThread1.m_Char_Test;
2029cdf0e10cSrcweir char cData2 = aThread2.m_Char_Test;
2030cdf0e10cSrcweir
203144e8df1fSDamjan Jovanovic ASSERT_TRUE(cData1 == 'a' && cData2 == 'b' && aChar == 'o') << "ThreadData setData: ";
2032cdf0e10cSrcweir
2033cdf0e10cSrcweir }
2034cdf0e10cSrcweir
2035cdf0e10cSrcweir //sal_Bool buildTwoThreads(char)
2036cdf0e10cSrcweir
203744e8df1fSDamjan Jovanovic class getData : public ::testing::Test
2038cdf0e10cSrcweir {
2039cdf0e10cSrcweir public:
2040cdf0e10cSrcweir // initialise your test code values here.
SetUp()204144e8df1fSDamjan Jovanovic void SetUp()
2042cdf0e10cSrcweir {
2043cdf0e10cSrcweir }
2044cdf0e10cSrcweir
TearDown()204544e8df1fSDamjan Jovanovic void TearDown()
2046cdf0e10cSrcweir {
2047cdf0e10cSrcweir }
204844e8df1fSDamjan Jovanovic }; // class getData
2049cdf0e10cSrcweir
2050cdf0e10cSrcweir // After setData in child threads, get Data in the main thread, should be independent
TEST_F(getData,getData_001)205144e8df1fSDamjan Jovanovic TEST_F(getData, getData_001)
2052cdf0e10cSrcweir {
2053cdf0e10cSrcweir char* pc = new char[2];
2054cdf0e10cSrcweir char m_nData[] = "i";
2055cdf0e10cSrcweir strcpy(pc, m_nData);
205644e8df1fSDamjan Jovanovic printf("pc %s\n", pc);
2057cdf0e10cSrcweir myThreadData.setData(pc);
2058cdf0e10cSrcweir
2059cdf0e10cSrcweir myKeyThread aThread1('c');
2060cdf0e10cSrcweir aThread1.create();
2061cdf0e10cSrcweir myKeyThread aThread2('d');
2062cdf0e10cSrcweir aThread2.create();
2063cdf0e10cSrcweir
2064cdf0e10cSrcweir aThread1.join();
2065cdf0e10cSrcweir aThread2.join();
2066cdf0e10cSrcweir
2067cdf0e10cSrcweir char cData1 = aThread1.m_Char_Test;
2068cdf0e10cSrcweir char cData2 = aThread2.m_Char_Test;
2069cdf0e10cSrcweir
2070cdf0e10cSrcweir char* pChar = (char*)myThreadData.getData();
2071cdf0e10cSrcweir char aChar = *pChar;
2072cdf0e10cSrcweir
207344e8df1fSDamjan Jovanovic ASSERT_TRUE(cData1 == 'c' && cData2 == 'd' && aChar == 'i') << "ThreadData setData: ";
2074cdf0e10cSrcweir
2075cdf0e10cSrcweir }
2076cdf0e10cSrcweir
2077cdf0e10cSrcweir // setData then change the value in the address data pointer points,
2078cdf0e10cSrcweir // and then getData, should get the new value
TEST_F(getData,getData_002)207944e8df1fSDamjan Jovanovic TEST_F(getData, getData_002)
2080cdf0e10cSrcweir {
2081cdf0e10cSrcweir char* pc = new char[2];
2082cdf0e10cSrcweir char m_nData = 'i';
2083cdf0e10cSrcweir // strcpy(pc, &m_nData);
2084cdf0e10cSrcweir memcpy(pc, &m_nData, 1);
2085cdf0e10cSrcweir pc[1] = '\0';
2086cdf0e10cSrcweir // strncpy(pc, &m_nData, sizeof(char);
2087cdf0e10cSrcweir
208844e8df1fSDamjan Jovanovic printf("pc %s\n", pc);
2089cdf0e10cSrcweir myThreadData.setData(pc);
2090cdf0e10cSrcweir
2091cdf0e10cSrcweir myKeyThread aThread1('a');
2092cdf0e10cSrcweir aThread1.create();
2093cdf0e10cSrcweir myKeyThread aThread2('b');
2094cdf0e10cSrcweir aThread2.create();
2095cdf0e10cSrcweir
2096cdf0e10cSrcweir // change the value which pc points
2097cdf0e10cSrcweir char m_nData2 = 'j';
2098cdf0e10cSrcweir // strcpy(pc, &m_nData2);
2099cdf0e10cSrcweir memcpy(pc, &m_nData2, 1);
2100cdf0e10cSrcweir pc[1] = '\0';
2101cdf0e10cSrcweir
210244e8df1fSDamjan Jovanovic //printf("pc %s\n", pc);
2103cdf0e10cSrcweir void* pChar = myThreadData.getData();
2104cdf0e10cSrcweir char aChar = *(char*)pChar;
2105cdf0e10cSrcweir
2106cdf0e10cSrcweir aThread1.join();
2107cdf0e10cSrcweir aThread2.join();
2108cdf0e10cSrcweir
2109cdf0e10cSrcweir char cData1 = aThread1.m_Char_Test;
2110cdf0e10cSrcweir char cData2 = aThread2.m_Char_Test;
2111cdf0e10cSrcweir
211244e8df1fSDamjan Jovanovic ASSERT_TRUE(cData1 == 'a' && cData2 == 'b' && aChar == 'j') << "ThreadData setData: ";
2113cdf0e10cSrcweir
2114cdf0e10cSrcweir }
2115cdf0e10cSrcweir
2116cdf0e10cSrcweir
2117cdf0e10cSrcweir } // namespace osl_ThreadData
2118cdf0e10cSrcweir
main(int argc,char ** argv)211944e8df1fSDamjan Jovanovic int main(int argc, char **argv)
212044e8df1fSDamjan Jovanovic {
212144e8df1fSDamjan Jovanovic ::testing::InitGoogleTest(&argc, argv);
212244e8df1fSDamjan Jovanovic return RUN_ALL_TESTS();
212344e8df1fSDamjan Jovanovic }
2124