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 :
20 :
21 : #include "SlsRequestQueue.hxx"
22 :
23 : #include <set>
24 :
25 : namespace sd { namespace slidesorter { namespace cache {
26 :
27 : /** This class extends the actual request data with additional information
28 : that is used by the priority queues.
29 : */
30 : class Request
31 : {
32 : public:
33 0 : Request (
34 : CacheKey aKey, sal_Int32 nPriority, RequestPriorityClass eClass)
35 0 : : maKey(aKey), mnPriorityInClass(nPriority), meClass(eClass)
36 0 : {}
37 : /** Sort requests according to priority classes and then to priorities.
38 : */
39 : class Comparator { public:
40 0 : bool operator() (const Request& rRequest1, const Request& rRequest2)
41 : {
42 0 : if (rRequest1.meClass == rRequest2.meClass)
43 : {
44 0 : if (rRequest1.mnPriorityInClass == rRequest2.mnPriorityInClass)
45 : {
46 0 : return rRequest1.maKey < rRequest2.maKey;
47 : }
48 0 : return rRequest1.mnPriorityInClass > rRequest2.mnPriorityInClass;
49 : }
50 0 : return rRequest1.meClass < rRequest2.meClass;
51 : }
52 : };
53 : /** Request data is compared arbitrarily by their addresses in memory.
54 : This just establishes an order so that the STL containers are happy.
55 : The order is not semantically interpreted.
56 : */
57 : class DataComparator
58 : {
59 : public:
60 0 : DataComparator (const CacheKey aKey)
61 0 : : maKey(aKey)
62 : {
63 0 : }
64 0 : bool operator() (const Request& rRequest) const
65 : {
66 0 : return maKey == rRequest.maKey;
67 : }
68 : private:
69 : const CacheKey maKey;
70 : };
71 :
72 : CacheKey maKey;
73 : sal_Int32 mnPriorityInClass;
74 : RequestPriorityClass meClass;
75 : };
76 :
77 :
78 0 : class RequestQueue::Container
79 : : public ::std::set<
80 : Request,
81 : Request::Comparator>
82 : {
83 : };
84 :
85 :
86 :
87 :
88 : //===== GenericRequestQueue =================================================
89 :
90 :
91 0 : RequestQueue::RequestQueue (const SharedCacheContext& rpCacheContext)
92 : : maMutex(),
93 0 : mpRequestQueue(new Container()),
94 : mpCacheContext(rpCacheContext),
95 : mnMinimumPriority(0),
96 0 : mnMaximumPriority(1)
97 : {
98 0 : }
99 :
100 :
101 :
102 :
103 0 : RequestQueue::~RequestQueue (void)
104 : {
105 0 : Clear();
106 0 : }
107 :
108 :
109 :
110 :
111 0 : void RequestQueue::AddRequest (
112 : CacheKey aKey,
113 : RequestPriorityClass eRequestClass,
114 : bool /*bInsertWithHighestPriority*/)
115 : {
116 0 : ::osl::MutexGuard aGuard (maMutex);
117 :
118 : OSL_ASSERT(eRequestClass>=MIN__CLASS && eRequestClass<=MAX__CLASS);
119 :
120 : // If the request is already a member of the queue then remove it so
121 : // that the following insertion will use the new prioritization.
122 : #if OSL_DEBUG_LEVEL >=2
123 : bool bRemoved =
124 : #endif
125 0 : RemoveRequest(aKey);
126 :
127 : // The priority of the request inside its priority class is defined by
128 : // the page number. This ensures a strict top-to-bottom, left-to-right
129 : // order.
130 0 : sal_Int32 nPriority (mpCacheContext->GetPriority(aKey));
131 0 : Request aRequest (aKey, nPriority, eRequestClass);
132 :
133 0 : std::pair<Container::iterator,bool> ret = mpRequestQueue->insert(aRequest);
134 0 : bool bInserted = ret.second == true;
135 :
136 0 : if (bInserted)
137 : {
138 0 : SdrPage *pPage = const_cast<SdrPage*>(aRequest.maKey);
139 0 : pPage->AddPageUser(*this);
140 : }
141 :
142 0 : SSCD_SET_REQUEST_CLASS(aKey,eRequestClass);
143 :
144 : #if OSL_DEBUG_LEVEL >=2
145 : SAL_INFO("sd.sls", OSL_THIS_FUNC << ": " << (bRemoved?"replaced":"added")
146 : << " request for page " << ((aKey->GetPageNum()-1)/2)
147 : << " with priority class " << static_cast<int>(eRequestClass));
148 : #endif
149 0 : }
150 :
151 0 : void RequestQueue::PageInDestruction(const SdrPage& rPage)
152 : {
153 : //remove any requests pending for this page which is going away now
154 0 : RemoveRequest(&rPage);
155 0 : }
156 :
157 0 : bool RequestQueue::RemoveRequest (
158 : CacheKey aKey)
159 : {
160 0 : bool bRequestWasRemoved (false);
161 0 : ::osl::MutexGuard aGuard (maMutex);
162 :
163 : while(true)
164 : {
165 : Container::const_iterator aRequestIterator = ::std::find_if (
166 0 : mpRequestQueue->begin(),
167 0 : mpRequestQueue->end(),
168 0 : Request::DataComparator(aKey));
169 0 : if (aRequestIterator != mpRequestQueue->end())
170 : {
171 0 : if (aRequestIterator->mnPriorityInClass == mnMinimumPriority+1)
172 0 : mnMinimumPriority++;
173 0 : else if (aRequestIterator->mnPriorityInClass == mnMaximumPriority-1)
174 0 : mnMaximumPriority--;
175 :
176 0 : SdrPage *pPage = const_cast<SdrPage*>(aRequestIterator->maKey);
177 0 : pPage->RemovePageUser(*this);
178 0 : mpRequestQueue->erase(aRequestIterator);
179 :
180 0 : bRequestWasRemoved = true;
181 :
182 : if (bRequestWasRemoved)
183 : {
184 : SSCD_SET_STATUS(aKey,NONE);
185 : }
186 : }
187 : else
188 0 : break;
189 : }
190 :
191 0 : return bRequestWasRemoved;
192 : }
193 :
194 :
195 :
196 :
197 0 : void RequestQueue::ChangeClass (
198 : CacheKey aKey,
199 : RequestPriorityClass eNewRequestClass)
200 : {
201 0 : ::osl::MutexGuard aGuard (maMutex);
202 :
203 : OSL_ASSERT(eNewRequestClass>=MIN__CLASS && eNewRequestClass<=MAX__CLASS);
204 :
205 : Container::const_iterator iRequest (
206 : ::std::find_if (
207 0 : mpRequestQueue->begin(),
208 0 : mpRequestQueue->end(),
209 0 : Request::DataComparator(aKey)));
210 0 : if (iRequest!=mpRequestQueue->end() && iRequest->meClass!=eNewRequestClass)
211 : {
212 0 : AddRequest(aKey, eNewRequestClass, true);
213 : SSCD_SET_REQUEST_CLASS(aKey,eNewRequestClass);
214 0 : }
215 0 : }
216 :
217 :
218 :
219 :
220 0 : CacheKey RequestQueue::GetFront (void)
221 : {
222 0 : ::osl::MutexGuard aGuard (maMutex);
223 :
224 0 : if (mpRequestQueue->empty())
225 : throw ::com::sun::star::uno::RuntimeException("RequestQueue::GetFront(): queue is empty",
226 0 : NULL);
227 :
228 0 : return mpRequestQueue->begin()->maKey;
229 : }
230 :
231 :
232 :
233 :
234 0 : RequestPriorityClass RequestQueue::GetFrontPriorityClass (void)
235 : {
236 0 : ::osl::MutexGuard aGuard (maMutex);
237 :
238 0 : if (mpRequestQueue->empty())
239 : throw ::com::sun::star::uno::RuntimeException("RequestQueue::GetFrontPriorityClass(): queue is empty",
240 0 : NULL);
241 :
242 0 : return mpRequestQueue->begin()->meClass;
243 : }
244 :
245 :
246 :
247 :
248 0 : void RequestQueue::PopFront (void)
249 : {
250 0 : ::osl::MutexGuard aGuard (maMutex);
251 :
252 0 : if ( ! mpRequestQueue->empty())
253 : {
254 : SSCD_SET_STATUS(maRequestQueue.begin()->mpData->GetPage(),NONE);
255 :
256 0 : Container::const_iterator aIter(mpRequestQueue->begin());
257 0 : SdrPage *pPage = const_cast<SdrPage*>(aIter->maKey);
258 0 : pPage->RemovePageUser(*this);
259 0 : mpRequestQueue->erase(aIter);
260 :
261 : // Reset the priority counter if possible.
262 0 : if (mpRequestQueue->empty())
263 : {
264 0 : mnMinimumPriority = 0;
265 0 : mnMaximumPriority = 1;
266 : }
267 0 : }
268 0 : }
269 :
270 :
271 :
272 :
273 0 : bool RequestQueue::IsEmpty (void)
274 : {
275 0 : ::osl::MutexGuard aGuard (maMutex);
276 0 : return mpRequestQueue->empty();
277 : }
278 :
279 :
280 :
281 :
282 0 : void RequestQueue::Clear (void)
283 : {
284 0 : ::osl::MutexGuard aGuard (maMutex);
285 :
286 0 : for (Container::iterator aI = mpRequestQueue->begin(), aEnd = mpRequestQueue->end(); aI != aEnd; ++aI)
287 : {
288 0 : SdrPage *pPage = const_cast<SdrPage*>(aI->maKey);
289 0 : pPage->RemovePageUser(*this);
290 : }
291 :
292 0 : mpRequestQueue->clear();
293 0 : mnMinimumPriority = 0;
294 0 : mnMaximumPriority = 1;
295 0 : }
296 :
297 :
298 :
299 :
300 0 : ::osl::Mutex& RequestQueue::GetMutex (void)
301 : {
302 0 : return maMutex;
303 : }
304 :
305 :
306 : } } } // end of namespace ::sd::slidesorter::cache
307 :
308 :
309 :
310 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|