DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (e67641c2e4cc)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

// Original author: ekr@rtfm.com

#ifndef runnable_utils_h__
#define runnable_utils_h__

#include "nsThreadUtils.h"
#include "mozilla/IndexSequence.h"
#include "mozilla/Move.h"
#include "mozilla/RefPtr.h"
#include "mozilla/Tuple.h"

// Abstract base class for all of our templates
namespace mozilla {

namespace detail {

enum RunnableResult { NoResult, ReturnsResult };

static inline nsresult RunOnThreadInternal(nsIEventTarget* thread,
                                           nsIRunnable* runnable,
                                           uint32_t flags) {
  return thread->Dispatch(runnable, flags);
}

template <RunnableResult result>
class runnable_args_base : public Runnable {
 public:
  runnable_args_base() : Runnable("media-runnable_args_base") {}

  NS_IMETHOD Run() override = 0;
};

template <typename R>
struct RunnableFunctionCallHelper {
  template <typename FunType, typename... Args, size_t... Indices>
  static R apply(FunType func, Tuple<Args...>& args,
                 IndexSequence<Indices...>) {
    return func(Get<Indices>(args)...);
  }
};

// A void specialization is needed in the case where the template instantiator
// knows we don't want to return a value, but we don't know whether the called
// function returns void or something else.
template <>
struct RunnableFunctionCallHelper<void> {
  template <typename FunType, typename... Args, size_t... Indices>
  static void apply(FunType func, Tuple<Args...>& args,
                    IndexSequence<Indices...>) {
    func(Get<Indices>(args)...);
  }
};

template <typename R>
struct RunnableMethodCallHelper {
  template <typename Class, typename M, typename... Args, size_t... Indices>
  static R apply(Class obj, M method, Tuple<Args...>& args,
                 IndexSequence<Indices...>) {
    return ((*obj).*method)(Get<Indices>(args)...);
  }
};

// A void specialization is needed in the case where the template instantiator
// knows we don't want to return a value, but we don't know whether the called
// method returns void or something else.
template <>
struct RunnableMethodCallHelper<void> {
  template <typename Class, typename M, typename... Args, size_t... Indices>
  static void apply(Class obj, M method, Tuple<Args...>& args,
                    IndexSequence<Indices...>) {
    ((*obj).*method)(Get<Indices>(args)...);
  }
};

}  // namespace detail

template <typename FunType, typename... Args>
class runnable_args_func : public detail::runnable_args_base<detail::NoResult> {
 public:
  // |explicit| to pacify static analysis when there are no |args|.
  template <typename... Arguments>
  explicit runnable_args_func(FunType f, Arguments&&... args)
      : mFunc(f), mArgs(Forward<Arguments>(args)...) {}

  NS_IMETHOD Run() override {
    detail::RunnableFunctionCallHelper<void>::apply(
        mFunc, mArgs, typename IndexSequenceFor<Args...>::Type());
    return NS_OK;
  }

