Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "nsEventQueue.h" |
michael@0 | 8 | #include "nsAutoPtr.h" |
michael@0 | 9 | #include "prlog.h" |
michael@0 | 10 | #include "nsThreadUtils.h" |
michael@0 | 11 | #include "prthread.h" |
michael@0 | 12 | #include "mozilla/ChaosMode.h" |
michael@0 | 13 | |
michael@0 | 14 | using namespace mozilla; |
michael@0 | 15 | |
michael@0 | 16 | #ifdef PR_LOGGING |
michael@0 | 17 | static PRLogModuleInfo * |
michael@0 | 18 | GetLog() |
michael@0 | 19 | { |
michael@0 | 20 | static PRLogModuleInfo *sLog; |
michael@0 | 21 | if (!sLog) |
michael@0 | 22 | sLog = PR_NewLogModule("nsEventQueue"); |
michael@0 | 23 | return sLog; |
michael@0 | 24 | } |
michael@0 | 25 | #endif |
michael@0 | 26 | #ifdef LOG |
michael@0 | 27 | #undef LOG |
michael@0 | 28 | #endif |
michael@0 | 29 | #define LOG(args) PR_LOG(GetLog(), PR_LOG_DEBUG, args) |
michael@0 | 30 | |
michael@0 | 31 | nsEventQueue::nsEventQueue() |
michael@0 | 32 | : mReentrantMonitor("nsEventQueue.mReentrantMonitor") |
michael@0 | 33 | , mHead(nullptr) |
michael@0 | 34 | , mTail(nullptr) |
michael@0 | 35 | , mOffsetHead(0) |
michael@0 | 36 | , mOffsetTail(0) |
michael@0 | 37 | { |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | nsEventQueue::~nsEventQueue() |
michael@0 | 41 | { |
michael@0 | 42 | // It'd be nice to be able to assert that no one else is holding the monitor, |
michael@0 | 43 | // but NSPR doesn't really expose APIs for it. |
michael@0 | 44 | NS_ASSERTION(IsEmpty(), "Non-empty event queue being destroyed; events being leaked."); |
michael@0 | 45 | |
michael@0 | 46 | if (mHead) |
michael@0 | 47 | FreePage(mHead); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | bool |
michael@0 | 51 | nsEventQueue::GetEvent(bool mayWait, nsIRunnable **result) |
michael@0 | 52 | { |
michael@0 | 53 | { |
michael@0 | 54 | ReentrantMonitorAutoEnter mon(mReentrantMonitor); |
michael@0 | 55 | |
michael@0 | 56 | while (IsEmpty()) { |
michael@0 | 57 | if (!mayWait) { |
michael@0 | 58 | if (result) |
michael@0 | 59 | *result = nullptr; |
michael@0 | 60 | return false; |
michael@0 | 61 | } |
michael@0 | 62 | LOG(("EVENTQ(%p): wait begin\n", this)); |
michael@0 | 63 | mon.Wait(); |
michael@0 | 64 | LOG(("EVENTQ(%p): wait end\n", this)); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | if (result) { |
michael@0 | 68 | *result = mHead->mEvents[mOffsetHead++]; |
michael@0 | 69 | |
michael@0 | 70 | // Check if mHead points to empty Page |
michael@0 | 71 | if (mOffsetHead == EVENTS_PER_PAGE) { |
michael@0 | 72 | Page *dead = mHead; |
michael@0 | 73 | mHead = mHead->mNext; |
michael@0 | 74 | FreePage(dead); |
michael@0 | 75 | mOffsetHead = 0; |
michael@0 | 76 | } |
michael@0 | 77 | } |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | return true; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | bool |
michael@0 | 84 | nsEventQueue::PutEvent(nsIRunnable *runnable) |
michael@0 | 85 | { |
michael@0 | 86 | // Avoid calling AddRef+Release while holding our monitor. |
michael@0 | 87 | nsRefPtr<nsIRunnable> event(runnable); |
michael@0 | 88 | bool rv = true; |
michael@0 | 89 | { |
michael@0 | 90 | if (ChaosMode::isActive()) { |
michael@0 | 91 | // With probability 0.5, yield so other threads have a chance to |
michael@0 | 92 | // dispatch events to this queue first. |
michael@0 | 93 | if (ChaosMode::randomUint32LessThan(2)) { |
michael@0 | 94 | PR_Sleep(PR_INTERVAL_NO_WAIT); |
michael@0 | 95 | } |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | ReentrantMonitorAutoEnter mon(mReentrantMonitor); |
michael@0 | 99 | |
michael@0 | 100 | if (!mHead) { |
michael@0 | 101 | mHead = NewPage(); |
michael@0 | 102 | if (!mHead) { |
michael@0 | 103 | rv = false; |
michael@0 | 104 | } else { |
michael@0 | 105 | mTail = mHead; |
michael@0 | 106 | mOffsetHead = 0; |
michael@0 | 107 | mOffsetTail = 0; |
michael@0 | 108 | } |
michael@0 | 109 | } else if (mOffsetTail == EVENTS_PER_PAGE) { |
michael@0 | 110 | Page *page = NewPage(); |
michael@0 | 111 | if (!page) { |
michael@0 | 112 | rv = false; |
michael@0 | 113 | } else { |
michael@0 | 114 | mTail->mNext = page; |
michael@0 | 115 | mTail = page; |
michael@0 | 116 | mOffsetTail = 0; |
michael@0 | 117 | } |
michael@0 | 118 | } |
michael@0 | 119 | if (rv) { |
michael@0 | 120 | event.swap(mTail->mEvents[mOffsetTail]); |
michael@0 | 121 | ++mOffsetTail; |
michael@0 | 122 | LOG(("EVENTQ(%p): notify\n", this)); |
michael@0 | 123 | mon.NotifyAll(); |
michael@0 | 124 | } |
michael@0 | 125 | } |
michael@0 | 126 | return rv; |
michael@0 | 127 | } |