1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: rtl_doublelocking.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sal.hxx"
33 //------------------------------------------------------------------------
35 //------------------------------------------------------------------------
36 #include <sal/types.h>
38 #ifndef _RTL_USTRING_HXX_
39 #include <rtl/string.hxx>
42 #ifndef _OSL_THREAD_HXX
43 #include <osl/thread.hxx>
47 #include <rtl/instance.hxx>
49 #include <cppunit/simpleheader.hxx>
51 // -----------------------------------------------------------------------------
52 #define CONST_TEST_STRING "gregorian"
55 struct Gregorian
: public rtl::StaticWithInit
<const ::rtl::OUString
, Gregorian
> {
56 const ::rtl::OUString
operator () () {
57 return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( CONST_TEST_STRING
));
62 inline void printOUString( ::rtl::OUString
const & _suStr
)
66 t_print( "OUString: " );
67 aString
= ::rtl::OUStringToOString( _suStr
, RTL_TEXTENCODING_ASCII_US
);
68 t_print( "'%s'\n", aString
.getStr( ) );
71 // -----------------------------------------------------------------------------
72 namespace ThreadHelper
77 // } eSleepVerboseMode;
79 void thread_sleep_tenth_sec(sal_Int32 _nTenthSec
/*, eSleepVerboseMode nVerbose = VERBOSE*/)
81 // if (nVerbose == VERBOSE)
83 // t_print("wait %d tenth seconds. ", _nTenthSec );
87 Sleep(_nTenthSec
* 100 );
89 #if ( defined UNX ) || ( defined OS2 ) //Unix
91 nTV
.Seconds
= static_cast<sal_uInt32
>( _nTenthSec
/10 );
92 nTV
.Nanosec
= ( (_nTenthSec
%10 ) * 100000000 );
95 // if (nVerbose == VERBOSE)
102 // -----------------------------------------------------------------------------
104 /** Simple thread for testing Thread-create.
105 * Just add 1 of value 0, and after running, result is 1.
107 class OGetThread
: public osl::Thread
112 rtl::OUString m_sConstStr
;
118 m_sConstStr
= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( CONST_TEST_STRING
));
121 sal_Int32
getOK() { return m_nOK
; }
122 sal_Int32
getFails() {return m_nFails
;}
126 /** guarded value which initialized 0
134 rtl::OUString aStr
= Gregorian::get();
135 // printOUString(aStr);
136 // printOUString(m_sConstStr);
137 if (aStr
.equals(m_sConstStr
))
145 ThreadHelper::thread_sleep_tenth_sec(1);
151 virtual void SAL_CALL
suspend()
153 ::osl::Thread::suspend();
160 t_print("error: not terminated.\n");
165 // -----------------------------------------------------------------------------
166 namespace rtl_DoubleLocking
169 /** Test of the osl::Thread::create method
172 class getValue
: public CppUnit::TestFixture
176 // initialise your test code values here.
188 rtl::OUString aStr
= Gregorian::get();
191 CPPUNIT_ASSERT_MESSAGE(
192 "Gregorian::get() failed, wrong value expected.",
193 aStr
.getLength() != 0
200 Here the function should show, that 2 different threads,
201 which only increase a value, should run at the same time with same prio.
202 The test fails, if the difference between the two values is more than 5%
203 but IMHO this isn't a failure, it's only a feature of the OS.
208 // initial 5 threads with different priorities
209 OGetThread
* pThread
= new OGetThread();
210 OGetThread
* p2Thread
= new OGetThread();
212 //Create them and start running at the same time
216 ThreadHelper::thread_sleep_tenth_sec(50);
218 pThread
->terminate();
219 p2Thread
->terminate();
221 sal_Int32 nValueOK
= 0;
222 nValueOK
= pThread
->getOK();
224 sal_Int32 nValueOK2
= 0;
225 nValueOK2
= p2Thread
->getOK();
227 t_print("Value in Thread #1 is %d\n", nValueOK
);
228 t_print("Value in Thread #2 is %d\n", nValueOK2
);
230 sal_Int32 nValueFails
= 0;
231 nValueFails
= pThread
->getFails();
233 sal_Int32 nValueFails2
= 0;
234 nValueFails2
= p2Thread
->getFails();
236 t_print("Fails in Thread #1 is %d\n", nValueFails
);
237 t_print("Fails in Thread #2 is %d\n", nValueFails2
);
239 // ThreadHelper::thread_sleep_tenth_sec(1);
246 CPPUNIT_ASSERT_MESSAGE(
247 "getValue() failed, wrong value expected.",
248 nValueOK
!= 0 && nValueFails
== 0 && nValueFails2
== 0
252 CPPUNIT_TEST_SUITE(getValue
);
253 CPPUNIT_TEST(getValue_001
);
254 CPPUNIT_TEST(getValue_002
);
255 CPPUNIT_TEST_SUITE_END();
257 // -----------------------------------------------------------------------------
258 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_DoubleLocking::getValue
, "rtl_DoubleLocking");
259 } // namespace rtl_DoubleLocking
261 // this macro creates an empty function, which will called by the RegisterAllFunctions()
262 // to let the user the possibility to also register some functions by hand.