MainThreadNotifier.h [plain text]
#ifndef MainThreadNotifier_h
#define MainThreadNotifier_h
#include <wtf/Lock.h>
#include <wtf/MainThread.h>
#include <wtf/RunLoop.h>
#include <wtf/WeakPtr.h>
namespace WebCore {
template <typename T>
class MainThreadNotifier {
public:
MainThreadNotifier()
: m_weakPtrFactory(this)
{
}
template<typename F>
void notify(T notificationType, const F& callbackFunctor)
{
if (isMainThread()) {
removePendingNotification(notificationType);
callbackFunctor();
return;
}
if (!addPendingNotification(notificationType))
return;
auto weakThis = m_weakPtrFactory.createWeakPtr();
std::function<void ()> callback(callbackFunctor);
RunLoop::main().dispatch([weakThis, notificationType, callback] {
if (weakThis && weakThis->removePendingNotification(notificationType))
callback();
});
}
void cancelPendingNotifications(unsigned mask = 0)
{
LockHolder locker(m_pendingNotificationsLock);
if (mask)
m_pendingNotifications &= ~mask;
else
m_pendingNotifications = 0;
}
private:
bool addPendingNotification(T notificationType)
{
LockHolder locker(m_pendingNotificationsLock);
if (notificationType & m_pendingNotifications)
return false;
m_pendingNotifications |= notificationType;
return true;
}
bool removePendingNotification(T notificationType)
{
LockHolder locker(m_pendingNotificationsLock);
if (notificationType & m_pendingNotifications) {
m_pendingNotifications &= ~notificationType;
return true;
}
return false;
}
WeakPtrFactory<MainThreadNotifier> m_weakPtrFactory;
Lock m_pendingNotificationsLock;
unsigned m_pendingNotifications { 0 };
};
}
#endif // MainThreadNotifier_h