media/mtransport/runnable_utils.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/mtransport/runnable_utils.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,87 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     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 file,
     1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +// Original author: ekr@rtfm.com
    1.11 +
    1.12 +#ifndef runnable_utils_h__
    1.13 +#define runnable_utils_h__
    1.14 +
    1.15 +#include "nsThreadUtils.h"
    1.16 +#include "mozilla/RefPtr.h"
    1.17 +
    1.18 +// Abstract base class for all of our templates
    1.19 +namespace mozilla {
    1.20 +
    1.21 +class runnable_args_base : public nsRunnable {
    1.22 + public:
    1.23 +  NS_IMETHOD Run() = 0;
    1.24 +  virtual bool returns_value() const { return false; }
    1.25 +};
    1.26 +
    1.27 +// The generated file contains four major function templates
    1.28 +// (in variants for arbitrary numbers of arguments up to 10,
    1.29 +// which is why it is machine generated). The four templates
    1.30 +// are:
    1.31 +//
    1.32 +// WrapRunnable(o, m, ...) -- wraps a member function m of an object ptr o
    1.33 +// WrapRunnableRet(o, m, ..., r) -- wraps a member function m of an object ptr o
    1.34 +//                                  the function returns something that can
    1.35 +//                                  be assigned to *r
    1.36 +// WrapRunnableNM(f, ...) -- wraps a function f
    1.37 +// WrapRunnableNMRet(f, ..., r) -- wraps a function f that returns something
    1.38 +//                                 that can be assigned to *r
    1.39 +//
    1.40 +// All of these template functions return a Runnable* which can be passed
    1.41 +// to Dispatch().
    1.42 +#include "runnable_utils_generated.h"
    1.43 +
    1.44 +// Temporary hack. Really we want to have a template which will do this
    1.45 +static inline nsresult RUN_ON_THREAD(nsIEventTarget *thread, nsIRunnable *runnable, uint32_t flags) {
    1.46 +  RefPtr<nsIRunnable> runnable_ref(runnable);
    1.47 +  if (thread) {
    1.48 +    bool on;
    1.49 +    nsresult rv;
    1.50 +    rv = thread->IsOnCurrentThread(&on);
    1.51 +
    1.52 +    // If the target thread has already shut down, we don't want to assert.
    1.53 +    if (rv != NS_ERROR_NOT_INITIALIZED) {
    1.54 +      MOZ_ASSERT(NS_SUCCEEDED(rv));
    1.55 +    }
    1.56 +
    1.57 +    NS_ENSURE_SUCCESS(rv, rv);
    1.58 +    if(!on) {
    1.59 +      return thread->Dispatch(runnable_ref, flags);
    1.60 +    }
    1.61 +  }
    1.62 +  return runnable_ref->Run();
    1.63 +}
    1.64 +
    1.65 +static inline nsresult RUN_ON_THREAD(nsIEventTarget *thread, runnable_args_base *runnable, uint32_t flags) {
    1.66 +  // Detect attempts to return a value when in async mode, since this
    1.67 +  // most likely means someone is trying to assign to a heap variable
    1.68 +  // which is now out of scope.
    1.69 +  MOZ_ASSERT((!(runnable->returns_value()) || (flags != NS_DISPATCH_NORMAL)));
    1.70 +
    1.71 +  return RUN_ON_THREAD(thread, static_cast<nsIRunnable *>(runnable), flags);
    1.72 +}
    1.73 +
    1.74 +#ifdef DEBUG
    1.75 +#define ASSERT_ON_THREAD(t) do {                \
    1.76 +    if (t) {                                    \
    1.77 +      bool on;                                    \
    1.78 +      nsresult rv;                                \
    1.79 +      rv = t->IsOnCurrentThread(&on);             \
    1.80 +      MOZ_ASSERT(NS_SUCCEEDED(rv));               \
    1.81 +      MOZ_ASSERT(on);                             \
    1.82 +    }                                           \
    1.83 +  } while(0)
    1.84 +#else
    1.85 +#define ASSERT_ON_THREAD(t)
    1.86 +#endif
    1.87 +
    1.88 +} /* namespace mozilla */
    1.89 +
    1.90 +#endif

mercurial