xref: /AOO41X/main/sw/source/core/docnode/observablethread.cxx (revision e1d5bd03a6ea7ac2b26b792c9e2a94e9f347a43b)
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 #include "precompiled_sw.hxx"
24 #include <observablethread.hxx>
25 
26 #include <boost/shared_ptr.hpp>
27 
28 /** class for an observable thread
29 
30     OD 2007-01-29 #i73788#
31 
32     @author OD
33 */
34 ObservableThread::ObservableThread()
35     : mnRefCount( 0 ),
36       mnThreadID( 0 ),
37       mpThreadListener()
38 {
39 }
40 
41 ObservableThread::~ObservableThread()
42 {
43 }
44 
45 oslInterlockedCount ObservableThread::acquire()
46 {
47     return osl_incrementInterlockedCount( &mnRefCount );
48 }
49 
50 oslInterlockedCount ObservableThread::release()
51 {
52     oslInterlockedCount nCount( osl_decrementInterlockedCount( &mnRefCount ) );
53     if ( nCount == 0 )
54     {
55         delete this;
56         return nCount;
57     }
58 
59     return nCount;
60 }
61 
62 void ObservableThread::SetListener( boost::weak_ptr< IFinishedThreadListener > pThreadListener,
63                                     const oslInterlockedCount nThreadID )
64 {
65     mpThreadListener = pThreadListener;
66     mnThreadID = nThreadID;
67 }
68 
69 void SAL_CALL ObservableThread::run()
70 {
71     acquire();
72 
73     threadFunction();
74 }
75 
76 void SAL_CALL ObservableThread::onTerminated()
77 {
78     threadFinished();
79 
80     // notify observer
81     boost::shared_ptr< IFinishedThreadListener > pThreadListener = mpThreadListener.lock();
82     if ( pThreadListener )
83     {
84         pThreadListener->NotifyAboutFinishedThread( mnThreadID );
85     }
86 
87     release();
88 }
89 
90 void ObservableThread::threadFinished()
91 {
92     // empty default implementation
93 }
94