2012-10-03 00:04:58 +04:00
|
|
|
/* -*- 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"
|
2012-12-21 19:11:02 +04:00
|
|
|
#include "mozilla/RefPtr.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
// Abstract base class for all of our templates
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-02-06 19:21:55 +04:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
enum RunnableResult {
|
|
|
|
NoResult,
|
|
|
|
ReturnsResult
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline nsresult
|
|
|
|
RunOnThreadInternal(nsIEventTarget *thread, nsIRunnable *runnable, uint32_t flags)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRunnable> runnable_ref(runnable);
|
|
|
|
if (thread) {
|
|
|
|
bool on;
|
|
|
|
nsresult rv;
|
|
|
|
rv = thread->IsOnCurrentThread(&on);
|
|
|
|
|
|
|
|
// If the target thread has already shut down, we don't want to assert.
|
|
|
|
if (rv != NS_ERROR_NOT_INITIALIZED) {
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!on) {
|
|
|
|
return thread->Dispatch(runnable_ref, flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return runnable_ref->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<RunnableResult result>
|
2012-10-03 00:04:58 +04:00
|
|
|
class runnable_args_base : public nsRunnable {
|
|
|
|
public:
|
|
|
|
NS_IMETHOD Run() = 0;
|
|
|
|
};
|
|
|
|
|
2014-02-06 19:21:55 +04:00
|
|
|
}
|
|
|
|
|
2012-12-03 04:53:47 +04:00
|
|
|
// The generated file contains four major function templates
|
|
|
|
// (in variants for arbitrary numbers of arguments up to 10,
|
|
|
|
// which is why it is machine generated). The four templates
|
|
|
|
// are:
|
|
|
|
//
|
2013-04-01 23:34:36 +04:00
|
|
|
// WrapRunnable(o, m, ...) -- wraps a member function m of an object ptr o
|
2012-12-03 04:53:47 +04:00
|
|
|
// WrapRunnableRet(o, m, ..., r) -- wraps a member function m of an object ptr o
|
|
|
|
// the function returns something that can
|
|
|
|
// be assigned to *r
|
|
|
|
// WrapRunnableNM(f, ...) -- wraps a function f
|
|
|
|
// WrapRunnableNMRet(f, ..., r) -- wraps a function f that returns something
|
|
|
|
// that can be assigned to *r
|
2013-04-01 23:34:36 +04:00
|
|
|
//
|
2012-12-03 04:53:47 +04:00
|
|
|
// All of these template functions return a Runnable* which can be passed
|
|
|
|
// to Dispatch().
|
2012-10-03 00:04:58 +04:00
|
|
|
#include "runnable_utils_generated.h"
|
|
|
|
|
2014-02-06 19:21:55 +04:00
|
|
|
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);
|
2012-12-21 19:11:02 +04:00
|
|
|
}
|
|
|
|
|
2014-02-06 19:21:55 +04:00
|
|
|
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);
|
2013-03-04 20:51:48 +04:00
|
|
|
}
|
|
|
|
|
2013-04-17 00:13:09 +04:00
|
|
|
#ifdef DEBUG
|
2012-12-21 18:03:22 +04:00
|
|
|
#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)
|
2013-03-26 14:07:02 +04:00
|
|
|
#else
|
|
|
|
#define ASSERT_ON_THREAD(t)
|
2012-10-03 00:04:58 +04:00
|
|
|
#endif
|
|
|
|
|
2013-03-26 14:07:02 +04:00
|
|
|
} /* namespace mozilla */
|
|
|
|
|
|
|
|
#endif
|