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 _THREADMANAGER_HXX
20 : #define _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 : @author OD
47 : */
48 : class ThreadManager : public IThreadListenerOwner
49 : {
50 : public:
51 :
52 : explicit ThreadManager( ::com::sun::star::uno::Reference< ::com::sun::star::util::XJobManager >& rThreadJoiner );
53 : ~ThreadManager();
54 :
55 : // --> IThreadListenerOwner
56 : virtual boost::weak_ptr< IFinishedThreadListener > GetThreadListenerWeakRef();
57 : virtual void NotifyAboutFinishedThread( const oslInterlockedCount nThreadID );
58 :
59 : /** initialization
60 :
61 : IMPORTANT NOTE: Needs to be called directly after construction
62 :
63 : @author OD
64 : */
65 : void Init();
66 :
67 : /** add thread to the thread manager and taking ownership for the thread
68 :
69 : @author OD
70 :
71 : @return unique ID for added thread
72 : */
73 : oslInterlockedCount AddThread(
74 : const ::rtl::Reference< ObservableThread >& rThread );
75 :
76 : void RemoveThread( const oslInterlockedCount nThreadID,
77 : const bool bThreadFinished = false );
78 :
79 : DECL_LINK( TryToStartNewThread, void* );
80 :
81 : /** suspend the starting of threads
82 :
83 : Suspending the starting of further threads is sensible during the
84 : destruction of a Writer document.
85 :
86 : @author OD
87 : */
88 0 : inline void SuspendStartingOfThreads()
89 : {
90 0 : osl::MutexGuard aGuard(maMutex);
91 :
92 0 : mbStartingOfThreadsSuspended = true;
93 0 : }
94 :
95 : /** continues the starting of threads after it has been suspended
96 :
97 : @author OD
98 : */
99 : void ResumeStartingOfThreads();
100 :
101 0 : inline bool StartingOfThreadsSuspended()
102 : {
103 0 : osl::MutexGuard aGuard(maMutex);
104 :
105 0 : return mbStartingOfThreadsSuspended;
106 : }
107 :
108 0 : struct tThreadData
109 : {
110 : oslInterlockedCount nThreadID;
111 : ::rtl::Reference< ObservableThread > pThread;
112 : com::sun::star::uno::Reference< com::sun::star::util::XCancellable > aJob;
113 :
114 0 : tThreadData()
115 : : nThreadID( 0 ),
116 : pThread( 0 ),
117 0 : aJob()
118 0 : {}
119 : };
120 :
121 : private:
122 :
123 : static const std::deque< tThreadData >::size_type mnStartedSize;
124 :
125 : osl::Mutex maMutex;
126 :
127 : ::com::sun::star::uno::WeakReference< ::com::sun::star::util::XJobManager > mrThreadJoiner;
128 :
129 : boost::shared_ptr< ThreadListener > mpThreadListener;
130 :
131 : oslInterlockedCount mnThreadIDCounter;
132 :
133 : std::deque< tThreadData > maWaitingForStartThreads;
134 : std::deque< tThreadData > maStartedThreads;
135 :
136 : Timer maStartNewThreadTimer;
137 :
138 : bool mbStartingOfThreadsSuspended;
139 :
140 : struct ThreadPred
141 : {
142 : oslInterlockedCount mnThreadID;
143 0 : explicit ThreadPred( oslInterlockedCount nThreadID )
144 0 : : mnThreadID( nThreadID )
145 0 : {}
146 :
147 0 : bool operator() ( const tThreadData& rThreadData ) const
148 : {
149 0 : return rThreadData.nThreadID == mnThreadID;
150 : }
151 : };
152 :
153 :
154 0 : inline oslInterlockedCount RetrieveNewThreadID()
155 : {
156 0 : return osl_atomic_increment( &mnThreadIDCounter );
157 : }
158 :
159 : bool StartWaitingThread();
160 :
161 : bool StartThread( const tThreadData& aThreadData );
162 : };
163 : #endif
164 :
165 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|