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

Generated by: LCOV version 1.10