Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 : #include <observablethread.hxx>
20 :
21 : #include <boost/shared_ptr.hpp>
22 :
23 : /** class for an observable thread
24 :
25 : #i73788#
26 :
27 : @author OD
28 : */
29 0 : ObservableThread::ObservableThread()
30 : : mnRefCount( 0 ),
31 : mnThreadID( 0 ),
32 0 : mpThreadListener()
33 : {
34 0 : }
35 :
36 0 : ObservableThread::~ObservableThread()
37 : {
38 0 : }
39 :
40 0 : oslInterlockedCount ObservableThread::acquire()
41 : {
42 0 : return osl_atomic_increment( &mnRefCount );
43 : }
44 :
45 0 : oslInterlockedCount ObservableThread::release()
46 : {
47 0 : oslInterlockedCount nCount( osl_atomic_decrement( &mnRefCount ) );
48 0 : if ( nCount == 0 )
49 0 : delete this;
50 0 : return nCount;
51 : }
52 :
53 0 : void ObservableThread::SetListener( boost::weak_ptr< IFinishedThreadListener > pThreadListener,
54 : const oslInterlockedCount nThreadID )
55 : {
56 0 : mpThreadListener = pThreadListener;
57 0 : mnThreadID = nThreadID;
58 0 : }
59 :
60 0 : void SAL_CALL ObservableThread::run()
61 : {
62 0 : acquire();
63 :
64 0 : threadFunction();
65 0 : }
66 :
67 0 : void SAL_CALL ObservableThread::onTerminated()
68 : {
69 0 : threadFinished();
70 :
71 : // notify observer
72 0 : boost::shared_ptr< IFinishedThreadListener > pThreadListener = mpThreadListener.lock();
73 0 : if ( pThreadListener )
74 : {
75 0 : pThreadListener->NotifyAboutFinishedThread( mnThreadID );
76 : }
77 :
78 0 : release();
79 0 : }
80 :
81 0 : void ObservableThread::threadFinished()
82 : {
83 : // empty default implementation
84 0 : }
85 :
86 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|