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 <comphelper/scopeguard.hxx>
23 #include <condition_variable>
42 struct ProcessableEvent
45 ::rtl::Reference
< IEventProcessor
> xProcessor
;
50 const ::rtl::Reference
< IEventProcessor
>& rProcessor
;
51 explicit EqualProcessor( const ::rtl::Reference
< IEventProcessor
>& _rProcessor
) :rProcessor( _rProcessor
) { }
53 bool operator()( const ProcessableEvent
& _rEvent
)
55 return _rEvent
.xProcessor
.get() == rProcessor
.get();
61 struct EventNotifierImpl
64 std::condition_variable aPendingActions
;
65 std::vector
< ProcessableEvent
> aEvents
;
67 // only used for AsyncEventNotifierAutoJoin
69 std::shared_ptr
<AsyncEventNotifierAutoJoin
> pKeepThisAlive
;
78 AsyncEventNotifierBase::AsyncEventNotifierBase()
79 : m_xImpl(new EventNotifierImpl
)
84 AsyncEventNotifierBase::~AsyncEventNotifierBase()
89 void AsyncEventNotifierBase::removeEventsForProcessor( const ::rtl::Reference
< IEventProcessor
>& _xProcessor
)
91 std::scoped_lock
aGuard( m_xImpl
->aMutex
);
93 // remove all events for this processor
94 m_xImpl
->aEvents
.erase(std::remove_if( m_xImpl
->aEvents
.begin(), m_xImpl
->aEvents
.end(), EqualProcessor( _xProcessor
) ), m_xImpl
->aEvents
.end());
98 void SAL_CALL
AsyncEventNotifierBase::terminate()
100 std::scoped_lock
aGuard( m_xImpl
->aMutex
);
102 // remember the termination request
103 m_xImpl
->bTerminate
= true;
106 m_xImpl
->aPendingActions
.notify_all();
110 void AsyncEventNotifierBase::addEvent( const AnyEventRef
& _rEvent
, const ::rtl::Reference
< IEventProcessor
>& _xProcessor
)
112 std::scoped_lock
aGuard( m_xImpl
->aMutex
);
114 // remember this event
115 m_xImpl
->aEvents
.emplace_back( ProcessableEvent
{_rEvent
, _xProcessor
} );
118 m_xImpl
->aPendingActions
.notify_all();
122 void AsyncEventNotifierBase::execute()
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(); } );
131 if (m_xImpl
->bTerminate
)
134 std::swap(aEvents
, m_xImpl
->aEvents
);
136 for (ProcessableEvent
& rEvent
: aEvents
)
138 assert(rEvent
.xProcessor
.is());
139 rEvent
.xProcessor
->processEvent(*rEvent
.aEvent
);
145 AsyncEventNotifier::AsyncEventNotifier(char const* name
)
146 : salhelper::Thread(name
)
150 AsyncEventNotifier::~AsyncEventNotifier()
154 void AsyncEventNotifier::execute()
156 return AsyncEventNotifierBase::execute();
159 void AsyncEventNotifier::terminate()
161 return AsyncEventNotifierBase::terminate();
166 std::mutex
& GetTheNotifiersMutex()
168 static std::mutex MUTEX
;
174 static std::vector
<std::weak_ptr
<AsyncEventNotifierAutoJoin
>> g_Notifiers
;
176 void JoinAsyncEventNotifiers()
178 std::vector
<std::weak_ptr
<AsyncEventNotifierAutoJoin
>> notifiers
;
180 std::scoped_lock
g(GetTheNotifiersMutex());
181 notifiers
= g_Notifiers
;
183 for (std::weak_ptr
<AsyncEventNotifierAutoJoin
> const& wNotifier
: notifiers
)
185 std::shared_ptr
<AsyncEventNotifierAutoJoin
> const pNotifier(
189 pNotifier
->terminate();
193 // note it's possible that g_Notifiers isn't empty now in case of leaks,
194 // particularly since the UNO service manager isn't disposed yet
197 AsyncEventNotifierAutoJoin::AsyncEventNotifierAutoJoin(char const* name
)
199 m_xImpl
->name
= name
;
202 AsyncEventNotifierAutoJoin::~AsyncEventNotifierAutoJoin()
204 std::scoped_lock
g(GetTheNotifiersMutex());
205 // note: this doesn't happen atomically with the refcount
206 // hence it's possible this deletes > 1 or 0 elements
208 std::remove_if(g_Notifiers
.begin(), g_Notifiers
.end(),
209 [](std::weak_ptr
<AsyncEventNotifierAutoJoin
> const& w
) {
215 std::shared_ptr
<AsyncEventNotifierAutoJoin
>
216 AsyncEventNotifierAutoJoin::newAsyncEventNotifierAutoJoin(char const* name
)
218 std::shared_ptr
<AsyncEventNotifierAutoJoin
> const ret(
219 new AsyncEventNotifierAutoJoin(name
));
220 std::scoped_lock
g(GetTheNotifiersMutex());
221 g_Notifiers
.push_back(ret
);
225 void AsyncEventNotifierAutoJoin::terminate()
227 return AsyncEventNotifierBase::terminate();
230 void AsyncEventNotifierAutoJoin::launch(std::shared_ptr
<AsyncEventNotifierAutoJoin
> const& xThis
)
232 // see salhelper::Thread::launch
233 xThis
->m_xImpl
->pKeepThisAlive
= xThis
;
234 comphelper::ScopeGuard
g([&xThis
] { xThis
->m_xImpl
->pKeepThisAlive
.reset(); });
235 if (!xThis
->create()) {
236 throw std::runtime_error("osl::Thread::create failed");
241 void AsyncEventNotifierAutoJoin::run()
243 // see salhelper::Thread::run
244 comphelper::ScopeGuard
g([this] { onTerminated(); });
245 setName(m_xImpl
->name
);
250 void AsyncEventNotifierAutoJoin::onTerminated()
252 // try to delete "this"
253 m_xImpl
->pKeepThisAlive
.reset();
256 } // namespace comphelper
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */