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
/* -*- 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 "CooperativeThreadPool.h"

#include "base/message_loop.h"
#include "mozilla/IOInterposer.h"
#include "mozilla/ServoBindings.h"
#include "nsError.h"
#include "nsThreadUtils.h"

using namespace mozilla;

static bool gCooperativeSchedulingEnabled;
MOZ_THREAD_LOCAL(CooperativeThreadPool::CooperativeThread*)
CooperativeThreadPool::sTlsCurrentThread;

// Windows silliness. winbase.h defines an empty no-argument Yield macro.
#undef Yield

CooperativeThreadPool::CooperativeThreadPool(size_t aNumThreads, Mutex& aMutex,
                                             Controller& aController)
    : mMutex(aMutex),
      mShutdownCondition(mMutex, "CoopShutdown"),
      mRunning(false),
      mNumThreads(std::min(aNumThreads, kMaxThreads)),
      mRunningThreads(0),
      mController(aController),
      mSelectedThread(size_t(0)) {
  MOZ_ASSERT(aNumThreads <= kMaxThreads);

  gCooperativeSchedulingEnabled = true;
  sTlsCurrentThread.infallibleInit();

  MutexAutoLock lock(mMutex);

  mRunning = true;
  mRunningThreads = mNumThreads;

  for (size_t i = 0; i < mNumThreads; i++) {
    mThreads[i] = MakeUnique<CooperativeThread>(this, i);
  }
}

CooperativeThreadPool::~CooperativeThreadPool() { MOZ_ASSERT(!mRunning); }

const size_t CooperativeThreadPool::kMaxThreads;

void CooperativeThreadPool::Shutdown() {
  // This will not be called on any of the cooperative threads.
  {
    MutexAutoLock lock(mMutex);
    MOZ_ASSERT(mRunning);
    mRunning = false;
  }

  for (size_t i = 0; i < mNumThreads; i++) {
    mThreads[i]->BeginShutdown();
  }

  {
    MutexAutoLock lock(mMutex);
    while (mRunningThreads) {
      mShutdownCondition.Wait();
    }
  }

  for (size_t i = 0; i < mNumThreads; i++) {
    mThreads[i]->EndShutdown();
  }
}

void CooperativeThreadPool::RecheckBlockers(const MutexAutoLock& aProofOfLock) {
  aProofOfLock.AssertOwns(mMutex);

  if (!mSelectedThread.is<AllThreadsBlocked>()) {
    return;
  }

  for (size_t i = 0; i < mNumThreads; i++) {
    if (mThreads[i]->mRunning && !mThreads[i]->IsBlocked(aProofOfLock)) {
      mSelectedThread = AsVariant(i);
      mThreads[i]->mCondVar.Notify();
      return;
    }
  }

  // It may be valid to reach this point. For example, if we are waiting for an
  // event to be posted from a non-main thread. Even if the queue is non-empty,
  // it may have only idle events that we do not want to run (because we are
  // expecting a vsync soon).
}

/* static */ void CooperativeThreadPool::Yield(
    Resource* aBlocker, const MutexAutoLock& aProofOfLock) {
  if (!gCooperativeSchedulingEnabled) {
    return;
  }

  CooperativeThread* thread = sTlsCurrentThread.get();
  MOZ_RELEASE_ASSERT(thread);
  thread->SetBlocker(aBlocker);
  thread->Yield(aProofOfLock);
}

/* static */ bool CooperativeThreadPool::IsCooperativeThread() {
  if (!gCooperativeSchedulingEnabled) {
    return false;
  }
  return !!sTlsCurrentThread.get();
}

CooperativeThreadPool::SelectedThread CooperativeThreadPool::CurrentThreadIndex(
    const MutexAutoLock& aProofOfLock) const {
  aProofOfLock.AssertOwns(mMutex);
  return mSelectedThread;
}

