gecko-dev/xpcom/threads/PrioritizedEventQueue.h

123 строки
5.1 KiB
C
Исходник Обычный вид История

Bug 1382922 - Refactor event queue to allow multiple implementations (r=erahm) This patch refactors the nsThread event queue to clean it up and to make it easier to restructure. The fundamental concepts are as follows: Each nsThread will have a pointer to a refcounted SynchronizedEventQueue. A SynchronizedEQ takes care of doing the locking and condition variable work when posting and popping events. For the actual storage of events, it delegates to an AbstractEventQueue data structure. It keeps a UniquePtr to the AbstractEventQueue that it uses for storage. Both SynchronizedEQ and AbstractEventQueue are abstract classes. There is only one concrete implementation of SynchronizedEQ in this patch, which is called ThreadEventQueue. ThreadEventQueue uses locks and condition variables to post and pop events the same way nsThread does. It also encapsulates the functionality that DOM workers need to implement their special event loops (PushEventQueue and PopEventQueue). In later Quantum DOM work, I plan to have another SynchronizedEQ implementation for the main thread, called SchedulerEventQueue. It will have special code for the cooperatively scheduling threads in Quantum DOM. There are two concrete implementations of AbstractEventQueue in this patch: EventQueue and PrioritizedEventQueue. EventQueue replaces the old nsEventQueue. The other AbstractEventQueue implementation is PrioritizedEventQueue, which uses multiple queues for different event priorities. The final major piece here is ThreadEventTarget, which splits some of the code for posting events out of nsThread. Eventually, my plan is for multiple cooperatively scheduled nsThreads to be able to share a ThreadEventTarget. In this patch, though, each nsThread has its own ThreadEventTarget. The class's purpose is just to collect some related code together. One final note: I tried to avoid virtual dispatch overhead as much as possible. Calls to SynchronizedEQ methods do use virtual dispatch, since I plan to use different implementations for different threads with Quantum DOM. But all the calls to EventQueue methods should be non-virtual. Although the methods are declared virtual, all the classes used are final and the concrete classes involved should all be known through templatization. MozReview-Commit-ID: 9Evtr9oIJvx
2017-06-21 05:42:13 +03:00
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_PrioritizedEventQueue_h
#define mozilla_PrioritizedEventQueue_h
#include "mozilla/AbstractEventQueue.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/TypeTraits.h"
#include "mozilla/UniquePtr.h"
#include "nsCOMPtr.h"
#include "nsIIdlePeriod.h"
class nsIRunnable;
namespace mozilla {
// This AbstractEventQueue implementation has one queue for each EventPriority.
// The type of queue used for each priority is determined by the template
// parameter.
//
// When an event is pushed, its priority is determined by QIing the runnable to
// nsIRunnablePriority, or by falling back to the aPriority parameter if the QI
// fails.
//
// When an event is popped, a queue is selected based on heuristics that
// optimize for performance. Roughly, events are selected from the highest
// priority queue that is non-empty. However, there are a few exceptions:
// - We try to avoid processing too many high-priority events in a row so
// that the normal priority queue is not starved. When there are high-
// and normal-priority events available, we interleave popping from the
// normal and high queues.
// - We do not select events from the idle queue if the current idle period
// is almost over.
template<class InnerQueueT>
class PrioritizedEventQueue final : public AbstractEventQueue
{
public:
PrioritizedEventQueue(UniquePtr<InnerQueueT> aHighQueue,
UniquePtr<InnerQueueT> aInputQueue,
UniquePtr<InnerQueueT> aNormalQueue,
UniquePtr<InnerQueueT> aIdleQueue,
already_AddRefed<nsIIdlePeriod> aIdlePeriod);
void PutEvent(already_AddRefed<nsIRunnable>&& aEvent,
EventPriority aPriority,
const MutexAutoLock& aProofOfLock) final;
already_AddRefed<nsIRunnable> GetEvent(EventPriority* aPriority,
const MutexAutoLock& aProofOfLock) final;
bool HasPendingEvent(const MutexAutoLock& aProofOfLock) final;
size_t Count(const MutexAutoLock& aProofOfLock) const final;
// When checking the idle deadline, we need to drop whatever mutex protects
// this queue. This method allows that mutex to be stored so that we can drop
// it and reacquire it when checking the idle deadline. The mutex must live at
// least as long as the queue.
void SetMutexRef(Mutex& aMutex) { mMutex = &aMutex; }
// nsThread.cpp sends telemetry containing the most recently computed idle
// deadline. We store a reference to a field in nsThread where this deadline
// will be stored so that it can be fetched quickly for telemetry.
void SetNextIdleDeadlineRef(TimeStamp& aDeadline) { mNextIdleDeadline = &aDeadline; }
void EnableInputEventPrioritization(const MutexAutoLock& aProofOfLock) final;
private:
class EnablePrioritizationRunnable;
// Returns a null TimeStamp if we're not in the idle period.
mozilla::TimeStamp GetIdleDeadline();
UniquePtr<InnerQueueT> mHighQueue;
UniquePtr<InnerQueueT> mInputQueue;
UniquePtr<InnerQueueT> mNormalQueue;
UniquePtr<InnerQueueT> mIdleQueue;
// We need to drop the queue mutex when checking the idle deadline, so we keep
// a pointer to it here.
Mutex* mMutex = nullptr;
// Pointer to a place where the most recently computed idle deadline is
// stored.
TimeStamp* mNextIdleDeadline = nullptr;
// Try to process one high priority runnable after each normal
// priority runnable. This gives the processing model HTML spec has for
// 'Update the rendering' in the case only vsync messages are in the
// secondary queue and prevents starving the normal queue.
bool mProcessHighPriorityQueue = false;
// mIdlePeriod keeps track of the current idle period. If at any
// time the main event queue is empty, calling
// mIdlePeriod->GetIdlePeriodHint() will give an estimate of when
// the current idle period will end.
nsCOMPtr<nsIIdlePeriod> mIdlePeriod;
// Set to true if HasPendingEvents() has been called and returned true because
// of a pending idle event. This is used to remember to return that idle
// event from GetIdleEvent() to ensure that HasPendingEvents() never lies.
bool mHasPendingEventsPromisedIdleEvent = false;
TimeStamp mInputHandlingStartTime;
// When we enable input event prioritization, we immediately begin adding new
// input events to the input event queue (and set
// mWriteToInputQueue). However, we do not begin processing events from the
// input queue until all events that were in the normal priority queue have
// been processed. That ensures that input events will not jump ahead of
// events that were in the queue before prioritization was enabled.
bool mWriteToInputQueue = false;
bool mReadFromInputQueue = false;
};
class EventQueue;
extern template class PrioritizedEventQueue<EventQueue>;
} // namespace mozilla
#endif // mozilla_PrioritizedEventQueue_h