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.

Header

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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
/* -*- 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/. */

#include "LabeledEventQueue.h"
#include "mozilla/dom/TabChild.h"
#include "mozilla/dom/TabGroup.h"
#include "mozilla/Scheduler.h"
#include "mozilla/SchedulerGroup.h"
#include "nsQueryObject.h"

using namespace mozilla::dom;

using EpochQueueEntry = SchedulerGroup::EpochQueueEntry;

LinkedList<SchedulerGroup>* LabeledEventQueue::sSchedulerGroups;
size_t LabeledEventQueue::sLabeledEventQueueCount;
SchedulerGroup* LabeledEventQueue::sCurrentSchedulerGroup;

LabeledEventQueue::LabeledEventQueue(EventPriority aPriority)
    : mPriority(aPriority) {
  // LabeledEventQueue should only be used by one consumer since it uses a
  // single static sSchedulerGroups field. It's hard to assert this, though, so
  // we assert NS_IsMainThread(), which is a reasonable proxy.
  MOZ_ASSERT(NS_IsMainThread());

  if (sLabeledEventQueueCount++ == 0) {
    sSchedulerGroups = new LinkedList<SchedulerGroup>();
  }
}

LabeledEventQueue::~LabeledEventQueue() {
  if (--sLabeledEventQueueCount == 0) {
    delete sSchedulerGroups;
    sSchedulerGroups = nullptr;
  }
}

static SchedulerGroup* GetSchedulerGroup(nsIRunnable* aEvent) {
  RefPtr<SchedulerGroup::Runnable> groupRunnable = do_QueryObject(aEvent);
  if (!groupRunnable) {
    // It's not labeled.
    return nullptr;
  }

  return groupRunnable->Group();
}

static bool IsReadyToRun(nsIRunnable* aEvent, SchedulerGroup* aEventGroup) {
  if (!Scheduler::AnyEventRunning()) {
    return true;
  }

  if (Scheduler::UnlabeledEventRunning()) {
    return false;
  }

  if (aEventGroup) {
    return !aEventGroup->IsRunning();
  }

  nsCOMPtr<nsILabelableRunnable> labelable = do_QueryInterface(aEvent);
  if (!labelable) {
    return false;
  }

  return labelable->IsReadyToRun();
}

void LabeledEventQueue::PutEvent(already_AddRefed<nsIRunnable>&& aEvent,
                                 EventPriority aPriority,
                                 const MutexAutoLock& aProofOfLock) {
  MOZ_ASSERT(aPriority == mPriority);

  nsCOMPtr<nsIRunnable> event(aEvent);

  MOZ_ASSERT(event.get());

  SchedulerGroup* group = GetSchedulerGroup(event);
  bool isLabeled = !!group;

  // Create a new epoch if necessary.
  Epoch* epoch;
  if (mEpochs.IsEmpty()) {
    epoch = &mEpochs.Push(Epoch::First(isLabeled));
  } else {
    Epoch& lastEpoch = mEpochs.LastElement();
    if (lastEpoch.IsLabeled() != isLabeled) {
      epoch = &mEpochs.Push(lastEpoch.NextEpoch(isLabeled));
    } else {
      epoch = &lastEpoch;
    }
  }

  mNumEvents++;
  epoch->mNumEvents++;

  RunnableEpochQueue& queue =
      isLabeled ? group->GetQueue(aPriority) : mUnlabeled;
  queue.Push(EpochQueueEntry(event.forget(), epoch->mEpochNumber));

  if (group && group->EnqueueEvent() == SchedulerGroup::NewlyQueued) {
    // This group didn't have any events before. Add it to the
    // sSchedulerGroups list.
    MOZ_ASSERT(!group->isInList());
    sSchedulerGroups->insertBack(group);
    if (!sCurrentSchedulerGroup) {
      sCurrentSchedulerGroup = group;
    }
  }
}

void LabeledEventQueue::PopEpoch() {
  Epoch& epoch = mEpochs.FirstElement();
  MOZ_ASSERT(epoch.mNumEvents > 0);
  if (epoch.mNumEvents == 1) {
    mEpochs.Pop();
  } else {
    epoch.mNumEvents--;
  }

  mNumEvents--;
}

// Returns the next SchedulerGroup after |aGroup| in sSchedulerGroups. Wraps
// around to the beginning of the list when we hit the end.
/* static */ SchedulerGroup* LabeledEventQueue::NextSchedulerGroup(
    SchedulerGroup* aGroup) {
  SchedulerGroup* result = aGroup->getNext();
  if (!result) {
    result = sSchedulerGroups->getFirst();
  }
  return result;
}

