Bug 1760868 [wpt PR 33305] - Update admins for wpt-live and wpt-pr-bot on GCP, a...
[gecko.git] / ipc / mscom / MainThreadInvoker.h
blob8b56f9bed024995e57490aafd0896968268ba485
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
10 #include <windows.h>
12 #include <utility>
14 #include "mozilla/AlreadyAddRefed.h"
15 #include "mozilla/StaticPtr.h"
16 #include "mozilla/TimeStamp.h"
17 #include "nsCOMPtr.h"
18 #include "nsThreadUtils.h"
20 class nsIRunnable;
22 namespace mozilla {
23 namespace mscom {
25 class MainThreadInvoker {
26 public:
27 MainThreadInvoker();
29 bool Invoke(already_AddRefed<nsIRunnable>&& aRunnable);
30 const TimeDuration& GetDuration() const { return mDuration; }
31 static HANDLE GetTargetThread() { return sMainThread; }
33 private:
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...),
45 Args&&... aArgs) {
46 nsCOMPtr<nsIRunnable> runnable(NewNonOwningRunnableMethod<Args...>(
47 aName, aObject, aMethod, std::forward<Args>(aArgs)...));
49 MainThreadInvoker invoker;
50 return invoker.Invoke(runnable.forget());
53 } // namespace mscom
54 } // namespace mozilla
56 #endif // mozilla_mscom_MainThreadInvoker_h