1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SlsQueueProcessorThread.hxx,v $
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
;
43 namespace sd
{ namespace slidesorter
{ namespace cache
{
46 template <class Queue
,
50 class QueueProcessorThread
51 : public ::osl::Thread
54 QueueProcessorThread (
55 view::SlideSorterView
& rView
,
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.
66 /** Stop the thread by suspending it. To re-start its execution call
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);
86 /** This virutal method is called (among others?) from the
87 inherited create method and acts as the main function of this
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.
95 virtual void SAL_CALL
onTerminated (void);
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
;
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
,
128 : mbIsTerminated (false),
129 mbCanBeJoined (false),
134 OSL_TRACE("QueueProcessorThread::constructor %p", this);
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);
163 OSL_TRACE("QueueProcessorThread::run(): running again thread %p", this);
166 else if (GetpApp()->AnyInput())
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
);
181 OSL_TRACE ("QueueProcessorThread::run(): Processing Query");
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
;
198 bool bRequestIsValid
= false;
202 OSL_TRACE ("QueueProcessorThread::ProcessQueueEntry(): testing for mbIsTerminated %p", this);
204 ::osl::MutexGuard
aGuard (maMutex
);
207 if (mrQueue
.IsEmpty())
210 OSL_TRACE ("QueueProcessorThread::ProcessQueueEntry():acquiring mutex for bitmap creation %p", this);
211 ::vos::OGuard
aSolarGuard (Application::GetSolarMutex());
212 ::osl::MutexGuard
aGuard (maMutex
);
216 if (mrQueue
.IsEmpty())
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();
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.
236 BitmapEx
aBitmap (Factory::CreateBitmap (*pRequest
, mrView
));
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
258 template <class Queue
,
262 void QueueProcessorThread
<
263 Queue
, RequestData
, BitmapCache
, BitmapFactory
266 OSL_TRACE ("QueueProcessorThread::Start %p", this);
273 template <class Queue
,
277 void QueueProcessorThread
<
278 Queue
, RequestData
, BitmapCache
, BitmapFactory
281 OSL_TRACE ("QueueProcessorThread::Stop %p", this);
288 template <class Queue
,
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
,
308 void QueueProcessorThread
<
309 Queue
, RequestData
, BitmapCache
, BitmapFactory
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;
326 /** This callback method is called when the run() method terminates.
328 template <class Queue
,
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);
343 ::osl::MutexGuard aGuard (maMutex);
348 TimeValue aTimeToWait;
349 aTimeToWait.Seconds = 0;
350 aTimeToWait.Nanosec = 50*1000*1000;
351 OSL_TRACE("QueueProcessorThread::Terminate(): waiting for join");
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