1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_mscom_MainThreadInvoker_h
8 #define mozilla_mscom_MainThreadInvoker_h
14 #include "mozilla/AlreadyAddRefed.h"
15 #include "mozilla/StaticPtr.h"
16 #include "mozilla/TimeStamp.h"
18 #include "nsThreadUtils.h"
25 class MainThreadInvoker
{
29 bool Invoke(already_AddRefed
<nsIRunnable
>&& aRunnable
);
30 const TimeDuration
& GetDuration() const { return mDuration
; }
31 static HANDLE
GetTargetThread() { return sMainThread
; }
34 TimeDuration mDuration
;
36 static bool InitStatics();
37 static VOID CALLBACK
MainThreadAPC(ULONG_PTR aParam
);
39 static HANDLE sMainThread
;
42 template <typename Class
, typename
... Args
>
43 inline bool InvokeOnMainThread(const char* aName
, Class
* aObject
,
44 void (Class::*aMethod
)(Args
...),
46 nsCOMPtr
<nsIRunnable
> runnable(NewNonOwningRunnableMethod
<Args
...>(
47 aName
, aObject
, aMethod
, std::forward
<Args
>(aArgs
)...));
49 MainThreadInvoker invoker
;
50 return invoker
.Invoke(runnable
.forget());
54 } // namespace mozilla
56 #endif // mozilla_mscom_MainThreadInvoker_h