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