michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsEventQueue.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "prlog.h" michael@0: #include "nsThreadUtils.h" michael@0: #include "prthread.h" michael@0: #include "mozilla/ChaosMode.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: #ifdef PR_LOGGING michael@0: static PRLogModuleInfo * michael@0: GetLog() michael@0: { michael@0: static PRLogModuleInfo *sLog; michael@0: if (!sLog) michael@0: sLog = PR_NewLogModule("nsEventQueue"); michael@0: return sLog; michael@0: } michael@0: #endif michael@0: #ifdef LOG michael@0: #undef LOG michael@0: #endif michael@0: #define LOG(args) PR_LOG(GetLog(), PR_LOG_DEBUG, args) michael@0: michael@0: nsEventQueue::nsEventQueue() michael@0: : mReentrantMonitor("nsEventQueue.mReentrantMonitor") michael@0: , mHead(nullptr) michael@0: , mTail(nullptr) michael@0: , mOffsetHead(0) michael@0: , mOffsetTail(0) michael@0: { michael@0: } michael@0: michael@0: nsEventQueue::~nsEventQueue() michael@0: { michael@0: // It'd be nice to be able to assert that no one else is holding the monitor, michael@0: // but NSPR doesn't really expose APIs for it. michael@0: NS_ASSERTION(IsEmpty(), "Non-empty event queue being destroyed; events being leaked."); michael@0: michael@0: if (mHead) michael@0: FreePage(mHead); michael@0: } michael@0: michael@0: bool michael@0: nsEventQueue::GetEvent(bool mayWait, nsIRunnable **result) michael@0: { michael@0: { michael@0: ReentrantMonitorAutoEnter mon(mReentrantMonitor); michael@0: michael@0: while (IsEmpty()) { michael@0: if (!mayWait) { michael@0: if (result) michael@0: *result = nullptr; michael@0: return false; michael@0: } michael@0: LOG(("EVENTQ(%p): wait begin\n", this)); michael@0: mon.Wait(); michael@0: LOG(("EVENTQ(%p): wait end\n", this)); michael@0: } michael@0: michael@0: if (result) { michael@0: *result = mHead->mEvents[mOffsetHead++]; michael@0: michael@0: // Check if mHead points to empty Page michael@0: if (mOffsetHead == EVENTS_PER_PAGE) { michael@0: Page *dead = mHead; michael@0: mHead = mHead->mNext; michael@0: FreePage(dead); michael@0: mOffsetHead = 0; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: nsEventQueue::PutEvent(nsIRunnable *runnable) michael@0: { michael@0: // Avoid calling AddRef+Release while holding our monitor. michael@0: nsRefPtr event(runnable); michael@0: bool rv = true; michael@0: { michael@0: if (ChaosMode::isActive()) { michael@0: // With probability 0.5, yield so other threads have a chance to michael@0: // dispatch events to this queue first. michael@0: if (ChaosMode::randomUint32LessThan(2)) { michael@0: PR_Sleep(PR_INTERVAL_NO_WAIT); michael@0: } michael@0: } michael@0: michael@0: ReentrantMonitorAutoEnter mon(mReentrantMonitor); michael@0: michael@0: if (!mHead) { michael@0: mHead = NewPage(); michael@0: if (!mHead) { michael@0: rv = false; michael@0: } else { michael@0: mTail = mHead; michael@0: mOffsetHead = 0; michael@0: mOffsetTail = 0; michael@0: } michael@0: } else if (mOffsetTail == EVENTS_PER_PAGE) { michael@0: Page *page = NewPage(); michael@0: if (!page) { michael@0: rv = false; michael@0: } else { michael@0: mTail->mNext = page; michael@0: mTail = page; michael@0: mOffsetTail = 0; michael@0: } michael@0: } michael@0: if (rv) { michael@0: event.swap(mTail->mEvents[mOffsetTail]); michael@0: ++mOffsetTail; michael@0: LOG(("EVENTQ(%p): notify\n", this)); michael@0: mon.NotifyAll(); michael@0: } michael@0: } michael@0: return rv; michael@0: }