1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
41 struct ProcessableEvent
44 ::rtl::Reference
< IEventProcessor
> xProcessor
;
50 ProcessableEvent( const AnyEventRef
& _rEvent
, const ::rtl::Reference
< IEventProcessor
>& _xProcessor
)
52 ,xProcessor( _xProcessor
)
58 typedef ::std::deque
< ProcessableEvent
> EventQueue
;
61 struct EqualProcessor
: public ::std::unary_function
< ProcessableEvent
, bool >
63 const ::rtl::Reference
< IEventProcessor
>& rProcessor
;
64 EqualProcessor( const ::rtl::Reference
< IEventProcessor
>& _rProcessor
) :rProcessor( _rProcessor
) { }
66 bool operator()( const ProcessableEvent
& _rEvent
)
68 return _rEvent
.xProcessor
.get() == rProcessor
.get();
73 struct EventNotifierImpl
76 ::osl::Condition aPendingActions
;
86 AsyncEventNotifier::AsyncEventNotifier(char const * name
):
87 Thread(name
), m_xImpl(new EventNotifierImpl
)
92 AsyncEventNotifier::~AsyncEventNotifier()
97 void AsyncEventNotifier::removeEventsForProcessor( const ::rtl::Reference
< IEventProcessor
>& _xProcessor
)
99 ::osl::MutexGuard
aGuard( m_xImpl
->aMutex
);
101 // remove all events for this processor
102 m_xImpl
->aEvents
.erase(::std::remove_if( m_xImpl
->aEvents
.begin(), m_xImpl
->aEvents
.end(), EqualProcessor( _xProcessor
) ), m_xImpl
->aEvents
.end());
106 void SAL_CALL
AsyncEventNotifier::terminate()
108 ::osl::MutexGuard
aGuard( m_xImpl
->aMutex
);
110 // remember the termination request
111 m_xImpl
->bTerminate
= true;
114 m_xImpl
->aPendingActions
.set();
118 void AsyncEventNotifier::addEvent( const AnyEventRef
& _rEvent
, const ::rtl::Reference
< IEventProcessor
>& _xProcessor
)
120 ::osl::MutexGuard
aGuard( m_xImpl
->aMutex
);
122 OSL_TRACE( "AsyncEventNotifier(%p): adding %p", this, _rEvent
.get() );
123 // remember this event
124 m_xImpl
->aEvents
.push_back( ProcessableEvent( _rEvent
, _xProcessor
) );
127 m_xImpl
->aPendingActions
.set();
131 void AsyncEventNotifier::execute()
135 m_xImpl
->aPendingActions
.wait();
136 ProcessableEvent aEvent
;
138 osl::MutexGuard
aGuard(m_xImpl
->aMutex
);
139 if (m_xImpl
->bTerminate
)
143 if (!m_xImpl
->aEvents
.empty())
145 aEvent
= m_xImpl
->aEvents
.front();
146 m_xImpl
->aEvents
.pop_front();
148 "AsyncEventNotifier(%p): popping %p", this,
149 aEvent
.aEvent
.get());
151 if (m_xImpl
->aEvents
.empty())
153 m_xImpl
->aPendingActions
.reset();
156 if (aEvent
.aEvent
.is()) {
157 assert(aEvent
.xProcessor
.is());
158 aEvent
.xProcessor
->processEvent(*aEvent
.aEvent
);
163 } // namespace comphelper
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */