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 #ifndef _OBSERVABLETHREAD_HXX
24 #define _OBSERVABLETHREAD_HXX
25 
26 #ifndef _OSL_THREAD_HXX_
27 #include <osl/thread.hxx>
28 #endif
29 #include <rtl/ref.hxx>
30 #include <osl/interlck.h>
31 
32 #include <boost/weak_ptr.hpp>
33 #include <ithreadlistenerowner.hxx>
34 
35 /** class for an observable thread
36 
37     OD 2007-01-29 #i73788#
38     Note: A thread is ref-counted. Thus, an instance of a derived class has to
39     to be hold via a reference. The thread itself assures during its execution,
40     that the ref-count is increased. Its execution starts with its <run()> method
41     and ends with its <onTerminated()> method.
42     Note: A thread can be only observed by one or none thread observer in order
43     to notify, that the thread has finished its work.
44 
45     @author OD
46 */
47 class ObservableThread : public osl::Thread,
48                          public rtl::IReference
49 {
50     public:
51 
52         virtual ~ObservableThread();
53 
54         void SetListener( boost::weak_ptr< IFinishedThreadListener > pThreadListener,
55                           const oslInterlockedCount nThreadID );
56 
57         // IReference
58         virtual oslInterlockedCount SAL_CALL acquire();
59         virtual oslInterlockedCount SAL_CALL release();
60 
61     protected:
62 
63         ObservableThread();
64 
65         /** intrinsic function of the thread
66 
67             Important note:
68             Do not override this method again. Instead override <threadFunction()>.
69             Otherwise, it's not guaranteed, that its ref-count is increased
70             during the execution of the thread.
71 
72             @author OD
73         */
74         virtual void SAL_CALL run();
75 
76         virtual void threadFunction() = 0;
77 
78         /** method called, when thread has finished its work
79 
80             Important note:
81             Do not override this method again. Instead override <threadFinished()>.
82             Otherwise, it's not guaranteed, that the ref-count is decreased at
83             the end of its execution and that the observer is notified, that
84             the thread has finished its work.
85 
86             @author OD
87         */
88         virtual void SAL_CALL onTerminated();
89 
90         virtual void threadFinished();
91 
92     private:
93 
94         oslInterlockedCount mnRefCount;
95 
96         oslInterlockedCount mnThreadID;
97 
98         boost::weak_ptr< IFinishedThreadListener > mpThreadListener;
99 
100 };
101 #endif
102