DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (1aeaa33a64f9)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
/* -*- 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_AbstractEventQueue_h
#define mozilla_AbstractEventQueue_h

#include "mozilla/AlreadyAddRefed.h"
#include "mozilla/Mutex.h"

class nsIRunnable;

namespace mozilla {

enum class EventPriority {
  High,
  Input,
  Normal,
  Idle,

  Count
};

// AbstractEventQueue is an abstract base class for all our unsynchronized event
// queue implementations:
// - EventQueue: A queue of runnables. Used for non-main threads.
// - PrioritizedEventQueue: Contains a queue for each priority level.
//       Has heuristics to decide which queue to pop from. Events are
//       pushed into the queue corresponding to their priority.
//       Used for the main thread.
//
// Since AbstractEventQueue implementations are unsynchronized, they should be
// wrapped in an outer SynchronizedEventQueue implementation (like
// ThreadEventQueue).
//
// Subclasses should also define a `static const bool SupportsPrioritization`
// member to indicate whether the subclass cares about runnable priorities
// implemented through nsIRunnablePriority.
class AbstractEventQueue {
 public:
  // Add an event to the end of the queue. Implementors are free to use
  // aPriority however they wish.  If the runnable supports nsIRunnablePriority
  // and the implementing class supports prioritization, aPriority represents
  // the result of calling nsIRunnablePriority::GetPriority().
  virtual void PutEvent(already_AddRefed<nsIRunnable>&& aEvent,
                        EventPriority aPriority,
                        const MutexAutoLock& aProofOfLock) = 0;

  // Get an event from the front of the queue. aPriority is an out param. If the
  // implementation supports priorities, then this should be the same priority
  // that the event was pushed with. aPriority may be null. This should return
  // null if the queue is non-empty but the event in front is not ready to run.
  virtual already_AddRefed<nsIRunnable> GetEvent(
      EventPriority* aPriority, const MutexAutoLock& aProofOfLock) = 0;

  // Returns true if the queue is empty. Implies !HasReadyEvent().
  virtual bool IsEmpty(const MutexAutoLock& aProofOfLock) = 0;

  // Returns true if the queue is non-empty and if the event in front is ready
  // to run. Implies !IsEmpty(). This should return true iff GetEvent returns a
  // non-null value.
  virtual bool HasReadyEvent(const MutexAutoLock& aProofOfLock) = 0;

  // Returns the number of events in the queue.
  virtual size_t Count(const MutexAutoLock& aProofOfLock) const = 0;

  virtual void EnableInputEventPrioritization(
      const MutexAutoLock& aProofOfLock) = 0;
  virtual void FlushInputEventPrioritization(
      const MutexAutoLock& aProofOfLock) = 0;
  virtual void SuspendInputEventPrioritization(
      const MutexAutoLock& aProofOfLock) = 0;
  virtual void ResumeInputEventPrioritization(
      const MutexAutoLock& aProofOfLock) = 0;

  virtual ~AbstractEventQueue() {}
};

}  // namespace mozilla

#endif  // mozilla_AbstractEventQueue_h