cid#1607171 Data race condition
[LibreOffice.git] / sd / source / ui / framework / configuration / ChangeRequestQueueProcessor.cxx
blobc67ecbb77a2235b6151bf721dbe7075623a75c7f
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 "debugtrace.hxx"
21 #include "ChangeRequestQueueProcessor.hxx"
22 #include "ConfigurationTracer.hxx"
24 #include "ConfigurationUpdater.hxx"
26 #include <utility>
27 #include <vcl/svapp.hxx>
28 #include <sal/log.hxx>
29 #include <com/sun/star/container/XNamed.hpp>
30 #include <com/sun/star/drawing/framework/XConfigurationChangeRequest.hpp>
32 using namespace ::com::sun::star;
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::drawing::framework;
36 namespace {
38 #if DEBUG_SD_CONFIGURATION_TRACE
40 void TraceRequest (const Reference<XConfigurationChangeRequest>& rxRequest)
42 Reference<container::XNamed> xNamed (rxRequest, UNO_QUERY);
43 if (xNamed.is())
44 SAL_INFO("sd.fwk", __func__ << ": " << xNamed->getName());
47 #endif
49 } // end of anonymous namespace
51 namespace sd::framework {
53 ChangeRequestQueueProcessor::ChangeRequestQueueProcessor (
54 std::shared_ptr<ConfigurationUpdater> pConfigurationUpdater)
55 : mnUserEventId(nullptr),
56 mpConfigurationUpdater(std::move(pConfigurationUpdater))
60 ChangeRequestQueueProcessor::~ChangeRequestQueueProcessor()
62 if (mnUserEventId != nullptr)
63 Application::RemoveUserEvent(mnUserEventId);
66 void ChangeRequestQueueProcessor::SetConfiguration (
67 const Reference<XConfiguration>& rxConfiguration)
69 ::osl::MutexGuard aGuard (maMutex);
71 mxConfiguration = rxConfiguration;
72 StartProcessing();
75 void ChangeRequestQueueProcessor::AddRequest (
76 const Reference<XConfigurationChangeRequest>& rxRequest)
78 ::osl::MutexGuard aGuard (maMutex);
80 #if DEBUG_SD_CONFIGURATION_TRACE
81 if (maQueue.empty())
83 SAL_INFO("sd.fwk", __func__ << ": Adding requests to empty queue");
84 ConfigurationTracer::TraceConfiguration(
85 mxConfiguration, "current configuration of queue processor");
87 SAL_INFO("sd.fwk", __func__ << ": Adding request");
88 TraceRequest(rxRequest);
89 #endif
91 maQueue.push(rxRequest);
92 StartProcessing();
95 void ChangeRequestQueueProcessor::StartProcessing()
97 ::osl::MutexGuard aGuard (maMutex);
99 if (mnUserEventId == nullptr
100 && mxConfiguration.is()
101 && ! maQueue.empty())
103 SAL_INFO("sd.fwk", __func__ << ": ChangeRequestQueueProcessor scheduling processing");
104 mnUserEventId = Application::PostUserEvent(
105 LINK(this,ChangeRequestQueueProcessor,ProcessEvent));
109 IMPL_LINK_NOARG(ChangeRequestQueueProcessor, ProcessEvent, void*, void)
111 ::osl::MutexGuard aGuard (maMutex);
113 mnUserEventId = nullptr;
115 ProcessOneEvent();
117 if ( ! maQueue.empty())
119 // Schedule the processing of the next event.
120 StartProcessing();
124 void ChangeRequestQueueProcessor::ProcessOneEvent()
126 ::osl::MutexGuard aGuard (maMutex);
128 SAL_INFO("sd.fwk", __func__ << ": ProcessOneEvent");
130 if (!mxConfiguration.is() || maQueue.empty())
131 return;
133 // Get and remove the first entry from the queue.
134 Reference<XConfigurationChangeRequest> xRequest (maQueue.front());
135 maQueue.pop();
137 // Execute the change request.
138 if (xRequest.is())
140 #if DEBUG_SD_CONFIGURATION_TRACE
141 TraceRequest(xRequest);
142 #endif
143 xRequest->execute(mxConfiguration);
146 if (!maQueue.empty())
147 return;
149 SAL_INFO("sd.fwk", __func__ << ": All requests are processed");
150 // The queue is empty so tell the ConfigurationManager to update
151 // its state.
152 if (mpConfigurationUpdater != nullptr)
154 #if DEBUG_SD_CONFIGURATION_TRACE
155 ConfigurationTracer::TraceConfiguration (
156 mxConfiguration, "updating to configuration");
157 #endif
158 mpConfigurationUpdater->RequestUpdate(mxConfiguration);
162 bool ChangeRequestQueueProcessor::IsEmpty() const
164 return maQueue.empty();
167 void ChangeRequestQueueProcessor::ProcessUntilEmpty()
169 while ( ! IsEmpty())
170 ProcessOneEvent();
173 void ChangeRequestQueueProcessor::Clear()
175 ::osl::MutexGuard aGuard (maMutex);
176 ChangeRequestQueue().swap(maQueue);
179 } // end of namespace sd::framework
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */