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_THREADMANAGER_HXX
20 : #define INCLUDED_SW_SOURCE_CORE_INC_THREADMANAGER_HXX
21 :
22 : #include <ithreadlistenerowner.hxx>
23 : #include <vcl/timer.hxx>
24 : #include <osl/mutex.hxx>
25 : #include <osl/interlck.h>
26 : #include <rtl/ref.hxx>
27 :
28 : #include <deque>
29 : #include <list>
30 : #include <cppuhelper/weak.hxx>
31 : #include "com/sun/star/util/XJobManager.hpp"
32 : #include <observablethread.hxx>
33 : #include <cancellablejob.hxx>
34 : #include <threadlistener.hxx>
35 :
36 : #include <boost/shared_ptr.hpp>
37 : #include <boost/weak_ptr.hpp>
38 :
39 : /** class to manage threads
40 :
41 : OD 2007-01-29 #i73788#
42 : An instance of this class takes care of the starting of threads.
43 : It assures that not more than <mnStartedSize> threads
44 : are started.
45 : */
46 : class ThreadManager : public IThreadListenerOwner
47 : {
48 : public:
49 :
50 : explicit ThreadManager( ::com::sun::star::uno::Reference< ::com::sun::star::util::XJobManager >& rThreadJoiner );
51 : virtual ~ThreadManager();
52 :
53 : // --> IThreadListenerOwner
54 : virtual boost::weak_ptr< IFinishedThreadListener > GetThreadListenerWeakRef() SAL_OVERRIDE;
55 : virtual void NotifyAboutFinishedThread( const oslInterlockedCount nThreadID ) SAL_OVERRIDE;
56 :
57 : /** initialization
58 :
59 : IMPORTANT NOTE: Needs to be called directly after construction
60 : */
61 : void Init();
62 :
63 : /** add thread to the thread manager and taking ownership for the thread
64 :
65 : @return unique ID for added thread
66 : */
67 : oslInterlockedCount AddThread(
68 : const ::rtl::Reference< ObservableThread >& rThread );
69 :
70 : void RemoveThread( const oslInterlockedCount nThreadID,
71 : const bool bThreadFinished = false );
72 :
73 : DECL_LINK( TryToStartNewThread, void* );
74 :
75 : /** suspend the starting of threads
76 :
77 : Suspending the starting of further threads is sensible during the
78 : destruction of a Writer document.
79 : */
80 0 : inline void SuspendStartingOfThreads()
81 : {
82 0 : osl::MutexGuard aGuard(maMutex);
83 :
84 0 : mbStartingOfThreadsSuspended = true;
85 0 : }
86 :
87 : /** continues the starting of threads after it has been suspended
88 : */
89 : void ResumeStartingOfThreads();
90 :
91 0 : inline bool StartingOfThreadsSuspended()
92 : {
93 0 : osl::MutexGuard aGuard(maMutex);
94 :
95 0 : return mbStartingOfThreadsSuspended;
96 : }
97 :
98 0 : struct tThreadData
99 : {
100 : oslInterlockedCount nThreadID;
101 : ::rtl::Reference< ObservableThread > pThread;
102 : com::sun::star::uno::Reference< com::sun::star::util::XCancellable > aJob;
103 :
104 0 : tThreadData()
105 : : nThreadID( 0 ),
106 : pThread( 0 ),
107 0 : aJob()
108 0 : {}
109 : };
110 :
111 : private:
112 :
113 : static const std::deque< tThreadData >::size_type mnStartedSize;
114 :
115 : osl::Mutex maMutex;
116 :
117 : ::com::sun::star::uno::WeakReference< ::com::sun::star::util::XJobManager > mrThreadJoiner;
118 :
119 : boost::shared_ptr< ThreadListener > mpThreadListener;
120 :
121 : oslInterlockedCount mnThreadIDCounter;
122 :
123 : std::deque< tThreadData > maWaitingForStartThreads;
124 : std::deque< tThreadData > maStartedThreads;
125 :
126 : Timer maStartNewThreadTimer;
127 :
128 : bool mbStartingOfThreadsSuspended;
129 :
130 : struct ThreadPred
131 : {
132 : oslInterlockedCount mnThreadID;
133 0 : explicit ThreadPred( oslInterlockedCount nThreadID )
134 0 : : mnThreadID( nThreadID )
135 0 : {}
136 :
137 0 : bool operator() ( const tThreadData& rThreadData ) const
138 : {
139 0 : return rThreadData.nThreadID == mnThreadID;
140 : }
141 : };
142 :
143 0 : inline oslInterlockedCount RetrieveNewThreadID()
144 : {
145 0 : return osl_atomic_increment( &mnThreadIDCounter );
146 : }
147 :
148 : bool StartWaitingThread();
149 :
150 : bool StartThread( const tThreadData& aThreadData );
151 : };
152 : #endif
153 :
154 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|