Coverage Report

Created: 2026-03-31 11:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/comphelper/source/misc/asyncnotification.cxx
Line
Count
Source
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 <comphelper/scopeguard.hxx>
22
#include <mutex>
23
#include <condition_variable>
24
25
#include <cassert>
26
#include <stdexcept>
27
#include <vector>
28
29
namespace comphelper
30
{
31
    AnyEvent::AnyEvent()
32
0
    {
33
0
    }
34
35
    AnyEvent::~AnyEvent()
36
0
    {
37
0
    }
38
39
    namespace {
40
41
    struct ProcessableEvent
42
    {
43
        AnyEventRef                         aEvent;
44
        ::rtl::Reference< IEventProcessor > xProcessor;
45
    };
46
47
    struct EqualProcessor
48
    {
49
        const ::rtl::Reference< IEventProcessor >&  rProcessor;
50
0
        explicit EqualProcessor( const ::rtl::Reference< IEventProcessor >& _rProcessor ) :rProcessor( _rProcessor ) { }
51
52
        bool operator()( const ProcessableEvent& _rEvent )
53
0
        {
54
0
            return _rEvent.xProcessor.get() == rProcessor.get();
55
0
        }
56
    };
57
58
    }
59
60
    struct EventNotifierImpl
61
    {
62
        std::mutex        aMutex;
63
        std::condition_variable aPendingActions;
64
        std::vector< ProcessableEvent > aEvents;
65
        bool                bTerminate;
66
        // only used for AsyncEventNotifierAutoJoin
67
        char const*         name;
68
        std::shared_ptr<AsyncEventNotifierAutoJoin> pKeepThisAlive;
69
70
        EventNotifierImpl()
71
0
            : bTerminate(false)
72
0
            , name(nullptr)
73
0
        {
74
0
        }
75
    };
76
77
    AsyncEventNotifierBase::AsyncEventNotifierBase()
78
0
        : m_xImpl(new EventNotifierImpl)
79
0
    {
80
0
    }
81
82
83
    AsyncEventNotifierBase::~AsyncEventNotifierBase()
84
0
    {
85
0
    }
86
87
88
    void AsyncEventNotifierBase::removeEventsForProcessor( const ::rtl::Reference< IEventProcessor >& _xProcessor )
89
0
    {
90
0
        std::scoped_lock aGuard( m_xImpl->aMutex );
91
92
        // remove all events for this processor
93
0
        std::erase_if( m_xImpl->aEvents, EqualProcessor( _xProcessor ) );
94
0
    }
95
96
97
    void SAL_CALL AsyncEventNotifierBase::terminate()
98
0
    {
99
0
        std::scoped_lock aGuard( m_xImpl->aMutex );
100
101
        // remember the termination request
102
0
        m_xImpl->bTerminate = true;
103
104
        // awake the thread
105
0
        m_xImpl->aPendingActions.notify_all();
106
0
    }
107
108
109
    void AsyncEventNotifierBase::addEvent( const AnyEventRef& _rEvent, const ::rtl::Reference< IEventProcessor >& _xProcessor )
110
0
    {
111
0
        std::scoped_lock aGuard( m_xImpl->aMutex );
112
113
        // remember this event
114
0
        m_xImpl->aEvents.emplace_back( ProcessableEvent {_rEvent, _xProcessor} );
115
116
        // awake the thread
117
0
        m_xImpl->aPendingActions.notify_all();
118
0
    }
119
120
121
    void AsyncEventNotifierBase::execute()
122
0
    {
123
0
        for (;;)
124
0
        {
125
0
            std::vector< ProcessableEvent > aEvents;
126
0
            {
127
0
                std::unique_lock aGuard(m_xImpl->aMutex);
128
0
                m_xImpl->aPendingActions.wait(aGuard,
129
0
                    [this] { return m_xImpl->bTerminate || !m_xImpl->aEvents.empty(); } );
130
0
                if (m_xImpl->bTerminate)
131
0
                    return;
132
0
                else
133
0
                    std::swap(aEvents, m_xImpl->aEvents);
134
0
            }
135
0
            for (ProcessableEvent& rEvent : aEvents)
136
0
            {
137
0
                assert(rEvent.xProcessor.is());
138
0
                rEvent.xProcessor->processEvent(*rEvent.aEvent);
139
0
            }
140
0
            aEvents.clear();
141
0
        }
142
0
    }
143
144
    AsyncEventNotifier::AsyncEventNotifier(char const* name)
145
0
        : salhelper::Thread(name)
146
0
    {
147
0
    }
148
149
    AsyncEventNotifier::~AsyncEventNotifier()
150
0
    {
151
0
    }
152
153
    void AsyncEventNotifier::execute()
154
0
    {
155
0
        return AsyncEventNotifierBase::execute();
156
0
    }
157
158
    void AsyncEventNotifier::terminate()
159
0
    {
160
0
        return AsyncEventNotifierBase::terminate();
161
0
    }
162
163
    namespace {
164
165
    std::mutex& GetTheNotifiersMutex()
166
0
    {
167
0
        static std::mutex MUTEX;
168
0
        return MUTEX;
169
0
    }
170
171
    }
172
173
    static std::vector<std::weak_ptr<AsyncEventNotifierAutoJoin>> g_Notifiers;
174
175
    template class EventHolder<css::document::DocumentEvent>;
176
177
    void JoinAsyncEventNotifiers()
178
0
    {
179
0
        std::vector<std::weak_ptr<AsyncEventNotifierAutoJoin>> notifiers;
180
0
        {
181
0
            std::scoped_lock g(GetTheNotifiersMutex());
182
0
            notifiers = g_Notifiers;
183
0
        }
184
0
        for (std::weak_ptr<AsyncEventNotifierAutoJoin> const& wNotifier : notifiers)
185
0
        {
186
0
            std::shared_ptr<AsyncEventNotifierAutoJoin> const pNotifier(
187
0
                    wNotifier.lock());
188
0
            if (pNotifier)
189
0
            {
190
0
                pNotifier->terminate();
191
0
                pNotifier->join();
192
0
            }
193
0
        }
194
        // note it's possible that g_Notifiers isn't empty now in case of leaks,
195
        // particularly since the UNO service manager isn't disposed yet
196
0
    }
197
198
    AsyncEventNotifierAutoJoin::AsyncEventNotifierAutoJoin(char const* name)
199
0
    {
200
0
        m_xImpl->name = name;
201
0
    }
202
203
    AsyncEventNotifierAutoJoin::~AsyncEventNotifierAutoJoin()
204
0
    {
205
0
        std::scoped_lock g(GetTheNotifiersMutex());
206
        // note: this doesn't happen atomically with the refcount
207
        // hence it's possible this deletes > 1 or 0 elements
208
0
        std::erase_if(g_Notifiers,
209
0
                [](std::weak_ptr<AsyncEventNotifierAutoJoin> const& w) {
210
0
                    return w.expired();
211
0
                });
212
0
    }
213
214
    std::shared_ptr<AsyncEventNotifierAutoJoin>
215
    AsyncEventNotifierAutoJoin::newAsyncEventNotifierAutoJoin(char const* name)
216
0
    {
217
0
        std::shared_ptr<AsyncEventNotifierAutoJoin> ret(
218
0
                new AsyncEventNotifierAutoJoin(name));
219
0
        std::scoped_lock g(GetTheNotifiersMutex());
220
0
        g_Notifiers.push_back(ret);
221
0
        return ret;
222
0
    }
223
224
    void AsyncEventNotifierAutoJoin::terminate()
225
0
    {
226
0
        return AsyncEventNotifierBase::terminate();
227
0
    }
228
229
    void AsyncEventNotifierAutoJoin::launch(std::shared_ptr<AsyncEventNotifierAutoJoin> const& xThis)
230
0
    {
231
        // see salhelper::Thread::launch
232
0
        xThis->m_xImpl->pKeepThisAlive = xThis;
233
0
        comphelper::ScopeGuard g([&xThis] { xThis->m_xImpl->pKeepThisAlive.reset(); });
234
0
        if (!xThis->create()) {
235
0
            throw std::runtime_error("osl::Thread::create failed");
236
0
        }
237
0
        g.dismiss();
238
0
    }
239
240
    void AsyncEventNotifierAutoJoin::run()
241
0
    {
242
        // see salhelper::Thread::run
243
0
        comphelper::ScopeGuard g([this] { onTerminated(); });
244
0
        setName(m_xImpl->name);
245
0
        execute();
246
0
        g.dismiss();
247
0
    }
248
249
    void AsyncEventNotifierAutoJoin::onTerminated()
250
0
    {
251
        // try to delete "this"
252
0
        m_xImpl->pKeepThisAlive.reset();
253
0
    }
254
255
} // namespace comphelper
256
257
258
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */