Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / helper / wakeupthread.cxx
blob63d52a82da769832aad6955984b94e6abd0b7a4b
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 <sal/config.h>
22 #include <com/sun/star/uno/Reference.hxx>
23 #include <com/sun/star/util/XUpdatable.hpp>
24 #include <rtl/ref.hxx>
25 #include <osl/thread.hxx>
26 #include <salhelper/thread.hxx>
27 #include <condition_variable>
28 #include <chrono>
29 #include <vector>
30 #include <mutex>
32 #include <helper/wakeupthread.hxx>
34 using namespace std::chrono_literals;
36 /// We only need one thread to wake everyone up.
38 namespace
40 class SharedWakeUpThread final : public salhelper::Thread
42 static std::vector<css::uno::WeakReference<css::util::XUpdatable>> updatables;
43 std::condition_variable condition;
44 bool terminate;
46 public:
47 static rtl::Reference<SharedWakeUpThread> wakeupThread;
49 static std::mutex& getMutex()
51 static std::mutex mutex;
52 return mutex;
55 SharedWakeUpThread()
56 : Thread("WakeUpThread")
57 , terminate(false)
59 assert(!wakeupThread);
60 launch();
63 void execute() override
65 while (true)
67 std::unique_lock g(getMutex());
68 condition.wait_for(g, 25ms, [this] { return terminate; });
69 if (terminate || updatables.empty())
70 break;
72 auto copyOfUpdatables = updatables;
73 g.unlock();
75 for (auto& it : copyOfUpdatables)
77 css::uno::Reference<css::util::XUpdatable> up(it);
78 if (up.is()) // check weak
79 up->update();
83 std::unique_lock g(getMutex());
84 if (updatables.empty())
86 terminate = false;
87 wakeupThread.clear();
91 static void startThread()
93 std::unique_lock g(getMutex());
94 if (!updatables.empty() && !wakeupThread)
95 wakeupThread = new SharedWakeUpThread();
98 void stopWithLock(std::unique_lock<std::mutex>& g)
100 terminate = true;
101 condition.notify_one();
102 g.unlock();
104 join();
107 static void disposeThreadWithLock(std::unique_lock<std::mutex>& g)
109 if (wakeupThread)
111 auto holdRef = wakeupThread;
112 wakeupThread.clear();
113 holdRef->stopWithLock(g);
115 assert(!wakeupThread);
118 static void add(css::uno::WeakReference<css::util::XUpdatable> up)
120 std::unique_lock g(getMutex());
121 updatables.push_back(up);
122 if (!wakeupThread)
123 wakeupThread = new SharedWakeUpThread();
126 static void remove(css::uno::WeakReference<css::util::XUpdatable> up)
128 std::unique_lock g(getMutex());
129 auto it = updatables.begin();
130 bool found = false;
131 for (; it != updatables.end(); ++it)
133 css::uno::Reference<css::util::XUpdatable> itValid(*it);
134 if (!itValid || *it == up)
136 it = updatables.erase(it);
137 found = true;
138 break;
141 (void)found; assert(found);
142 if (updatables.empty())
143 disposeThreadWithLock(g);
146 static void joinThread()
148 std::unique_lock g(getMutex());
149 disposeThreadWithLock(g);
153 rtl::Reference<SharedWakeUpThread> SharedWakeUpThread::wakeupThread;
154 std::vector<css::uno::WeakReference<css::util::XUpdatable>> SharedWakeUpThread::updatables;
157 namespace framework
159 /* static */ void WakeUpThread::startThread() { SharedWakeUpThread::startThread(); }
161 WakeUpThread::WakeUpThread(css::uno::Reference<css::util::XUpdatable> const& up)
162 : _updatable(up)
164 assert(_updatable);
165 SharedWakeUpThread::add(_updatable);
168 void WakeUpThread::stop() { SharedWakeUpThread::remove(_updatable); }
170 /* static */ void WakeUpThread::joinThread() { SharedWakeUpThread::joinThread(); }
172 } // namespace framework
174 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */