android: Update app-specific/MIME type icons
[LibreOffice.git] / comphelper / source / misc / asyncnotification.cxx
blobcb8a2f2511642cf24fa4658d4c59b77fd336fb33
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
22 #include <mutex>
23 #include <condition_variable>
25 #include <cassert>
26 #include <stdexcept>
27 #include <vector>
28 #include <algorithm>
30 namespace comphelper
32 AnyEvent::AnyEvent()
36 AnyEvent::~AnyEvent()
40 namespace {
42 struct ProcessableEvent
44 AnyEventRef aEvent;
45 ::rtl::Reference< IEventProcessor > xProcessor;
48 struct EqualProcessor
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
63 std::mutex aMutex;
64 std::condition_variable aPendingActions;
65 std::vector< ProcessableEvent > aEvents;
66 bool bTerminate;
67 // only used for AsyncEventNotifierAutoJoin
68 char const* name;
69 std::shared_ptr<AsyncEventNotifierAutoJoin> pKeepThisAlive;
71 EventNotifierImpl()
72 : bTerminate(false)
73 , name(nullptr)
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;
105 // awake the thread
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} );
117 // awake the thread
118 m_xImpl->aPendingActions.notify_all();
122 void AsyncEventNotifierBase::execute()
124 for (;;)
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)
132 return;
133 else
134 std::swap(aEvents, m_xImpl->aEvents);
136 for (ProcessableEvent& rEvent : aEvents)
138 assert(rEvent.xProcessor.is());
139 rEvent.xProcessor->processEvent(*rEvent.aEvent);
141 aEvents.clear();
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();
164 namespace {
166 std::mutex& GetTheNotifiersMutex()
168 static std::mutex MUTEX;
169 return 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(
186 wNotifier.lock());
187 if (pNotifier)
189 pNotifier->terminate();
190 pNotifier->join();
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
207 g_Notifiers.erase(
208 std::remove_if(g_Notifiers.begin(), g_Notifiers.end(),
209 [](std::weak_ptr<AsyncEventNotifierAutoJoin> const& w) {
210 return w.expired();
211 } ),
212 g_Notifiers.end());
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);
222 return 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");
238 g.dismiss();
241 void AsyncEventNotifierAutoJoin::run()
243 // see salhelper::Thread::run
244 comphelper::ScopeGuard g([this] { onTerminated(); });
245 setName(m_xImpl->name);
246 execute();
247 g.dismiss();
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: */