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 0 : return (rRequest1.mnPriorityInClass > rRequest2.mnPriorityInClass);
44 : else
45 0 : 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 0 : DataComparator (const CacheKey aKey):maKey(aKey){}
55 0 : 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 0 : class RequestQueue::Container
66 : : public ::std::set<
67 : Request,
68 : Request::Comparator>
69 : {
70 : };
71 :
72 :
73 :
74 :
75 : //===== GenericRequestQueue =================================================
76 :
77 :
78 0 : RequestQueue::RequestQueue (const SharedCacheContext& rpCacheContext)
79 : : maMutex(),
80 0 : mpRequestQueue(new Container()),
81 : mpCacheContext(rpCacheContext),
82 : mnMinimumPriority(0),
83 0 : mnMaximumPriority(1)
84 : {
85 0 : }
86 :
87 :
88 :
89 :
90 0 : RequestQueue::~RequestQueue (void)
91 : {
92 0 : }
93 :
94 :
95 :
96 :
97 0 : void RequestQueue::AddRequest (
98 : CacheKey aKey,
99 : RequestPriorityClass eRequestClass,
100 : bool /*bInsertWithHighestPriority*/)
101 : {
102 0 : ::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 0 : 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 0 : sal_Int32 nPriority (mpCacheContext->GetPriority(aKey));
117 0 : Request aRequest (aKey, nPriority, eRequestClass);
118 0 : mpRequestQueue->insert(aRequest);
119 :
120 0 : 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 0 : }
128 :
129 :
130 :
131 :
132 0 : bool RequestQueue::RemoveRequest (
133 : CacheKey aKey)
134 : {
135 0 : bool bRequestWasRemoved (false);
136 0 : ::osl::MutexGuard aGuard (maMutex);
137 :
138 0 : while(true)
139 : {
140 : Container::const_iterator aRequestIterator = ::std::find_if (
141 0 : mpRequestQueue->begin(),
142 0 : mpRequestQueue->end(),
143 0 : Request::DataComparator(aKey));
144 0 : if (aRequestIterator != mpRequestQueue->end())
145 : {
146 0 : if (aRequestIterator->mnPriorityInClass == mnMinimumPriority+1)
147 0 : mnMinimumPriority++;
148 0 : else if (aRequestIterator->mnPriorityInClass == mnMaximumPriority-1)
149 0 : mnMaximumPriority--;
150 0 : mpRequestQueue->erase(aRequestIterator);
151 0 : bRequestWasRemoved = true;
152 :
153 : if (bRequestWasRemoved)
154 : {
155 : SSCD_SET_STATUS(aKey,NONE);
156 : }
157 : }
158 : else
159 : break;
160 : }
161 :
162 0 : return bRequestWasRemoved;
163 : }
164 :
165 :
166 :
167 :
168 0 : void RequestQueue::ChangeClass (
169 : CacheKey aKey,
170 : RequestPriorityClass eNewRequestClass)
171 : {
172 0 : ::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 0 : mpRequestQueue->begin(),
179 0 : mpRequestQueue->end(),
180 0 : Request::DataComparator(aKey)));
181 0 : if (iRequest!=mpRequestQueue->end() && iRequest->meClass!=eNewRequestClass)
182 : {
183 0 : AddRequest(aKey, eNewRequestClass, true);
184 : SSCD_SET_REQUEST_CLASS(aKey,eNewRequestClass);
185 0 : }
186 0 : }
187 :
188 :
189 :
190 :
191 0 : CacheKey RequestQueue::GetFront (void)
192 : {
193 0 : ::osl::MutexGuard aGuard (maMutex);
194 :
195 0 : if (mpRequestQueue->empty())
196 : throw ::com::sun::star::uno::RuntimeException("RequestQueue::GetFront(): queue is empty",
197 0 : NULL);
198 :
199 0 : return mpRequestQueue->begin()->maKey;
200 : }
201 :
202 :
203 :
204 :
205 0 : RequestPriorityClass RequestQueue::GetFrontPriorityClass (void)
206 : {
207 0 : ::osl::MutexGuard aGuard (maMutex);
208 :
209 0 : if (mpRequestQueue->empty())
210 : throw ::com::sun::star::uno::RuntimeException("RequestQueue::GetFrontPriorityClass(): queue is empty",
211 0 : NULL);
212 :
213 0 : return mpRequestQueue->begin()->meClass;
214 : }
215 :
216 :
217 :
218 :
219 0 : void RequestQueue::PopFront (void)
220 : {
221 0 : ::osl::MutexGuard aGuard (maMutex);
222 :
223 0 : if ( ! mpRequestQueue->empty())
224 : {
225 : SSCD_SET_STATUS(maRequestQueue.begin()->mpData->GetPage(),NONE);
226 :
227 0 : mpRequestQueue->erase(mpRequestQueue->begin());
228 :
229 : // Reset the priority counter if possible.
230 0 : if (mpRequestQueue->empty())
231 : {
232 0 : mnMinimumPriority = 0;
233 0 : mnMaximumPriority = 1;
234 : }
235 0 : }
236 0 : }
237 :
238 :
239 :
240 :
241 0 : bool RequestQueue::IsEmpty (void)
242 : {
243 0 : ::osl::MutexGuard aGuard (maMutex);
244 0 : return mpRequestQueue->empty();
245 : }
246 :
247 :
248 :
249 :
250 0 : void RequestQueue::Clear (void)
251 : {
252 0 : ::osl::MutexGuard aGuard (maMutex);
253 :
254 0 : mpRequestQueue->clear();
255 0 : mnMinimumPriority = 0;
256 0 : mnMaximumPriority = 1;
257 0 : }
258 :
259 :
260 :
261 :
262 0 : ::osl::Mutex& RequestQueue::GetMutex (void)
263 : {
264 0 : return maMutex;
265 : }
266 :
267 :
268 : } } } // end of namespace ::sd::slidesorter::cache
269 :
270 :
271 :
272 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|