CooperativeThreadPool::CooperativeThread::CooperativeThread(
    CooperativeThreadPool* aPool, size_t aIndex)
    : mPool(aPool),
      mCondVar(aPool->mMutex, "CooperativeThreadPool"),
      mBlocker(nullptr),
      mIndex(aIndex),
      mRunning(true) {
  mThread =
      PR_CreateThread(PR_USER_THREAD, ThreadFunc, this, PR_PRIORITY_NORMAL,
                      PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
  MOZ_RELEASE_ASSERT(mThread);
}

void CooperativeThreadPool::CooperativeThread::ThreadMethod() {
  char stackTop;

  MOZ_ASSERT(gCooperativeSchedulingEnabled);
  sTlsCurrentThread.set(this);

  nsCString name = mPool->mThreadNaming.GetNextThreadName("Main");
  PR_SetCurrentThreadName(name.get());

  mozilla::IOInterposer::RegisterCurrentThread();

  {
    // Make sure only one thread at a time can proceed. This only happens during
    // thread startup.
    MutexAutoLock lock(mPool->mMutex);
    while (mPool->mSelectedThread != AsVariant(mIndex)) {
      mCondVar.Wait();
    }
  }

  mPool->mController.OnStartThread(mIndex, name, &stackTop);

  nsCOMPtr<nsIThread> thread = do_GetCurrentThread();
  mEventTarget = thread;

  // The main event loop for this thread.
  for (;;) {
    {
      MutexAutoLock lock(mPool->mMutex);
      if (!mPool->mRunning) {
        break;
      }
    }

    bool processedEvent;
    thread->ProcessNextEvent(true, &processedEvent);
  }

  mPool->mController.OnStopThread(mIndex);
  mozilla::IOInterposer::UnregisterCurrentThread();

  MutexAutoLock lock(mPool->mMutex);
  mPool->mRunningThreads--;
  mRunning = false;
  mPool->mSelectedThread = AsVariant(AllThreadsBlocked::Blocked);
  mPool->RecheckBlockers(lock);
  mPool->mShutdownCondition.Notify();
}

/* static */ void CooperativeThreadPool::CooperativeThread::ThreadFunc(
    void* aArg) {
  auto thread = static_cast<CooperativeThreadPool::CooperativeThread*>(aArg);
  thread->ThreadMethod();
}

void CooperativeThreadPool::CooperativeThread::BeginShutdown() {
  mEventTarget->Dispatch(new mozilla::Runnable("CooperativeShutdownEvent"),
                         nsIEventTarget::DISPATCH_NORMAL);
}

void CooperativeThreadPool::CooperativeThread::EndShutdown() {
  PR_JoinThread(mThread);
}

bool CooperativeThreadPool::CooperativeThread::IsBlocked(
    const MutexAutoLock& aProofOfLock) {
  if (!mBlocker) {
    return false;
  }

  return !mBlocker->IsAvailable(aProofOfLock);
}

void CooperativeThreadPool::CooperativeThread::Yield(
    const MutexAutoLock& aProofOfLock) {
  aProofOfLock.AssertOwns(mPool->mMutex);

  // First select the next thread to run.
  size_t selected = mIndex + 1;
  bool found = false;
  do {
    if (selected >= mPool->mNumThreads) {
      selected = 0;
    }

    if (mPool->mThreads[selected]->mRunning &&
        !mPool->mThreads[selected]->IsBlocked(aProofOfLock)) {
      found = true;
      break;
    }
    selected++;
  } while (selected != mIndex + 1);

  if (found) {
    mPool->mSelectedThread = AsVariant(selected);
    mPool->mThreads[selected]->mCondVar.Notify();
  } else {
    // We need to block all threads. Some thread will be unblocked when
    // RecheckBlockers is called (if a new event is posted for an outside
    // thread, for example).
    mPool->mSelectedThread = AsVariant(AllThreadsBlocked::Blocked);
  }

  mPool->mController.OnSuspendThread(mIndex);

  while (mPool->mSelectedThread != AsVariant(mIndex)) {
    mCondVar.Wait();
  }

  mPool->mController.OnResumeThread(mIndex);
}