already_AddRefed<nsIRunnable> LabeledEventQueue::GetEvent(
    EventPriority* aPriority, const MutexAutoLock& aProofOfLock) {
  if (mEpochs.IsEmpty()) {
    return nullptr;
  }

  Epoch epoch = mEpochs.FirstElement();
  if (!epoch.IsLabeled()) {
    EpochQueueEntry& first = mUnlabeled.FirstElement();
    if (!IsReadyToRun(first.mRunnable, nullptr)) {
      return nullptr;
    }

    PopEpoch();
    EpochQueueEntry entry = mUnlabeled.Pop();
    MOZ_ASSERT(entry.mEpochNumber == epoch.mEpochNumber);
    MOZ_ASSERT(entry.mRunnable.get());
    return entry.mRunnable.forget();
  }

  if (!sCurrentSchedulerGroup) {
    return nullptr;
  }

  // Move visible tabs to the front of the queue. The mAvoidVisibleTabCount
  // field prevents us from preferentially processing events from visible tabs
  // twice in a row. This scheme is designed to prevent starvation.
  if (TabChild::HasVisibleTabs() && mAvoidVisibleTabCount <= 0) {
    for (auto iter = TabChild::GetVisibleTabs().ConstIter(); !iter.Done();
         iter.Next()) {
      SchedulerGroup* group = iter.Get()->GetKey()->TabGroup();
      if (!group->isInList() || group == sCurrentSchedulerGroup) {
        continue;
      }

      // For each visible tab we move to the front of the queue, we have to
      // process two SchedulerGroups (the visible tab and another one,
      // presumably a background group) before we prioritize visible tabs again.
      mAvoidVisibleTabCount += 2;

      // We move |group| right before sCurrentSchedulerGroup and then set
      // sCurrentSchedulerGroup to group.
      MOZ_ASSERT(group != sCurrentSchedulerGroup);
      group->removeFrom(*sSchedulerGroups);
      sCurrentSchedulerGroup->setPrevious(group);
      sCurrentSchedulerGroup = group;
    }
  }

  // Iterate over each SchedulerGroup once, starting at sCurrentSchedulerGroup.
  SchedulerGroup* firstGroup = sCurrentSchedulerGroup;
  SchedulerGroup* group = firstGroup;
  do {
    mAvoidVisibleTabCount--;

    RunnableEpochQueue& queue = group->GetQueue(mPriority);

    if (queue.IsEmpty()) {
      // This can happen if |group| is in a different LabeledEventQueue than
      // |this|.
      group = NextSchedulerGroup(group);
      continue;
    }

    EpochQueueEntry& first = queue.FirstElement();
    if (first.mEpochNumber == epoch.mEpochNumber &&
        IsReadyToRun(first.mRunnable, group)) {
      sCurrentSchedulerGroup = NextSchedulerGroup(group);

      PopEpoch();

      if (group->DequeueEvent() == SchedulerGroup::NoLongerQueued) {
        // Now we can take group out of sSchedulerGroups.
        if (sCurrentSchedulerGroup == group) {
          // Since we changed sCurrentSchedulerGroup above, we'll only get here
          // if |group| was the only element in sSchedulerGroups. In that case
          // set sCurrentSchedulerGroup to null.
          MOZ_ASSERT(group->getNext() == nullptr);
          MOZ_ASSERT(group->getPrevious() == nullptr);
          sCurrentSchedulerGroup = nullptr;
        }
        group->removeFrom(*sSchedulerGroups);
      }
      EpochQueueEntry entry = queue.Pop();
      return entry.mRunnable.forget();
    }

    group = NextSchedulerGroup(group);
  } while (group != firstGroup);

  return nullptr;
}

bool LabeledEventQueue::IsEmpty(const MutexAutoLock& aProofOfLock) {
  return mEpochs.IsEmpty();
}

size_t LabeledEventQueue::Count(const MutexAutoLock& aProofOfLock) const {
  return mNumEvents;
}

bool LabeledEventQueue::HasReadyEvent(const MutexAutoLock& aProofOfLock) {
  if (mEpochs.IsEmpty()) {
    return false;
  }

  Epoch& frontEpoch = mEpochs.FirstElement();

  if (!frontEpoch.IsLabeled()) {
    EpochQueueEntry& entry = mUnlabeled.FirstElement();
    return IsReadyToRun(entry.mRunnable, nullptr);
  }

  // Go through the scheduler groups and look for one that has events
  // for the priority of this labeled queue that is in the current
  // epoch and is allowed to run.
  uintptr_t currentEpoch = frontEpoch.mEpochNumber;
  for (SchedulerGroup* group : *sSchedulerGroups) {
    RunnableEpochQueue& queue = group->GetQueue(mPriority);
    if (queue.IsEmpty()) {
      continue;
    }

    EpochQueueEntry& entry = queue.FirstElement();
    if (entry.mEpochNumber != currentEpoch) {
      continue;
    }

    if (IsReadyToRun(entry.mRunnable, group)) {
      return true;
    }
  }

  return false;
}