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"
19 #include "nsIThread.h"
20 #include "nsThreadUtils.h"
21 #include "nsWindowsHelpers.h"
29 // Forward declarations
37 struct MTAReleaseInChildProcess
;
39 struct PreservedStreamDeleter
;
45 // This class is OK to use as a temporary on the stack.
46 class MOZ_STACK_CLASS EnsureMTA final
{
50 // Forcibly dispatch to the thread returned by GetPersistentMTAThread(),
51 // even if the current thread is already inside a MTA.
52 ForceDispatchToPersistentThread
,
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
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
);
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()) {
93 nsCOMPtr
<nsIThread
> thread(GetPersistentMTAThread());
99 DebugOnly
<nsresult
> rv
= thread
->Dispatch(
100 NS_NewRunnableFunction("mscom::EnsureMTA::AsyncOperation",
101 std::move(aClosure
)),
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.
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
;
127 } // namespace mozilla
129 #endif // mozilla_mscom_EnsureMTA_h