Backed out changeset f85447f6f56d (bug 1891145) for causing mochitest failures @...
[gecko.git] / ipc / mscom / EnsureMTA.h
blob662192c476559257e72fdc12c59013ba43496bee
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_EnsureMTA_h
8 #define mozilla_mscom_EnsureMTA_h
10 #include "MainThreadUtils.h"
11 #include "mozilla/Attributes.h"
12 #include "mozilla/DebugOnly.h"
13 #include "mozilla/MozPromise.h"
14 #include "mozilla/Unused.h"
15 #include "mozilla/mscom/AgileReference.h"
16 #include "mozilla/mscom/Utils.h"
17 #include "mozilla/RefPtr.h"
18 #include "nsCOMPtr.h"
19 #include "nsIThread.h"
20 #include "nsThreadUtils.h"
21 #include "nsWindowsHelpers.h"
23 #include <windows.h>
25 namespace mozilla {
26 namespace mscom {
27 namespace detail {
29 // Forward declarations
30 template <typename T>
31 struct MTADelete;
33 template <typename T>
34 struct MTARelease;
36 template <typename T>
37 struct MTAReleaseInChildProcess;
39 struct PreservedStreamDeleter;
41 } // namespace detail
43 class ProcessRuntime;
45 // This class is OK to use as a temporary on the stack.
46 class MOZ_STACK_CLASS EnsureMTA final {
47 public:
48 enum class Option {
49 Default,
50 // Forcibly dispatch to the thread returned by GetPersistentMTAThread(),
51 // even if the current thread is already inside a MTA.
52 ForceDispatchToPersistentThread,
55 /**
56 * Synchronously run |aClosure| on a thread living in the COM multithreaded
57 * apartment. If the current thread lives inside the COM MTA, then it runs
58 * |aClosure| immediately unless |aOpt| ==
59 * Option::ForceDispatchToPersistentThread.
61 template <typename FuncT>
62 explicit EnsureMTA(FuncT&& aClosure, Option aOpt = Option::Default) {
63 if (aOpt != Option::ForceDispatchToPersistentThread &&
64 IsCurrentThreadMTA()) {
65 // We're already on the MTA, we can run aClosure directly
66 aClosure();
67 return;
70 // In this case we need to run aClosure on a background thread in the MTA
71 nsCOMPtr<nsIRunnable> runnable(
72 NS_NewRunnableFunction("EnsureMTA::EnsureMTA", std::move(aClosure)));
73 SyncDispatch(std::move(runnable), aOpt);
76 private:
77 static nsCOMPtr<nsIThread> GetPersistentMTAThread();
79 static void SyncDispatch(nsCOMPtr<nsIRunnable>&& aRunnable, Option aOpt);
80 static void SyncDispatchToPersistentThread(nsIRunnable* aRunnable);
82 // The following function is private in order to force any consumers to be
83 // declared as friends of EnsureMTA. The intention is to prevent
84 // AsyncOperation from becoming some kind of free-for-all mechanism for
85 // asynchronously executing work on a background thread.
86 template <typename FuncT>
87 static void AsyncOperation(FuncT&& aClosure) {
88 if (IsCurrentThreadMTA()) {
89 aClosure();
90 return;
93 nsCOMPtr<nsIThread> thread(GetPersistentMTAThread());
94 MOZ_ASSERT(thread);
95 if (!thread) {
96 return;
99 DebugOnly<nsresult> rv = thread->Dispatch(
100 NS_NewRunnableFunction("mscom::EnsureMTA::AsyncOperation",
101 std::move(aClosure)),
102 NS_DISPATCH_NORMAL);
103 MOZ_ASSERT(NS_SUCCEEDED(rv));
107 * This constructor just ensures that the MTA is up and running. This should
108 * only be called by ProcessRuntime.
110 EnsureMTA();
112 friend class mozilla::mscom::ProcessRuntime;
114 template <typename T>
115 friend struct mozilla::mscom::detail::MTADelete;
117 template <typename T>
118 friend struct mozilla::mscom::detail::MTARelease;
120 template <typename T>
121 friend struct mozilla::mscom::detail::MTAReleaseInChildProcess;
123 friend struct mozilla::mscom::detail::PreservedStreamDeleter;
126 } // namespace mscom
127 } // namespace mozilla
129 #endif // mozilla_mscom_EnsureMTA_h