1*1d2dbeb0SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*1d2dbeb0SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*1d2dbeb0SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*1d2dbeb0SAndrew Rist  * distributed with this work for additional information
6*1d2dbeb0SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*1d2dbeb0SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*1d2dbeb0SAndrew Rist  * "License"); you may not use this file except in compliance
9*1d2dbeb0SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*1d2dbeb0SAndrew Rist  *
11*1d2dbeb0SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*1d2dbeb0SAndrew Rist  *
13*1d2dbeb0SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*1d2dbeb0SAndrew Rist  * software distributed under the License is distributed on an
15*1d2dbeb0SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*1d2dbeb0SAndrew Rist  * KIND, either express or implied.  See the License for the
17*1d2dbeb0SAndrew Rist  * specific language governing permissions and limitations
18*1d2dbeb0SAndrew Rist  * under the License.
19*1d2dbeb0SAndrew Rist  *
20*1d2dbeb0SAndrew Rist  *************************************************************/
21*1d2dbeb0SAndrew Rist 
22*1d2dbeb0SAndrew Rist 
23cdf0e10cSrcweir #ifndef _OBSERVABLETHREAD_HXX
24cdf0e10cSrcweir #define _OBSERVABLETHREAD_HXX
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #ifndef _OSL_THREAD_HXX_
27cdf0e10cSrcweir #include <osl/thread.hxx>
28cdf0e10cSrcweir #endif
29cdf0e10cSrcweir #include <rtl/ref.hxx>
30cdf0e10cSrcweir #include <osl/interlck.h>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <boost/weak_ptr.hpp>
33cdf0e10cSrcweir #include <ithreadlistenerowner.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir /** class for an observable thread
36cdf0e10cSrcweir 
37cdf0e10cSrcweir     OD 2007-01-29 #i73788#
38cdf0e10cSrcweir     Note: A thread is ref-counted. Thus, an instance of a derived class has to
39cdf0e10cSrcweir     to be hold via a reference. The thread itself assures during its execution,
40cdf0e10cSrcweir     that the ref-count is increased. Its execution starts with its <run()> method
41cdf0e10cSrcweir     and ends with its <onTerminated()> method.
42cdf0e10cSrcweir     Note: A thread can be only observed by one or none thread observer in order
43cdf0e10cSrcweir     to notify, that the thread has finished its work.
44cdf0e10cSrcweir 
45cdf0e10cSrcweir     @author OD
46cdf0e10cSrcweir */
47cdf0e10cSrcweir class ObservableThread : public osl::Thread,
48cdf0e10cSrcweir                          public rtl::IReference
49cdf0e10cSrcweir {
50cdf0e10cSrcweir     public:
51cdf0e10cSrcweir 
52cdf0e10cSrcweir         virtual ~ObservableThread();
53cdf0e10cSrcweir 
54cdf0e10cSrcweir         void SetListener( boost::weak_ptr< IFinishedThreadListener > pThreadListener,
55cdf0e10cSrcweir                           const oslInterlockedCount nThreadID );
56cdf0e10cSrcweir 
57cdf0e10cSrcweir         // IReference
58cdf0e10cSrcweir         virtual oslInterlockedCount SAL_CALL acquire();
59cdf0e10cSrcweir         virtual oslInterlockedCount SAL_CALL release();
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     protected:
62cdf0e10cSrcweir 
63cdf0e10cSrcweir         ObservableThread();
64cdf0e10cSrcweir 
65cdf0e10cSrcweir         /** intrinsic function of the thread
66cdf0e10cSrcweir 
67cdf0e10cSrcweir             Important note:
68cdf0e10cSrcweir             Do not override this method again. Instead override <threadFunction()>.
69cdf0e10cSrcweir             Otherwise, it's not guaranteed, that its ref-count is increased
70cdf0e10cSrcweir             during the execution of the thread.
71cdf0e10cSrcweir 
72cdf0e10cSrcweir             @author OD
73cdf0e10cSrcweir         */
74cdf0e10cSrcweir         virtual void SAL_CALL run();
75cdf0e10cSrcweir 
76cdf0e10cSrcweir         virtual void threadFunction() = 0;
77cdf0e10cSrcweir 
78cdf0e10cSrcweir         /** method called, when thread has finished its work
79cdf0e10cSrcweir 
80cdf0e10cSrcweir             Important note:
81cdf0e10cSrcweir             Do not override this method again. Instead override <threadFinished()>.
82cdf0e10cSrcweir             Otherwise, it's not guaranteed, that the ref-count is decreased at
83cdf0e10cSrcweir             the end of its execution and that the observer is notified, that
84cdf0e10cSrcweir             the thread has finished its work.
85cdf0e10cSrcweir 
86cdf0e10cSrcweir             @author OD
87cdf0e10cSrcweir         */
88cdf0e10cSrcweir         virtual void SAL_CALL onTerminated();
89cdf0e10cSrcweir 
90cdf0e10cSrcweir         virtual void threadFinished();
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     private:
93cdf0e10cSrcweir 
94cdf0e10cSrcweir         oslInterlockedCount mnRefCount;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir         oslInterlockedCount mnThreadID;
97cdf0e10cSrcweir 
98cdf0e10cSrcweir         boost::weak_ptr< IFinishedThreadListener > mpThreadListener;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir };
101cdf0e10cSrcweir #endif
102