1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/glue/MessagePump.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,342 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "MessagePump.h" 1.9 + 1.10 +#include "nsIRunnable.h" 1.11 +#include "nsIThread.h" 1.12 +#include "nsITimer.h" 1.13 + 1.14 +#include "base/basictypes.h" 1.15 +#include "base/logging.h" 1.16 +#include "base/scoped_nsautorelease_pool.h" 1.17 +#include "mozilla/Assertions.h" 1.18 +#include "mozilla/DebugOnly.h" 1.19 +#include "nsComponentManagerUtils.h" 1.20 +#include "nsDebug.h" 1.21 +#include "nsServiceManagerUtils.h" 1.22 +#include "nsString.h" 1.23 +#include "nsThreadUtils.h" 1.24 +#include "nsTimerImpl.h" 1.25 +#include "nsXULAppAPI.h" 1.26 +#include "prthread.h" 1.27 + 1.28 +#ifdef MOZ_WIDGET_ANDROID 1.29 +#include "AndroidBridge.h" 1.30 +#endif 1.31 + 1.32 +#ifdef MOZ_NUWA_PROCESS 1.33 +#include "ipc/Nuwa.h" 1.34 +#endif 1.35 + 1.36 +using base::TimeTicks; 1.37 +using namespace mozilla::ipc; 1.38 + 1.39 +NS_DEFINE_NAMED_CID(NS_TIMER_CID); 1.40 + 1.41 +static mozilla::DebugOnly<MessagePump::Delegate*> gFirstDelegate; 1.42 + 1.43 +namespace mozilla { 1.44 +namespace ipc { 1.45 + 1.46 +class DoWorkRunnable MOZ_FINAL : public nsIRunnable, 1.47 + public nsITimerCallback 1.48 +{ 1.49 +public: 1.50 + DoWorkRunnable(MessagePump* aPump) 1.51 + : mPump(aPump) 1.52 + { 1.53 + MOZ_ASSERT(aPump); 1.54 + } 1.55 + 1.56 + NS_DECL_THREADSAFE_ISUPPORTS 1.57 + NS_DECL_NSIRUNNABLE 1.58 + NS_DECL_NSITIMERCALLBACK 1.59 + 1.60 +private: 1.61 + ~DoWorkRunnable() 1.62 + { } 1.63 + 1.64 + MessagePump* mPump; 1.65 +}; 1.66 + 1.67 +} /* namespace ipc */ 1.68 +} /* namespace mozilla */ 1.69 + 1.70 +MessagePump::MessagePump() 1.71 +: mThread(nullptr) 1.72 +{ 1.73 + mDoWorkEvent = new DoWorkRunnable(this); 1.74 +} 1.75 + 1.76 +MessagePump::~MessagePump() 1.77 +{ 1.78 +} 1.79 + 1.80 +void 1.81 +MessagePump::Run(MessagePump::Delegate* aDelegate) 1.82 +{ 1.83 + MOZ_ASSERT(keep_running_); 1.84 + MOZ_ASSERT(NS_IsMainThread(), 1.85 + "Use mozilla::ipc::MessagePumpForNonMainThreads instead!"); 1.86 + 1.87 + mThread = NS_GetCurrentThread(); 1.88 + MOZ_ASSERT(mThread); 1.89 + 1.90 + mDelayedWorkTimer = do_CreateInstance(kNS_TIMER_CID); 1.91 + MOZ_ASSERT(mDelayedWorkTimer); 1.92 + 1.93 + base::ScopedNSAutoreleasePool autoReleasePool; 1.94 + 1.95 + for (;;) { 1.96 + autoReleasePool.Recycle(); 1.97 + 1.98 + bool did_work = NS_ProcessNextEvent(mThread, false) ? true : false; 1.99 + if (!keep_running_) 1.100 + break; 1.101 + 1.102 + // NB: it is crucial *not* to directly call |aDelegate->DoWork()| 1.103 + // here. To ensure that MessageLoop tasks and XPCOM events have 1.104 + // equal priority, we sensitively rely on processing exactly one 1.105 + // Task per DoWorkRunnable XPCOM event. 1.106 + 1.107 +#ifdef MOZ_WIDGET_ANDROID 1.108 + // This processes messages in the Android Looper. Note that we only 1.109 + // get here if the normal Gecko event loop has been awoken above. 1.110 + // Bug 750713 1.111 + if (MOZ_LIKELY(AndroidBridge::HasEnv())) { 1.112 + did_work |= mozilla::widget::android::GeckoAppShell::PumpMessageLoop(); 1.113 + } 1.114 +#endif 1.115 + 1.116 + did_work |= aDelegate->DoDelayedWork(&delayed_work_time_); 1.117 + 1.118 +if (did_work && delayed_work_time_.is_null() 1.119 +#ifdef MOZ_NUWA_PROCESS 1.120 + && (!IsNuwaReady() || !IsNuwaProcess()) 1.121 +#endif 1.122 + ) 1.123 + mDelayedWorkTimer->Cancel(); 1.124 + 1.125 + if (!keep_running_) 1.126 + break; 1.127 + 1.128 + if (did_work) 1.129 + continue; 1.130 + 1.131 + did_work = aDelegate->DoIdleWork(); 1.132 + if (!keep_running_) 1.133 + break; 1.134 + 1.135 + if (did_work) 1.136 + continue; 1.137 + 1.138 + // This will either sleep or process an event. 1.139 + NS_ProcessNextEvent(mThread, true); 1.140 + } 1.141 + 1.142 +#ifdef MOZ_NUWA_PROCESS 1.143 + if (!IsNuwaReady() || !IsNuwaProcess()) 1.144 +#endif 1.145 + mDelayedWorkTimer->Cancel(); 1.146 + 1.147 + keep_running_ = true; 1.148 +} 1.149 + 1.150 +void 1.151 +MessagePump::ScheduleWork() 1.152 +{ 1.153 + // Make sure the event loop wakes up. 1.154 + if (mThread) { 1.155 + mThread->Dispatch(mDoWorkEvent, NS_DISPATCH_NORMAL); 1.156 + } 1.157 + else { 1.158 + // Some things (like xpcshell) don't use the app shell and so Run hasn't 1.159 + // been called. We still need to wake up the main thread. 1.160 + NS_DispatchToMainThread(mDoWorkEvent, NS_DISPATCH_NORMAL); 1.161 + } 1.162 + event_.Signal(); 1.163 +} 1.164 + 1.165 +void 1.166 +MessagePump::ScheduleWorkForNestedLoop() 1.167 +{ 1.168 + // This method is called when our MessageLoop has just allowed 1.169 + // nested tasks. In our setup, whenever that happens we know that 1.170 + // DoWork() will be called "soon", so there's no need to pay the 1.171 + // cost of what will be a no-op nsThread::Dispatch(mDoWorkEvent). 1.172 +} 1.173 + 1.174 +void 1.175 +MessagePump::ScheduleDelayedWork(const base::TimeTicks& aDelayedTime) 1.176 +{ 1.177 +#ifdef MOZ_NUWA_PROCESS 1.178 + if (IsNuwaReady() && IsNuwaProcess()) 1.179 + return; 1.180 +#endif 1.181 + 1.182 + if (!mDelayedWorkTimer) { 1.183 + mDelayedWorkTimer = do_CreateInstance(kNS_TIMER_CID); 1.184 + if (!mDelayedWorkTimer) { 1.185 + // Called before XPCOM has started up? We can't do this correctly. 1.186 + NS_WARNING("Delayed task might not run!"); 1.187 + delayed_work_time_ = aDelayedTime; 1.188 + return; 1.189 + } 1.190 + } 1.191 + 1.192 + if (!delayed_work_time_.is_null()) { 1.193 + mDelayedWorkTimer->Cancel(); 1.194 + } 1.195 + 1.196 + delayed_work_time_ = aDelayedTime; 1.197 + 1.198 + // TimeDelta's constructor initializes to 0 1.199 + base::TimeDelta delay; 1.200 + if (aDelayedTime > base::TimeTicks::Now()) 1.201 + delay = aDelayedTime - base::TimeTicks::Now(); 1.202 + 1.203 + uint32_t delayMS = uint32_t(delay.InMilliseconds()); 1.204 + mDelayedWorkTimer->InitWithCallback(mDoWorkEvent, delayMS, 1.205 + nsITimer::TYPE_ONE_SHOT); 1.206 +} 1.207 + 1.208 +void 1.209 +MessagePump::DoDelayedWork(base::MessagePump::Delegate* aDelegate) 1.210 +{ 1.211 + aDelegate->DoDelayedWork(&delayed_work_time_); 1.212 + if (!delayed_work_time_.is_null()) { 1.213 + ScheduleDelayedWork(delayed_work_time_); 1.214 + } 1.215 +} 1.216 + 1.217 +NS_IMPL_ISUPPORTS(DoWorkRunnable, nsIRunnable, nsITimerCallback) 1.218 + 1.219 +NS_IMETHODIMP 1.220 +DoWorkRunnable::Run() 1.221 +{ 1.222 + MessageLoop* loop = MessageLoop::current(); 1.223 + MOZ_ASSERT(loop); 1.224 + 1.225 + bool nestableTasksAllowed = loop->NestableTasksAllowed(); 1.226 + 1.227 + // MessageLoop::RunTask() disallows nesting, but our Frankenventloop will 1.228 + // always dispatch DoWork() below from what looks to MessageLoop like a nested 1.229 + // context. So we unconditionally allow nesting here. 1.230 + loop->SetNestableTasksAllowed(true); 1.231 + loop->DoWork(); 1.232 + loop->SetNestableTasksAllowed(nestableTasksAllowed); 1.233 + 1.234 + return NS_OK; 1.235 +} 1.236 + 1.237 +NS_IMETHODIMP 1.238 +DoWorkRunnable::Notify(nsITimer* aTimer) 1.239 +{ 1.240 + MessageLoop* loop = MessageLoop::current(); 1.241 + MOZ_ASSERT(loop); 1.242 + 1.243 + mPump->DoDelayedWork(loop); 1.244 + 1.245 + return NS_OK; 1.246 +} 1.247 + 1.248 +void 1.249 +MessagePumpForChildProcess::Run(base::MessagePump::Delegate* aDelegate) 1.250 +{ 1.251 + if (mFirstRun) { 1.252 + MOZ_ASSERT(aDelegate && !gFirstDelegate); 1.253 + gFirstDelegate = aDelegate; 1.254 + 1.255 + mFirstRun = false; 1.256 + if (NS_FAILED(XRE_RunAppShell())) { 1.257 + NS_WARNING("Failed to run app shell?!"); 1.258 + } 1.259 + 1.260 + MOZ_ASSERT(aDelegate && aDelegate == gFirstDelegate); 1.261 + gFirstDelegate = nullptr; 1.262 + 1.263 + return; 1.264 + } 1.265 + 1.266 + MOZ_ASSERT(aDelegate && aDelegate == gFirstDelegate); 1.267 + 1.268 + // We can get to this point in startup with Tasks in our loop's 1.269 + // incoming_queue_ or pending_queue_, but without a matching 1.270 + // DoWorkRunnable(). In MessagePump::Run() above, we sensitively 1.271 + // depend on *not* directly calling delegate->DoWork(), because that 1.272 + // prioritizes Tasks above XPCOM events. However, from this point 1.273 + // forward, any Task posted to our loop is guaranteed to have a 1.274 + // DoWorkRunnable enqueued for it. 1.275 + // 1.276 + // So we just flush the pending work here and move on. 1.277 + MessageLoop* loop = MessageLoop::current(); 1.278 + bool nestableTasksAllowed = loop->NestableTasksAllowed(); 1.279 + loop->SetNestableTasksAllowed(true); 1.280 + 1.281 + while (aDelegate->DoWork()); 1.282 + 1.283 + loop->SetNestableTasksAllowed(nestableTasksAllowed); 1.284 + 1.285 + // Really run. 1.286 + mozilla::ipc::MessagePump::Run(aDelegate); 1.287 +} 1.288 + 1.289 +void 1.290 +MessagePumpForNonMainThreads::Run(base::MessagePump::Delegate* aDelegate) 1.291 +{ 1.292 + MOZ_ASSERT(keep_running_); 1.293 + MOZ_ASSERT(!NS_IsMainThread(), "Use mozilla::ipc::MessagePump instead!"); 1.294 + 1.295 + mThread = NS_GetCurrentThread(); 1.296 + MOZ_ASSERT(mThread); 1.297 + 1.298 + mDelayedWorkTimer = do_CreateInstance(kNS_TIMER_CID); 1.299 + MOZ_ASSERT(mDelayedWorkTimer); 1.300 + 1.301 + if (NS_FAILED(mDelayedWorkTimer->SetTarget(mThread))) { 1.302 + MOZ_CRASH("Failed to set timer target!"); 1.303 + } 1.304 + 1.305 + base::ScopedNSAutoreleasePool autoReleasePool; 1.306 + 1.307 + for (;;) { 1.308 + autoReleasePool.Recycle(); 1.309 + 1.310 + bool didWork = NS_ProcessNextEvent(mThread, false) ? true : false; 1.311 + if (!keep_running_) { 1.312 + break; 1.313 + } 1.314 + 1.315 + didWork |= aDelegate->DoDelayedWork(&delayed_work_time_); 1.316 + 1.317 + if (didWork && delayed_work_time_.is_null()) { 1.318 + mDelayedWorkTimer->Cancel(); 1.319 + } 1.320 + 1.321 + if (!keep_running_) { 1.322 + break; 1.323 + } 1.324 + 1.325 + if (didWork) { 1.326 + continue; 1.327 + } 1.328 + 1.329 + didWork = aDelegate->DoIdleWork(); 1.330 + if (!keep_running_) { 1.331 + break; 1.332 + } 1.333 + 1.334 + if (didWork) { 1.335 + continue; 1.336 + } 1.337 + 1.338 + // This will either sleep or process an event. 1.339 + NS_ProcessNextEvent(mThread, true); 1.340 + } 1.341 + 1.342 + mDelayedWorkTimer->Cancel(); 1.343 + 1.344 + keep_running_ = true; 1.345 +}