23#include <condition_variable>
42 struct ProcessableEvent
51 explicit EqualProcessor( const ::rtl::Reference< IEventProcessor >& _rProcessor ) :
rProcessor( _rProcessor ) { }
53 bool operator()(
const ProcessableEvent& _rEvent )
55 return _rEvent.xProcessor.get() ==
rProcessor.get();
91 std::scoped_lock aGuard(
m_xImpl->aMutex );
94 m_xImpl->aEvents.erase(std::remove_if(
m_xImpl->aEvents.begin(),
m_xImpl->aEvents.end(), EqualProcessor( _xProcessor ) ),
m_xImpl->aEvents.end());
100 std::scoped_lock aGuard(
m_xImpl->aMutex );
106 m_xImpl->aPendingActions.notify_all();
112 std::scoped_lock aGuard(
m_xImpl->aMutex );
115 m_xImpl->aEvents.emplace_back( ProcessableEvent {_rEvent, _xProcessor} );
118 m_xImpl->aPendingActions.notify_all();
126 std::vector< ProcessableEvent > aEvents;
128 std::unique_lock aGuard(
m_xImpl->aMutex);
129 m_xImpl->aPendingActions.wait(aGuard,
130 [
this] {
return m_xImpl->bTerminate || !
m_xImpl->aEvents.empty(); } );
134 std::swap(aEvents,
m_xImpl->aEvents);
136 for (ProcessableEvent& rEvent : aEvents)
138 assert(rEvent.xProcessor.is());
139 rEvent.xProcessor->processEvent(*rEvent.aEvent);
174 static std::vector<std::weak_ptr<AsyncEventNotifierAutoJoin>>
g_Notifiers;
178 std::vector<std::weak_ptr<AsyncEventNotifierAutoJoin>> notifiers;
180 std::scoped_lock g(GetTheNotifiersMutex());
183 for (std::weak_ptr<AsyncEventNotifierAutoJoin>
const& wNotifier : notifiers)
185 std::shared_ptr<AsyncEventNotifierAutoJoin>
const pNotifier(
189 pNotifier->terminate();
204 std::scoped_lock g(GetTheNotifiersMutex());
209 [](std::weak_ptr<AsyncEventNotifierAutoJoin>
const&
w) {
215 std::shared_ptr<AsyncEventNotifierAutoJoin>
218 std::shared_ptr<AsyncEventNotifierAutoJoin>
const ret(
220 std::scoped_lock g(GetTheNotifiersMutex());
233 xThis->m_xImpl->pKeepThisAlive = xThis;
235 if (!xThis->create()) {
236 throw std::runtime_error(
"osl::Thread::create failed");
253 m_xImpl->pKeepThisAlive.reset();
const ::rtl::Reference< IEventProcessor > & rProcessor
::rtl::Reference< IEventProcessor > xProcessor
virtual ~AnyEvent() override
virtual SAL_DLLPRIVATE void SAL_CALL run() override
virtual void SAL_CALL terminate() override
terminates the thread
static std::shared_ptr< AsyncEventNotifierAutoJoin > newAsyncEventNotifierAutoJoin(char const *name)
static void launch(std::shared_ptr< AsyncEventNotifierAutoJoin > const &)
virtual SAL_DLLPRIVATE void SAL_CALL onTerminated() override
SAL_DLLPRIVATE AsyncEventNotifierAutoJoin(char const *name)
virtual SAL_DLLPRIVATE ~AsyncEventNotifierAutoJoin() override
void removeEventsForProcessor(const ::rtl::Reference< IEventProcessor > &_xProcessor)
removes all events for the given event processor from the queue
virtual SAL_DLLPRIVATE ~AsyncEventNotifierBase()
void addEvent(const AnyEventRef &_rEvent, const ::rtl::Reference< IEventProcessor > &_xProcessor)
adds an event to the queue, together with the instance which is responsible for processing it
virtual SAL_DLLPRIVATE void execute()
virtual void SAL_CALL terminate()
terminates the thread
std::unique_ptr< EventNotifierImpl > m_xImpl
virtual SAL_DLLPRIVATE void execute() override
virtual void SAL_CALL terminate() override
terminates the thread
virtual SAL_DLLPRIVATE ~AsyncEventNotifier() override
AsyncEventNotifier(char const *name)
constructs a notifier thread
ScopeGuard to ease writing exception-safe code.
static std::vector< std::weak_ptr< AsyncEventNotifierAutoJoin > > g_Notifiers
void JoinAsyncEventNotifiers()
::rtl::Reference< AnyEvent > AnyEventRef
std::vector< ProcessableEvent > aEvents
std::condition_variable aPendingActions
std::shared_ptr< AsyncEventNotifierAutoJoin > pKeepThisAlive