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