/src/libreoffice/framework/source/helper/wakeupthread.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 <sal/config.h> |
21 | | |
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> |
31 | | |
32 | | #include <helper/wakeupthread.hxx> |
33 | | |
34 | | using namespace std::chrono_literals; |
35 | | |
36 | | /// We only need one thread to wake everyone up. |
37 | | |
38 | | namespace |
39 | | { |
40 | | class SharedWakeUpThread final : public salhelper::Thread |
41 | | { |
42 | | static std::vector<css::uno::WeakReference<css::util::XUpdatable>> updatables; |
43 | | std::condition_variable condition; |
44 | | bool terminate; |
45 | | |
46 | | public: |
47 | | static rtl::Reference<SharedWakeUpThread> wakeupThread; |
48 | | |
49 | | static std::mutex& getMutex() |
50 | 0 | { |
51 | 0 | static std::mutex mutex; |
52 | 0 | return mutex; |
53 | 0 | } |
54 | | |
55 | | SharedWakeUpThread() |
56 | 0 | : Thread("WakeUpThread") |
57 | 0 | , terminate(false) |
58 | 0 | { |
59 | 0 | assert(!wakeupThread); |
60 | 0 | launch(); |
61 | 0 | } |
62 | | |
63 | | void execute() override |
64 | 0 | { |
65 | 0 | while (true) |
66 | 0 | { |
67 | 0 | std::unique_lock g(getMutex()); |
68 | 0 | condition.wait_for(g, 200ms, [this] { return terminate; }); |
69 | 0 | if (terminate || updatables.empty()) |
70 | 0 | break; |
71 | | |
72 | 0 | auto copyOfUpdatables = updatables; |
73 | 0 | g.unlock(); |
74 | |
|
75 | 0 | for (auto& it : copyOfUpdatables) |
76 | 0 | { |
77 | 0 | css::uno::Reference<css::util::XUpdatable> up(it); |
78 | 0 | if (up.is()) // check weak |
79 | 0 | up->update(); |
80 | 0 | } |
81 | 0 | } |
82 | |
|
83 | 0 | std::unique_lock g(getMutex()); |
84 | 0 | if (updatables.empty()) |
85 | 0 | { |
86 | 0 | terminate = false; |
87 | 0 | wakeupThread.clear(); |
88 | 0 | } |
89 | 0 | } |
90 | | |
91 | | static void startThread() |
92 | 0 | { |
93 | 0 | std::unique_lock g(getMutex()); |
94 | 0 | if (!updatables.empty() && !wakeupThread) |
95 | 0 | wakeupThread = new SharedWakeUpThread(); |
96 | 0 | } |
97 | | |
98 | | void stopWithLock(std::unique_lock<std::mutex>& g) |
99 | 0 | { |
100 | 0 | terminate = true; |
101 | 0 | condition.notify_one(); |
102 | 0 | g.unlock(); |
103 | |
|
104 | 0 | join(); |
105 | 0 | } |
106 | | |
107 | | static void disposeThreadWithLock(std::unique_lock<std::mutex>& g) |
108 | 0 | { |
109 | 0 | if (wakeupThread) |
110 | 0 | { |
111 | 0 | auto holdRef = wakeupThread; |
112 | 0 | wakeupThread.clear(); |
113 | 0 | holdRef->stopWithLock(g); |
114 | 0 | } |
115 | 0 | assert(!wakeupThread); |
116 | 0 | } |
117 | | |
118 | | static void add(css::uno::WeakReference<css::util::XUpdatable> up) |
119 | 0 | { |
120 | 0 | std::unique_lock g(getMutex()); |
121 | 0 | updatables.push_back(up); |
122 | 0 | if (!wakeupThread) |
123 | 0 | wakeupThread = new SharedWakeUpThread(); |
124 | 0 | } |
125 | | |
126 | | static void remove(css::uno::WeakReference<css::util::XUpdatable> up) |
127 | 0 | { |
128 | 0 | std::unique_lock g(getMutex()); |
129 | 0 | auto it = updatables.begin(); |
130 | 0 | bool found = false; |
131 | 0 | for (; it != updatables.end(); ++it) |
132 | 0 | { |
133 | 0 | css::uno::Reference<css::util::XUpdatable> itValid(*it); |
134 | 0 | if (!itValid || *it == up) |
135 | 0 | { |
136 | 0 | it = updatables.erase(it); |
137 | 0 | found = true; |
138 | 0 | break; |
139 | 0 | } |
140 | 0 | } |
141 | 0 | (void)found; assert(found); |
142 | 0 | if (updatables.empty()) |
143 | 0 | disposeThreadWithLock(g); |
144 | 0 | } |
145 | | |
146 | | static void joinThread() |
147 | 0 | { |
148 | 0 | std::unique_lock g(getMutex()); |
149 | 0 | disposeThreadWithLock(g); |
150 | 0 | } |
151 | | }; |
152 | | |
153 | | rtl::Reference<SharedWakeUpThread> SharedWakeUpThread::wakeupThread; |
154 | | std::vector<css::uno::WeakReference<css::util::XUpdatable>> SharedWakeUpThread::updatables; |
155 | | } |
156 | | |
157 | | namespace framework |
158 | | { |
159 | 0 | /* static */ void WakeUpThread::startThread() { SharedWakeUpThread::startThread(); } |
160 | | |
161 | | WakeUpThread::WakeUpThread(css::uno::Reference<css::util::XUpdatable> const& up) |
162 | 0 | : _updatable(up) |
163 | 0 | { |
164 | 0 | assert(_updatable); |
165 | 0 | SharedWakeUpThread::add(_updatable); |
166 | 0 | } |
167 | | |
168 | 0 | void WakeUpThread::stop() { SharedWakeUpThread::remove(_updatable); } |
169 | | |
170 | 0 | /* static */ void WakeUpThread::joinThread() { SharedWakeUpThread::joinThread(); } |
171 | | |
172 | | } // namespace framework |
173 | | |
174 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |