LCOV - code coverage report
Current view: top level - libreoffice/slideshow/source/engine - eventqueue.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 84 0.0 %
Date: 2012-12-27 Functions: 0 12 0.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             : 
      21             : // must be first
      22             : #include <canvas/debug.hxx>
      23             : #include <tools/diagnose_ex.h>
      24             : #include <canvas/verbosetrace.hxx>
      25             : 
      26             : #include <comphelper/anytostring.hxx>
      27             : #include <cppuhelper/exc_hlp.hxx>
      28             : 
      29             : #include <event.hxx>
      30             : #include <eventqueue.hxx>
      31             : #include <slideshowexceptions.hxx>
      32             : 
      33             : #include <boost/shared_ptr.hpp>
      34             : #include <limits>
      35             : 
      36             : 
      37             : using namespace ::com::sun::star;
      38             : 
      39             : namespace slideshow
      40             : {
      41             :     namespace internal
      42             :     {
      43           0 :         bool EventQueue::EventEntry::operator<( const EventEntry& rEvent ) const
      44             :         {
      45             :             // negate comparison, we want priority queue to be sorted
      46             :             // in increasing order of activation times
      47           0 :             return this->nTime > rEvent.nTime;
      48             :         }
      49             : 
      50             : 
      51           0 :         EventQueue::EventQueue(
      52             :             boost::shared_ptr<canvas::tools::ElapsedTime> const & pPresTimer )
      53             :             : maMutex(),
      54             :               maEvents(),
      55             :               maNextEvents(),
      56             :               maNextNextEvents(),
      57           0 :               mpTimer( pPresTimer )
      58             :         {
      59           0 :         }
      60             : 
      61           0 :         EventQueue::~EventQueue()
      62             :         {
      63             :             // add in all that have been added explicitly for this round:
      64           0 :             EventEntryVector::const_iterator const iEnd( maNextEvents.end() );
      65           0 :             for ( EventEntryVector::const_iterator iPos( maNextEvents.begin() );
      66             :                   iPos != iEnd; ++iPos )
      67             :             {
      68           0 :                 maEvents.push(*iPos);
      69             :             }
      70           0 :             EventEntryVector().swap( maNextEvents );
      71             : 
      72             :             // dispose event queue
      73           0 :             while( !maEvents.empty() )
      74             :             {
      75             :                 try
      76             :                 {
      77           0 :                     maEvents.top().pEvent->dispose();
      78             :                 }
      79           0 :                 catch (uno::Exception &)
      80             :                 {
      81             :                     OSL_FAIL( rtl::OUStringToOString(
      82             :                                     comphelper::anyToString(
      83             :                                         cppu::getCaughtException() ),
      84             :                                     RTL_TEXTENCODING_UTF8 ).getStr() );
      85             :                 }
      86           0 :                 maEvents.pop();
      87             :             }
      88           0 :         }
      89             : 
      90           0 :         bool EventQueue::addEvent( const EventSharedPtr& rEvent )
      91             :         {
      92           0 :             ::osl::MutexGuard aGuard( maMutex );
      93             : 
      94             :             SAL_INFO("slideshow.eventqueue", "adding event \"" << rEvent->GetDescription()
      95             :                 << "\" [" << rEvent.get()
      96             :                 << "] at " << mpTimer->getElapsedTime()
      97             :                 << " with delay " << rEvent->getActivationTime(0.0)
      98             :                 );
      99           0 :             ENSURE_OR_RETURN_FALSE( rEvent,
     100             :                                "EventQueue::addEvent: event ptr NULL" );
     101             : 
     102             :             // prepare entry
     103             : 
     104             :             // A seemingly obvious optimization cannot be used here,
     105             :             // because it breaks assumed order of notification: zero
     106             :             // timeout events could be fired() immediately, but that
     107             :             // would not unwind the stack and furthermore changes
     108             :             // order of notification
     109             : 
     110             :             // add entry
     111           0 :             maEvents.push( EventEntry( rEvent, rEvent->getActivationTime(
     112           0 :                                            mpTimer->getElapsedTime()) ) );
     113           0 :             return true;
     114             :         }
     115             : 
     116           0 :         bool EventQueue::addEventForNextRound( EventSharedPtr const& rEvent )
     117             :         {
     118           0 :             ::osl::MutexGuard aGuard( maMutex );
     119             : 
     120             :             SAL_INFO("slideshow.eventqueue", "adding event \"" << rEvent->GetDescription()
     121             :                 << "\" [" << rEvent.get()
     122             :                 << "] for the next round at " << mpTimer->getElapsedTime()
     123             :                 << " with delay " << rEvent->getActivationTime(0.0)
     124             :                 );
     125             : 
     126           0 :             ENSURE_OR_RETURN_FALSE( rEvent.get() != NULL,
     127             :                                "EventQueue::addEvent: event ptr NULL" );
     128             :             maNextEvents.push_back(
     129           0 :                 EventEntry( rEvent, rEvent->getActivationTime(
     130           0 :                                 mpTimer->getElapsedTime()) ) );
     131           0 :             return true;
     132             :         }
     133             : 
     134           0 :         bool EventQueue::addEventWhenQueueIsEmpty (const EventSharedPtr& rpEvent)
     135             :         {
     136           0 :             ::osl::MutexGuard aGuard( maMutex );
     137             : 
     138             :             SAL_INFO("slideshow.eventqueue", "adding event \"" << rpEvent->GetDescription()
     139             :                 << "\" [" << rpEvent.get()
     140             :                 << "] for execution when the queue is empty at " << mpTimer->getElapsedTime()
     141             :                 << " with delay " << rpEvent->getActivationTime(0.0)
     142             :                 );
     143             : 
     144           0 :             ENSURE_OR_RETURN_FALSE(
     145             :                 rpEvent.get() != NULL,
     146             :                     "EventQueue::addEvent: event ptr NULL");
     147             : 
     148             :             maNextNextEvents.push(
     149             :                 EventEntry(
     150             :                     rpEvent,
     151           0 :                     rpEvent->getActivationTime(mpTimer->getElapsedTime())));
     152             : 
     153           0 :             return true;
     154             :         }
     155             : 
     156           0 :         void EventQueue::forceEmpty()
     157             :         {
     158           0 :             ::osl::MutexGuard aGuard( maMutex );
     159             : 
     160           0 :             process_(true);
     161           0 :         }
     162             : 
     163           0 :         void EventQueue::process()
     164             :         {
     165           0 :             ::osl::MutexGuard aGuard( maMutex );
     166             : 
     167           0 :             process_(false);
     168           0 :         }
     169             : 
     170           0 :         void EventQueue::process_( bool bFireAllEvents )
     171             :         {
     172             :             VERBOSE_TRACE( "EventQueue: heartbeat" );
     173             : 
     174             :             // add in all that have been added explicitly for this round:
     175           0 :             EventEntryVector::const_iterator const iEnd( maNextEvents.end() );
     176           0 :             for ( EventEntryVector::const_iterator iPos( maNextEvents.begin() );
     177             :                   iPos != iEnd; ++iPos ) {
     178           0 :                 maEvents.push(*iPos);
     179             :             }
     180           0 :             EventEntryVector().swap( maNextEvents );
     181             : 
     182             :             // perform topmost, ready-to-execute event
     183             :             // =======================================
     184             : 
     185           0 :             const double nCurrTime( mpTimer->getElapsedTime() );
     186             : 
     187             :             // When maEvents does not contain any events that are due now
     188             :             // then process one event from maNextNextEvents.
     189           0 :             if (!maNextNextEvents.empty()
     190           0 :                 && !bFireAllEvents
     191           0 :                 && (maEvents.empty() || maEvents.top().nTime > nCurrTime))
     192             :             {
     193           0 :                 const EventEntry aEvent (maNextNextEvents.top());
     194           0 :                 maNextNextEvents.pop();
     195           0 :                 maEvents.push(aEvent);
     196             :             }
     197             : 
     198             :             // process ready/elapsed events. Note that the 'perceived'
     199             :             // current time remains constant for this loop, thus we're
     200             :             // processing only those events which where ready when we
     201             :             // entered this method.
     202           0 :             while( !maEvents.empty() &&
     203           0 :                    (bFireAllEvents || maEvents.top().nTime <= nCurrTime) )
     204             :             {
     205           0 :                 EventEntry event( maEvents.top() );
     206           0 :                 maEvents.pop();
     207             : 
     208             :                 // only process event, if it is still 'charged',
     209             :                 // i.e. the fire() call effects something. This is
     210             :                 // used when e.g. having events registered at multiple
     211             :                 // places, which should fire only once: after the
     212             :                 // initial fire() call, those events become inactive
     213             :                 // and return false on isCharged. This frees us from
     214             :                 // the need to prune queues of those inactive shells.
     215           0 :                 if( event.pEvent->isCharged() )
     216             :                 {
     217             :                     try
     218             :                     {
     219             :                         SAL_INFO("slideshow.eventqueue", "firing event \""
     220             :                                 << event.pEvent->GetDescription()
     221             :                                 << "\" [" << event.pEvent.get()
     222             :                                 << "] at " << mpTimer->getElapsedTime()
     223             :                                 << " with delay " << event.pEvent->getActivationTime(0.0)
     224             :                                 );
     225           0 :                         event.pEvent->fire();
     226             :                         SAL_INFO("slideshow.eventqueue", "event \""
     227             :                                 << event.pEvent->GetDescription()
     228             :                                 << "\" [" << event.pEvent.get() << "] fired"
     229             :                                 );
     230             :                     }
     231           0 :                     catch( uno::RuntimeException& )
     232             :                     {
     233           0 :                         throw;
     234             :                     }
     235           0 :                     catch( uno::Exception& )
     236             :                     {
     237             :                         // catch anything here, we don't want
     238             :                         // to leave this scope under _any_
     239             :                         // circumstance. Although, do _not_
     240             :                         // reinsert an activity that threw
     241             :                         // once.
     242             : 
     243             :                         // NOTE: we explicitly don't catch(...) here,
     244             :                         // since this will also capture segmentation
     245             :                         // violations and the like. In such a case, we
     246             :                         // still better let our clients now...
     247             :                         OSL_FAIL( rtl::OUStringToOString(
     248             :                                         comphelper::anyToString( cppu::getCaughtException() ),
     249             :                                         RTL_TEXTENCODING_UTF8 ).getStr() );
     250             :                     }
     251           0 :                     catch( SlideShowException& )
     252             :                     {
     253             :                         // catch anything here, we don't want
     254             :                         // to leave this scope under _any_
     255             :                         // circumstance. Although, do _not_
     256             :                         // reinsert an activity that threw
     257             :                         // once.
     258             : 
     259             :                         // NOTE: we explicitly don't catch(...) here,
     260             :                         // since this will also capture segmentation
     261             :                         // violations and the like. In such a case, we
     262             :                         // still better let our clients now...
     263             :                         OSL_TRACE( "::presentation::internal::EventQueue: Event threw a SlideShowException, action might not have been fully performed" );
     264             :                     }
     265             :                 }
     266             :                 else
     267             :                 {
     268             : #if OSL_DEBUG_LEVEL > 0
     269             :                     VERBOSE_TRACE( "Ignoring discharged event: unknown (0x%X), timeout was: %f",
     270             :                                    event.pEvent.get(),
     271             :                                    event.pEvent->getActivationTime(0.0) );
     272             : #endif
     273             :                 }
     274           0 :             }
     275           0 :         }
     276             : 
     277           0 :         bool EventQueue::isEmpty() const
     278             :         {
     279           0 :             ::osl::MutexGuard aGuard( maMutex );
     280             : 
     281           0 :             return maEvents.empty() && maNextEvents.empty() && maNextNextEvents.empty();
     282             :         }
     283             : 
     284           0 :         double EventQueue::nextTimeout() const
     285             :         {
     286           0 :             ::osl::MutexGuard aGuard( maMutex );
     287             : 
     288             :             // return time for next entry (if any)
     289           0 :             double nTimeout (::std::numeric_limits<double>::max());
     290           0 :             const double nCurrentTime (mpTimer->getElapsedTime());
     291           0 :             if ( ! maEvents.empty())
     292           0 :                 nTimeout = maEvents.top().nTime - nCurrentTime;
     293           0 :             if ( ! maNextEvents.empty())
     294           0 :                 nTimeout = ::std::min(nTimeout, maNextEvents.front().nTime - nCurrentTime);
     295           0 :             if ( ! maNextNextEvents.empty())
     296           0 :                 nTimeout = ::std::min(nTimeout, maNextNextEvents.top().nTime - nCurrentTime);
     297             : 
     298           0 :             return nTimeout;
     299             :         }
     300             : 
     301           0 :         void EventQueue::clear()
     302             :         {
     303           0 :             ::osl::MutexGuard aGuard( maMutex );
     304             : 
     305             :             // TODO(P1): Maybe a plain vector and vector.swap will
     306             :             // be faster here. Profile.
     307           0 :             maEvents = ImplQueueType();
     308             : 
     309           0 :             maNextEvents.clear();
     310           0 :             maNextNextEvents = ImplQueueType();
     311           0 :         }
     312             :     }
     313             : }
     314             : 
     315             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10