LCOV - code coverage report
Current view: top level - sd/source/ui/slidesorter/cache - SlsRequestQueue.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 91 98 92.9 %
Date: 2015-06-13 12:38:46 Functions: 18 20 90.0 %
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             : #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         572 :     Request (
      33             :         CacheKey aKey, sal_Int32 nPriority, RequestPriorityClass eClass)
      34         572 :         : maKey(aKey), mnPriorityInClass(nPriority), meClass(eClass)
      35         572 :     {}
      36             :     /** Sort requests according to priority classes and then to priorities.
      37             :     */
      38             :     class Comparator { public:
      39         321 :         bool operator() (const Request& rRequest1, const Request& rRequest2)
      40             :         {
      41         321 :             if (rRequest1.meClass == rRequest2.meClass)
      42             :             {
      43         238 :                 if (rRequest1.mnPriorityInClass == rRequest2.mnPriorityInClass)
      44             :                 {
      45         177 :                     return rRequest1.maKey < rRequest2.maKey;
      46             :                 }
      47          61 :                 return rRequest1.mnPriorityInClass > rRequest2.mnPriorityInClass;
      48             :             }
      49          83 :             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        1665 :         DataComparator (const CacheKey aKey)
      60        1665 :             : maKey(aKey)
      61             :         {
      62        1665 :         }
      63        1035 :         bool operator() (const Request& rRequest) const
      64             :         {
      65        1035 :             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         128 : class RequestQueue::Container
      77             :     : public ::std::set<
      78             :         Request,
      79             :         Request::Comparator>
      80             : {
      81             : };
      82             : 
      83             : //=====  GenericRequestQueue  =================================================
      84             : 
      85          64 : RequestQueue::RequestQueue (const SharedCacheContext& rpCacheContext)
      86             :     : maMutex(),
      87           0 :       mpRequestQueue(new Container()),
      88             :       mpCacheContext(rpCacheContext),
      89             :       mnMinimumPriority(0),
      90          64 :       mnMaximumPriority(1)
      91             : {
      92          64 : }
      93             : 
      94         128 : RequestQueue::~RequestQueue()
      95             : {
      96          64 :     Clear();
      97          64 : }
      98             : 
      99         572 : void RequestQueue::AddRequest (
     100             :     CacheKey aKey,
     101             :     RequestPriorityClass eRequestClass,
     102             :     bool /*bInsertWithHighestPriority*/)
     103             : {
     104         572 :     ::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         572 :         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         572 :     sal_Int32 nPriority (mpCacheContext->GetPriority(aKey));
     119         572 :     Request aRequest (aKey, nPriority, eRequestClass);
     120             : 
     121         572 :     std::pair<Container::iterator,bool> ret = mpRequestQueue->insert(aRequest);
     122         572 :     bool bInserted = ret.second;
     123             : 
     124         572 :     if (bInserted)
     125             :     {
     126         572 :         SdrPage *pPage = const_cast<SdrPage*>(aRequest.maKey);
     127         572 :         pPage->AddPageUser(*this);
     128             :     }
     129             : 
     130         572 :     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         572 : }
     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         572 : bool RequestQueue::RemoveRequest (
     146             :     CacheKey aKey)
     147             : {
     148         572 :     bool bRequestWasRemoved (false);
     149         572 :     ::osl::MutexGuard aGuard (maMutex);
     150             : 
     151             :     while(true)
     152             :     {
     153             :         Container::const_iterator aRequestIterator = ::std::find_if (
     154         984 :             mpRequestQueue->begin(),
     155         984 :             mpRequestQueue->end(),
     156        2952 :             Request::DataComparator(aKey));
     157         984 :         if (aRequestIterator != mpRequestQueue->end())
     158             :         {
     159         412 :             if (aRequestIterator->mnPriorityInClass == mnMinimumPriority+1)
     160           0 :                 mnMinimumPriority++;
     161         412 :             else if (aRequestIterator->mnPriorityInClass == mnMaximumPriority-1)
     162          92 :                 mnMaximumPriority--;
     163             : 
     164         412 :             SdrPage *pPage = const_cast<SdrPage*>(aRequestIterator->maKey);
     165         412 :             pPage->RemovePageUser(*this);
     166         412 :             mpRequestQueue->erase(aRequestIterator);
     167             : 
     168         412 :             bRequestWasRemoved = true;
     169             : 
     170             :             if (bRequestWasRemoved)
     171             :             {
     172             :                 SSCD_SET_STATUS(aKey,NONE);
     173             :             }
     174             :         }
     175             :         else
     176         572 :             break;
     177             :     }
     178             : 
     179         572 :     return bRequestWasRemoved;
     180             : }
     181             : 
     182         681 : void RequestQueue::ChangeClass (
     183             :     CacheKey aKey,
     184             :     RequestPriorityClass eNewRequestClass)
     185             : {
     186         681 :     ::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         681 :             mpRequestQueue->begin(),
     193         681 :             mpRequestQueue->end(),
     194        2043 :             Request::DataComparator(aKey)));
     195         681 :     if (iRequest!=mpRequestQueue->end() && iRequest->meClass!=eNewRequestClass)
     196             :     {
     197           2 :         AddRequest(aKey, eNewRequestClass, true);
     198             :         SSCD_SET_REQUEST_CLASS(aKey,eNewRequestClass);
     199         681 :     }
     200         681 : }
     201             : 
     202         145 : CacheKey RequestQueue::GetFront()
     203             : {
     204         145 :     ::osl::MutexGuard aGuard (maMutex);
     205             : 
     206         145 :     if (mpRequestQueue->empty())
     207             :         throw ::com::sun::star::uno::RuntimeException("RequestQueue::GetFront(): queue is empty",
     208           0 :             NULL);
     209             : 
     210         145 :     return mpRequestQueue->begin()->maKey;
     211             : }
     212             : 
     213         175 : RequestPriorityClass RequestQueue::GetFrontPriorityClass()
     214             : {
     215         175 :     ::osl::MutexGuard aGuard (maMutex);
     216             : 
     217         175 :     if (mpRequestQueue->empty())
     218             :         throw ::com::sun::star::uno::RuntimeException("RequestQueue::GetFrontPriorityClass(): queue is empty",
     219           0 :             NULL);
     220             : 
     221         175 :     return mpRequestQueue->begin()->meClass;
     222             : }
     223             : 
     224         145 : void RequestQueue::PopFront()
     225             : {
     226         145 :     ::osl::MutexGuard aGuard (maMutex);
     227             : 
     228         145 :     if ( ! mpRequestQueue->empty())
     229             :     {
     230             :         SSCD_SET_STATUS(maRequestQueue.begin()->mpData->GetPage(),NONE);
     231             : 
     232         145 :         Container::const_iterator aIter(mpRequestQueue->begin());
     233         145 :         SdrPage *pPage = const_cast<SdrPage*>(aIter->maKey);
     234         145 :         pPage->RemovePageUser(*this);
     235         145 :         mpRequestQueue->erase(aIter);
     236             : 
     237             :         // Reset the priority counter if possible.
     238         145 :         if (mpRequestQueue->empty())
     239             :         {
     240         115 :             mnMinimumPriority = 0;
     241         115 :             mnMaximumPriority = 1;
     242             :         }
     243         145 :     }
     244         145 : }
     245             : 
     246         435 : bool RequestQueue::IsEmpty()
     247             : {
     248         435 :     ::osl::MutexGuard aGuard (maMutex);
     249         435 :     return mpRequestQueue->empty();
     250             : }
     251             : 
     252         128 : void RequestQueue::Clear()
     253             : {
     254         128 :     ::osl::MutexGuard aGuard (maMutex);
     255             : 
     256         143 :     for (Container::iterator aI = mpRequestQueue->begin(), aEnd = mpRequestQueue->end(); aI != aEnd; ++aI)
     257             :     {
     258          15 :         SdrPage *pPage = const_cast<SdrPage*>(aI->maKey);
     259          15 :         pPage->RemovePageUser(*this);
     260             :     }
     261             : 
     262         128 :     mpRequestQueue->clear();
     263         128 :     mnMinimumPriority = 0;
     264         128 :     mnMaximumPriority = 1;
     265         128 : }
     266             : 
     267          66 : } } } // end of namespace ::sd::slidesorter::cache
     268             : 
     269             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11