LCOV - code coverage report
Current view: top level - comphelper/source/misc - asyncnotification.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 61 0.0 %
Date: 2014-04-14 Functions: 0 22 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             : #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 <cassert>
      27             : #include <deque>
      28             : #include <functional>
      29             : #include <algorithm>
      30             : 
      31             : 
      32             : namespace comphelper
      33             : {
      34             : 
      35             : 
      36             : 
      37             :     //= AnyEvent
      38             : 
      39             : 
      40           0 :     AnyEvent::AnyEvent()
      41           0 :         :m_refCount( 0 )
      42             :     {
      43           0 :     }
      44             : 
      45             : 
      46           0 :     AnyEvent::~AnyEvent()
      47             :     {
      48           0 :     }
      49             : 
      50             : 
      51           0 :     oslInterlockedCount SAL_CALL AnyEvent::acquire()
      52             :     {
      53           0 :         return osl_atomic_increment( &m_refCount );
      54             :     }
      55             : 
      56             : 
      57           0 :     oslInterlockedCount SAL_CALL AnyEvent::release()
      58             :     {
      59           0 :         if ( 0 == osl_atomic_decrement( &m_refCount ) )
      60             :         {
      61           0 :             delete this;
      62           0 :             return 0;
      63             :         }
      64           0 :         return m_refCount;
      65             :     }
      66             : 
      67             : 
      68             :     //= ProcessableEvent
      69             : 
      70           0 :     struct ProcessableEvent
      71             :     {
      72             :         AnyEventRef                         aEvent;
      73             :         ::rtl::Reference< IEventProcessor > xProcessor;
      74             : 
      75           0 :         ProcessableEvent()
      76           0 :         {
      77           0 :         }
      78             : 
      79           0 :         ProcessableEvent( const AnyEventRef& _rEvent, const ::rtl::Reference< IEventProcessor >& _xProcessor )
      80             :             :aEvent( _rEvent )
      81           0 :             ,xProcessor( _xProcessor )
      82             :         {
      83           0 :         }
      84             :     };
      85             : 
      86             : 
      87             :     typedef ::std::deque< ProcessableEvent >    EventQueue;
      88             : 
      89             : 
      90             :     struct EqualProcessor : public ::std::unary_function< ProcessableEvent, bool >
      91             :     {
      92             :         const ::rtl::Reference< IEventProcessor >&  rProcessor;
      93           0 :         EqualProcessor( const ::rtl::Reference< IEventProcessor >& _rProcessor ) :rProcessor( _rProcessor ) { }
      94             : 
      95           0 :         bool operator()( const ProcessableEvent& _rEvent )
      96             :         {
      97           0 :             return _rEvent.xProcessor.get() == rProcessor.get();
      98             :         }
      99             :     };
     100             : 
     101             : 
     102             :     //= EventNotifierImpl
     103             : 
     104           0 :     struct EventNotifierImpl
     105             :     {
     106             :         ::osl::Mutex        aMutex;
     107             :         ::osl::Condition    aPendingActions;
     108             :         EventQueue          aEvents;
     109             :         bool                bTerminate;
     110             : 
     111           0 :         EventNotifierImpl()
     112           0 :             :bTerminate( false )
     113             :         {
     114           0 :         }
     115             :     };
     116             : 
     117             : 
     118             :     //= AsyncEventNotifier
     119             : 
     120             : 
     121           0 :     AsyncEventNotifier::AsyncEventNotifier(char const * name):
     122           0 :         Thread(name), m_pImpl(new EventNotifierImpl)
     123             :     {
     124           0 :     }
     125             : 
     126             : 
     127           0 :     AsyncEventNotifier::~AsyncEventNotifier()
     128             :     {
     129           0 :     }
     130             : 
     131             : 
     132           0 :     void AsyncEventNotifier::removeEventsForProcessor( const ::rtl::Reference< IEventProcessor >& _xProcessor )
     133             :     {
     134           0 :         ::osl::MutexGuard aGuard( m_pImpl->aMutex );
     135             : 
     136             :         // remove all events for this processor
     137           0 :         ::std::remove_if( m_pImpl->aEvents.begin(), m_pImpl->aEvents.end(), EqualProcessor( _xProcessor ) );
     138           0 :     }
     139             : 
     140             : 
     141           0 :     void SAL_CALL AsyncEventNotifier::terminate()
     142             :     {
     143           0 :         ::osl::MutexGuard aGuard( m_pImpl->aMutex );
     144             : 
     145             :         // remember the termination request
     146           0 :         m_pImpl->bTerminate = true;
     147             : 
     148             :         // awake the thread
     149           0 :         m_pImpl->aPendingActions.set();
     150           0 :     }
     151             : 
     152             : 
     153           0 :     void AsyncEventNotifier::addEvent( const AnyEventRef& _rEvent, const ::rtl::Reference< IEventProcessor >& _xProcessor )
     154             :     {
     155           0 :         ::osl::MutexGuard aGuard( m_pImpl->aMutex );
     156             : 
     157             :         OSL_TRACE( "AsyncEventNotifier(%p): adding %p", this, _rEvent.get() );
     158             :         // remember this event
     159           0 :         m_pImpl->aEvents.push_back( ProcessableEvent( _rEvent, _xProcessor ) );
     160             : 
     161             :         // awake the thread
     162           0 :         m_pImpl->aPendingActions.set();
     163           0 :     }
     164             : 
     165             : 
     166           0 :     void AsyncEventNotifier::execute()
     167             :     {
     168             :         for (;;)
     169             :         {
     170           0 :             m_pImpl->aPendingActions.wait();
     171           0 :             ProcessableEvent aEvent;
     172             :             {
     173           0 :                 osl::MutexGuard aGuard(m_pImpl->aMutex);
     174           0 :                 if (m_pImpl->bTerminate)
     175             :                 {
     176           0 :                     break;
     177             :                 }
     178           0 :                 if (!m_pImpl->aEvents.empty())
     179             :                 {
     180           0 :                     aEvent = m_pImpl->aEvents.front();
     181           0 :                     m_pImpl->aEvents.pop_front();
     182             :                     OSL_TRACE(
     183             :                         "AsyncEventNotifier(%p): popping %p", this,
     184             :                         aEvent.aEvent.get());
     185             :                 }
     186           0 :                 if (m_pImpl->aEvents.empty())
     187             :                 {
     188           0 :                     m_pImpl->aPendingActions.reset();
     189           0 :                 }
     190             :             }
     191           0 :             if (aEvent.aEvent.is()) {
     192             :                 assert(aEvent.xProcessor.is());
     193           0 :                 aEvent.xProcessor->processEvent(*aEvent.aEvent);
     194             :             }
     195           0 :         }
     196           0 :     }
     197             : 
     198             : } // namespace comphelper
     199             : 
     200             : 
     201             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10