| 1 | /* |
| 2 | * Copyright (C) 2015 Igalia S.L |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include <functional> |
| 22 | #include <wtf/Atomics.h> |
| 23 | #include <wtf/Lock.h> |
| 24 | #include <wtf/MainThread.h> |
| 25 | #include <wtf/RunLoop.h> |
| 26 | #include <wtf/ThreadSafeRefCounted.h> |
| 27 | |
| 28 | namespace WebCore { |
| 29 | |
| 30 | template <typename T> |
| 31 | class MainThreadNotifier final : public ThreadSafeRefCounted<MainThreadNotifier<T>> { |
| 32 | public: |
| 33 | static Ref<MainThreadNotifier> create() |
| 34 | { |
| 35 | return adoptRef(*new MainThreadNotifier()); |
| 36 | } |
| 37 | |
| 38 | ~MainThreadNotifier() |
| 39 | { |
| 40 | ASSERT(!m_isValid.load()); |
| 41 | } |
| 42 | |
| 43 | bool isValid() const { return m_isValid.load(); } |
| 44 | |
| 45 | template<typename F> |
| 46 | void notify(T notificationType, F&& callbackFunctor) |
| 47 | { |
| 48 | ASSERT(m_isValid.load()); |
| 49 | // Assert that there is only one bit on at a time. |
| 50 | ASSERT(!(static_cast<int>(notificationType) & (static_cast<int>(notificationType) - 1))); |
| 51 | if (isMainThread()) { |
| 52 | removePendingNotification(notificationType); |
| 53 | callbackFunctor(); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | if (!addPendingNotification(notificationType)) |
| 58 | return; |
| 59 | |
| 60 | RunLoop::main().dispatch([this, protectedThis = makeRef(*this), notificationType, callback = WTF::Function<void()>(WTFMove(callbackFunctor))] { |
| 61 | if (!m_isValid.load()) |
| 62 | return; |
| 63 | if (removePendingNotification(notificationType)) |
| 64 | callback(); |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | template<typename F> |
| 69 | void notifyAndWait(T notificationType, F&& callbackFunctor) |
| 70 | { |
| 71 | Lock mutex; |
| 72 | Condition condition; |
| 73 | |
| 74 | notify(notificationType, [functor = WTFMove(callbackFunctor), &condition, &mutex] { |
| 75 | functor(); |
| 76 | LockHolder holder(mutex); |
| 77 | condition.notifyOne(); |
| 78 | }); |
| 79 | |
| 80 | if (!isMainThread()) { |
| 81 | LockHolder holder(mutex); |
| 82 | condition.wait(mutex); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void cancelPendingNotifications(unsigned mask = 0) |
| 87 | { |
| 88 | ASSERT(m_isValid.load()); |
| 89 | LockHolder locker(m_pendingNotificationsLock); |
| 90 | if (mask) |
| 91 | m_pendingNotifications &= ~mask; |
| 92 | else |
| 93 | m_pendingNotifications = 0; |
| 94 | } |
| 95 | |
| 96 | void invalidate() |
| 97 | { |
| 98 | ASSERT(m_isValid.load()); |
| 99 | m_isValid.store(false); |
| 100 | } |
| 101 | |
| 102 | private: |
| 103 | MainThreadNotifier() |
| 104 | { |
| 105 | m_isValid.store(true); |
| 106 | } |
| 107 | |
| 108 | bool addPendingNotification(T notificationType) |
| 109 | { |
| 110 | LockHolder locker(m_pendingNotificationsLock); |
| 111 | if (notificationType & m_pendingNotifications) |
| 112 | return false; |
| 113 | m_pendingNotifications |= notificationType; |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | bool removePendingNotification(T notificationType) |
| 118 | { |
| 119 | LockHolder locker(m_pendingNotificationsLock); |
| 120 | if (notificationType & m_pendingNotifications) { |
| 121 | m_pendingNotifications &= ~notificationType; |
| 122 | return true; |
| 123 | } |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | Lock m_pendingNotificationsLock; |
| 128 | unsigned m_pendingNotifications { 0 }; |
| 129 | Atomic<bool> m_isValid; |
| 130 | }; |
| 131 | |
| 132 | } // namespace WebCore |
| 133 | |
| 134 | |