xpcom/threads/SyncRunnable.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/threads/SyncRunnable.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,93 @@
     1.4 +/* -*- Mode: C++; tab-width: 12; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef mozilla_SyncRunnable_h
    1.10 +#define mozilla_SyncRunnable_h
    1.11 +
    1.12 +#include "nsThreadUtils.h"
    1.13 +#include "mozilla/Monitor.h"
    1.14 +
    1.15 +namespace mozilla {
    1.16 +
    1.17 +/**
    1.18 + * This class will wrap a nsIRunnable and dispatch it to the main thread
    1.19 + * synchronously. This is different from nsIEventTarget.DISPATCH_SYNC:
    1.20 + * this class does not spin the event loop waiting for the event to be
    1.21 + * dispatched. This means that you don't risk reentrance from pending
    1.22 + * messages, but you must be sure that the target thread does not ever block
    1.23 + * on this thread, or else you will deadlock.
    1.24 + *
    1.25 + * Typical usage:
    1.26 + * nsRefPtr<SyncRunnable> sr = new SyncRunnable(new myrunnable...());
    1.27 + * sr->DispatchToThread(t);
    1.28 + *
    1.29 + * We also provide a convenience wrapper:
    1.30 + * SyncRunnable::DispatchToThread(new myrunnable...());
    1.31 + *
    1.32 + */
    1.33 +class SyncRunnable : public nsRunnable
    1.34 +{
    1.35 +public:
    1.36 +  SyncRunnable(nsIRunnable* r)
    1.37 +    : mRunnable(r)
    1.38 +    , mMonitor("SyncRunnable")
    1.39 +    , mDone(false)
    1.40 +  { }
    1.41 +
    1.42 +  void DispatchToThread(nsIEventTarget* thread,
    1.43 +                        bool forceDispatch = false)
    1.44 +  {
    1.45 +    nsresult rv;
    1.46 +    bool on;
    1.47 +
    1.48 +    if (!forceDispatch) {
    1.49 +      rv = thread->IsOnCurrentThread(&on);
    1.50 +      MOZ_ASSERT(NS_SUCCEEDED(rv));
    1.51 +      if (NS_SUCCEEDED(rv) && on) {
    1.52 +        mRunnable->Run();
    1.53 +        return;
    1.54 +      }
    1.55 +    }
    1.56 +
    1.57 +    rv = thread->Dispatch(this, NS_DISPATCH_NORMAL);
    1.58 +    if (NS_SUCCEEDED(rv)) {
    1.59 +      mozilla::MonitorAutoLock lock(mMonitor);
    1.60 +      while (!mDone) {
    1.61 +        lock.Wait();
    1.62 +      }
    1.63 +    }
    1.64 +  }
    1.65 +
    1.66 +  static void DispatchToThread(nsIEventTarget* thread,
    1.67 +                               nsIRunnable* r,
    1.68 +                               bool forceDispatch = false)
    1.69 +  {
    1.70 +    nsRefPtr<SyncRunnable> s(new SyncRunnable(r));
    1.71 +    s->DispatchToThread(thread, forceDispatch);
    1.72 +  }
    1.73 +
    1.74 +protected:
    1.75 +  NS_IMETHODIMP Run()
    1.76 +  {
    1.77 +    mRunnable->Run();
    1.78 +
    1.79 +    mozilla::MonitorAutoLock lock(mMonitor);
    1.80 +    MOZ_ASSERT(!mDone);
    1.81 +
    1.82 +    mDone = true;
    1.83 +    mMonitor.Notify();
    1.84 +
    1.85 +    return NS_OK;
    1.86 +  }
    1.87 +
    1.88 +private:
    1.89 +  nsCOMPtr<nsIRunnable> mRunnable;
    1.90 +  mozilla::Monitor mMonitor;
    1.91 +  bool mDone;
    1.92 +};
    1.93 +
    1.94 +} // namespace mozilla
    1.95 +
    1.96 +#endif // mozilla_SyncRunnable_h

mercurial