xref: /aoo42x/main/sal/qa/osl/thread/test_thread.cxx (revision 87d2adbc)
1*87d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*87d2adbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*87d2adbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*87d2adbcSAndrew Rist  * distributed with this work for additional information
6*87d2adbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*87d2adbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*87d2adbcSAndrew Rist  * "License"); you may not use this file except in compliance
9*87d2adbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*87d2adbcSAndrew Rist  *
11*87d2adbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*87d2adbcSAndrew Rist  *
13*87d2adbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*87d2adbcSAndrew Rist  * software distributed under the License is distributed on an
15*87d2adbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*87d2adbcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*87d2adbcSAndrew Rist  * specific language governing permissions and limitations
18*87d2adbcSAndrew Rist  * under the License.
19*87d2adbcSAndrew Rist  *
20*87d2adbcSAndrew Rist  *************************************************************/
21*87d2adbcSAndrew Rist 
22*87d2adbcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sal.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "sal/config.h"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "testshl/simpleheader.hxx"
30cdf0e10cSrcweir #include "osl/conditn.hxx"
31cdf0e10cSrcweir #include "osl/thread.hxx"
32cdf0e10cSrcweir #include "osl/time.h"
33cdf0e10cSrcweir #include "sal/types.h"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir namespace {
36cdf0e10cSrcweir 
37cdf0e10cSrcweir osl::Condition global;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir class Thread: public osl::Thread {
40cdf0e10cSrcweir public:
41cdf0e10cSrcweir     explicit Thread(osl::Condition & cond): m_cond(cond) {}
42cdf0e10cSrcweir 
43cdf0e10cSrcweir private:
44cdf0e10cSrcweir     virtual void SAL_CALL run() {}
45cdf0e10cSrcweir 
46cdf0e10cSrcweir     virtual void SAL_CALL onTerminated() {
47cdf0e10cSrcweir         m_cond.set();
48cdf0e10cSrcweir         CPPUNIT_ASSERT_EQUAL(osl::Condition::result_ok, global.wait());
49cdf0e10cSrcweir     }
50cdf0e10cSrcweir 
51cdf0e10cSrcweir     osl::Condition & m_cond;
52cdf0e10cSrcweir };
53cdf0e10cSrcweir 
54cdf0e10cSrcweir class Test: public CppUnit::TestFixture {
55cdf0e10cSrcweir public:
56cdf0e10cSrcweir     // Nondeterministic, best effort test that an osl::Thread can be destroyed
57cdf0e10cSrcweir     // (and in particular osl_destroyThread---indirectly---be called) before the
58cdf0e10cSrcweir     // corresponding thread has terminated:
59cdf0e10cSrcweir     void test() {
60cdf0e10cSrcweir         for (int i = 0; i < 50; ++i) {
61cdf0e10cSrcweir             osl::Condition c;
62cdf0e10cSrcweir             Thread t(c);
63cdf0e10cSrcweir             CPPUNIT_ASSERT(t.create());
64cdf0e10cSrcweir             // Make sure virtual Thread::run/onTerminated are called before
65cdf0e10cSrcweir             // Thread::~Thread:
66cdf0e10cSrcweir             CPPUNIT_ASSERT_EQUAL(osl::Condition::result_ok, c.wait());
67cdf0e10cSrcweir         }
68cdf0e10cSrcweir         // Make sure Thread::~Thread is called before each spawned thread
69cdf0e10cSrcweir         // terminates:
70cdf0e10cSrcweir         global.set();
71cdf0e10cSrcweir         // Give the spawned threads enough time to terminate:
72cdf0e10cSrcweir         TimeValue const twentySeconds = { 20, 0 };
73cdf0e10cSrcweir         osl::Thread::wait(twentySeconds);
74cdf0e10cSrcweir     }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     CPPUNIT_TEST_SUITE(Test);
77cdf0e10cSrcweir     CPPUNIT_TEST(test);
78cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_END();
79cdf0e10cSrcweir };
80cdf0e10cSrcweir 
81cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(Test, "alltests");
82cdf0e10cSrcweir 
83cdf0e10cSrcweir }
84cdf0e10cSrcweir 
85cdf0e10cSrcweir NOADDITIONAL;
86