LCOV - code coverage report
Current view: top level - sd/source/ui/slidesorter/cache - SlsRequestQueue.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 92 99 92.9 %
Date: 2014-04-11 Functions: 17 19 89.5 %
Legend: Lines: hit not hit

          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         619 :     Request (
      34             :         CacheKey aKey, sal_Int32 nPriority, RequestPriorityClass eClass)
      35         619 :         : maKey(aKey), mnPriorityInClass(nPriority), meClass(eClass)
      36         619 :     {}
      37             :     /** Sort requests according to priority classes and then to priorities.
      38             :     */
      39             :     class Comparator { public:
      40         257 :         bool operator() (const Request& rRequest1, const Request& rRequest2)
      41             :         {
      42         257 :             if (rRequest1.meClass == rRequest2.meClass)
      43             :             {
      44         153 :                 if (rRequest1.mnPriorityInClass == rRequest2.mnPriorityInClass)
      45             :                 {
      46          92 :                     return rRequest1.maKey < rRequest2.maKey;
      47             :                 }
      48          61 :                 return rRequest1.mnPriorityInClass > rRequest2.mnPriorityInClass;
      49             :             }
      50         104 :             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        1804 :         DataComparator (const CacheKey aKey)
      61        1804 :             : maKey(aKey)
      62             :         {
      63        1804 :         }
      64        1136 :         bool operator() (const Request& rRequest) const
      65             :         {
      66        1136 :             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         126 : class RequestQueue::Container
      79             :     : public ::std::set<
      80             :         Request,
      81             :         Request::Comparator>
      82             : {
      83             : };
      84             : 
      85             : 
      86             : 
      87             : 
      88             : //=====  GenericRequestQueue  =================================================
      89             : 
      90             : 
      91          63 : RequestQueue::RequestQueue (const SharedCacheContext& rpCacheContext)
      92             :     : maMutex(),
      93           0 :       mpRequestQueue(new Container()),
      94             :       mpCacheContext(rpCacheContext),
      95             :       mnMinimumPriority(0),
      96          63 :       mnMaximumPriority(1)
      97             : {
      98          63 : }
      99             : 
     100             : 
     101             : 
     102             : 
     103         126 : RequestQueue::~RequestQueue (void)
     104             : {
     105          63 :     Clear();
     106          63 : }
     107             : 
     108             : 
     109             : 
     110             : 
     111         619 : void RequestQueue::AddRequest (
     112             :     CacheKey aKey,
     113             :     RequestPriorityClass eRequestClass,
     114             :     bool /*bInsertWithHighestPriority*/)
     115             : {
     116         619 :     ::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         619 :         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         619 :     sal_Int32 nPriority (mpCacheContext->GetPriority(aKey));
     131         619 :     Request aRequest (aKey, nPriority, eRequestClass);
     132             : 
     133         619 :     std::pair<Container::iterator,bool> ret = mpRequestQueue->insert(aRequest);
     134         619 :     bool bInserted = ret.second == true;
     135             : 
     136         619 :     if (bInserted)
     137             :     {
     138         619 :         SdrPage *pPage = const_cast<SdrPage*>(aRequest.maKey);
     139         619 :         pPage->AddPageUser(*this);
     140             :     }
     141             : 
     142         619 :     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         619 : }
     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         619 : bool RequestQueue::RemoveRequest (
     158             :     CacheKey aKey)
     159             : {
     160         619 :     bool bRequestWasRemoved (false);
     161         619 :     ::osl::MutexGuard aGuard (maMutex);
     162             : 
     163             :     while(true)
     164             :     {
     165             :         Container::const_iterator aRequestIterator = ::std::find_if (
     166        1087 :             mpRequestQueue->begin(),
     167        1087 :             mpRequestQueue->end(),
     168        3261 :             Request::DataComparator(aKey));
     169        1087 :         if (aRequestIterator != mpRequestQueue->end())
     170             :         {
     171         468 :             if (aRequestIterator->mnPriorityInClass == mnMinimumPriority+1)
     172           0 :                 mnMinimumPriority++;
     173         468 :             else if (aRequestIterator->mnPriorityInClass == mnMaximumPriority-1)
     174         120 :                 mnMaximumPriority--;
     175             : 
     176         468 :             SdrPage *pPage = const_cast<SdrPage*>(aRequestIterator->maKey);
     177         468 :             pPage->RemovePageUser(*this);
     178         468 :             mpRequestQueue->erase(aRequestIterator);
     179             : 
     180         468 :             bRequestWasRemoved = true;
     181             : 
     182             :             if (bRequestWasRemoved)
     183             :             {
     184             :                 SSCD_SET_STATUS(aKey,NONE);
     185             :             }
     186             :         }
     187             :         else
     188         619 :             break;
     189             :     }
     190             : 
     191         619 :     return bRequestWasRemoved;
     192             : }
     193             : 
     194             : 
     195             : 
     196             : 
     197         717 : void RequestQueue::ChangeClass (
     198             :     CacheKey aKey,
     199             :     RequestPriorityClass eNewRequestClass)
     200             : {
     201         717 :     ::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         717 :             mpRequestQueue->begin(),
     208         717 :             mpRequestQueue->end(),
     209        2151 :             Request::DataComparator(aKey)));
     210         717 :     if (iRequest!=mpRequestQueue->end() && iRequest->meClass!=eNewRequestClass)
     211             :     {
     212           1 :         AddRequest(aKey, eNewRequestClass, true);
     213             :         SSCD_SET_REQUEST_CLASS(aKey,eNewRequestClass);
     214         717 :     }
     215         717 : }
     216             : 
     217             : 
     218             : 
     219             : 
     220         134 : CacheKey RequestQueue::GetFront (void)
     221             : {
     222         134 :     ::osl::MutexGuard aGuard (maMutex);
     223             : 
     224         134 :     if (mpRequestQueue->empty())
     225             :         throw ::com::sun::star::uno::RuntimeException("RequestQueue::GetFront(): queue is empty",
     226           0 :             NULL);
     227             : 
     228         134 :     return mpRequestQueue->begin()->maKey;
     229             : }
     230             : 
     231             : 
     232             : 
     233             : 
     234         153 : RequestPriorityClass RequestQueue::GetFrontPriorityClass (void)
     235             : {
     236         153 :     ::osl::MutexGuard aGuard (maMutex);
     237             : 
     238         153 :     if (mpRequestQueue->empty())
     239             :         throw ::com::sun::star::uno::RuntimeException("RequestQueue::GetFrontPriorityClass(): queue is empty",
     240           0 :             NULL);
     241             : 
     242         153 :     return mpRequestQueue->begin()->meClass;
     243             : }
     244             : 
     245             : 
     246             : 
     247             : 
     248         134 : void RequestQueue::PopFront (void)
     249             : {
     250         134 :     ::osl::MutexGuard aGuard (maMutex);
     251             : 
     252         134 :     if ( ! mpRequestQueue->empty())
     253             :     {
     254             :         SSCD_SET_STATUS(maRequestQueue.begin()->mpData->GetPage(),NONE);
     255             : 
     256         134 :         Container::const_iterator aIter(mpRequestQueue->begin());
     257         134 :         SdrPage *pPage = const_cast<SdrPage*>(aIter->maKey);
     258         134 :         pPage->RemovePageUser(*this);
     259         134 :         mpRequestQueue->erase(aIter);
     260             : 
     261             :         // Reset the priority counter if possible.
     262         134 :         if (mpRequestQueue->empty())
     263             :         {
     264         115 :             mnMinimumPriority = 0;
     265         115 :             mnMaximumPriority = 1;
     266             :         }
     267         134 :     }
     268         134 : }
     269             : 
     270             : 
     271             : 
     272             : 
     273         402 : bool RequestQueue::IsEmpty (void)
     274             : {
     275         402 :     ::osl::MutexGuard aGuard (maMutex);
     276         402 :     return mpRequestQueue->empty();
     277             : }
     278             : 
     279             : 
     280             : 
     281             : 
     282         126 : void RequestQueue::Clear (void)
     283             : {
     284         126 :     ::osl::MutexGuard aGuard (maMutex);
     285             : 
     286         143 :     for (Container::iterator aI = mpRequestQueue->begin(), aEnd = mpRequestQueue->end(); aI != aEnd; ++aI)
     287             :     {
     288          17 :         SdrPage *pPage = const_cast<SdrPage*>(aI->maKey);
     289          17 :         pPage->RemovePageUser(*this);
     290             :     }
     291             : 
     292         126 :     mpRequestQueue->clear();
     293         126 :     mnMinimumPriority = 0;
     294         126 :     mnMaximumPriority = 1;
     295         126 : }
     296             : 
     297             : 
     298             : 
     299             : 
     300         268 : ::osl::Mutex& RequestQueue::GetMutex (void)
     301             : {
     302         268 :     return maMutex;
     303             : }
     304             : 
     305             : 
     306             : } } } // end of namespace ::sd::slidesorter::cache
     307             : 
     308             : 
     309             : 
     310             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10