xpcom/threads/nsEventQueue.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/threads/nsEventQueue.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,127 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "nsEventQueue.h"
    1.11 +#include "nsAutoPtr.h"
    1.12 +#include "prlog.h"
    1.13 +#include "nsThreadUtils.h"
    1.14 +#include "prthread.h"
    1.15 +#include "mozilla/ChaosMode.h"
    1.16 +
    1.17 +using namespace mozilla;
    1.18 +
    1.19 +#ifdef PR_LOGGING
    1.20 +static PRLogModuleInfo *
    1.21 +GetLog()
    1.22 +{
    1.23 +  static PRLogModuleInfo *sLog;
    1.24 +  if (!sLog)
    1.25 +    sLog = PR_NewLogModule("nsEventQueue");
    1.26 +  return sLog;
    1.27 +}
    1.28 +#endif
    1.29 +#ifdef LOG
    1.30 +#undef LOG
    1.31 +#endif
    1.32 +#define LOG(args) PR_LOG(GetLog(), PR_LOG_DEBUG, args)
    1.33 +
    1.34 +nsEventQueue::nsEventQueue()
    1.35 +  : mReentrantMonitor("nsEventQueue.mReentrantMonitor")
    1.36 +  , mHead(nullptr)
    1.37 +  , mTail(nullptr)
    1.38 +  , mOffsetHead(0)
    1.39 +  , mOffsetTail(0)
    1.40 +{
    1.41 +}
    1.42 +
    1.43 +nsEventQueue::~nsEventQueue()
    1.44 +{
    1.45 +  // It'd be nice to be able to assert that no one else is holding the monitor,
    1.46 +  // but NSPR doesn't really expose APIs for it.
    1.47 +  NS_ASSERTION(IsEmpty(), "Non-empty event queue being destroyed; events being leaked.");
    1.48 +
    1.49 +  if (mHead)
    1.50 +    FreePage(mHead);
    1.51 +}
    1.52 +
    1.53 +bool
    1.54 +nsEventQueue::GetEvent(bool mayWait, nsIRunnable **result)
    1.55 +{
    1.56 +  {
    1.57 +    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
    1.58 +    
    1.59 +    while (IsEmpty()) {
    1.60 +      if (!mayWait) {
    1.61 +        if (result)
    1.62 +          *result = nullptr;
    1.63 +        return false;
    1.64 +      }
    1.65 +      LOG(("EVENTQ(%p): wait begin\n", this)); 
    1.66 +      mon.Wait();
    1.67 +      LOG(("EVENTQ(%p): wait end\n", this)); 
    1.68 +    }
    1.69 +    
    1.70 +    if (result) {
    1.71 +      *result = mHead->mEvents[mOffsetHead++];
    1.72 +      
    1.73 +      // Check if mHead points to empty Page
    1.74 +      if (mOffsetHead == EVENTS_PER_PAGE) {
    1.75 +        Page *dead = mHead;
    1.76 +        mHead = mHead->mNext;
    1.77 +        FreePage(dead);
    1.78 +        mOffsetHead = 0;
    1.79 +      }
    1.80 +    }
    1.81 +  }
    1.82 +
    1.83 +  return true;
    1.84 +}
    1.85 +
    1.86 +bool
    1.87 +nsEventQueue::PutEvent(nsIRunnable *runnable)
    1.88 +{
    1.89 +  // Avoid calling AddRef+Release while holding our monitor.
    1.90 +  nsRefPtr<nsIRunnable> event(runnable);
    1.91 +  bool rv = true;
    1.92 +  {
    1.93 +    if (ChaosMode::isActive()) {
    1.94 +      // With probability 0.5, yield so other threads have a chance to
    1.95 +      // dispatch events to this queue first.
    1.96 +      if (ChaosMode::randomUint32LessThan(2)) {
    1.97 +        PR_Sleep(PR_INTERVAL_NO_WAIT);
    1.98 +      }
    1.99 +    }
   1.100 +
   1.101 +    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
   1.102 +
   1.103 +    if (!mHead) {
   1.104 +      mHead = NewPage();
   1.105 +      if (!mHead) {
   1.106 +        rv = false;
   1.107 +      } else {
   1.108 +        mTail = mHead;
   1.109 +        mOffsetHead = 0;
   1.110 +        mOffsetTail = 0;
   1.111 +      }
   1.112 +    } else if (mOffsetTail == EVENTS_PER_PAGE) {
   1.113 +      Page *page = NewPage();
   1.114 +      if (!page) {
   1.115 +        rv = false;
   1.116 +      } else {
   1.117 +        mTail->mNext = page;
   1.118 +        mTail = page;
   1.119 +        mOffsetTail = 0;
   1.120 +      }
   1.121 +    }
   1.122 +    if (rv) {
   1.123 +      event.swap(mTail->mEvents[mOffsetTail]);
   1.124 +      ++mOffsetTail;
   1.125 +      LOG(("EVENTQ(%p): notify\n", this)); 
   1.126 +      mon.NotifyAll();
   1.127 +    }
   1.128 +  }
   1.129 +  return rv;
   1.130 +}

mercurial