1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
32 #include <helper/wakeupthread.hxx>
34 using namespace std::chrono_literals
;
36 /// We only need one thread to wake everyone up.
40 class SharedWakeUpThread final
: public salhelper::Thread
42 static std::vector
<css::uno::WeakReference
<css::util::XUpdatable
>> updatables
;
43 std::condition_variable condition
;
47 static rtl::Reference
<SharedWakeUpThread
> wakeupThread
;
49 static std::mutex
& getMutex()
51 static std::mutex mutex
;
56 : Thread("WakeUpThread")
59 assert(!wakeupThread
);
63 void execute() override
67 std::unique_lock
g(getMutex());
68 condition
.wait_for(g
, 25ms
, [this] { return terminate
; });
69 if (terminate
|| updatables
.empty())
72 auto copyOfUpdatables
= updatables
;
75 for (auto& it
: copyOfUpdatables
)
77 css::uno::Reference
<css::util::XUpdatable
> up(it
);
78 if (up
.is()) // check weak
83 std::unique_lock
g(getMutex());
84 if (updatables
.empty())
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
)
101 condition
.notify_one();
107 static void disposeThreadWithLock(std::unique_lock
<std::mutex
>& g
)
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
);
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();
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
);
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
;
159 /* static */ void WakeUpThread::startThread() { SharedWakeUpThread::startThread(); }
161 WakeUpThread::WakeUpThread(css::uno::Reference
<css::util::XUpdatable
> const& up
)
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: */