 private:
  FunType mFunc;
  Tuple<Args...> mArgs;
};

template <typename FunType, typename... Args>
runnable_args_func<FunType, typename mozilla::Decay<Args>::Type...>*
WrapRunnableNM(FunType f, Args&&... args) {
  return new runnable_args_func<FunType,
                                typename mozilla::Decay<Args>::Type...>(
      f, Forward<Args>(args)...);
}

template <typename Ret, typename FunType, typename... Args>
class runnable_args_func_ret
    : public detail::runnable_args_base<detail::ReturnsResult> {
 public:
  template <typename... Arguments>
  runnable_args_func_ret(Ret* ret, FunType f, Arguments&&... args)
      : mReturn(ret), mFunc(f), mArgs(Forward<Arguments>(args)...) {}

  NS_IMETHOD Run() override {
    *mReturn = detail::RunnableFunctionCallHelper<Ret>::apply(
        mFunc, mArgs, typename IndexSequenceFor<Args...>::Type());
    return NS_OK;
  }

 private:
  Ret* mReturn;
  FunType mFunc;
  Tuple<Args...> mArgs;
};

template <typename R, typename FunType, typename... Args>
runnable_args_func_ret<R, FunType, typename mozilla::Decay<Args>::Type...>*
WrapRunnableNMRet(R* ret, FunType f, Args&&... args) {
  return new runnable_args_func_ret<R, FunType,
                                    typename mozilla::Decay<Args>::Type...>(
      ret, f, Forward<Args>(args)...);
}

template <typename Class, typename M, typename... Args>
class runnable_args_memfn
    : public detail::runnable_args_base<detail::NoResult> {
 public:
  template <typename... Arguments>
  runnable_args_memfn(Class obj, M method, Arguments&&... args)
      : mObj(obj), mMethod(method), mArgs(Forward<Arguments>(args)...) {}

  NS_IMETHOD Run() override {
    detail::RunnableMethodCallHelper<void>::apply(
        mObj, mMethod, mArgs, typename IndexSequenceFor<Args...>::Type());
    return NS_OK;
  }

 private:
  Class mObj;
  M mMethod;
  Tuple<Args...> mArgs;
};

template <typename Class, typename M, typename... Args>
runnable_args_memfn<Class, M, typename mozilla::Decay<Args>::Type...>*
WrapRunnable(Class obj, M method, Args&&... args) {
  return new runnable_args_memfn<Class, M,
                                 typename mozilla::Decay<Args>::Type...>(
      obj, method, Forward<Args>(args)...);
}

template <typename Ret, typename Class, typename M, typename... Args>
class runnable_args_memfn_ret
    : public detail::runnable_args_base<detail::ReturnsResult> {
 public:
  template <typename... Arguments>
  runnable_args_memfn_ret(Ret* ret, Class obj, M method, Arguments... args)
      : mReturn(ret),
        mObj(obj),
        mMethod(method),
        mArgs(Forward<Arguments>(args)...) {}

  NS_IMETHOD Run() override {
    *mReturn = detail::RunnableMethodCallHelper<Ret>::apply(
        mObj, mMethod, mArgs, typename IndexSequenceFor<Args...>::Type());
    return NS_OK;
  }

 private:
  Ret* mReturn;
  Class mObj;
  M mMethod;
  Tuple<Args...> mArgs;
};

template <typename R, typename Class, typename M, typename... Args>
runnable_args_memfn_ret<R, Class, M, typename mozilla::Decay<Args>::Type...>*
WrapRunnableRet(R* ret, Class obj, M method, Args&&... args) {
  return new runnable_args_memfn_ret<R, Class, M,
                                     typename mozilla::Decay<Args>::Type...>(
      ret, obj, method, Forward<Args>(args)...);
}

static inline nsresult RUN_ON_THREAD(
    nsIEventTarget* thread,
    detail::runnable_args_base<detail::NoResult>* runnable, uint32_t flags) {
  return detail::RunOnThreadInternal(
      thread, static_cast<nsIRunnable*>(runnable), flags);
}

static inline nsresult RUN_ON_THREAD(
    nsIEventTarget* thread,
    detail::runnable_args_base<detail::ReturnsResult>* runnable) {
  return detail::RunOnThreadInternal(
      thread, static_cast<nsIRunnable*>(runnable), NS_DISPATCH_SYNC);
}

#ifdef DEBUG
#define ASSERT_ON_THREAD(t)           \
  do {                                \
    if (t) {                          \
      bool on;                        \
      nsresult rv;                    \
      rv = t->IsOnCurrentThread(&on); \
      MOZ_ASSERT(NS_SUCCEEDED(rv));   \
      MOZ_ASSERT(on);                 \
    }                                 \
  } while (0)
#else
#define ASSERT_ON_THREAD(t)
#endif

template <class T>
class DispatchedRelease : public detail::runnable_args_base<detail::NoResult> {
 public:
  explicit DispatchedRelease(already_AddRefed<T>& ref) : ref_(ref) {}

  NS_IMETHOD Run() override {
    ref_ = nullptr;
    return NS_OK;
  }

 private:
  RefPtr<T> ref_;
};

template <typename T>
DispatchedRelease<T>* WrapRelease(already_AddRefed<T>&& ref) {
  return new DispatchedRelease<T>(ref);
}

} /* namespace mozilla */

#endif