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