Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / core / inc / observablethread.hxx
blob3d10a7f3843960fa0f0d1da0330ec71f197f9365
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #ifndef INCLUDED_SW_SOURCE_CORE_INC_OBSERVABLETHREAD_HXX
20 #define INCLUDED_SW_SOURCE_CORE_INC_OBSERVABLETHREAD_HXX
22 #include <osl/thread.hxx>
23 #include <osl/interlck.h>
24 #include <salhelper/simplereferenceobject.hxx>
25 #include <memory>
27 class IFinishedThreadListener;
29 /** class for an observable thread
31 OD 2007-01-29 #i73788#
32 Note: A thread is ref-counted. Thus, an instance of a derived class has to
33 to be hold via a reference. The thread itself assures during its execution,
34 that the ref-count is increased. Its execution starts with its <run()> method
35 and ends with its <onTerminated()> method.
36 Note: A thread can be only observed by one or none thread observer in order
37 to notify, that the thread has finished its work.
39 class ObservableThread : public osl::Thread,
40 public salhelper::SimpleReferenceObject
42 public:
44 virtual ~ObservableThread() override;
46 void SetListener( std::weak_ptr< IFinishedThreadListener > const & pThreadListener,
47 const oslInterlockedCount nThreadID );
49 // resolve ambiguity
50 using SimpleReferenceObject::operator new;
51 using SimpleReferenceObject::operator delete;
53 protected:
55 ObservableThread();
57 /** intrinsic function of the thread
59 Important note:
60 Do not override this method again. Instead override <threadFunction()>.
61 Otherwise, it's not guaranteed, that its ref-count is increased
62 during the execution of the thread.
64 virtual void SAL_CALL run() override;
66 virtual void threadFunction() = 0;
68 /** method called, when thread has finished its work
70 Important note:
71 Do not override this method again. Instead override <threadFinished()>.
72 Otherwise, it's not guaranteed, that the ref-count is decreased at
73 the end of its execution and that the observer is notified, that
74 the thread has finished its work.
76 virtual void SAL_CALL onTerminated() override;
78 private:
80 oslInterlockedCount mnThreadID;
82 std::weak_ptr< IFinishedThreadListener > mpThreadListener;
85 #endif
87 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */