merge the formfield patch from ooo-build
[ooovba.git] / sd / source / ui / slidesorter / cache / SlsQueueProcessorThread.hxx
blob16bce1b2e3608f7b6521eaf5d52a83ae2f20aa29
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SlsQueueProcessorThread.hxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef SD_SLIDESORTER_QUEUE_PROCESSOR_THREAD_HXX
32 #define SD_SLIDESORTER_QUEUE_PROCESSOR_THREAD_HXX
34 #include "view/SlsPageObjectViewObjectContact.hxx"
35 #include <vcl/svapp.hxx>
36 #include <osl/thread.hxx>
38 namespace sd { namespace slidesorter { namespace view {
39 class SlideSorterView;
40 } } }
43 namespace sd { namespace slidesorter { namespace cache {
46 template <class Queue,
47 class RequestData,
48 class BitmapCache,
49 class BitmapFactory>
50 class QueueProcessorThread
51 : public ::osl::Thread
53 public:
54 QueueProcessorThread (
55 view::SlideSorterView& rView,
56 Queue& rQueue,
57 BitmapCache& rCache);
58 ~QueueProcessorThread (void);
60 /** Start the execution of a suspended thread. A thread is suspended
61 after Stop() is called or when the queue on which it operates is
62 empty. Calling Start() on a running thread is OK.
64 void Start (void);
66 /** Stop the thread by suspending it. To re-start its execution call
67 Start().
69 void Stop (void);
71 /** As we can not really terminate the rendering of a preview bitmap for
72 a request in midair this method acts more like a semaphor. It
73 returns only when it is save for the caller to delete the request.
74 For this to work it is important to remove the request from the
75 queue before calling this method.
77 void RemoveRequest (RequestData& rRequest);
79 /** Terminate the execution of the thread. When the thread is detached
80 it deletes itself. Otherwise the caller of this method may call
81 delete after this method returnes.
83 void SAL_CALL Terminate (void);
85 protected:
86 /** This virutal method is called (among others?) from the
87 inherited create method and acts as the main function of this
88 thread.
90 virtual void SAL_CALL run (void);
92 /** Called after the thread is terminated via the terminate
93 method. Used to kill the thread by calling delete on this.
94 */
95 virtual void SAL_CALL onTerminated (void);
97 private:
98 /** Flag that indicates wether the onTerminated method has been already
99 called. If so then a subsequent call to detach deletes the thread.
101 volatile bool mbIsTerminated;
103 volatile bool mbCanBeJoined;
105 /** This mutex is used to guard the queue processor. Be carefull not to
106 mix its use with that of the solar mutex.
108 ::osl::Mutex maMutex;
110 view::SlideSorterView& mrView;
111 Queue& mrQueue;
112 BitmapCache& mrCache;
114 void ProcessQueueEntry (void);
120 //===== QueueProcessorThread ================================================
122 template <class Queue, class Request, class Cache, class Factory>
123 QueueProcessorThread<Queue, Request, Cache, Factory>
124 ::QueueProcessorThread (
125 view::SlideSorterView& rView,
126 Queue& rQueue,
127 Cache& rCache)
128 : mbIsTerminated (false),
129 mbCanBeJoined (false),
130 mrView (rView),
131 mrQueue (rQueue),
132 mrCache (rCache)
134 OSL_TRACE("QueueProcessorThread::constructor %p", this);
135 create();
141 template <class Queue, class Request, class Cache, class Factory>
142 QueueProcessorThread<Queue, Request, Cache, Factory>
143 ::~QueueProcessorThread (void)
145 OSL_TRACE("QueueProcessorThread::destructor %p", this);
151 template <class Queue, class Request, class Cache, class Factory>
152 void SAL_CALL QueueProcessorThread<Queue, Request, Cache, Factory>::run (void)
154 OSL_TRACE("QueueProcessorThread::run(): running thread %p", this);
155 while ( ! mbIsTerminated)
157 OSL_TRACE("QueueProcessorThread::run(): still running thread %p: %d", this, mbIsTerminated?1:0);
158 if (mrQueue.IsEmpty())
160 // Sleep while the queue is empty.
161 OSL_TRACE("QueueProcessorThread::run(): suspending thread %p", this);
162 suspend();
163 OSL_TRACE("QueueProcessorThread::run(): running again thread %p", this);
166 else if (GetpApp()->AnyInput())
168 yield();
169 // When there is input waiting to be processed we wait a short
170 // time and try again.
171 TimeValue aTimeToWait;
172 aTimeToWait.Seconds = 0;
173 aTimeToWait.Nanosec = 50*1000*1000;
174 OSL_TRACE("QueueProcessorThread::run(): input pending: waiting %d nanoseconds",
175 aTimeToWait.Nanosec);
176 wait (aTimeToWait);
179 else
181 OSL_TRACE ("QueueProcessorThread::run(): Processing Query");
182 ProcessQueueEntry();
183 yield ();
186 OSL_TRACE("QueueProcessorThread::run(): exiting run %p", this);
192 template <class Queue, class Request, class Cache, class Factory>
193 void QueueProcessorThread<Queue, Request, Cache, Factory>
194 ::ProcessQueueEntry (void)
196 Request* pRequest = NULL;
197 int nPriorityClass;
198 bool bRequestIsValid = false;
202 OSL_TRACE ("QueueProcessorThread::ProcessQueueEntry(): testing for mbIsTerminated %p", this);
204 ::osl::MutexGuard aGuard (maMutex);
205 if (mbIsTerminated)
206 break;
207 if (mrQueue.IsEmpty())
208 break;
210 OSL_TRACE ("QueueProcessorThread::ProcessQueueEntry():acquiring mutex for bitmap creation %p", this);
211 ::vos::OGuard aSolarGuard (Application::GetSolarMutex());
212 ::osl::MutexGuard aGuard (maMutex);
213 if (mbIsTerminated)
214 break;
216 if (mrQueue.IsEmpty())
217 break;
219 OSL_TRACE ("QueueProcessorThread::ProcessQueueEntry(): have mutexes %p", this);
221 // Get the requeuest with the highest priority from the queue.
222 nPriorityClass = mrQueue.GetFrontPriorityClass();
223 pRequest = &mrQueue.GetFront();
224 mrQueue.PopFront();
225 bRequestIsValid = true;
228 OSL_TRACE ("QueueProcessorThread::ProcessQueueEntry():using request %p for creating bitmap", pRequest);
229 OSL_TRACE ("QueueProcessorThread::ProcessQueueEntry():processing request for page %d with priority class ",
230 pRequest->GetPage()->GetPageNum(), nPriorityClass);
233 // Create a new preview bitmap and store it in the cache.
234 if (mbIsTerminated)
235 break;
236 BitmapEx aBitmap (Factory::CreateBitmap (*pRequest, mrView));
237 if (mbIsTerminated)
238 break;
239 mrCache.SetBitmap (
240 pRequest->GetPage(),
241 aBitmap,
242 nPriorityClass==0);
244 catch (...)
246 OSL_TRACE ("QueueProcessorThread::ProcessQueueEntry(): caught exception; %p", this);
247 // We are rendering a preview and can do without if need
248 // be. So keep going if something happens that should
249 // not happen.
252 while (false);
258 template <class Queue,
259 class RequestData,
260 class BitmapCache,
261 class BitmapFactory>
262 void QueueProcessorThread<
263 Queue, RequestData, BitmapCache, BitmapFactory
264 >::Start (void)
266 OSL_TRACE ("QueueProcessorThread::Start %p", this);
267 resume ();
273 template <class Queue,
274 class RequestData,
275 class BitmapCache,
276 class BitmapFactory>
277 void QueueProcessorThread<
278 Queue, RequestData, BitmapCache, BitmapFactory
279 >::Stop (void)
281 OSL_TRACE ("QueueProcessorThread::Stop %p", this);
282 suspend();
288 template <class Queue,
289 class RequestData,
290 class BitmapCache,
291 class BitmapFactory>
292 void QueueProcessorThread<
293 Queue, RequestData, BitmapCache, BitmapFactory
294 >::RemoveRequest (RequestData& rRequest)
296 OSL_TRACE ("QueueProcessorThread::RemoveRequest %p", this);
297 // Do nothing else then wait for the mutex to be released.
298 ::osl::MutexGuard aGuard (mrQueue.GetMutex());
304 template <class Queue,
305 class RequestData,
306 class BitmapCache,
307 class BitmapFactory>
308 void QueueProcessorThread<
309 Queue, RequestData, BitmapCache, BitmapFactory
310 >::Terminate (void)
312 // ::vos::OGuard aSolarGuard (Application::GetSolarMutex());
313 OSL_TRACE("QueueProcessorThread::Terminate(): terminating thread %p", this);
314 ::osl::Thread::terminate ();
316 ::osl::MutexGuard aGuard (maMutex);
317 OSL_TRACE("QueueProcessorThread::Terminate(): starting to join %p, %d", this, mbIsTerminated?1:0);
318 mbIsTerminated = true;
320 Start();
326 /** This callback method is called when the run() method terminates.
328 template <class Queue,
329 class RequestData,
330 class BitmapCache,
331 class BitmapFactory>
332 void SAL_CALL QueueProcessorThread<
333 Queue, RequestData, BitmapCache, BitmapFactory
334 >::onTerminated (void)
336 ::osl::MutexGuard aGuard (maMutex);
337 mbCanBeJoined = true;
339 OSL_TRACE("QueueProcessorThread::Terminate():join %p, %d", this, mbIsTerminated?1:0);
340 while (true)
343 ::osl::MutexGuard aGuard (maMutex);
344 if (mbCanBeJoined)
345 break;
347 Start();
348 TimeValue aTimeToWait;
349 aTimeToWait.Seconds = 0;
350 aTimeToWait.Nanosec = 50*1000*1000;
351 OSL_TRACE("QueueProcessorThread::Terminate(): waiting for join");
352 wait (aTimeToWait);
354 if (mbCanBeJoined)
355 join();
356 else
357 OSL_TRACE("Can not join");
358 OSL_TRACE("QueueProcessorThread::Terminate():terminated thread %p :%d",
359 this, mbIsTerminated?1:0);
366 } } } // end of namespace ::sd::slidesorter::cache
368 #endif