LCOV - code coverage report
Current view: top level - comphelper/source/misc - asyncnotification.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 51 53 96.2 %
Date: 2015-06-13 12:38:46 Functions: 17 20 85.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             : namespace comphelper
      32             : {
      33         967 :     AnyEvent::AnyEvent()
      34             :     {
      35         967 :     }
      36             : 
      37         966 :     AnyEvent::~AnyEvent()
      38             :     {
      39         966 :     }
      40             : 
      41        1196 :     struct ProcessableEvent
      42             :     {
      43             :         AnyEventRef                         aEvent;
      44             :         ::rtl::Reference< IEventProcessor > xProcessor;
      45             : 
      46         317 :         ProcessableEvent()
      47         317 :         {
      48         317 :         }
      49             : 
      50         220 :         ProcessableEvent( const AnyEventRef& _rEvent, const ::rtl::Reference< IEventProcessor >& _xProcessor )
      51             :             :aEvent( _rEvent )
      52         220 :             ,xProcessor( _xProcessor )
      53             :         {
      54         220 :         }
      55             :     };
      56             : 
      57             : 
      58             :     typedef ::std::deque< ProcessableEvent >    EventQueue;
      59             : 
      60             : 
      61             :     struct EqualProcessor : public ::std::unary_function< ProcessableEvent, bool >
      62             :     {
      63             :         const ::rtl::Reference< IEventProcessor >&  rProcessor;
      64          98 :         explicit EqualProcessor( const ::rtl::Reference< IEventProcessor >& _rProcessor ) :rProcessor( _rProcessor ) { }
      65             : 
      66           0 :         bool operator()( const ProcessableEvent& _rEvent )
      67             :         {
      68           0 :             return _rEvent.xProcessor.get() == rProcessor.get();
      69             :         }
      70             :     };
      71             : 
      72             : 
      73          97 :     struct EventNotifierImpl
      74             :     {
      75             :         ::osl::Mutex        aMutex;
      76             :         ::osl::Condition    aPendingActions;
      77             :         EventQueue          aEvents;
      78             :         bool                bTerminate;
      79             : 
      80          99 :         EventNotifierImpl()
      81          99 :             :bTerminate( false )
      82             :         {
      83          99 :         }
      84             :     };
      85             : 
      86          99 :     AsyncEventNotifier::AsyncEventNotifier(char const * name):
      87          99 :         Thread(name), m_xImpl(new EventNotifierImpl)
      88             :     {
      89          99 :     }
      90             : 
      91             : 
      92         194 :     AsyncEventNotifier::~AsyncEventNotifier()
      93             :     {
      94         194 :     }
      95             : 
      96             : 
      97          98 :     void AsyncEventNotifier::removeEventsForProcessor( const ::rtl::Reference< IEventProcessor >& _xProcessor )
      98             :     {
      99          98 :         ::osl::MutexGuard aGuard( m_xImpl->aMutex );
     100             : 
     101             :         // remove all events for this processor
     102          98 :         m_xImpl->aEvents.erase(::std::remove_if( m_xImpl->aEvents.begin(), m_xImpl->aEvents.end(), EqualProcessor( _xProcessor ) ), m_xImpl->aEvents.end());
     103          98 :     }
     104             : 
     105             : 
     106          98 :     void SAL_CALL AsyncEventNotifier::terminate()
     107             :     {
     108          98 :         ::osl::MutexGuard aGuard( m_xImpl->aMutex );
     109             : 
     110             :         // remember the termination request
     111          98 :         m_xImpl->bTerminate = true;
     112             : 
     113             :         // awake the thread
     114          98 :         m_xImpl->aPendingActions.set();
     115          98 :     }
     116             : 
     117             : 
     118         220 :     void AsyncEventNotifier::addEvent( const AnyEventRef& _rEvent, const ::rtl::Reference< IEventProcessor >& _xProcessor )
     119             :     {
     120         220 :         ::osl::MutexGuard aGuard( m_xImpl->aMutex );
     121             : 
     122             :         OSL_TRACE( "AsyncEventNotifier(%p): adding %p", this, _rEvent.get() );
     123             :         // remember this event
     124         220 :         m_xImpl->aEvents.push_back( ProcessableEvent( _rEvent, _xProcessor ) );
     125             : 
     126             :         // awake the thread
     127         220 :         m_xImpl->aPendingActions.set();
     128         220 :     }
     129             : 
     130             : 
     131         318 :     void AsyncEventNotifier::execute()
     132             :     {
     133             :         for (;;)
     134             :         {
     135         318 :             m_xImpl->aPendingActions.wait();
     136         317 :             ProcessableEvent aEvent;
     137             :             {
     138         317 :                 osl::MutexGuard aGuard(m_xImpl->aMutex);
     139         317 :                 if (m_xImpl->bTerminate)
     140             :                 {
     141          97 :                     break;
     142             :                 }
     143         220 :                 if (!m_xImpl->aEvents.empty())
     144             :                 {
     145         220 :                     aEvent = m_xImpl->aEvents.front();
     146         220 :                     m_xImpl->aEvents.pop_front();
     147             :                     OSL_TRACE(
     148             :                         "AsyncEventNotifier(%p): popping %p", this,
     149             :                         aEvent.aEvent.get());
     150             :                 }
     151         220 :                 if (m_xImpl->aEvents.empty())
     152             :                 {
     153         195 :                     m_xImpl->aPendingActions.reset();
     154         220 :                 }
     155             :             }
     156         220 :             if (aEvent.aEvent.is()) {
     157             :                 assert(aEvent.xProcessor.is());
     158         220 :                 aEvent.xProcessor->processEvent(*aEvent.aEvent);
     159             :             }
     160         219 :         }
     161          97 :     }
     162             : 
     163             : } // namespace comphelper
     164             : 
     165             : 
     166             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11