1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #ifndef _OBSERVABLETHREAD_HXX
28 #define _OBSERVABLETHREAD_HXX
29 
30 #ifndef _OSL_THREAD_HXX_
31 #include <osl/thread.hxx>
32 #endif
33 #include <rtl/ref.hxx>
34 #include <osl/interlck.h>
35 
36 #include <boost/weak_ptr.hpp>
37 #include <ithreadlistenerowner.hxx>
38 
39 /** class for an observable thread
40 
41     OD 2007-01-29 #i73788#
42     Note: A thread is ref-counted. Thus, an instance of a derived class has to
43     to be hold via a reference. The thread itself assures during its execution,
44     that the ref-count is increased. Its execution starts with its <run()> method
45     and ends with its <onTerminated()> method.
46     Note: A thread can be only observed by one or none thread observer in order
47     to notify, that the thread has finished its work.
48 
49     @author OD
50 */
51 class ObservableThread : public osl::Thread,
52                          public rtl::IReference
53 {
54     public:
55 
56         virtual ~ObservableThread();
57 
58         void SetListener( boost::weak_ptr< IFinishedThreadListener > pThreadListener,
59                           const oslInterlockedCount nThreadID );
60 
61         // IReference
62         virtual oslInterlockedCount SAL_CALL acquire();
63         virtual oslInterlockedCount SAL_CALL release();
64 
65     protected:
66 
67         ObservableThread();
68 
69         /** intrinsic function of the thread
70 
71             Important note:
72             Do not override this method again. Instead override <threadFunction()>.
73             Otherwise, it's not guaranteed, that its ref-count is increased
74             during the execution of the thread.
75 
76             @author OD
77         */
78         virtual void SAL_CALL run();
79 
80         virtual void threadFunction() = 0;
81 
82         /** method called, when thread has finished its work
83 
84             Important note:
85             Do not override this method again. Instead override <threadFinished()>.
86             Otherwise, it's not guaranteed, that the ref-count is decreased at
87             the end of its execution and that the observer is notified, that
88             the thread has finished its work.
89 
90             @author OD
91         */
92         virtual void SAL_CALL onTerminated();
93 
94         virtual void threadFinished();
95 
96     private:
97 
98         oslInterlockedCount mnRefCount;
99 
100         oslInterlockedCount mnThreadID;
101 
102         boost::weak_ptr< IFinishedThreadListener > mpThreadListener;
103 
104 };
105 #endif
106