LCOV - code coverage report
Current view: top level - comphelper/source/misc - asyncnotification.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 66 75 88.0 %
Date: 2012-08-25 Functions: 17 20 85.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 63 119 52.9 %

           Branch data     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 <comphelper/asyncnotification.hxx>
      21                 :            : #include <osl/diagnose.h>
      22                 :            : #include <osl/mutex.hxx>
      23                 :            : #include <osl/conditn.hxx>
      24                 :            : #include <comphelper/guarding.hxx>
      25                 :            : 
      26                 :            : #include <deque>
      27                 :            : #include <set>
      28                 :            : #include <functional>
      29                 :            : #include <algorithm>
      30                 :            : 
      31                 :            : //........................................................................
      32                 :            : namespace comphelper
      33                 :            : {
      34                 :            : //........................................................................
      35                 :            : 
      36                 :            :     //====================================================================
      37                 :            :     //= AnyEvent
      38                 :            :     //====================================================================
      39                 :            :     //--------------------------------------------------------------------
      40                 :       1922 :     AnyEvent::AnyEvent()
      41                 :       1922 :         :m_refCount( 0 )
      42                 :            :     {
      43                 :       1922 :     }
      44                 :            : 
      45                 :            :     //--------------------------------------------------------------------
      46                 :       1922 :     AnyEvent::~AnyEvent()
      47                 :            :     {
      48         [ -  + ]:       1922 :     }
      49                 :            : 
      50                 :            :     //--------------------------------------------------------------------
      51                 :       6622 :     oslInterlockedCount SAL_CALL AnyEvent::acquire()
      52                 :            :     {
      53                 :       6622 :         return osl_incrementInterlockedCount( &m_refCount );
      54                 :            :     }
      55                 :            : 
      56                 :            :     //--------------------------------------------------------------------
      57                 :       6622 :     oslInterlockedCount SAL_CALL AnyEvent::release()
      58                 :            :     {
      59         [ +  + ]:       6622 :         if ( 0 == osl_decrementInterlockedCount( &m_refCount ) )
      60                 :            :         {
      61         [ +  - ]:       1922 :             delete this;
      62                 :       1922 :             return 0;
      63                 :            :         }
      64                 :       6622 :         return m_refCount;
      65                 :            :     }
      66                 :            : 
      67                 :            :     //====================================================================
      68                 :            :     //= ProcessableEvent
      69                 :            :     //====================================================================
      70         [ +  - ]:       1284 :     struct ProcessableEvent
      71                 :            :     {
      72                 :            :         AnyEventRef                         aEvent;
      73                 :            :         ::rtl::Reference< IEventProcessor > xProcessor;
      74                 :            : 
      75                 :        428 :         ProcessableEvent( const AnyEventRef& _rEvent, const ::rtl::Reference< IEventProcessor >& _xProcessor )
      76                 :            :             :aEvent( _rEvent )
      77         [ +  - ]:        428 :             ,xProcessor( _xProcessor )
      78                 :            :         {
      79                 :        428 :         }
      80                 :            : 
      81                 :        856 :         ProcessableEvent( const ProcessableEvent& _rRHS )
      82                 :            :             :aEvent( _rRHS.aEvent )
      83         [ +  - ]:        856 :             ,xProcessor( _rRHS.xProcessor )
      84                 :            :         {
      85                 :        856 :         }
      86                 :            : 
      87                 :          0 :         ProcessableEvent& operator=( const ProcessableEvent& _rRHS )
      88                 :            :         {
      89                 :          0 :             aEvent = _rRHS.aEvent;
      90                 :          0 :             xProcessor = _rRHS.xProcessor;
      91                 :          0 :             return *this;
      92                 :            :         }
      93                 :            :     };
      94                 :            : 
      95                 :            :     //====================================================================
      96                 :            :     typedef ::std::deque< ProcessableEvent >    EventQueue;
      97                 :            : 
      98                 :            :     //====================================================================
      99                 :            :     struct EqualProcessor : public ::std::unary_function< ProcessableEvent, bool >
     100                 :            :     {
     101                 :            :         const ::rtl::Reference< IEventProcessor >&  rProcessor;
     102                 :        174 :         EqualProcessor( const ::rtl::Reference< IEventProcessor >& _rProcessor ) :rProcessor( _rProcessor ) { }
     103                 :            : 
     104                 :          0 :         bool operator()( const ProcessableEvent& _rEvent )
     105                 :            :         {
     106                 :          0 :             return _rEvent.xProcessor.get() == rProcessor.get();
     107                 :            :         }
     108                 :            :     };
     109                 :            : 
     110                 :            :     //====================================================================
     111                 :            :     //= EventNotifierImpl
     112                 :            :     //====================================================================
     113         [ +  - ]:        174 :     struct EventNotifierImpl
     114                 :            :     {
     115                 :            :         ::osl::Mutex        aMutex;
     116                 :            :         oslInterlockedCount m_refCount;
     117                 :            :         ::osl::Condition    aPendingActions;
     118                 :            :         EventQueue          aEvents;
     119                 :            :         ::std::set< ::rtl::Reference< IEventProcessor > >
     120                 :            :                             m_aDeadProcessors;
     121                 :            : 
     122                 :        174 :         EventNotifierImpl()
     123 [ +  - ][ +  - ]:        174 :             :m_refCount( 0 )
                 [ +  - ]
     124                 :            :         {
     125                 :        174 :         }
     126                 :            : 
     127                 :            :     private:
     128                 :            :         EventNotifierImpl( const EventNotifierImpl& );              // never implemented
     129                 :            :         EventNotifierImpl& operator=( const EventNotifierImpl& );   // never implemented
     130                 :            :     };
     131                 :            : 
     132                 :            :     //====================================================================
     133                 :            :     //= AsyncEventNotifier
     134                 :            :     //====================================================================
     135                 :            :     //--------------------------------------------------------------------
     136                 :        174 :     AsyncEventNotifier::AsyncEventNotifier(char const * name):
     137 [ +  - ][ +  - ]:        174 :         Thread(name), m_pImpl(new EventNotifierImpl)
     138                 :            :     {
     139                 :        174 :     }
     140                 :            : 
     141                 :            :     //--------------------------------------------------------------------
     142         [ +  - ]:        174 :     AsyncEventNotifier::~AsyncEventNotifier()
     143                 :            :     {
     144         [ -  + ]:        348 :     }
     145                 :            : 
     146                 :            :     //--------------------------------------------------------------------
     147                 :        174 :     void AsyncEventNotifier::removeEventsForProcessor( const ::rtl::Reference< IEventProcessor >& _xProcessor )
     148                 :            :     {
     149         [ +  - ]:        174 :         ::osl::MutexGuard aGuard( m_pImpl->aMutex );
     150                 :            : 
     151                 :            :         // remove all events for this processor
     152         [ +  - ]:        174 :         ::std::remove_if( m_pImpl->aEvents.begin(), m_pImpl->aEvents.end(), EqualProcessor( _xProcessor ) );
     153                 :            : 
     154                 :            :         // and just in case that an event for exactly this processor has just been
     155                 :            :         // popped from the queue, but not yet processed: remember it:
     156 [ +  - ][ +  - ]:        174 :         m_pImpl->m_aDeadProcessors.insert( _xProcessor );
     157                 :        174 :     }
     158                 :            : 
     159                 :            :     //--------------------------------------------------------------------
     160                 :        174 :     void SAL_CALL AsyncEventNotifier::terminate()
     161                 :            :     {
     162         [ +  - ]:        174 :         ::osl::MutexGuard aGuard( m_pImpl->aMutex );
     163                 :            : 
     164                 :            :         // remember the termination request
     165         [ +  - ]:        174 :         Thread::terminate();
     166                 :            : 
     167                 :            :         // awake the thread
     168 [ +  - ][ +  - ]:        174 :         m_pImpl->aPendingActions.set();
     169                 :        174 :     }
     170                 :            : 
     171                 :            :     //--------------------------------------------------------------------
     172                 :        428 :     void AsyncEventNotifier::addEvent( const AnyEventRef& _rEvent, const ::rtl::Reference< IEventProcessor >& _xProcessor )
     173                 :            :     {
     174         [ +  - ]:        428 :         ::osl::MutexGuard aGuard( m_pImpl->aMutex );
     175                 :            : 
     176                 :            :         OSL_TRACE( "AsyncEventNotifier(%p): adding %p", this, _rEvent.get() );
     177                 :            :         // remember this event
     178 [ +  - ][ +  - ]:        428 :         m_pImpl->aEvents.push_back( ProcessableEvent( _rEvent, _xProcessor ) );
                 [ +  - ]
     179                 :            : 
     180                 :            :         // awake the thread
     181 [ +  - ][ +  - ]:        428 :         m_pImpl->aPendingActions.set();
     182                 :        428 :     }
     183                 :            : 
     184                 :            :     //--------------------------------------------------------------------
     185                 :        391 :     void AsyncEventNotifier::execute()
     186                 :            :     {
     187                 :        391 :         do
     188                 :            :         {
     189                 :        391 :             AnyEventRef aNextEvent;
     190                 :        391 :             ::rtl::Reference< IEventProcessor > xNextProcessor;
     191                 :            : 
     192         [ +  - ]:        391 :             ::osl::ClearableMutexGuard aGuard( m_pImpl->aMutex );
     193         [ +  + ]:        819 :             while ( m_pImpl->aEvents.size() > 0 )
     194                 :            :             {
     195 [ +  - ][ +  - ]:        428 :                 ProcessableEvent aEvent( m_pImpl->aEvents.front() );
     196         [ +  - ]:        428 :                 aNextEvent = aEvent.aEvent;
     197         [ +  - ]:        428 :                 xNextProcessor = aEvent.xProcessor;
     198         [ +  - ]:        428 :                 m_pImpl->aEvents.pop_front();
     199                 :            : 
     200                 :            :                 OSL_TRACE( "AsyncEventNotifier(%p): popping %p", this, aNextEvent.get() );
     201                 :            : 
     202         [ -  + ]:        428 :                 if ( !aNextEvent.get() )
     203                 :          0 :                     continue;
     204                 :            : 
     205                 :            :                 // process the event, but only if it's processor did not die inbetween
     206         [ +  - ]:        428 :                 ::std::set< ::rtl::Reference< IEventProcessor > >::iterator deadPos = m_pImpl->m_aDeadProcessors.find( xNextProcessor );
     207         [ -  + ]:        428 :                 if ( deadPos != m_pImpl->m_aDeadProcessors.end() )
     208                 :            :                 {
     209         [ #  # ]:          0 :                     m_pImpl->m_aDeadProcessors.erase( xNextProcessor );
     210         [ #  # ]:          0 :                     xNextProcessor.clear();
     211                 :            :                     OSL_TRACE( "AsyncEventNotifier(%p): removing %p", this, aNextEvent.get() );
     212                 :            :                 }
     213                 :            : 
     214                 :            :                 // if there was a termination request (->terminate), respect it
     215 [ +  - ][ -  + ]:        428 :                 if ( !schedule() )
     216                 :            :                     return;
     217                 :            : 
     218                 :            :                 {
     219         [ +  - ]:        428 :                     ::comphelper::MutexRelease aReleaseOnce( m_pImpl->aMutex );
     220         [ +  - ]:        428 :                     if ( xNextProcessor.get() )
     221 [ +  - ][ +  - ]:        428 :                         xNextProcessor->processEvent( *aNextEvent.get() );
     222                 :            :                 }
     223         [ +  - ]:        428 :             }
              [ +  -  - ]
     224                 :            : 
     225                 :            :             // if there was a termination request (->terminate), respect it
     226 [ +  - ][ +  + ]:        391 :             if ( !schedule() )
     227                 :            :                 return;
     228                 :            : 
     229                 :            :             // wait for new events to process
     230         [ +  - ]:        217 :             aGuard.clear();
     231         [ +  - ]:        217 :             m_pImpl->aPendingActions.reset();
     232 [ +  - ][ +  - ]:        391 :             m_pImpl->aPendingActions.wait();
         [ +  + ][ +  - ]
         [ +  + ][ +  - ]
                 [ +  + ]
     233                 :            :         }
     234                 :            :         while ( sal_True );
     235                 :            :     }
     236                 :            : 
     237                 :            : //........................................................................
     238                 :            : } // namespace comphelper
     239                 :            : //........................................................................
     240                 :            : 
     241                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10