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