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