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 (bffa34334d58)

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
/* -*- 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 https://mozilla.org/MPL/2.0/. */

#include "GraphRunner.h"

#include "GraphDriver.h"
#include "MediaStreamGraph.h"
#include "MediaStreamGraphImpl.h"
#include "mozilla/dom/WorkletThread.h"
#include "nsISupportsImpl.h"
#include "prthread.h"
#include "Tracing.h"
#include "audio_thread_priority.h"

namespace mozilla {

static void Start(void* aArg) {
  NS_SetCurrentThreadName("GraphRunner");
  GraphRunner* th = static_cast<GraphRunner*>(aArg);
  th->Run();
}

GraphRunner::GraphRunner(MediaStreamGraphImpl* aGraph)
    : mMonitor("GraphRunner::mMonitor"),
      mGraph(aGraph),
      mStateEnd(0),
      mStillProcessing(true),
      mThreadState(ThreadState::Wait),
      // Note that mThread needs to be initialized last, as it may pre-empt the
      // thread running this ctor and enter Run() with uninitialized members.
      mThread(PR_CreateThread(PR_SYSTEM_THREAD, &Start, this,
                              PR_PRIORITY_URGENT, PR_GLOBAL_THREAD,
                              PR_JOINABLE_THREAD, 0)) {
  MOZ_COUNT_CTOR(GraphRunner);
}

GraphRunner::~GraphRunner() {
  MOZ_COUNT_DTOR(GraphRunner);
  MOZ_ASSERT(mThreadState == ThreadState::Shutdown);
}

void GraphRunner::Shutdown() {
  {
    MonitorAutoLock lock(mMonitor);
    MOZ_ASSERT(mThreadState == ThreadState::Wait);
    mThreadState = ThreadState::Shutdown;
    mMonitor.Notify();
  }
  // We need to wait for runner thread shutdown here for the sake of the
  // xpcomWillShutdown case, so that the main thread is not shut down before
  // cleanup messages are sent for objects destroyed in
  // CycleCollectedJSContext shutdown.
  PR_JoinThread(mThread);
  mThread = nullptr;
}

bool GraphRunner::OneIteration(GraphTime aStateEnd) {
  TRACE_AUDIO_CALLBACK();

  MonitorAutoLock lock(mMonitor);
  MOZ_ASSERT(mThreadState == ThreadState::Wait);
  mStateEnd = aStateEnd;

#ifdef DEBUG
  if (auto audioDriver = mGraph->CurrentDriver()->AsAudioCallbackDriver()) {
    mAudioDriverThreadId = audioDriver->ThreadId();
  } else if (auto clockDriver =
                 mGraph->CurrentDriver()->AsSystemClockDriver()) {
    mClockDriverThread = clockDriver->Thread();
  } else {
    MOZ_CRASH("Unknown GraphDriver");
  }
#endif
  // Signal that mStateEnd was updated
  mThreadState = ThreadState::Run;
  mMonitor.Notify();
  // Wait for mStillProcessing to update
  do {
    mMonitor.Wait();
  } while (mThreadState == ThreadState::Run);

#ifdef DEBUG
  mAudioDriverThreadId = std::thread::id();
  mClockDriverThread = nullptr;
#endif

  return mStillProcessing;
}

void GraphRunner::Run() {
  PR_SetCurrentThreadName("GraphRunner");

  atp_handle* handle =
      atp_promote_current_thread_to_real_time(0, mGraph->GraphRate());

  MonitorAutoLock lock(mMonitor);
  while (true) {
    while (mThreadState == ThreadState::Wait) {
      mMonitor.Wait();  // Wait for mStateEnd to update or for shutdown
    }
    if (mThreadState == ThreadState::Shutdown) {
      break;
    }
    TRACE();
    mStillProcessing = mGraph->OneIterationImpl(mStateEnd);
    // Signal that mStillProcessing was updated
    mThreadState = ThreadState::Wait;
    mMonitor.Notify();
  }

  if (handle) {
    atp_demote_current_thread_from_real_time(handle);
  }

  dom::WorkletThread::DeleteCycleCollectedJSContext();
}

bool GraphRunner::OnThread() { return PR_GetCurrentThread() == mThread; }

#ifdef DEBUG
bool GraphRunner::RunByGraphDriver(GraphDriver* aDriver) {
  if (!OnThread()) {
    return false;
  }

  if (auto audioDriver = aDriver->AsAudioCallbackDriver()) {
    return audioDriver->ThreadId() == mAudioDriverThreadId;
  }

  if (auto clockDriver = aDriver->AsSystemClockDriver()) {
    return clockDriver->Thread() == mClockDriverThread;
  }

  MOZ_CRASH("Unknown driver");
}
#endif

}  // namespace mozilla