2014-06-30 19:39:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
#ifndef nsThreadUtils_h__
|
|
|
|
#define nsThreadUtils_h__
|
|
|
|
|
2020-03-28 16:57:15 +03:00
|
|
|
#include <type_traits>
|
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj
This was done by:
This was done by applying:
```
diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
index 789affde7bbf..fe33c4c7d4d1 100644
--- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py
+++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
@@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase):
from subprocess import Popen, PIPE, check_output, CalledProcessError
diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE)
- args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format]
+ args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes']
if not output_file:
args.append("-i")
```
Then running `./mach clang-format -c <commit-hash>`
Then undoing that patch.
Then running check_spidermonkey_style.py --fixup
Then running `./mach clang-format`
I had to fix four things:
* I needed to move <utility> back down in GuardObjects.h because I was hitting
obscure problems with our system include wrappers like this:
0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration
0:03.94 extern void *realloc (void *__ptr, size_t __size)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here
0:03.94 MALLOC_DECL(realloc, void*, void*, size_t)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL'
0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
0:03.94 ^
0:03.94 <scratch space>:178:1: note: expanded from here
0:03.94 realloc_impl
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl'
0:03.94 #define realloc_impl mozmem_malloc_impl(realloc)
Which I really didn't feel like digging into.
* I had to restore the order of TrustOverrideUtils.h and related files in nss
because the .inc files depend on TrustOverrideUtils.h being included earlier.
* I had to add a missing include to RollingNumber.h
* Also had to partially restore include order in JsepSessionImpl.cpp to avoid
some -WError issues due to some static inline functions being defined in a
header but not used in the rest of the compilation unit.
Differential Revision: https://phabricator.services.mozilla.com/D60327
--HG--
extra : moz-landing-system : lando
2020-01-20 19:19:48 +03:00
|
|
|
#include <utility>
|
|
|
|
|
2013-09-19 17:54:41 +04:00
|
|
|
#include "MainThreadUtils.h"
|
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj
This was done by:
This was done by applying:
```
diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
index 789affde7bbf..fe33c4c7d4d1 100644
--- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py
+++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
@@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase):
from subprocess import Popen, PIPE, check_output, CalledProcessError
diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE)
- args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format]
+ args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes']
if not output_file:
args.append("-i")
```
Then running `./mach clang-format -c <commit-hash>`
Then undoing that patch.
Then running check_spidermonkey_style.py --fixup
Then running `./mach clang-format`
I had to fix four things:
* I needed to move <utility> back down in GuardObjects.h because I was hitting
obscure problems with our system include wrappers like this:
0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration
0:03.94 extern void *realloc (void *__ptr, size_t __size)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here
0:03.94 MALLOC_DECL(realloc, void*, void*, size_t)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL'
0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
0:03.94 ^
0:03.94 <scratch space>:178:1: note: expanded from here
0:03.94 realloc_impl
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl'
0:03.94 #define realloc_impl mozmem_malloc_impl(realloc)
Which I really didn't feel like digging into.
* I had to restore the order of TrustOverrideUtils.h and related files in nss
because the .inc files depend on TrustOverrideUtils.h being included earlier.
* I had to add a missing include to RollingNumber.h
* Also had to partially restore include order in JsepSessionImpl.cpp to avoid
some -WError issues due to some static inline functions being defined in a
header but not used in the rest of the compilation unit.
Differential Revision: https://phabricator.services.mozilla.com/D60327
--HG--
extra : moz-landing-system : lando
2020-01-20 19:19:48 +03:00
|
|
|
#include "mozilla/AbstractEventQueue.h"
|
|
|
|
#include "mozilla/Atomics.h"
|
|
|
|
#include "mozilla/Likely.h"
|
|
|
|
#include "mozilla/Maybe.h"
|
|
|
|
#include "mozilla/TimeStamp.h"
|
|
|
|
#include "mozilla/Tuple.h"
|
|
|
|
#include "mozilla/TypeTraits.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2012-12-11 22:13:29 +04:00
|
|
|
#include "nsICancelableRunnable.h"
|
2016-08-24 17:18:06 +03:00
|
|
|
#include "nsIIdlePeriod.h"
|
2017-05-16 18:18:33 +03:00
|
|
|
#include "nsIIdleRunnable.h"
|
2016-11-23 03:18:52 +03:00
|
|
|
#include "nsINamed.h"
|
|
|
|
#include "nsIRunnable.h"
|
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj
This was done by:
This was done by applying:
```
diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
index 789affde7bbf..fe33c4c7d4d1 100644
--- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py
+++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
@@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase):
from subprocess import Popen, PIPE, check_output, CalledProcessError
diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE)
- args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format]
+ args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes']
if not output_file:
args.append("-i")
```
Then running `./mach clang-format -c <commit-hash>`
Then undoing that patch.
Then running check_spidermonkey_style.py --fixup
Then running `./mach clang-format`
I had to fix four things:
* I needed to move <utility> back down in GuardObjects.h because I was hitting
obscure problems with our system include wrappers like this:
0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration
0:03.94 extern void *realloc (void *__ptr, size_t __size)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here
0:03.94 MALLOC_DECL(realloc, void*, void*, size_t)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL'
0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
0:03.94 ^
0:03.94 <scratch space>:178:1: note: expanded from here
0:03.94 realloc_impl
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl'
0:03.94 #define realloc_impl mozmem_malloc_impl(realloc)
Which I really didn't feel like digging into.
* I had to restore the order of TrustOverrideUtils.h and related files in nss
because the .inc files depend on TrustOverrideUtils.h being included earlier.
* I had to add a missing include to RollingNumber.h
* Also had to partially restore include order in JsepSessionImpl.cpp to avoid
some -WError issues due to some static inline functions being defined in a
header but not used in the rest of the compilation unit.
Differential Revision: https://phabricator.services.mozilla.com/D60327
--HG--
extra : moz-landing-system : lando
2020-01-20 19:19:48 +03:00
|
|
|
#include "nsIThread.h"
|
2016-11-23 03:18:52 +03:00
|
|
|
#include "nsIThreadManager.h"
|
2017-05-25 00:34:50 +03:00
|
|
|
#include "nsITimer.h"
|
2017-12-07 03:52:51 +03:00
|
|
|
#include "nsString.h"
|
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj
This was done by:
This was done by applying:
```
diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
index 789affde7bbf..fe33c4c7d4d1 100644
--- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py
+++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
@@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase):
from subprocess import Popen, PIPE, check_output, CalledProcessError
diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE)
- args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format]
+ args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes']
if not output_file:
args.append("-i")
```
Then running `./mach clang-format -c <commit-hash>`
Then undoing that patch.
Then running check_spidermonkey_style.py --fixup
Then running `./mach clang-format`
I had to fix four things:
* I needed to move <utility> back down in GuardObjects.h because I was hitting
obscure problems with our system include wrappers like this:
0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration
0:03.94 extern void *realloc (void *__ptr, size_t __size)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here
0:03.94 MALLOC_DECL(realloc, void*, void*, size_t)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL'
0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
0:03.94 ^
0:03.94 <scratch space>:178:1: note: expanded from here
0:03.94 realloc_impl
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl'
0:03.94 #define realloc_impl mozmem_malloc_impl(realloc)
Which I really didn't feel like digging into.
* I had to restore the order of TrustOverrideUtils.h and related files in nss
because the .inc files depend on TrustOverrideUtils.h being included earlier.
* I had to add a missing include to RollingNumber.h
* Also had to partially restore include order in JsepSessionImpl.cpp to avoid
some -WError issues due to some static inline functions being defined in a
header but not used in the rest of the compilation unit.
Differential Revision: https://phabricator.services.mozilla.com/D60327
--HG--
extra : moz-landing-system : lando
2020-01-20 19:19:48 +03:00
|
|
|
#include "prinrval.h"
|
|
|
|
#include "prthread.h"
|
2018-09-04 19:59:56 +03:00
|
|
|
#include "xpcpublic.h"
|
2018-04-09 14:12:13 +03:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// These methods are alternatives to the methods on nsIThreadManager, provided
|
|
|
|
// for convenience.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new thread, and optionally provide an initial event for the thread.
|
|
|
|
*
|
2020-02-12 00:01:56 +03:00
|
|
|
* @param aName
|
|
|
|
* The name of the thread.
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aResult
|
2006-05-10 21:30:15 +04:00
|
|
|
* The resulting nsIThread object.
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aInitialEvent
|
2006-05-10 21:30:15 +04:00
|
|
|
* The initial event to run on this thread. This parameter may be null.
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aStackSize
|
2011-07-27 07:26:47 +04:00
|
|
|
* The size in bytes to reserve for the thread's stack.
|
2006-05-10 21:30:15 +04:00
|
|
|
*
|
|
|
|
* @returns NS_ERROR_INVALID_ARG
|
|
|
|
* Indicates that the given name is not unique.
|
|
|
|
*/
|
|
|
|
|
2016-12-20 16:18:22 +03:00
|
|
|
extern nsresult NS_NewNamedThread(
|
2016-12-22 02:05:51 +03:00
|
|
|
const nsACString& aName, nsIThread** aResult,
|
2014-06-27 05:35:39 +04:00
|
|
|
nsIRunnable* aInitialEvent = nullptr,
|
2016-12-20 16:18:22 +03:00
|
|
|
uint32_t aStackSize = nsIThreadManager::DEFAULT_STACK_SIZE);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-12-22 02:05:51 +03:00
|
|
|
template <size_t LEN>
|
|
|
|
inline nsresult NS_NewNamedThread(
|
|
|
|
const char (&aName)[LEN], nsIThread** aResult,
|
|
|
|
nsIRunnable* aInitialEvent = nullptr,
|
|
|
|
uint32_t aStackSize = nsIThreadManager::DEFAULT_STACK_SIZE) {
|
|
|
|
static_assert(LEN <= 16, "Thread name must be no more than 16 characters");
|
|
|
|
return NS_NewNamedThread(nsDependentCString(aName, LEN - 1), aResult,
|
|
|
|
aInitialEvent, aStackSize);
|
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
/**
|
2018-01-19 18:41:22 +03:00
|
|
|
* Get a reference to the current thread, creating it if it does not exist yet.
|
2006-05-10 21:30:15 +04:00
|
|
|
*
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aResult
|
2006-05-10 21:30:15 +04:00
|
|
|
* The resulting nsIThread object.
|
|
|
|
*/
|
2016-08-15 07:29:43 +03:00
|
|
|
extern nsresult NS_GetCurrentThread(nsIThread** aResult);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch the given event to the current thread.
|
|
|
|
*
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aEvent
|
2006-05-10 21:30:15 +04:00
|
|
|
* The event to dispatch.
|
|
|
|
*
|
|
|
|
* @returns NS_ERROR_INVALID_ARG
|
|
|
|
* If event is null.
|
|
|
|
*/
|
2016-08-15 07:29:43 +03:00
|
|
|
extern nsresult NS_DispatchToCurrentThread(nsIRunnable* aEvent);
|
2015-10-06 05:00:59 +03:00
|
|
|
extern nsresult NS_DispatchToCurrentThread(
|
|
|
|
already_AddRefed<nsIRunnable>&& aEvent);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch the given event to the main thread.
|
|
|
|
*
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aEvent
|
2006-05-10 21:30:15 +04:00
|
|
|
* The event to dispatch.
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aDispatchFlags
|
2006-05-10 21:30:15 +04:00
|
|
|
* The flags to pass to the main thread's dispatch method.
|
|
|
|
*
|
|
|
|
* @returns NS_ERROR_INVALID_ARG
|
|
|
|
* If event is null.
|
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
extern nsresult NS_DispatchToMainThread(
|
|
|
|
nsIRunnable* aEvent, uint32_t aDispatchFlags = NS_DISPATCH_NORMAL);
|
2015-07-10 06:21:46 +03:00
|
|
|
extern nsresult NS_DispatchToMainThread(
|
|
|
|
already_AddRefed<nsIRunnable>&& aEvent,
|
|
|
|
uint32_t aDispatchFlags = NS_DISPATCH_NORMAL);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2016-08-22 15:52:45 +03:00
|
|
|
extern nsresult NS_DelayedDispatchToCurrentThread(
|
|
|
|
already_AddRefed<nsIRunnable>&& aEvent, uint32_t aDelayMs);
|
|
|
|
|
2017-05-25 00:34:50 +03:00
|
|
|
/**
|
2019-01-26 20:18:05 +03:00
|
|
|
* Dispatch the given event to the specified queue of the current thread.
|
2017-05-25 00:34:50 +03:00
|
|
|
*
|
2019-01-26 20:18:05 +03:00
|
|
|
* @param aEvent The event to dispatch.
|
|
|
|
* @param aQueue The event queue for the thread to use
|
2017-05-25 00:34:50 +03:00
|
|
|
*
|
|
|
|
* @returns NS_ERROR_INVALID_ARG
|
|
|
|
* If event is null.
|
|
|
|
* @returns NS_ERROR_UNEXPECTED
|
|
|
|
* If the thread is shutting down.
|
|
|
|
*/
|
2019-01-26 20:18:05 +03:00
|
|
|
extern nsresult NS_DispatchToCurrentThreadQueue(
|
|
|
|
already_AddRefed<nsIRunnable>&& aEvent, mozilla::EventQueuePriority aQueue);
|
2016-08-24 17:18:06 +03:00
|
|
|
|
2018-10-02 00:38:55 +03:00
|
|
|
/**
|
2019-01-26 20:18:05 +03:00
|
|
|
* Dispatch the given event to the specified queue of the main thread.
|
2018-10-02 00:38:55 +03:00
|
|
|
*
|
|
|
|
* @param aEvent The event to dispatch.
|
2019-01-26 20:18:05 +03:00
|
|
|
* @param aQueue The event queue for the thread to use
|
2018-10-02 00:38:55 +03:00
|
|
|
*
|
|
|
|
* @returns NS_ERROR_INVALID_ARG
|
|
|
|
* If event is null.
|
|
|
|
* @returns NS_ERROR_UNEXPECTED
|
|
|
|
* If the thread is shutting down.
|
|
|
|
*/
|
2019-01-26 20:18:05 +03:00
|
|
|
extern nsresult NS_DispatchToMainThreadQueue(
|
|
|
|
already_AddRefed<nsIRunnable>&& aEvent, mozilla::EventQueuePriority aQueue);
|
2018-10-02 00:38:55 +03:00
|
|
|
|
2017-05-25 00:34:50 +03:00
|
|
|
/**
|
2019-01-26 20:18:05 +03:00
|
|
|
* Dispatch the given event to an idle queue of the current thread.
|
2017-05-25 00:34:50 +03:00
|
|
|
*
|
|
|
|
* @param aEvent The event to dispatch. If the event implements
|
|
|
|
* nsIIdleRunnable, it will receive a call on
|
|
|
|
* nsIIdleRunnable::SetTimer when dispatched, with the value of
|
|
|
|
* aTimeout.
|
|
|
|
*
|
|
|
|
* @param aTimeout The time in milliseconds until the event should be
|
2019-01-26 20:18:05 +03:00
|
|
|
* moved from an idle queue to the regular queue, if it hasn't been
|
2017-05-25 00:34:50 +03:00
|
|
|
* executed. If aEvent is also an nsIIdleRunnable, it is expected
|
|
|
|
* that it should handle the timeout itself, after a call to
|
|
|
|
* nsIIdleRunnable::SetTimer.
|
|
|
|
*
|
2019-01-26 20:18:05 +03:00
|
|
|
* @param aQueue
|
|
|
|
* The event queue for the thread to use. Must be an idle queue
|
|
|
|
* (Idle or DeferredTimers)
|
|
|
|
*
|
2017-05-25 00:34:50 +03:00
|
|
|
* @returns NS_ERROR_INVALID_ARG
|
|
|
|
* If event is null.
|
|
|
|
* @returns NS_ERROR_UNEXPECTED
|
|
|
|
* If the thread is shutting down.
|
|
|
|
*/
|
2019-01-26 20:18:05 +03:00
|
|
|
extern nsresult NS_DispatchToCurrentThreadQueue(
|
|
|
|
already_AddRefed<nsIRunnable>&& aEvent, uint32_t aTimeout,
|
|
|
|
mozilla::EventQueuePriority aQueue);
|
2017-05-25 00:34:50 +03:00
|
|
|
|
2017-06-14 11:05:53 +03:00
|
|
|
/**
|
2019-01-26 20:18:05 +03:00
|
|
|
* Dispatch the given event to a queue of a thread.
|
2017-06-14 11:05:53 +03:00
|
|
|
*
|
|
|
|
* @param aEvent The event to dispatch.
|
|
|
|
* @param aThread The target thread for the dispatch.
|
2019-01-26 20:18:05 +03:00
|
|
|
* @param aQueue The event queue for the thread to use.
|
2017-06-14 11:05:53 +03:00
|
|
|
*
|
|
|
|
* @returns NS_ERROR_INVALID_ARG
|
|
|
|
* If event is null.
|
|
|
|
* @returns NS_ERROR_UNEXPECTED
|
|
|
|
* If the thread is shutting down.
|
|
|
|
*/
|
2019-01-26 20:18:05 +03:00
|
|
|
extern nsresult NS_DispatchToThreadQueue(already_AddRefed<nsIRunnable>&& aEvent,
|
|
|
|
nsIThread* aThread,
|
|
|
|
mozilla::EventQueuePriority aQueue);
|
2017-06-14 11:05:53 +03:00
|
|
|
|
|
|
|
/**
|
2019-01-26 20:18:05 +03:00
|
|
|
* Dispatch the given event to an idle queue of a thread.
|
2017-06-14 11:05:53 +03:00
|
|
|
*
|
|
|
|
* @param aEvent The event to dispatch. If the event implements
|
|
|
|
* nsIIdleRunnable, it will receive a call on
|
|
|
|
* nsIIdleRunnable::SetTimer when dispatched, with the value of
|
|
|
|
* aTimeout.
|
|
|
|
*
|
|
|
|
* @param aTimeout The time in milliseconds until the event should be
|
2019-01-26 20:18:05 +03:00
|
|
|
* moved from an idle queue to the regular queue, if it hasn't been
|
2017-06-14 11:05:53 +03:00
|
|
|
* executed. If aEvent is also an nsIIdleRunnable, it is expected
|
|
|
|
* that it should handle the timeout itself, after a call to
|
|
|
|
* nsIIdleRunnable::SetTimer.
|
|
|
|
*
|
|
|
|
* @param aThread The target thread for the dispatch.
|
|
|
|
*
|
2019-01-26 20:18:05 +03:00
|
|
|
* @param aQueue
|
|
|
|
* The event queue for the thread to use. Must be an idle queue
|
|
|
|
* (Idle or DeferredTimers)
|
|
|
|
*
|
2017-06-14 11:05:53 +03:00
|
|
|
* @returns NS_ERROR_INVALID_ARG
|
|
|
|
* If event is null.
|
|
|
|
* @returns NS_ERROR_UNEXPECTED
|
|
|
|
* If the thread is shutting down.
|
|
|
|
*/
|
2019-01-26 20:18:05 +03:00
|
|
|
extern nsresult NS_DispatchToThreadQueue(already_AddRefed<nsIRunnable>&& aEvent,
|
|
|
|
uint32_t aTimeout, nsIThread* aThread,
|
|
|
|
mozilla::EventQueuePriority aQueue);
|
2017-06-14 11:05:53 +03:00
|
|
|
|
2007-04-02 19:29:41 +04:00
|
|
|
#ifndef XPCOM_GLUE_AVOID_NSPR
|
2006-05-10 21:30:15 +04:00
|
|
|
/**
|
|
|
|
* Process all pending events for the given thread before returning. This
|
|
|
|
* method simply calls ProcessNextEvent on the thread while HasPendingEvents
|
|
|
|
* continues to return true and the time spent in NS_ProcessPendingEvents
|
|
|
|
* does not exceed the given timeout value.
|
|
|
|
*
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aThread
|
2006-05-10 21:30:15 +04:00
|
|
|
* The thread object for which to process pending events. If null, then
|
|
|
|
* events will be processed for the current thread.
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aTimeout
|
2006-05-10 21:30:15 +04:00
|
|
|
* The maximum number of milliseconds to spend processing pending events.
|
|
|
|
* Events are not pre-empted to honor this timeout. Rather, the timeout
|
|
|
|
* value is simply used to determine whether or not to process another event.
|
|
|
|
* Pass PR_INTERVAL_NO_TIMEOUT to specify no timeout.
|
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
extern nsresult NS_ProcessPendingEvents(
|
|
|
|
nsIThread* aThread, PRIntervalTime aTimeout = PR_INTERVAL_NO_TIMEOUT);
|
2007-04-02 19:29:41 +04:00
|
|
|
#endif
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut for nsIThread::HasPendingEvents.
|
|
|
|
*
|
|
|
|
* It is an error to call this function when the given thread is not the
|
2011-10-17 18:59:28 +04:00
|
|
|
* current thread. This function will return false if called from some
|
2006-05-10 21:30:15 +04:00
|
|
|
* other thread.
|
|
|
|
*
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aThread
|
2006-05-10 21:30:15 +04:00
|
|
|
* The current thread or null.
|
|
|
|
*
|
|
|
|
* @returns
|
|
|
|
* A boolean value that if "true" indicates that there are pending events
|
|
|
|
* in the current thread's event queue.
|
|
|
|
*/
|
2014-08-28 02:47:27 +04:00
|
|
|
extern bool NS_HasPendingEvents(nsIThread* aThread = nullptr);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shortcut for nsIThread::ProcessNextEvent.
|
2014-06-27 05:35:39 +04:00
|
|
|
*
|
2006-05-10 21:30:15 +04:00
|
|
|
* It is an error to call this function when the given thread is not the
|
2011-10-17 18:59:28 +04:00
|
|
|
* current thread. This function will simply return false if called
|
2006-05-10 21:30:15 +04:00
|
|
|
* from some other thread.
|
|
|
|
*
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aThread
|
2006-05-10 21:30:15 +04:00
|
|
|
* The current thread or null.
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aMayWait
|
2006-05-10 21:30:15 +04:00
|
|
|
* A boolean parameter that if "true" indicates that the method may block
|
|
|
|
* the calling thread to wait for a pending event.
|
|
|
|
*
|
|
|
|
* @returns
|
|
|
|
* A boolean value that if "true" indicates that an event from the current
|
|
|
|
* thread's event queue was processed.
|
|
|
|
*/
|
2014-08-28 02:47:27 +04:00
|
|
|
extern bool NS_ProcessNextEvent(nsIThread* aThread = nullptr,
|
|
|
|
bool aMayWait = true);
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2017-05-15 16:34:19 +03:00
|
|
|
// A wrapper for nested event loops.
|
|
|
|
//
|
|
|
|
// This function is intended to make code more obvious (do you remember
|
|
|
|
// what NS_ProcessNextEvent(nullptr, true) means?) and slightly more
|
|
|
|
// efficient, as people often pass nullptr or NS_GetCurrentThread to
|
|
|
|
// NS_ProcessNextEvent, which results in needless querying of the current
|
|
|
|
// thread every time through the loop.
|
|
|
|
//
|
|
|
|
// You should use this function in preference to NS_ProcessNextEvent inside
|
|
|
|
// a loop unless one of the following is true:
|
|
|
|
//
|
|
|
|
// * You need to pass `false` to NS_ProcessNextEvent; or
|
|
|
|
// * You need to do unusual things around the call to NS_ProcessNextEvent,
|
|
|
|
// such as unlocking mutexes that you are holding.
|
|
|
|
//
|
|
|
|
// If you *do* need to call NS_ProcessNextEvent manually, please do call
|
|
|
|
// NS_GetCurrentThread() outside of your loop and pass the returned pointer
|
|
|
|
// into NS_ProcessNextEvent for a tiny efficiency win.
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
// You should normally not need to deal with this template parameter. If
|
|
|
|
// you enjoy esoteric event loop details, read on.
|
|
|
|
//
|
|
|
|
// If you specify that NS_ProcessNextEvent wait for an event, it is possible
|
|
|
|
// for NS_ProcessNextEvent to return false, i.e. to indicate that an event
|
|
|
|
// was not processed. This can only happen when the thread has been shut
|
|
|
|
// down by another thread, but is still attempting to process events outside
|
|
|
|
// of a nested event loop.
|
|
|
|
//
|
|
|
|
// This behavior is admittedly strange. The scenario it deals with is the
|
|
|
|
// following:
|
|
|
|
//
|
|
|
|
// * The current thread has been shut down by some owner thread.
|
|
|
|
// * The current thread is spinning an event loop waiting for some condition
|
|
|
|
// to become true.
|
|
|
|
// * Said condition is actually being fulfilled by another thread, so there
|
|
|
|
// are timing issues in play.
|
|
|
|
//
|
|
|
|
// Thus, there is a small window where the current thread's event loop
|
|
|
|
// spinning can check the condition, find it false, and call
|
|
|
|
// NS_ProcessNextEvent to wait for another event. But we don't actually
|
|
|
|
// want it to wait indefinitely, because there might not be any other events
|
|
|
|
// in the event loop, and the current thread can't accept dispatched events
|
|
|
|
// because it's being shut down. Thus, actually blocking would hang the
|
|
|
|
// thread, which is bad. The solution, then, is to detect such a scenario
|
|
|
|
// and not actually block inside NS_ProcessNextEvent.
|
|
|
|
//
|
|
|
|
// But this is a problem, because we want to return the status of
|
|
|
|
// NS_ProcessNextEvent to the caller of SpinEventLoopUntil if possible. In
|
|
|
|
// the above scenario, however, we'd stop spinning prematurely and cause
|
|
|
|
// all sorts of havoc. We therefore have this template parameter to
|
|
|
|
// control whether errors are ignored or passed out to the caller of
|
|
|
|
// SpinEventLoopUntil. The latter is the default; if you find yourself
|
|
|
|
// wanting to use the former, you should think long and hard before doing
|
|
|
|
// so, and write a comment like this defending your choice.
|
|
|
|
|
|
|
|
enum class ProcessFailureBehavior {
|
|
|
|
IgnoreAndContinue,
|
|
|
|
ReportToCaller,
|
|
|
|
};
|
|
|
|
|
|
|
|
template <
|
|
|
|
ProcessFailureBehavior Behavior = ProcessFailureBehavior::ReportToCaller,
|
|
|
|
typename Pred>
|
|
|
|
bool SpinEventLoopUntil(Pred&& aPredicate, nsIThread* aThread = nullptr) {
|
|
|
|
nsIThread* thread = aThread ? aThread : NS_GetCurrentThread();
|
|
|
|
|
2018-09-04 19:59:56 +03:00
|
|
|
// From a latency perspective, spinning the event loop is like leaving script
|
|
|
|
// and returning to the event loop. Tell the watchdog we stopped running
|
|
|
|
// script (until we return).
|
|
|
|
mozilla::Maybe<xpc::AutoScriptActivity> asa;
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
asa.emplace(false);
|
|
|
|
}
|
|
|
|
|
2017-05-15 16:34:19 +03:00
|
|
|
while (!aPredicate()) {
|
|
|
|
bool didSomething = NS_ProcessNextEvent(thread, true);
|
|
|
|
|
|
|
|
if (Behavior == ProcessFailureBehavior::IgnoreAndContinue) {
|
|
|
|
// Don't care what happened, continue on.
|
|
|
|
continue;
|
|
|
|
} else if (!didSomething) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2017-03-28 00:26:41 +03:00
|
|
|
/**
|
|
|
|
* Returns true if we're in the compositor thread.
|
|
|
|
*
|
|
|
|
* We declare this here because the headers required to invoke
|
|
|
|
* CompositorThreadHolder::IsInCompositorThread() also pull in a bunch of system
|
|
|
|
* headers that #define various tokens in a way that can break the build.
|
|
|
|
*/
|
|
|
|
extern bool NS_IsInCompositorThread();
|
|
|
|
|
2020-02-24 14:15:41 +03:00
|
|
|
extern bool NS_IsInCanvasThreadOrWorker();
|
2018-12-02 17:19:11 +03:00
|
|
|
|
2017-11-02 07:44:12 +03:00
|
|
|
extern bool NS_IsInVRThread();
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Helpers that work with nsCOMPtr:
|
|
|
|
|
|
|
|
inline already_AddRefed<nsIThread> do_GetCurrentThread() {
|
2014-06-27 05:35:39 +04:00
|
|
|
nsIThread* thread = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_GetCurrentThread(&thread);
|
|
|
|
return already_AddRefed<nsIThread>(thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline already_AddRefed<nsIThread> do_GetMainThread() {
|
2014-06-27 05:35:39 +04:00
|
|
|
nsIThread* thread = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_GetMainThread(&thread);
|
|
|
|
return already_AddRefed<nsIThread>(thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
2018-01-19 18:41:22 +03:00
|
|
|
// Fast access to the current thread. Will create an nsIThread if one does not
|
|
|
|
// exist already! Do not release the returned pointer! If you want to use this
|
|
|
|
// pointer from some other thread, then you will need to AddRef it. Otherwise,
|
|
|
|
// you should only consider this pointer valid from code running on the current
|
|
|
|
// thread.
|
2014-08-28 02:47:27 +04:00
|
|
|
extern nsIThread* NS_GetCurrentThread();
|
2017-02-07 13:57:23 +03:00
|
|
|
|
2018-01-19 18:41:22 +03:00
|
|
|
// Exactly the same as NS_GetCurrentThread, except it will not create an
|
|
|
|
// nsThread if one does not exist yet. This is useful in cases where you have
|
|
|
|
// code that runs on threads that may or may not not be driven by an nsThread
|
|
|
|
// event loop, and wish to avoid inadvertently creating a superfluous nsThread.
|
|
|
|
extern nsIThread* NS_GetCurrentThreadNoCreate();
|
|
|
|
|
2017-02-07 13:57:23 +03:00
|
|
|
/**
|
|
|
|
* Set the name of the current thread. Prefer this function over
|
|
|
|
* PR_SetCurrentThreadName() if possible. The name will also be included in the
|
|
|
|
* crash report.
|
|
|
|
*
|
|
|
|
* @param aName
|
|
|
|
* Name of the thread. A C language null-terminated string.
|
|
|
|
*/
|
|
|
|
extern void NS_SetCurrentThreadName(const char* aName);
|
2006-05-10 21:30:15 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#ifndef XPCOM_GLUE_AVOID_NSPR
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
2016-08-24 17:18:06 +03:00
|
|
|
// This class is designed to be subclassed.
|
|
|
|
class IdlePeriod : public nsIIdlePeriod {
|
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIIDLEPERIOD
|
|
|
|
|
2020-02-12 14:13:33 +03:00
|
|
|
IdlePeriod() = default;
|
2016-08-24 17:18:06 +03:00
|
|
|
|
|
|
|
protected:
|
2020-02-12 14:13:33 +03:00
|
|
|
virtual ~IdlePeriod() = default;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-08-24 17:18:06 +03:00
|
|
|
private:
|
|
|
|
IdlePeriod(const IdlePeriod&) = delete;
|
|
|
|
IdlePeriod& operator=(const IdlePeriod&) = delete;
|
|
|
|
IdlePeriod& operator=(const IdlePeriod&&) = delete;
|
|
|
|
};
|
|
|
|
|
2017-05-25 00:34:50 +03:00
|
|
|
// Cancelable runnable methods implement nsICancelableRunnable, and
|
|
|
|
// Idle and IdleWithTimer also nsIIdleRunnable.
|
2018-06-25 19:17:27 +03:00
|
|
|
enum class RunnableKind { Standard, Cancelable, Idle, IdleWithTimer };
|
2017-05-25 00:34:50 +03:00
|
|
|
|
2018-03-23 21:53:55 +03:00
|
|
|
// Implementing nsINamed on Runnable bloats vtables for the hundreds of
|
|
|
|
// Runnable subclasses that we have, so we want to avoid that overhead
|
|
|
|
// when we're not using nsINamed for anything.
|
|
|
|
# ifndef RELEASE_OR_BETA
|
|
|
|
# define MOZ_COLLECTING_RUNNABLE_TELEMETRY
|
|
|
|
# endif
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// This class is designed to be subclassed.
|
2018-03-23 21:53:55 +03:00
|
|
|
class Runnable : public nsIRunnable
|
|
|
|
# ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
|
|
|
|
,
|
|
|
|
public nsINamed
|
|
|
|
# endif
|
2006-05-10 21:30:15 +04:00
|
|
|
{
|
|
|
|
public:
|
2020-02-27 20:39:15 +03:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_DECL_NSIRUNNABLE
|
2018-03-23 21:53:55 +03:00
|
|
|
# ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
|
2016-11-23 03:18:52 +03:00
|
|
|
NS_DECL_NSINAMED
|
2018-03-23 21:53:55 +03:00
|
|
|
# endif
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2017-06-12 23:39:47 +03:00
|
|
|
Runnable() = delete;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2018-03-23 21:53:55 +03:00
|
|
|
# ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
|
2017-01-18 03:50:34 +03:00
|
|
|
explicit Runnable(const char* aName) : mName(aName) {}
|
2018-03-23 21:53:55 +03:00
|
|
|
# else
|
|
|
|
explicit Runnable(const char* aName) {}
|
2017-01-18 03:50:34 +03:00
|
|
|
# endif
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
protected:
|
2020-02-12 14:13:33 +03:00
|
|
|
virtual ~Runnable() = default;
|
2016-11-23 03:18:52 +03:00
|
|
|
|
2018-03-23 21:53:55 +03:00
|
|
|
# ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
|
2016-11-23 03:18:52 +03:00
|
|
|
const char* mName = nullptr;
|
|
|
|
# endif
|
2017-06-13 13:10:13 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
Runnable(const Runnable&) = delete;
|
|
|
|
Runnable& operator=(const Runnable&) = delete;
|
|
|
|
Runnable& operator=(const Runnable&&) = delete;
|
2006-05-10 21:30:15 +04:00
|
|
|
};
|
|
|
|
|
2012-12-11 22:13:29 +04:00
|
|
|
// This class is designed to be subclassed.
|
2016-04-26 03:23:21 +03:00
|
|
|
class CancelableRunnable : public Runnable, public nsICancelableRunnable {
|
2012-12-11 22:13:29 +04:00
|
|
|
public:
|
2016-04-11 21:40:06 +03:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
// nsICancelableRunnable
|
|
|
|
virtual nsresult Cancel() override;
|
2012-12-11 22:13:29 +04:00
|
|
|
|
2017-06-12 23:39:47 +03:00
|
|
|
CancelableRunnable() = delete;
|
2017-01-18 03:50:34 +03:00
|
|
|
explicit CancelableRunnable(const char* aName) : Runnable(aName) {}
|
2012-12-11 22:13:29 +04:00
|
|
|
|
|
|
|
protected:
|
2020-02-12 14:13:33 +03:00
|
|
|
virtual ~CancelableRunnable() = default;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-05-26 04:04:35 +03:00
|
|
|
private:
|
|
|
|
CancelableRunnable(const CancelableRunnable&) = delete;
|
|
|
|
CancelableRunnable& operator=(const CancelableRunnable&) = delete;
|
|
|
|
CancelableRunnable& operator=(const CancelableRunnable&&) = delete;
|
2012-12-11 22:13:29 +04:00
|
|
|
};
|
|
|
|
|
2016-08-24 17:18:06 +03:00
|
|
|
// This class is designed to be subclassed.
|
2017-05-16 18:18:33 +03:00
|
|
|
class IdleRunnable : public CancelableRunnable, public nsIIdleRunnable {
|
2016-08-24 17:18:06 +03:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
2017-06-12 22:34:10 +03:00
|
|
|
IdleRunnable() : CancelableRunnable("IdleRunnable") {}
|
2017-05-25 00:34:50 +03:00
|
|
|
explicit IdleRunnable(const char* aName) : CancelableRunnable(aName) {}
|
2016-08-24 17:18:06 +03:00
|
|
|
|
|
|
|
protected:
|
2020-02-12 14:13:33 +03:00
|
|
|
virtual ~IdleRunnable() = default;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-08-24 17:18:06 +03:00
|
|
|
private:
|
2017-05-16 18:18:33 +03:00
|
|
|
IdleRunnable(const IdleRunnable&) = delete;
|
|
|
|
IdleRunnable& operator=(const IdleRunnable&) = delete;
|
|
|
|
IdleRunnable& operator=(const IdleRunnable&&) = delete;
|
2016-08-24 17:18:06 +03:00
|
|
|
};
|
|
|
|
|
2017-05-19 10:41:24 +03:00
|
|
|
// This class is designed to be a wrapper of a real runnable to support event
|
|
|
|
// prioritizable.
|
|
|
|
class PrioritizableRunnable : public Runnable, public nsIRunnablePriority {
|
|
|
|
public:
|
|
|
|
PrioritizableRunnable(already_AddRefed<nsIRunnable>&& aRunnable,
|
|
|
|
uint32_t aPriority);
|
|
|
|
|
2018-03-23 21:53:55 +03:00
|
|
|
# ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
|
2017-05-19 10:41:24 +03:00
|
|
|
NS_IMETHOD GetName(nsACString& aName) override;
|
2018-03-23 21:53:55 +03:00
|
|
|
# endif
|
2017-05-19 10:41:24 +03:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
NS_DECL_NSIRUNNABLEPRIORITY
|
|
|
|
|
|
|
|
protected:
|
2020-02-12 14:13:33 +03:00
|
|
|
virtual ~PrioritizableRunnable() = default;
|
|
|
|
;
|
2017-05-19 10:41:24 +03:00
|
|
|
nsCOMPtr<nsIRunnable> mRunnable;
|
|
|
|
uint32_t mPriority;
|
|
|
|
};
|
|
|
|
|
2019-02-28 23:38:53 +03:00
|
|
|
extern already_AddRefed<nsIRunnable> CreateMediumHighRunnable(
|
|
|
|
already_AddRefed<nsIRunnable>&& aRunnable);
|
|
|
|
|
2016-06-29 05:24:54 +03:00
|
|
|
namespace detail {
|
2016-04-26 03:23:21 +03:00
|
|
|
|
2015-04-10 15:13:00 +03:00
|
|
|
// An event that can be used to call a C++11 functions or function objects,
|
|
|
|
// including lambdas. The function must have no required arguments, and must
|
|
|
|
// return void.
|
2016-06-02 02:21:36 +03:00
|
|
|
template <typename StoredFunction>
|
2016-06-29 05:24:54 +03:00
|
|
|
class RunnableFunction : public Runnable {
|
2015-04-10 15:13:00 +03:00
|
|
|
public:
|
2016-06-02 02:21:36 +03:00
|
|
|
template <typename F>
|
2017-07-21 06:50:43 +03:00
|
|
|
explicit RunnableFunction(const char* aName, F&& aFunction)
|
2018-06-01 19:30:30 +03:00
|
|
|
: Runnable(aName), mFunction(std::forward<F>(aFunction)) {}
|
2015-04-10 15:13:00 +03:00
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2016-06-29 05:24:54 +03:00
|
|
|
static_assert(IsVoid<decltype(mFunction())>::value,
|
2015-04-10 15:13:00 +03:00
|
|
|
"The lambda must return void!");
|
|
|
|
mFunction();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-04-10 15:13:00 +03:00
|
|
|
private:
|
2016-06-02 02:21:36 +03:00
|
|
|
StoredFunction mFunction;
|
2015-04-10 15:13:00 +03:00
|
|
|
};
|
|
|
|
|
2017-02-06 13:08:01 +03:00
|
|
|
// Type alias for NS_NewRunnableFunction
|
|
|
|
template <typename Function>
|
|
|
|
using RunnableFunctionImpl =
|
|
|
|
// Make sure we store a non-reference in nsRunnableFunction.
|
|
|
|
typename detail::RunnableFunction<typename RemoveReference<Function>::Type>;
|
2016-06-29 05:24:54 +03:00
|
|
|
} // namespace detail
|
|
|
|
|
2017-01-09 00:28:49 +03:00
|
|
|
namespace detail {
|
2017-01-09 00:21:44 +03:00
|
|
|
|
2017-01-09 00:28:49 +03:00
|
|
|
template <typename CVRemoved>
|
|
|
|
struct IsRefcountedSmartPointerHelper : FalseType {};
|
|
|
|
|
|
|
|
template <typename Pointee>
|
|
|
|
struct IsRefcountedSmartPointerHelper<RefPtr<Pointee>> : TrueType {};
|
|
|
|
|
|
|
|
template <typename Pointee>
|
|
|
|
struct IsRefcountedSmartPointerHelper<nsCOMPtr<Pointee>> : TrueType {};
|
|
|
|
|
|
|
|
} // namespace detail
|
2017-01-09 00:21:44 +03:00
|
|
|
|
|
|
|
template <typename T>
|
2017-01-09 00:28:49 +03:00
|
|
|
struct IsRefcountedSmartPointer
|
|
|
|
: detail::IsRefcountedSmartPointerHelper<typename RemoveCV<T>::Type> {};
|
2017-01-09 00:21:44 +03:00
|
|
|
|
2017-01-09 00:28:49 +03:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <typename T, typename CVRemoved>
|
|
|
|
struct RemoveSmartPointerHelper {
|
|
|
|
typedef T Type;
|
2017-01-09 00:21:44 +03:00
|
|
|
};
|
|
|
|
|
2017-01-09 00:28:49 +03:00
|
|
|
template <typename T, typename Pointee>
|
|
|
|
struct RemoveSmartPointerHelper<T, RefPtr<Pointee>> {
|
|
|
|
typedef Pointee Type;
|
2017-01-09 00:21:44 +03:00
|
|
|
};
|
|
|
|
|
2017-01-09 00:28:49 +03:00
|
|
|
template <typename T, typename Pointee>
|
|
|
|
struct RemoveSmartPointerHelper<T, nsCOMPtr<Pointee>> {
|
|
|
|
typedef Pointee Type;
|
2017-01-09 00:21:44 +03:00
|
|
|
};
|
|
|
|
|
2017-01-09 00:28:49 +03:00
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct RemoveSmartPointer
|
|
|
|
: detail::RemoveSmartPointerHelper<T, typename RemoveCV<T>::Type> {};
|
|
|
|
|
2017-01-09 01:32:36 +03:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <typename T, typename CVRemoved>
|
|
|
|
struct RemoveRawOrSmartPointerHelper {
|
|
|
|
typedef T Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename Pointee>
|
|
|
|
struct RemoveRawOrSmartPointerHelper<T, Pointee*> {
|
|
|
|
typedef Pointee Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename Pointee>
|
|
|
|
struct RemoveRawOrSmartPointerHelper<T, RefPtr<Pointee>> {
|
|
|
|
typedef Pointee Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename Pointee>
|
|
|
|
struct RemoveRawOrSmartPointerHelper<T, nsCOMPtr<Pointee>> {
|
|
|
|
typedef Pointee Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct RemoveRawOrSmartPointer
|
|
|
|
: detail::RemoveRawOrSmartPointerHelper<T, typename RemoveCV<T>::Type> {};
|
|
|
|
|
2016-06-29 05:24:54 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2016-11-23 03:18:52 +03:00
|
|
|
inline nsISupports* ToSupports(mozilla::Runnable* p) {
|
|
|
|
return static_cast<nsIRunnable*>(p);
|
|
|
|
}
|
|
|
|
|
2015-04-10 15:13:00 +03:00
|
|
|
template <typename Function>
|
2016-06-29 05:24:54 +03:00
|
|
|
already_AddRefed<mozilla::Runnable> NS_NewRunnableFunction(
|
2017-06-15 23:30:27 +03:00
|
|
|
const char* aName, Function&& aFunction) {
|
2017-02-06 13:08:01 +03:00
|
|
|
// We store a non-reference in RunnableFunction, but still forward aFunction
|
|
|
|
// to move if possible.
|
2017-06-15 23:30:27 +03:00
|
|
|
return do_AddRef(new mozilla::detail::RunnableFunctionImpl<Function>(
|
2018-06-01 19:30:30 +03:00
|
|
|
aName, std::forward<Function>(aFunction)));
|
2017-02-07 08:02:57 +03:00
|
|
|
}
|
|
|
|
|
2019-12-19 20:13:06 +03:00
|
|
|
// Creates a new object implementing nsIRunnable and nsICancelableRunnable,
|
|
|
|
// which runs a given function on Run and clears the stored function object on a
|
|
|
|
// call to `Cancel` (and thus destroys all objects it holds).
|
|
|
|
template <typename Function>
|
|
|
|
already_AddRefed<mozilla::CancelableRunnable> NS_NewCancelableRunnableFunction(
|
|
|
|
const char* aName, Function&& aFunc) {
|
|
|
|
class FuncCancelableRunnable final : public mozilla::CancelableRunnable {
|
|
|
|
public:
|
|
|
|
static_assert(std::is_void_v<decltype(
|
|
|
|
std::declval<std::remove_reference_t<Function>>()())>);
|
|
|
|
|
|
|
|
NS_INLINE_DECL_REFCOUNTING_INHERITED(FuncCancelableRunnable,
|
|
|
|
CancelableRunnable)
|
|
|
|
|
|
|
|
explicit FuncCancelableRunnable(const char* aName, Function&& aFunc)
|
|
|
|
: CancelableRunnable{aName},
|
|
|
|
mFunc{mozilla::Some(std::forward<Function>(aFunc))} {}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
MOZ_ASSERT(mFunc);
|
|
|
|
|
|
|
|
(*mFunc)();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Cancel() override {
|
|
|
|
mFunc.reset();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~FuncCancelableRunnable() = default;
|
|
|
|
|
|
|
|
mozilla::Maybe<std::remove_reference_t<Function>> mFunc;
|
|
|
|
};
|
|
|
|
|
|
|
|
return mozilla::MakeAndAddRef<FuncCancelableRunnable>(
|
|
|
|
aName, std::forward<Function>(aFunc));
|
|
|
|
}
|
|
|
|
|
2017-05-25 00:34:50 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <RunnableKind Kind>
|
|
|
|
class TimerBehaviour {
|
|
|
|
public:
|
|
|
|
nsITimer* GetTimer() { return nullptr; }
|
|
|
|
void CancelTimer() {}
|
|
|
|
|
|
|
|
protected:
|
2020-02-12 14:13:33 +03:00
|
|
|
~TimerBehaviour() = default;
|
2017-05-25 00:34:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2018-06-25 19:17:27 +03:00
|
|
|
class TimerBehaviour<RunnableKind::IdleWithTimer> {
|
2017-05-25 00:34:50 +03:00
|
|
|
public:
|
|
|
|
nsITimer* GetTimer() {
|
|
|
|
if (!mTimer) {
|
2017-10-16 09:11:22 +03:00
|
|
|
mTimer = NS_NewTimer();
|
2017-05-25 00:34:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return mTimer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CancelTimer() {
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
~TimerBehaviour() { CancelTimer(); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-05-25 00:34:50 +03:00
|
|
|
private:
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// An event that can be used to call a method on a class. The class type must
|
2008-08-18 06:10:28 +04:00
|
|
|
// support reference counting. This event supports Revoke for use
|
|
|
|
// with nsRevocableEventPtr.
|
2014-06-27 05:35:39 +04:00
|
|
|
template <class ClassType, typename ReturnType = void, bool Owning = true,
|
2018-06-25 19:17:27 +03:00
|
|
|
mozilla::RunnableKind Kind = mozilla::RunnableKind::Standard>
|
2017-05-25 00:34:50 +03:00
|
|
|
class nsRunnableMethod
|
2018-06-25 19:17:27 +03:00
|
|
|
: public mozilla::Conditional<
|
|
|
|
Kind == mozilla::RunnableKind::Standard, mozilla::Runnable,
|
2017-05-25 00:34:50 +03:00
|
|
|
typename mozilla::Conditional<
|
2018-06-25 19:17:27 +03:00
|
|
|
Kind == mozilla::RunnableKind::Cancelable,
|
2017-05-25 00:34:50 +03:00
|
|
|
mozilla::CancelableRunnable, mozilla::IdleRunnable>::Type>::Type,
|
|
|
|
protected mozilla::detail::TimerBehaviour<Kind> {
|
2018-06-25 19:17:27 +03:00
|
|
|
using BaseType = typename mozilla::Conditional<
|
|
|
|
Kind == mozilla::RunnableKind::Standard, mozilla::Runnable,
|
|
|
|
typename mozilla::Conditional<Kind == mozilla::RunnableKind::Cancelable,
|
2017-06-12 22:34:10 +03:00
|
|
|
mozilla::CancelableRunnable,
|
|
|
|
mozilla::IdleRunnable>::Type>::Type;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
public:
|
2017-07-21 06:50:43 +03:00
|
|
|
nsRunnableMethod(const char* aName) : BaseType(aName) {}
|
2017-06-12 22:34:10 +03:00
|
|
|
|
2010-04-21 03:21:32 +04:00
|
|
|
virtual void Revoke() = 0;
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2009-02-18 07:11:09 +03:00
|
|
|
// These ReturnTypeEnforcer classes set up a blacklist for return types that
|
|
|
|
// we know are not safe. The default ReturnTypeEnforcer compiles just fine but
|
|
|
|
// already_AddRefed will not.
|
2014-06-27 05:35:39 +04:00
|
|
|
template <typename OtherReturnType>
|
2009-02-18 07:11:09 +03:00
|
|
|
class ReturnTypeEnforcer {
|
|
|
|
public:
|
|
|
|
typedef int ReturnTypeIsSafe;
|
|
|
|
};
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template <class T>
|
|
|
|
class ReturnTypeEnforcer<already_AddRefed<T>> {
|
2009-02-18 07:11:09 +03:00
|
|
|
// No ReturnTypeIsSafe makes this illegal!
|
|
|
|
};
|
|
|
|
|
|
|
|
// Make sure this return type is safe.
|
|
|
|
typedef typename ReturnTypeEnforcer<ReturnType>::ReturnTypeIsSafe check;
|
2010-04-21 03:21:32 +04:00
|
|
|
};
|
2009-02-18 07:11:09 +03:00
|
|
|
|
2017-06-16 08:50:00 +03:00
|
|
|
template <class ClassType, bool Owning>
|
2015-03-01 20:12:00 +03:00
|
|
|
struct nsRunnableMethodReceiver {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ClassType> mObj;
|
2015-03-01 20:12:00 +03:00
|
|
|
explicit nsRunnableMethodReceiver(ClassType* aObj) : mObj(aObj) {}
|
2013-09-07 05:29:24 +04:00
|
|
|
~nsRunnableMethodReceiver() { Revoke(); }
|
2015-03-01 20:12:00 +03:00
|
|
|
ClassType* Get() const { return mObj.get(); }
|
2014-12-24 01:30:55 +03:00
|
|
|
void Revoke() { mObj = nullptr; }
|
2017-05-25 00:34:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class ClassType>
|
2017-06-16 08:50:00 +03:00
|
|
|
struct nsRunnableMethodReceiver<ClassType, false> {
|
2017-05-25 00:34:50 +03:00
|
|
|
ClassType* MOZ_NON_OWNING_REF mObj;
|
|
|
|
explicit nsRunnableMethodReceiver(ClassType* aObj) : mObj(aObj) {}
|
|
|
|
ClassType* Get() const { return mObj; }
|
|
|
|
void Revoke() { mObj = nullptr; }
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline constexpr bool IsIdle(mozilla::RunnableKind aKind) {
|
2018-06-25 19:17:27 +03:00
|
|
|
return aKind == mozilla::RunnableKind::Idle ||
|
|
|
|
aKind == mozilla::RunnableKind::IdleWithTimer;
|
2017-05-25 00:34:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename PtrType, typename Method, bool Owning,
|
|
|
|
mozilla::RunnableKind Kind>
|
2017-01-10 02:49:08 +03:00
|
|
|
struct nsRunnableMethodTraits;
|
2010-04-21 03:21:32 +04:00
|
|
|
|
2017-05-25 00:34:50 +03:00
|
|
|
template <typename PtrType, class C, typename R, bool Owning,
|
|
|
|
mozilla::RunnableKind Kind, typename... As>
|
|
|
|
struct nsRunnableMethodTraits<PtrType, R (C::*)(As...), Owning, Kind> {
|
2017-01-10 02:49:08 +03:00
|
|
|
typedef typename mozilla::RemoveRawOrSmartPointer<PtrType>::Type class_type;
|
2020-01-08 17:52:10 +03:00
|
|
|
static_assert(std::is_base_of<C, class_type>::value,
|
2017-01-10 02:49:08 +03:00
|
|
|
"Stored class must inherit from method's class");
|
2013-09-07 05:29:24 +04:00
|
|
|
typedef R return_type;
|
2017-05-25 00:34:50 +03:00
|
|
|
typedef nsRunnableMethod<C, R, Owning, Kind> base_type;
|
2018-06-25 19:17:27 +03:00
|
|
|
static const bool can_cancel = Kind == mozilla::RunnableKind::Cancelable;
|
2010-04-21 03:21:32 +04:00
|
|
|
};
|
|
|
|
|
2017-05-25 00:34:50 +03:00
|
|
|
template <typename PtrType, class C, typename R, bool Owning,
|
|
|
|
mozilla::RunnableKind Kind, typename... As>
|
|
|
|
struct nsRunnableMethodTraits<PtrType, R (C::*)(As...) const, Owning, Kind> {
|
2017-01-10 02:49:08 +03:00
|
|
|
typedef const typename mozilla::RemoveRawOrSmartPointer<PtrType>::Type
|
|
|
|
class_type;
|
2020-01-08 17:52:10 +03:00
|
|
|
static_assert(std::is_base_of<C, class_type>::value,
|
2017-01-10 02:49:08 +03:00
|
|
|
"Stored class must inherit from method's class");
|
2016-05-05 11:44:59 +03:00
|
|
|
typedef R return_type;
|
2017-05-25 00:34:50 +03:00
|
|
|
typedef nsRunnableMethod<C, R, Owning, Kind> base_type;
|
2018-06-25 19:17:27 +03:00
|
|
|
static const bool can_cancel = Kind == mozilla::RunnableKind::Cancelable;
|
2016-05-05 11:44:59 +03:00
|
|
|
};
|
|
|
|
|
2013-07-17 06:01:50 +04:00
|
|
|
# ifdef NS_HAVE_STDCALL
|
2017-05-25 00:34:50 +03:00
|
|
|
template <typename PtrType, class C, typename R, bool Owning,
|
|
|
|
mozilla::RunnableKind Kind, typename... As>
|
|
|
|
struct nsRunnableMethodTraits<PtrType, R (__stdcall C::*)(As...), Owning,
|
|
|
|
Kind> {
|
2017-01-10 02:49:08 +03:00
|
|
|
typedef typename mozilla::RemoveRawOrSmartPointer<PtrType>::Type class_type;
|
2020-01-08 17:52:10 +03:00
|
|
|
static_assert(std::is_base_of<C, class_type>::value,
|
2017-01-10 02:49:08 +03:00
|
|
|
"Stored class must inherit from method's class");
|
2013-09-07 05:29:24 +04:00
|
|
|
typedef R return_type;
|
2017-05-25 00:34:50 +03:00
|
|
|
typedef nsRunnableMethod<C, R, Owning, Kind> base_type;
|
2018-06-25 19:17:27 +03:00
|
|
|
static const bool can_cancel = Kind == mozilla::RunnableKind::Cancelable;
|
2013-09-07 05:29:24 +04:00
|
|
|
};
|
|
|
|
|
2017-05-25 00:34:50 +03:00
|
|
|
template <typename PtrType, class C, typename R, bool Owning,
|
|
|
|
mozilla::RunnableKind Kind>
|
|
|
|
struct nsRunnableMethodTraits<PtrType, R (NS_STDCALL C::*)(), Owning, Kind> {
|
2017-01-10 02:49:08 +03:00
|
|
|
typedef typename mozilla::RemoveRawOrSmartPointer<PtrType>::Type class_type;
|
2020-01-08 17:52:10 +03:00
|
|
|
static_assert(std::is_base_of<C, class_type>::value,
|
2017-01-10 02:49:08 +03:00
|
|
|
"Stored class must inherit from method's class");
|
2010-04-21 03:21:32 +04:00
|
|
|
typedef R return_type;
|
2017-05-25 00:34:50 +03:00
|
|
|
typedef nsRunnableMethod<C, R, Owning, Kind> base_type;
|
2018-06-25 19:17:27 +03:00
|
|
|
static const bool can_cancel = Kind == mozilla::RunnableKind::Cancelable;
|
2010-04-21 03:21:32 +04:00
|
|
|
};
|
2017-05-25 00:34:50 +03:00
|
|
|
|
|
|
|
template <typename PtrType, class C, typename R, bool Owning,
|
|
|
|
mozilla::RunnableKind Kind, typename... As>
|
|
|
|
struct nsRunnableMethodTraits<PtrType, R (__stdcall C::*)(As...) const, Owning,
|
|
|
|
Kind> {
|
2017-01-10 02:49:08 +03:00
|
|
|
typedef const typename mozilla::RemoveRawOrSmartPointer<PtrType>::Type
|
|
|
|
class_type;
|
2020-01-08 17:52:10 +03:00
|
|
|
static_assert(std::is_base_of<C, class_type>::value,
|
2017-01-10 02:49:08 +03:00
|
|
|
"Stored class must inherit from method's class");
|
2016-05-05 11:44:59 +03:00
|
|
|
typedef R return_type;
|
2017-05-25 00:34:50 +03:00
|
|
|
typedef nsRunnableMethod<C, R, Owning, Kind> base_type;
|
2018-06-25 19:17:27 +03:00
|
|
|
static const bool can_cancel = Kind == mozilla::RunnableKind::Cancelable;
|
2016-05-05 11:44:59 +03:00
|
|
|
};
|
|
|
|
|
2017-05-25 00:34:50 +03:00
|
|
|
template <typename PtrType, class C, typename R, bool Owning,
|
|
|
|
mozilla::RunnableKind Kind>
|
|
|
|
struct nsRunnableMethodTraits<PtrType, R (NS_STDCALL C::*)() const, Owning,
|
|
|
|
Kind> {
|
2017-01-10 02:49:08 +03:00
|
|
|
typedef const typename mozilla::RemoveRawOrSmartPointer<PtrType>::Type
|
|
|
|
class_type;
|
2020-01-08 17:52:10 +03:00
|
|
|
static_assert(std::is_base_of<C, class_type>::value,
|
2017-01-10 02:49:08 +03:00
|
|
|
"Stored class must inherit from method's class");
|
2016-05-05 11:44:59 +03:00
|
|
|
typedef R return_type;
|
2017-05-25 00:34:50 +03:00
|
|
|
typedef nsRunnableMethod<C, R, Owning, Kind> base_type;
|
2018-06-25 19:17:27 +03:00
|
|
|
static const bool can_cancel = Kind == mozilla::RunnableKind::Cancelable;
|
2016-05-05 11:44:59 +03:00
|
|
|
};
|
2010-04-21 03:21:32 +04:00
|
|
|
# endif
|
|
|
|
|
2015-03-01 20:12:00 +03:00
|
|
|
// IsParameterStorageClass<T>::value is true if T is a parameter-storage class
|
|
|
|
// that will be recognized by NS_New[NonOwning]RunnableMethodWithArg[s] to
|
|
|
|
// force a specific storage&passing strategy (instead of inferring one,
|
|
|
|
// see ParameterStorage).
|
|
|
|
// When creating a new storage class, add a specialization for it to be
|
|
|
|
// recognized.
|
|
|
|
template <typename T>
|
|
|
|
struct IsParameterStorageClass : public mozilla::FalseType {};
|
|
|
|
|
|
|
|
// StoreXPassByY structs used to inform nsRunnableMethodArguments how to
|
|
|
|
// store arguments, and how to pass them to the target method.
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct StoreCopyPassByValue {
|
2020-03-28 16:57:15 +03:00
|
|
|
using stored_type = std::decay_t<T>;
|
2017-09-07 12:02:13 +03:00
|
|
|
typedef stored_type passed_type;
|
2015-03-01 20:12:00 +03:00
|
|
|
stored_type m;
|
|
|
|
template <typename A>
|
2018-06-01 19:30:30 +03:00
|
|
|
MOZ_IMPLICIT StoreCopyPassByValue(A&& a) : m(std::forward<A>(a)) {}
|
2015-03-01 20:12:00 +03:00
|
|
|
passed_type PassAsParameter() { return m; }
|
|
|
|
};
|
|
|
|
template <typename S>
|
|
|
|
struct IsParameterStorageClass<StoreCopyPassByValue<S>>
|
|
|
|
: public mozilla::TrueType {};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct StoreCopyPassByConstLRef {
|
2020-03-28 16:57:15 +03:00
|
|
|
using stored_type = std::decay_t<T>;
|
2017-09-07 12:02:13 +03:00
|
|
|
typedef const stored_type& passed_type;
|
2015-03-01 20:12:00 +03:00
|
|
|
stored_type m;
|
|
|
|
template <typename A>
|
2018-06-01 19:30:30 +03:00
|
|
|
MOZ_IMPLICIT StoreCopyPassByConstLRef(A&& a) : m(std::forward<A>(a)) {}
|
2015-03-01 20:12:00 +03:00
|
|
|
passed_type PassAsParameter() { return m; }
|
|
|
|
};
|
|
|
|
template <typename S>
|
|
|
|
struct IsParameterStorageClass<StoreCopyPassByConstLRef<S>>
|
|
|
|
: public mozilla::TrueType {};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct StoreCopyPassByLRef {
|
2020-03-28 16:57:15 +03:00
|
|
|
using stored_type = std::decay_t<T>;
|
2017-09-07 12:02:13 +03:00
|
|
|
typedef stored_type& passed_type;
|
2015-03-01 20:12:00 +03:00
|
|
|
stored_type m;
|
|
|
|
template <typename A>
|
2018-06-01 19:30:30 +03:00
|
|
|
MOZ_IMPLICIT StoreCopyPassByLRef(A&& a) : m(std::forward<A>(a)) {}
|
2015-03-01 20:12:00 +03:00
|
|
|
passed_type PassAsParameter() { return m; }
|
|
|
|
};
|
|
|
|
template <typename S>
|
|
|
|
struct IsParameterStorageClass<StoreCopyPassByLRef<S>>
|
|
|
|
: public mozilla::TrueType {};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct StoreCopyPassByRRef {
|
2020-03-28 16:57:15 +03:00
|
|
|
using stored_type = std::decay_t<T>;
|
2017-09-07 12:02:13 +03:00
|
|
|
typedef stored_type&& passed_type;
|
2015-03-01 20:12:00 +03:00
|
|
|
stored_type m;
|
|
|
|
template <typename A>
|
2018-06-01 19:30:30 +03:00
|
|
|
MOZ_IMPLICIT StoreCopyPassByRRef(A&& a) : m(std::forward<A>(a)) {}
|
2018-05-30 22:15:35 +03:00
|
|
|
passed_type PassAsParameter() { return std::move(m); }
|
2015-03-01 20:12:00 +03:00
|
|
|
};
|
|
|
|
template <typename S>
|
|
|
|
struct IsParameterStorageClass<StoreCopyPassByRRef<S>>
|
|
|
|
: public mozilla::TrueType {};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct StoreRefPassByLRef {
|
|
|
|
typedef T& stored_type;
|
|
|
|
typedef T& passed_type;
|
|
|
|
stored_type m;
|
|
|
|
template <typename A>
|
2015-05-12 00:24:21 +03:00
|
|
|
MOZ_IMPLICIT StoreRefPassByLRef(A& a) : m(a) {}
|
2015-03-01 20:12:00 +03:00
|
|
|
passed_type PassAsParameter() { return m; }
|
|
|
|
};
|
|
|
|
template <typename S>
|
|
|
|
struct IsParameterStorageClass<StoreRefPassByLRef<S>>
|
|
|
|
: public mozilla::TrueType {};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct StoreConstRefPassByConstLRef {
|
|
|
|
typedef const T& stored_type;
|
|
|
|
typedef const T& passed_type;
|
|
|
|
stored_type m;
|
|
|
|
template <typename A>
|
2015-05-12 00:24:21 +03:00
|
|
|
MOZ_IMPLICIT StoreConstRefPassByConstLRef(const A& a) : m(a) {}
|
2015-03-01 20:12:00 +03:00
|
|
|
passed_type PassAsParameter() { return m; }
|
|
|
|
};
|
|
|
|
template <typename S>
|
|
|
|
struct IsParameterStorageClass<StoreConstRefPassByConstLRef<S>>
|
|
|
|
: public mozilla::TrueType {};
|
|
|
|
|
|
|
|
template <typename T>
|
2017-01-08 23:55:19 +03:00
|
|
|
struct StoreRefPtrPassByPtr {
|
2015-10-18 08:24:48 +03:00
|
|
|
typedef RefPtr<T> stored_type;
|
2015-03-01 20:12:00 +03:00
|
|
|
typedef T* passed_type;
|
|
|
|
stored_type m;
|
|
|
|
template <typename A>
|
2018-06-01 19:30:30 +03:00
|
|
|
MOZ_IMPLICIT StoreRefPtrPassByPtr(A&& a) : m(std::forward<A>(a)) {}
|
2015-03-01 20:12:00 +03:00
|
|
|
passed_type PassAsParameter() { return m.get(); }
|
|
|
|
};
|
|
|
|
template <typename S>
|
2017-01-08 23:55:19 +03:00
|
|
|
struct IsParameterStorageClass<StoreRefPtrPassByPtr<S>>
|
2015-03-01 20:12:00 +03:00
|
|
|
: public mozilla::TrueType {};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct StorePtrPassByPtr {
|
|
|
|
typedef T* stored_type;
|
|
|
|
typedef T* passed_type;
|
|
|
|
stored_type m;
|
|
|
|
template <typename A>
|
2015-05-12 00:24:21 +03:00
|
|
|
MOZ_IMPLICIT StorePtrPassByPtr(A a) : m(a) {}
|
2015-03-01 20:12:00 +03:00
|
|
|
passed_type PassAsParameter() { return m; }
|
|
|
|
};
|
|
|
|
template <typename S>
|
|
|
|
struct IsParameterStorageClass<StorePtrPassByPtr<S>>
|
|
|
|
: public mozilla::TrueType {};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct StoreConstPtrPassByConstPtr {
|
|
|
|
typedef const T* stored_type;
|
|
|
|
typedef const T* passed_type;
|
|
|
|
stored_type m;
|
|
|
|
template <typename A>
|
2015-05-12 00:24:21 +03:00
|
|
|
MOZ_IMPLICIT StoreConstPtrPassByConstPtr(A a) : m(a) {}
|
2015-03-01 20:12:00 +03:00
|
|
|
passed_type PassAsParameter() { return m; }
|
|
|
|
};
|
|
|
|
template <typename S>
|
|
|
|
struct IsParameterStorageClass<StoreConstPtrPassByConstPtr<S>>
|
|
|
|
: public mozilla::TrueType {};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct StoreCopyPassByConstPtr {
|
|
|
|
typedef T stored_type;
|
|
|
|
typedef const T* passed_type;
|
|
|
|
stored_type m;
|
|
|
|
template <typename A>
|
2018-06-01 19:30:30 +03:00
|
|
|
MOZ_IMPLICIT StoreCopyPassByConstPtr(A&& a) : m(std::forward<A>(a)) {}
|
2015-03-01 20:12:00 +03:00
|
|
|
passed_type PassAsParameter() { return &m; }
|
|
|
|
};
|
|
|
|
template <typename S>
|
|
|
|
struct IsParameterStorageClass<StoreCopyPassByConstPtr<S>>
|
|
|
|
: public mozilla::TrueType {};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct StoreCopyPassByPtr {
|
|
|
|
typedef T stored_type;
|
|
|
|
typedef T* passed_type;
|
|
|
|
stored_type m;
|
|
|
|
template <typename A>
|
2018-06-01 19:30:30 +03:00
|
|
|
MOZ_IMPLICIT StoreCopyPassByPtr(A&& a) : m(std::forward<A>(a)) {}
|
2015-03-01 20:12:00 +03:00
|
|
|
passed_type PassAsParameter() { return &m; }
|
|
|
|
};
|
|
|
|
template <typename S>
|
|
|
|
struct IsParameterStorageClass<StoreCopyPassByPtr<S>>
|
|
|
|
: public mozilla::TrueType {};
|
|
|
|
|
2016-11-17 06:34:24 +03:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <typename>
|
|
|
|
struct SFINAE1True : mozilla::TrueType {};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
static auto HasRefCountMethodsTest(int)
|
|
|
|
-> SFINAE1True<decltype(mozilla::DeclVal<T>().AddRef(),
|
|
|
|
mozilla::DeclVal<T>().Release())>;
|
|
|
|
template <class>
|
|
|
|
static auto HasRefCountMethodsTest(long) -> mozilla::FalseType;
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
struct HasRefCountMethods : decltype(HasRefCountMethodsTest<T>(0)) {};
|
|
|
|
|
|
|
|
template <typename TWithoutPointer>
|
|
|
|
struct NonnsISupportsPointerStorageClass
|
|
|
|
: mozilla::Conditional<
|
2020-03-28 16:57:14 +03:00
|
|
|
std::is_const_v<TWithoutPointer>,
|
2016-11-17 06:34:24 +03:00
|
|
|
StoreConstPtrPassByConstPtr<
|
|
|
|
typename mozilla::RemoveConst<TWithoutPointer>::Type>,
|
|
|
|
StorePtrPassByPtr<TWithoutPointer>> {};
|
|
|
|
|
2015-03-01 20:12:00 +03:00
|
|
|
template <typename TWithoutPointer>
|
|
|
|
struct PointerStorageClass
|
2015-04-26 11:52:00 +03:00
|
|
|
: mozilla::Conditional<
|
|
|
|
HasRefCountMethods<TWithoutPointer>::value,
|
2017-01-08 23:55:19 +03:00
|
|
|
StoreRefPtrPassByPtr<TWithoutPointer>,
|
2015-03-01 20:12:00 +03:00
|
|
|
typename NonnsISupportsPointerStorageClass<TWithoutPointer>::Type> {};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-03-01 20:12:00 +03:00
|
|
|
template <typename TWithoutRef>
|
|
|
|
struct LValueReferenceStorageClass
|
|
|
|
: mozilla::Conditional<
|
2020-03-28 16:57:14 +03:00
|
|
|
std::is_const_v<TWithoutRef>,
|
2015-03-01 20:12:00 +03:00
|
|
|
StoreConstRefPassByConstLRef<
|
|
|
|
typename mozilla::RemoveConst<TWithoutRef>::Type>,
|
|
|
|
StoreRefPassByLRef<TWithoutRef>> {};
|
|
|
|
|
2015-07-02 18:17:49 +03:00
|
|
|
template <typename T>
|
|
|
|
struct SmartPointerStorageClass
|
2016-11-17 06:34:24 +03:00
|
|
|
: mozilla::Conditional<
|
|
|
|
mozilla::IsRefcountedSmartPointer<T>::value,
|
|
|
|
StoreRefPtrPassByPtr<typename mozilla::RemoveSmartPointer<T>::Type>,
|
2016-11-17 09:07:02 +03:00
|
|
|
StoreCopyPassByConstLRef<T>> {};
|
2015-07-02 18:17:49 +03:00
|
|
|
|
2015-03-01 20:12:00 +03:00
|
|
|
template <typename T>
|
|
|
|
struct NonLValueReferenceStorageClass
|
|
|
|
: mozilla::Conditional<
|
2020-01-10 13:40:34 +03:00
|
|
|
std::is_rvalue_reference_v<T>,
|
2015-03-01 20:12:00 +03:00
|
|
|
StoreCopyPassByRRef<typename mozilla::RemoveReference<T>::Type>,
|
2015-07-02 18:17:49 +03:00
|
|
|
typename SmartPointerStorageClass<T>::Type> {};
|
2015-03-01 20:12:00 +03:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct NonPointerStorageClass
|
|
|
|
: mozilla::Conditional<
|
2020-01-10 13:40:34 +03:00
|
|
|
std::is_lvalue_reference_v<T>,
|
2015-03-01 20:12:00 +03:00
|
|
|
typename LValueReferenceStorageClass<
|
|
|
|
typename mozilla::RemoveReference<T>::Type>::Type,
|
|
|
|
typename NonLValueReferenceStorageClass<T>::Type> {};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-03-01 20:12:00 +03:00
|
|
|
template <typename T>
|
|
|
|
struct NonParameterStorageClass
|
2020-03-28 16:57:14 +03:00
|
|
|
: mozilla::Conditional<std::is_pointer_v<T>,
|
2015-03-01 20:12:00 +03:00
|
|
|
typename PointerStorageClass<
|
|
|
|
typename mozilla::RemovePointer<T>::Type>::Type,
|
|
|
|
typename NonPointerStorageClass<T>::Type> {};
|
|
|
|
|
|
|
|
// Choose storage&passing strategy based on preferred storage type:
|
|
|
|
// - If IsParameterStorageClass<T>::value is true, use as-is.
|
2018-11-29 13:30:46 +03:00
|
|
|
// - RC* -> StoreRefPtrPassByPtr<RC> :Store RefPtr<RC>, pass RC*
|
2015-04-26 11:52:00 +03:00
|
|
|
// ^^ RC quacks like a ref-counted type (i.e., has AddRef and Release methods)
|
2018-11-29 13:30:46 +03:00
|
|
|
// - const T* -> StoreConstPtrPassByConstPtr<T> :Store const T*, pass const T*
|
|
|
|
// - T* -> StorePtrPassByPtr<T> :Store T*, pass T*.
|
|
|
|
// - const T& -> StoreConstRefPassByConstLRef<T>:Store const T&, pass const T&.
|
|
|
|
// - T& -> StoreRefPassByLRef<T> :Store T&, pass T&.
|
|
|
|
// - T&& -> StoreCopyPassByRRef<T> :Store T, pass std::move(T).
|
2015-10-18 08:24:48 +03:00
|
|
|
// - RefPtr<T>, nsCOMPtr<T>
|
2018-11-29 13:30:46 +03:00
|
|
|
// -> StoreRefPtrPassByPtr<T> :Store RefPtr<T>, pass T*
|
|
|
|
// - Other T -> StoreCopyPassByConstLRef<T> :Store T, pass const T&.
|
2015-03-01 20:12:00 +03:00
|
|
|
// Other available explicit options:
|
2018-11-29 13:30:46 +03:00
|
|
|
// - StoreCopyPassByValue<T> :Store T, pass T.
|
|
|
|
// - StoreCopyPassByLRef<T> :Store T, pass T& (of copy!)
|
|
|
|
// - StoreCopyPassByConstPtr<T> :Store T, pass const T*
|
|
|
|
// - StoreCopyPassByPtr<T> :Store T, pass T* (of copy!)
|
2015-03-01 20:12:00 +03:00
|
|
|
// Or create your own class with PassAsParameter() method, optional
|
|
|
|
// clean-up in destructor, and with associated IsParameterStorageClass<>.
|
|
|
|
template <typename T>
|
|
|
|
struct ParameterStorage
|
|
|
|
: mozilla::Conditional<IsParameterStorageClass<T>::value, T,
|
|
|
|
typename NonParameterStorageClass<T>::Type> {};
|
|
|
|
|
2017-06-16 08:50:00 +03:00
|
|
|
template <class T>
|
2017-06-20 00:59:00 +03:00
|
|
|
static auto HasSetDeadlineTest(int) -> SFINAE1True<decltype(
|
|
|
|
mozilla::DeclVal<T>().SetDeadline(mozilla::DeclVal<mozilla::TimeStamp>()))>;
|
2017-06-16 08:50:00 +03:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
static auto HasSetDeadlineTest(long) -> mozilla::FalseType;
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
struct HasSetDeadline : decltype(HasSetDeadlineTest<T>(0)) {};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
typename mozilla::EnableIf<::detail::HasSetDeadline<T>::value>::Type
|
|
|
|
SetDeadlineImpl(T* aObj, mozilla::TimeStamp aTimeStamp) {
|
|
|
|
aObj->SetDeadline(aTimeStamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
typename mozilla::EnableIf<!::detail::HasSetDeadline<T>::value>::Type
|
|
|
|
SetDeadlineImpl(T* aObj, mozilla::TimeStamp aTimeStamp) {}
|
2015-03-01 20:12:00 +03:00
|
|
|
} /* namespace detail */
|
|
|
|
|
2016-06-29 05:24:54 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace detail {
|
|
|
|
|
2015-03-01 20:12:00 +03:00
|
|
|
// struct used to store arguments and later apply them to a method.
|
2015-05-12 00:24:21 +03:00
|
|
|
template <typename... Ts>
|
2016-06-29 05:24:54 +03:00
|
|
|
struct RunnableMethodArguments final {
|
2016-06-29 05:24:54 +03:00
|
|
|
Tuple<typename ::detail::ParameterStorage<Ts>::Type...> mArguments;
|
2015-05-12 00:24:21 +03:00
|
|
|
template <typename... As>
|
2016-06-29 05:24:54 +03:00
|
|
|
explicit RunnableMethodArguments(As&&... aArguments)
|
2018-06-01 19:30:30 +03:00
|
|
|
: mArguments(std::forward<As>(aArguments)...) {}
|
2015-05-12 00:24:21 +03:00
|
|
|
template <typename C, typename M, typename... Args, size_t... Indices>
|
2018-04-09 14:12:13 +03:00
|
|
|
static auto applyImpl(C* o, M m, Tuple<Args...>& args,
|
|
|
|
std::index_sequence<Indices...>)
|
2016-06-29 05:24:54 +03:00
|
|
|
-> decltype(((*o).*m)(Get<Indices>(args).PassAsParameter()...)) {
|
|
|
|
return ((*o).*m)(Get<Indices>(args).PassAsParameter()...);
|
2015-04-14 18:36:35 +03:00
|
|
|
}
|
2015-05-12 00:24:21 +03:00
|
|
|
template <class C, typename M>
|
|
|
|
auto apply(C* o, M m)
|
|
|
|
-> decltype(applyImpl(o, m, mArguments,
|
2018-04-09 14:12:13 +03:00
|
|
|
std::index_sequence_for<Ts...>{})) {
|
|
|
|
return applyImpl(o, m, mArguments, std::index_sequence_for<Ts...>{});
|
2015-04-14 18:36:35 +03:00
|
|
|
}
|
|
|
|
};
|
2013-09-07 05:29:24 +04:00
|
|
|
|
2017-05-25 00:34:50 +03:00
|
|
|
template <typename PtrType, typename Method, bool Owning, RunnableKind Kind,
|
|
|
|
typename... Storages>
|
2016-06-29 05:24:54 +03:00
|
|
|
class RunnableMethodImpl final
|
2017-05-25 00:34:50 +03:00
|
|
|
: public ::nsRunnableMethodTraits<PtrType, Method, Owning,
|
|
|
|
Kind>::base_type {
|
|
|
|
typedef typename ::nsRunnableMethodTraits<PtrType, Method, Owning, Kind>
|
|
|
|
Traits;
|
|
|
|
|
|
|
|
typedef typename Traits::class_type ClassType;
|
|
|
|
typedef typename Traits::base_type BaseType;
|
2017-06-16 08:50:00 +03:00
|
|
|
::nsRunnableMethodReceiver<ClassType, Owning> mReceiver;
|
2009-02-18 07:11:09 +03:00
|
|
|
Method mMethod;
|
2016-06-29 05:24:54 +03:00
|
|
|
RunnableMethodArguments<Storages...> mArgs;
|
2017-05-25 00:34:50 +03:00
|
|
|
using BaseType::CancelTimer;
|
|
|
|
using BaseType::GetTimer;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-05 22:03:18 +03:00
|
|
|
private:
|
2016-06-29 05:24:54 +03:00
|
|
|
virtual ~RunnableMethodImpl() { Revoke(); };
|
2017-05-25 00:34:50 +03:00
|
|
|
static void TimedOut(nsITimer* aTimer, void* aClosure) {
|
|
|
|
static_assert(IsIdle(Kind), "Don't use me!");
|
|
|
|
RefPtr<IdleRunnable> r = static_cast<IdleRunnable*>(aClosure);
|
|
|
|
r->SetDeadline(TimeStamp());
|
|
|
|
r->Run();
|
|
|
|
r->Cancel();
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-05 22:03:18 +03:00
|
|
|
public:
|
2017-01-10 02:49:08 +03:00
|
|
|
template <typename ForwardedPtrType, typename... Args>
|
2017-07-21 06:50:43 +03:00
|
|
|
explicit RunnableMethodImpl(const char* aName, ForwardedPtrType&& aObj,
|
|
|
|
Method aMethod, Args&&... aArgs)
|
|
|
|
: BaseType(aName),
|
2018-06-01 19:30:30 +03:00
|
|
|
mReceiver(std::forward<ForwardedPtrType>(aObj)),
|
2014-06-27 05:35:39 +04:00
|
|
|
mMethod(aMethod),
|
2018-06-01 19:30:30 +03:00
|
|
|
mArgs(std::forward<Args>(aArgs)...) {
|
2015-03-01 20:12:00 +03:00
|
|
|
static_assert(sizeof...(Storages) == sizeof...(Args),
|
|
|
|
"Storages and Args should have equal sizes");
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2017-11-06 06:37:28 +03:00
|
|
|
|
2018-01-17 22:39:34 +03:00
|
|
|
NS_IMETHOD Run() {
|
2017-05-25 00:34:50 +03:00
|
|
|
CancelTimer();
|
|
|
|
|
2015-03-01 20:12:00 +03:00
|
|
|
if (MOZ_LIKELY(mReceiver.Get())) {
|
|
|
|
mArgs.apply(mReceiver.Get(), mMethod);
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2017-05-25 00:34:50 +03:00
|
|
|
|
2010-04-21 03:21:32 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-05-25 00:34:50 +03:00
|
|
|
|
|
|
|
nsresult Cancel() {
|
2018-06-25 19:17:27 +03:00
|
|
|
static_assert(Kind >= RunnableKind::Cancelable, "Don't use me!");
|
2016-05-05 11:44:59 +03:00
|
|
|
Revoke();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-05-25 00:34:50 +03:00
|
|
|
|
|
|
|
void Revoke() {
|
|
|
|
CancelTimer();
|
|
|
|
mReceiver.Revoke();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetDeadline(TimeStamp aDeadline) {
|
2017-06-16 08:50:00 +03:00
|
|
|
if (MOZ_LIKELY(mReceiver.Get())) {
|
|
|
|
::detail::SetDeadlineImpl(mReceiver.Get(), aDeadline);
|
|
|
|
}
|
2017-05-25 00:34:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetTimer(uint32_t aDelay, nsIEventTarget* aTarget) {
|
|
|
|
MOZ_ASSERT(aTarget);
|
|
|
|
|
|
|
|
if (nsCOMPtr<nsITimer> timer = GetTimer()) {
|
|
|
|
timer->Cancel();
|
|
|
|
timer->SetTarget(aTarget);
|
2017-06-12 22:34:10 +03:00
|
|
|
timer->InitWithNamedFuncCallback(TimedOut, this, aDelay,
|
|
|
|
nsITimer::TYPE_ONE_SHOT,
|
|
|
|
"detail::RunnableMethodImpl::SetTimer");
|
2017-05-25 00:34:50 +03:00
|
|
|
}
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
};
|
|
|
|
|
2017-02-06 13:08:01 +03:00
|
|
|
// Type aliases for NewRunnableMethod.
|
|
|
|
template <typename PtrType, typename Method>
|
|
|
|
using OwningRunnableMethod =
|
2018-06-25 19:17:27 +03:00
|
|
|
typename ::nsRunnableMethodTraits<typename RemoveReference<PtrType>::Type,
|
|
|
|
Method, true,
|
|
|
|
RunnableKind::Standard>::base_type;
|
2017-02-06 13:08:01 +03:00
|
|
|
template <typename PtrType, typename Method, typename... Storages>
|
|
|
|
using OwningRunnableMethodImpl =
|
2018-06-25 19:17:27 +03:00
|
|
|
RunnableMethodImpl<typename RemoveReference<PtrType>::Type, Method, true,
|
|
|
|
RunnableKind::Standard, Storages...>;
|
2017-02-06 13:08:01 +03:00
|
|
|
|
|
|
|
// Type aliases for NewCancelableRunnableMethod.
|
|
|
|
template <typename PtrType, typename Method>
|
|
|
|
using CancelableRunnableMethod =
|
2018-06-25 19:17:27 +03:00
|
|
|
typename ::nsRunnableMethodTraits<typename RemoveReference<PtrType>::Type,
|
|
|
|
Method, true,
|
|
|
|
RunnableKind::Cancelable>::base_type;
|
2017-02-06 13:08:01 +03:00
|
|
|
template <typename PtrType, typename Method, typename... Storages>
|
|
|
|
using CancelableRunnableMethodImpl =
|
2018-06-25 19:17:27 +03:00
|
|
|
RunnableMethodImpl<typename RemoveReference<PtrType>::Type, Method, true,
|
|
|
|
RunnableKind::Cancelable, Storages...>;
|
2017-05-25 00:34:50 +03:00
|
|
|
|
|
|
|
// Type aliases for NewIdleRunnableMethod.
|
|
|
|
template <typename PtrType, typename Method>
|
|
|
|
using IdleRunnableMethod =
|
2018-06-25 19:17:27 +03:00
|
|
|
typename ::nsRunnableMethodTraits<typename RemoveReference<PtrType>::Type,
|
|
|
|
Method, true,
|
|
|
|
RunnableKind::Idle>::base_type;
|
2017-05-25 00:34:50 +03:00
|
|
|
template <typename PtrType, typename Method, typename... Storages>
|
|
|
|
using IdleRunnableMethodImpl =
|
2018-06-25 19:17:27 +03:00
|
|
|
RunnableMethodImpl<typename RemoveReference<PtrType>::Type, Method, true,
|
|
|
|
RunnableKind::Idle, Storages...>;
|
2017-05-25 00:34:50 +03:00
|
|
|
|
|
|
|
// Type aliases for NewIdleRunnableMethodWithTimer.
|
|
|
|
template <typename PtrType, typename Method>
|
|
|
|
using IdleRunnableMethodWithTimer =
|
2018-06-25 19:17:27 +03:00
|
|
|
typename ::nsRunnableMethodTraits<typename RemoveReference<PtrType>::Type,
|
|
|
|
Method, true,
|
|
|
|
RunnableKind::IdleWithTimer>::base_type;
|
2017-05-25 00:34:50 +03:00
|
|
|
template <typename PtrType, typename Method, typename... Storages>
|
|
|
|
using IdleRunnableMethodWithTimerImpl =
|
2018-06-25 19:17:27 +03:00
|
|
|
RunnableMethodImpl<typename RemoveReference<PtrType>::Type, Method, true,
|
|
|
|
RunnableKind::IdleWithTimer, Storages...>;
|
2017-02-06 13:08:01 +03:00
|
|
|
|
|
|
|
// Type aliases for NewNonOwningRunnableMethod.
|
|
|
|
template <typename PtrType, typename Method>
|
|
|
|
using NonOwningRunnableMethod =
|
2018-06-25 19:17:27 +03:00
|
|
|
typename ::nsRunnableMethodTraits<typename RemoveReference<PtrType>::Type,
|
|
|
|
Method, false,
|
|
|
|
RunnableKind::Standard>::base_type;
|
2017-02-06 13:08:01 +03:00
|
|
|
template <typename PtrType, typename Method, typename... Storages>
|
|
|
|
using NonOwningRunnableMethodImpl =
|
2018-06-25 19:17:27 +03:00
|
|
|
RunnableMethodImpl<typename RemoveReference<PtrType>::Type, Method, false,
|
|
|
|
RunnableKind::Standard, Storages...>;
|
2017-02-06 13:08:01 +03:00
|
|
|
|
|
|
|
// Type aliases for NonOwningCancelableRunnableMethod
|
|
|
|
template <typename PtrType, typename Method>
|
|
|
|
using NonOwningCancelableRunnableMethod =
|
2018-06-25 19:17:27 +03:00
|
|
|
typename ::nsRunnableMethodTraits<typename RemoveReference<PtrType>::Type,
|
|
|
|
Method, false,
|
|
|
|
RunnableKind::Cancelable>::base_type;
|
2017-02-06 13:08:01 +03:00
|
|
|
template <typename PtrType, typename Method, typename... Storages>
|
|
|
|
using NonOwningCancelableRunnableMethodImpl =
|
2018-06-25 19:17:27 +03:00
|
|
|
RunnableMethodImpl<typename RemoveReference<PtrType>::Type, Method, false,
|
|
|
|
RunnableKind::Cancelable, Storages...>;
|
2017-05-25 00:34:50 +03:00
|
|
|
|
|
|
|
// Type aliases for NonOwningIdleRunnableMethod
|
|
|
|
template <typename PtrType, typename Method>
|
|
|
|
using NonOwningIdleRunnableMethod =
|
2018-06-25 19:17:27 +03:00
|
|
|
typename ::nsRunnableMethodTraits<typename RemoveReference<PtrType>::Type,
|
|
|
|
Method, false,
|
|
|
|
RunnableKind::Idle>::base_type;
|
2017-05-25 00:34:50 +03:00
|
|
|
template <typename PtrType, typename Method, typename... Storages>
|
|
|
|
using NonOwningIdleRunnableMethodImpl =
|
2018-06-25 19:17:27 +03:00
|
|
|
RunnableMethodImpl<typename RemoveReference<PtrType>::Type, Method, false,
|
|
|
|
RunnableKind::Idle, Storages...>;
|
2017-05-25 00:34:50 +03:00
|
|
|
|
|
|
|
// Type aliases for NewIdleRunnableMethodWithTimer.
|
|
|
|
template <typename PtrType, typename Method>
|
|
|
|
using NonOwningIdleRunnableMethodWithTimer =
|
2018-06-25 19:17:27 +03:00
|
|
|
typename ::nsRunnableMethodTraits<typename RemoveReference<PtrType>::Type,
|
|
|
|
Method, false,
|
|
|
|
RunnableKind::IdleWithTimer>::base_type;
|
2017-05-25 00:34:50 +03:00
|
|
|
template <typename PtrType, typename Method, typename... Storages>
|
|
|
|
using NonOwningIdleRunnableMethodWithTimerImpl =
|
2018-06-25 19:17:27 +03:00
|
|
|
RunnableMethodImpl<typename RemoveReference<PtrType>::Type, Method, false,
|
|
|
|
RunnableKind::IdleWithTimer, Storages...>;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-06-29 05:24:54 +03:00
|
|
|
} // namespace detail
|
|
|
|
|
2017-04-05 22:34:36 +03:00
|
|
|
// NewRunnableMethod and friends
|
|
|
|
//
|
|
|
|
// Very often in Gecko, you'll find yourself in a situation where you want
|
|
|
|
// to invoke a method (with or without arguments) asynchronously. You
|
|
|
|
// could write a small helper class inheriting from nsRunnable to handle
|
|
|
|
// all these details, or you could let NewRunnableMethod take care of all
|
|
|
|
// those details for you.
|
|
|
|
//
|
|
|
|
// The simplest use of NewRunnableMethod looks like:
|
2006-05-10 21:30:15 +04:00
|
|
|
//
|
|
|
|
// nsCOMPtr<nsIRunnable> event =
|
2018-11-29 13:30:46 +03:00
|
|
|
// mozilla::NewRunnableMethod("description", myObject,
|
|
|
|
// &MyClass::HandleEvent);
|
2006-05-10 21:30:15 +04:00
|
|
|
// NS_DispatchToCurrentThread(event);
|
|
|
|
//
|
2010-04-21 03:21:32 +04:00
|
|
|
// Statically enforced constraints:
|
|
|
|
// - myObject must be of (or implicitly convertible to) type MyClass
|
2016-06-29 05:24:54 +03:00
|
|
|
// - MyClass must define AddRef and Release methods
|
2006-05-10 21:30:15 +04:00
|
|
|
//
|
2017-04-05 22:34:36 +03:00
|
|
|
// The "description" string should specify a human-readable name for the
|
|
|
|
// runnable; the provided string is used by various introspection tools
|
|
|
|
// in the browser.
|
|
|
|
//
|
|
|
|
// The created runnable will take a strong reference to `myObject`. For
|
|
|
|
// non-refcounted objects, or refcounted objects with unusual refcounting
|
|
|
|
// requirements, and if and only if you are 110% certain that `myObject`
|
|
|
|
// will live long enough, you can use NewNonOwningRunnableMethod instead,
|
|
|
|
// which will, as its name implies, take a non-owning reference. If you
|
|
|
|
// find yourself having to use this function, you should accompany your use
|
|
|
|
// with a proof comment describing why the runnable will not lead to
|
|
|
|
// use-after-frees.
|
|
|
|
//
|
|
|
|
// (If you find yourself writing contorted code to Release() an object
|
|
|
|
// asynchronously on a different thread, you should use the
|
|
|
|
// NS_ProxyRelease function.)
|
|
|
|
//
|
|
|
|
// Invoking a method with arguments takes a little more care. The
|
|
|
|
// natural extension of the above:
|
|
|
|
//
|
|
|
|
// nsCOMPtr<nsIRunnable> event =
|
2018-11-29 13:30:46 +03:00
|
|
|
// mozilla::NewRunnableMethod("description", myObject,
|
|
|
|
// &MyClass::HandleEvent,
|
2017-04-05 22:34:36 +03:00
|
|
|
// arg1, arg2, ...);
|
|
|
|
//
|
|
|
|
// can lead to security hazards (e.g. passing in raw pointers to refcounted
|
|
|
|
// objects and storing those raw pointers in the runnable). We therefore
|
|
|
|
// require you to specify the storage types used by the runnable, just as
|
|
|
|
// you would if you were writing out the class by hand:
|
|
|
|
//
|
|
|
|
// nsCOMPtr<nsIRunnable> event =
|
|
|
|
// mozilla::NewRunnableMethod<RefPtr<T>, nsTArray<U>>
|
|
|
|
// ("description", myObject, &MyClass::HandleEvent, arg1, arg2);
|
|
|
|
//
|
|
|
|
// Please note that you do not have to pass the same argument type as you
|
|
|
|
// specify in the template arguments. For example, if you want to transfer
|
|
|
|
// ownership to a runnable, you can write:
|
|
|
|
//
|
|
|
|
// RefPtr<T> ptr = ...;
|
|
|
|
// nsTArray<U> array = ...;
|
|
|
|
// nsCOMPtr<nsIRunnable> event =
|
|
|
|
// mozilla::NewRunnableMethod<RefPtr<T>, nsTArray<U>>
|
|
|
|
// ("description", myObject, &MyClass::DoSomething,
|
2018-05-30 22:15:35 +03:00
|
|
|
// std::move(ptr), std::move(array));
|
2017-04-05 22:34:36 +03:00
|
|
|
//
|
|
|
|
// and there will be no extra AddRef/Release traffic, or copying of the array.
|
|
|
|
//
|
|
|
|
// Each type that you specify as a template argument to NewRunnableMethod
|
|
|
|
// comes with its own style of storage in the runnable and its own style
|
|
|
|
// of argument passing to the invoked method. See the comment for
|
|
|
|
// ParameterStorage above for more details.
|
|
|
|
//
|
|
|
|
// If you need to customize the storage type and/or argument passing type,
|
|
|
|
// you can write your own class to use as a template argument to
|
|
|
|
// NewRunnableMethod. If you find yourself having to do that frequently,
|
|
|
|
// please file a bug in Core::XPCOM about adding the custom type to the
|
|
|
|
// core code in this file, and/or for custom rules for ParameterStorage
|
|
|
|
// to select that strategy.
|
|
|
|
//
|
|
|
|
// For places that require you to use cancelable runnables, such as
|
|
|
|
// workers, there's also NewCancelableRunnableMethod and its non-owning
|
|
|
|
// counterpart. The runnables returned by these methods additionally
|
|
|
|
// implement nsICancelableRunnable.
|
|
|
|
//
|
|
|
|
// Finally, all of the functions discussed above have additional overloads
|
|
|
|
// that do not take a `const char*` as their first parameter; you may see
|
|
|
|
// these in older code. The `const char*` overload is preferred and
|
|
|
|
// should be used in new code exclusively.
|
2016-05-05 11:45:00 +03:00
|
|
|
|
2017-02-07 08:02:57 +03:00
|
|
|
template <typename PtrType, typename Method>
|
|
|
|
already_AddRefed<detail::OwningRunnableMethod<PtrType, Method>>
|
|
|
|
NewRunnableMethod(const char* aName, PtrType&& aPtr, Method aMethod) {
|
2017-06-15 23:30:27 +03:00
|
|
|
return do_AddRef(new detail::OwningRunnableMethodImpl<PtrType, Method>(
|
2018-06-01 19:30:30 +03:00
|
|
|
aName, std::forward<PtrType>(aPtr), aMethod));
|
2016-05-05 11:44:59 +03:00
|
|
|
}
|
|
|
|
|
2017-02-07 08:02:57 +03:00
|
|
|
template <typename PtrType, typename Method>
|
|
|
|
already_AddRefed<detail::CancelableRunnableMethod<PtrType, Method>>
|
|
|
|
NewCancelableRunnableMethod(const char* aName, PtrType&& aPtr, Method aMethod) {
|
2017-06-15 23:30:27 +03:00
|
|
|
return do_AddRef(new detail::CancelableRunnableMethodImpl<PtrType, Method>(
|
2018-06-01 19:30:30 +03:00
|
|
|
aName, std::forward<PtrType>(aPtr), aMethod));
|
2017-05-25 00:34:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename PtrType, typename Method>
|
|
|
|
already_AddRefed<detail::IdleRunnableMethod<PtrType, Method>>
|
|
|
|
NewIdleRunnableMethod(const char* aName, PtrType&& aPtr, Method aMethod) {
|
2017-06-15 23:30:27 +03:00
|
|
|
return do_AddRef(new detail::IdleRunnableMethodImpl<PtrType, Method>(
|
2018-06-01 19:30:30 +03:00
|
|
|
aName, std::forward<PtrType>(aPtr), aMethod));
|
2017-05-25 00:34:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename PtrType, typename Method>
|
|
|
|
already_AddRefed<detail::IdleRunnableMethodWithTimer<PtrType, Method>>
|
|
|
|
NewIdleRunnableMethodWithTimer(const char* aName, PtrType&& aPtr,
|
|
|
|
Method aMethod) {
|
2017-06-15 23:30:27 +03:00
|
|
|
return do_AddRef(new detail::IdleRunnableMethodWithTimerImpl<PtrType, Method>(
|
2018-06-01 19:30:30 +03:00
|
|
|
aName, std::forward<PtrType>(aPtr), aMethod));
|
2016-05-05 11:44:59 +03:00
|
|
|
}
|
|
|
|
|
2017-02-07 08:02:57 +03:00
|
|
|
template <typename PtrType, typename Method>
|
|
|
|
already_AddRefed<detail::NonOwningRunnableMethod<PtrType, Method>>
|
|
|
|
NewNonOwningRunnableMethod(const char* aName, PtrType&& aPtr, Method aMethod) {
|
2017-06-15 23:30:27 +03:00
|
|
|
return do_AddRef(new detail::NonOwningRunnableMethodImpl<PtrType, Method>(
|
2018-06-01 19:30:30 +03:00
|
|
|
aName, std::forward<PtrType>(aPtr), aMethod));
|
2015-03-01 20:12:00 +03:00
|
|
|
}
|
2013-09-07 05:29:24 +04:00
|
|
|
|
2017-02-07 08:02:57 +03:00
|
|
|
template <typename PtrType, typename Method>
|
|
|
|
already_AddRefed<detail::NonOwningCancelableRunnableMethod<PtrType, Method>>
|
|
|
|
NewNonOwningCancelableRunnableMethod(const char* aName, PtrType&& aPtr,
|
|
|
|
Method aMethod) {
|
2017-07-21 06:50:43 +03:00
|
|
|
return do_AddRef(
|
2017-06-15 23:30:27 +03:00
|
|
|
new detail::NonOwningCancelableRunnableMethodImpl<PtrType, Method>(
|
2018-06-01 19:30:30 +03:00
|
|
|
aName, std::forward<PtrType>(aPtr), aMethod));
|
2017-05-25 00:34:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename PtrType, typename Method>
|
|
|
|
already_AddRefed<detail::NonOwningIdleRunnableMethod<PtrType, Method>>
|
|
|
|
NewNonOwningIdleRunnableMethod(const char* aName, PtrType&& aPtr,
|
|
|
|
Method aMethod) {
|
2017-06-15 23:30:27 +03:00
|
|
|
return do_AddRef(new detail::NonOwningIdleRunnableMethodImpl<PtrType, Method>(
|
2018-06-01 19:30:30 +03:00
|
|
|
aName, std::forward<PtrType>(aPtr), aMethod));
|
2017-05-25 00:34:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename PtrType, typename Method>
|
|
|
|
already_AddRefed<detail::NonOwningIdleRunnableMethodWithTimer<PtrType, Method>>
|
|
|
|
NewNonOwningIdleRunnableMethodWithTimer(const char* aName, PtrType&& aPtr,
|
|
|
|
Method aMethod) {
|
2017-07-21 06:50:43 +03:00
|
|
|
return do_AddRef(
|
2017-06-15 23:30:27 +03:00
|
|
|
new detail::NonOwningIdleRunnableMethodWithTimerImpl<PtrType, Method>(
|
2018-06-01 19:30:30 +03:00
|
|
|
aName, std::forward<PtrType>(aPtr), aMethod));
|
2017-05-25 00:34:50 +03:00
|
|
|
}
|
|
|
|
|
2016-05-05 11:45:00 +03:00
|
|
|
// Similar to NewRunnableMethod. Call like so:
|
2013-09-07 05:29:24 +04:00
|
|
|
// nsCOMPtr<nsIRunnable> event =
|
2016-05-05 11:45:00 +03:00
|
|
|
// NewRunnableMethod<Types,...>(myObject, &MyClass::HandleEvent, myArg1,...);
|
2015-03-01 20:12:00 +03:00
|
|
|
// 'Types' are the stored type for each argument, see ParameterStorage for
|
|
|
|
// details.
|
2017-02-07 08:02:57 +03:00
|
|
|
template <typename... Storages, typename PtrType, typename Method,
|
|
|
|
typename... Args>
|
|
|
|
already_AddRefed<detail::OwningRunnableMethod<PtrType, Method>>
|
|
|
|
NewRunnableMethod(const char* aName, PtrType&& aPtr, Method aMethod,
|
|
|
|
Args&&... aArgs) {
|
|
|
|
static_assert(sizeof...(Storages) == sizeof...(Args),
|
|
|
|
"<Storages...> size should be equal to number of arguments");
|
2017-07-21 06:50:43 +03:00
|
|
|
return do_AddRef(
|
2017-06-15 23:30:27 +03:00
|
|
|
new detail::OwningRunnableMethodImpl<PtrType, Method, Storages...>(
|
2018-06-01 19:30:30 +03:00
|
|
|
aName, std::forward<PtrType>(aPtr), aMethod,
|
|
|
|
std::forward<Args>(aArgs)...));
|
2016-05-05 11:44:59 +03:00
|
|
|
}
|
|
|
|
|
2017-02-07 08:02:57 +03:00
|
|
|
template <typename... Storages, typename PtrType, typename Method,
|
|
|
|
typename... Args>
|
|
|
|
already_AddRefed<detail::NonOwningRunnableMethod<PtrType, Method>>
|
|
|
|
NewNonOwningRunnableMethod(const char* aName, PtrType&& aPtr, Method aMethod,
|
|
|
|
Args&&... aArgs) {
|
|
|
|
static_assert(sizeof...(Storages) == sizeof...(Args),
|
|
|
|
"<Storages...> size should be equal to number of arguments");
|
2017-07-21 06:50:43 +03:00
|
|
|
return do_AddRef(
|
2017-06-15 23:30:27 +03:00
|
|
|
new detail::NonOwningRunnableMethodImpl<PtrType, Method, Storages...>(
|
2018-06-01 19:30:30 +03:00
|
|
|
aName, std::forward<PtrType>(aPtr), aMethod,
|
|
|
|
std::forward<Args>(aArgs)...));
|
2016-05-05 11:44:59 +03:00
|
|
|
}
|
|
|
|
|
2017-02-07 08:02:57 +03:00
|
|
|
template <typename... Storages, typename PtrType, typename Method,
|
|
|
|
typename... Args>
|
|
|
|
already_AddRefed<detail::CancelableRunnableMethod<PtrType, Method>>
|
|
|
|
NewCancelableRunnableMethod(const char* aName, PtrType&& aPtr, Method aMethod,
|
|
|
|
Args&&... aArgs) {
|
|
|
|
static_assert(sizeof...(Storages) == sizeof...(Args),
|
|
|
|
"<Storages...> size should be equal to number of arguments");
|
2017-07-21 06:50:43 +03:00
|
|
|
return do_AddRef(
|
2017-06-15 23:30:27 +03:00
|
|
|
new detail::CancelableRunnableMethodImpl<PtrType, Method, Storages...>(
|
2018-06-01 19:30:30 +03:00
|
|
|
aName, std::forward<PtrType>(aPtr), aMethod,
|
|
|
|
std::forward<Args>(aArgs)...));
|
2009-02-18 07:11:09 +03:00
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2017-02-07 08:02:57 +03:00
|
|
|
template <typename... Storages, typename PtrType, typename Method,
|
|
|
|
typename... Args>
|
|
|
|
already_AddRefed<detail::NonOwningCancelableRunnableMethod<PtrType, Method>>
|
|
|
|
NewNonOwningCancelableRunnableMethod(const char* aName, PtrType&& aPtr,
|
|
|
|
Method aMethod, Args&&... aArgs) {
|
|
|
|
static_assert(sizeof...(Storages) == sizeof...(Args),
|
|
|
|
"<Storages...> size should be equal to number of arguments");
|
2017-07-21 06:50:43 +03:00
|
|
|
return do_AddRef(
|
2017-06-15 23:30:27 +03:00
|
|
|
new detail::NonOwningCancelableRunnableMethodImpl<PtrType, Method,
|
|
|
|
Storages...>(
|
2018-06-01 19:30:30 +03:00
|
|
|
aName, std::forward<PtrType>(aPtr), aMethod,
|
|
|
|
std::forward<Args>(aArgs)...));
|
2017-05-25 00:34:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Storages, typename PtrType, typename Method,
|
|
|
|
typename... Args>
|
|
|
|
already_AddRefed<detail::IdleRunnableMethod<PtrType, Method>>
|
|
|
|
NewIdleRunnableMethod(const char* aName, PtrType&& aPtr, Method aMethod,
|
|
|
|
Args&&... aArgs) {
|
|
|
|
static_assert(sizeof...(Storages) == sizeof...(Args),
|
|
|
|
"<Storages...> size should be equal to number of arguments");
|
2017-07-21 06:50:43 +03:00
|
|
|
return do_AddRef(
|
2017-06-15 23:30:27 +03:00
|
|
|
new detail::IdleRunnableMethodImpl<PtrType, Method, Storages...>(
|
2018-06-01 19:30:30 +03:00
|
|
|
aName, std::forward<PtrType>(aPtr), aMethod,
|
|
|
|
std::forward<Args>(aArgs)...));
|
2017-05-25 00:34:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Storages, typename PtrType, typename Method,
|
|
|
|
typename... Args>
|
|
|
|
already_AddRefed<detail::NonOwningIdleRunnableMethod<PtrType, Method>>
|
|
|
|
NewNonOwningIdleRunnableMethod(const char* aName, PtrType&& aPtr,
|
|
|
|
Method aMethod, Args&&... aArgs) {
|
|
|
|
static_assert(sizeof...(Storages) == sizeof...(Args),
|
|
|
|
"<Storages...> size should be equal to number of arguments");
|
2017-07-21 06:50:43 +03:00
|
|
|
return do_AddRef(
|
2017-06-15 23:30:27 +03:00
|
|
|
new detail::NonOwningIdleRunnableMethodImpl<PtrType, Method, Storages...>(
|
2018-06-01 19:30:30 +03:00
|
|
|
aName, std::forward<PtrType>(aPtr), aMethod,
|
|
|
|
std::forward<Args>(aArgs)...));
|
2017-05-25 00:34:50 +03:00
|
|
|
}
|
|
|
|
|
2016-05-05 11:45:00 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2006-05-13 00:18:13 +04:00
|
|
|
#endif // XPCOM_GLUE_AVOID_NSPR
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
// This class is designed to be used when you have an event class E that has a
|
|
|
|
// pointer back to resource class R. If R goes away while E is still pending,
|
|
|
|
// then it is important to "revoke" E so that it does not try use R after R has
|
|
|
|
// been destroyed. nsRevocableEventPtr makes it easy for R to manage such
|
|
|
|
// situations:
|
|
|
|
//
|
|
|
|
// class R;
|
|
|
|
//
|
2016-04-26 03:23:21 +03:00
|
|
|
// class E : public mozilla::Runnable {
|
2006-05-10 21:30:15 +04:00
|
|
|
// public:
|
|
|
|
// void Revoke() {
|
2012-07-30 18:20:58 +04:00
|
|
|
// mResource = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
// }
|
|
|
|
// private:
|
|
|
|
// R *mResource;
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// class R {
|
|
|
|
// public:
|
|
|
|
// void EventHandled() {
|
|
|
|
// mEvent.Forget();
|
|
|
|
// }
|
|
|
|
// private:
|
|
|
|
// nsRevocableEventPtr<E> mEvent;
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// void R::PostEvent() {
|
|
|
|
// // Make sure any pending event is revoked.
|
|
|
|
// mEvent->Revoke();
|
|
|
|
//
|
|
|
|
// nsCOMPtr<nsIRunnable> event = new E();
|
|
|
|
// if (NS_SUCCEEDED(NS_DispatchToCurrentThread(event))) {
|
|
|
|
// // Keep pointer to event so we can revoke it.
|
|
|
|
// mEvent = event;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// NS_IMETHODIMP E::Run() {
|
|
|
|
// if (!mResource)
|
|
|
|
// return NS_OK;
|
|
|
|
// ...
|
|
|
|
// mResource->EventHandled();
|
|
|
|
// return NS_OK;
|
|
|
|
// }
|
|
|
|
//
|
2014-06-27 05:35:39 +04:00
|
|
|
template <class T>
|
|
|
|
class nsRevocableEventPtr {
|
2006-05-10 21:30:15 +04:00
|
|
|
public:
|
2014-06-27 05:35:39 +04:00
|
|
|
nsRevocableEventPtr() : mEvent(nullptr) {}
|
|
|
|
~nsRevocableEventPtr() { Revoke(); }
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2017-07-19 10:03:59 +03:00
|
|
|
const nsRevocableEventPtr& operator=(RefPtr<T>&& aEvent) {
|
2014-06-27 05:35:39 +04:00
|
|
|
if (mEvent != aEvent) {
|
2010-05-11 05:05:40 +04:00
|
|
|
Revoke();
|
2018-05-30 22:15:35 +03:00
|
|
|
mEvent = std::move(aEvent);
|
2016-05-05 11:45:00 +03:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
void Revoke() {
|
2006-05-10 21:30:15 +04:00
|
|
|
if (mEvent) {
|
|
|
|
mEvent->Revoke();
|
2012-07-30 18:20:58 +04:00
|
|
|
mEvent = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
void Forget() { mEvent = nullptr; }
|
|
|
|
bool IsPending() { return mEvent != nullptr; }
|
|
|
|
T* get() { return mEvent; }
|
2006-05-10 21:30:15 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Not implemented
|
|
|
|
nsRevocableEventPtr(const nsRevocableEventPtr&);
|
|
|
|
nsRevocableEventPtr& operator=(const nsRevocableEventPtr&);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<T> mEvent;
|
2006-05-10 21:30:15 +04:00
|
|
|
};
|
|
|
|
|
2017-10-26 05:46:50 +03:00
|
|
|
template <class T>
|
|
|
|
inline already_AddRefed<T> do_AddRef(nsRevocableEventPtr<T>& aObj) {
|
|
|
|
return do_AddRef(aObj.get());
|
|
|
|
}
|
|
|
|
|
2012-06-12 21:06:20 +04:00
|
|
|
/**
|
|
|
|
* A simple helper to suffix thread pool name
|
|
|
|
* with incremental numbers.
|
|
|
|
*/
|
|
|
|
class nsThreadPoolNaming {
|
|
|
|
public:
|
2020-02-12 14:13:33 +03:00
|
|
|
nsThreadPoolNaming() = default;
|
2012-06-12 21:06:20 +04:00
|
|
|
|
2016-12-22 02:38:41 +03:00
|
|
|
/**
|
|
|
|
* Returns a thread name as "<aPoolName> #<n>" and increments the counter.
|
|
|
|
*/
|
|
|
|
nsCString GetNextThreadName(const nsACString& aPoolName);
|
|
|
|
|
|
|
|
template <size_t LEN>
|
|
|
|
nsCString GetNextThreadName(const char (&aPoolName)[LEN]) {
|
|
|
|
return GetNextThreadName(nsDependentCString(aPoolName, LEN - 1));
|
|
|
|
}
|
|
|
|
|
2012-06-12 21:06:20 +04:00
|
|
|
private:
|
2020-02-12 14:13:33 +03:00
|
|
|
mozilla::Atomic<uint32_t> mCounter{0};
|
2012-06-12 21:06:20 +04:00
|
|
|
|
2015-01-07 02:35:02 +03:00
|
|
|
nsThreadPoolNaming(const nsThreadPoolNaming&) = delete;
|
|
|
|
void operator=(const nsThreadPoolNaming&) = delete;
|
2012-06-12 21:06:20 +04:00
|
|
|
};
|
|
|
|
|
2012-07-25 18:25:08 +04:00
|
|
|
/**
|
|
|
|
* Thread priority in most operating systems affect scheduling, not IO. This
|
|
|
|
* helper is used to set the current thread to low IO priority for the lifetime
|
|
|
|
* of the created object. You can only use this low priority IO setting within
|
|
|
|
* the context of the current thread.
|
|
|
|
*/
|
2013-04-12 07:21:40 +04:00
|
|
|
class MOZ_STACK_CLASS nsAutoLowPriorityIO {
|
2012-07-25 18:25:08 +04:00
|
|
|
public:
|
|
|
|
nsAutoLowPriorityIO();
|
|
|
|
~nsAutoLowPriorityIO();
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool lowIOPrioritySet;
|
|
|
|
#if defined(XP_MACOSX)
|
|
|
|
int oldPriority;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2014-07-04 14:34:15 +04:00
|
|
|
void NS_SetMainThread();
|
2012-07-25 18:25:08 +04:00
|
|
|
|
2017-07-29 00:56:49 +03:00
|
|
|
// Used only on cooperatively scheduled "main" threads. Causes the thread to be
|
|
|
|
// considered a main thread and also causes GetCurrentVirtualThread to return
|
|
|
|
// aVirtualThread.
|
|
|
|
void NS_SetMainThread(PRThread* aVirtualThread);
|
|
|
|
|
|
|
|
// Used only on cooperatively scheduled "main" threads. Causes the thread to no
|
|
|
|
// longer be considered a main thread. Also causes GetCurrentVirtualThread() to
|
|
|
|
// return a unique value.
|
|
|
|
void NS_UnsetMainThread();
|
|
|
|
|
2017-05-25 04:12:55 +03:00
|
|
|
/**
|
|
|
|
* Return the expiration time of the next timer to run on the current
|
|
|
|
* thread. If that expiration time is greater than aDefault, then
|
2017-05-25 04:14:29 +03:00
|
|
|
* return aDefault. aSearchBound specifies a maximum number of timers
|
|
|
|
* to examine to find a timer on the current thread. If no timer that
|
|
|
|
* will run on the current thread is found after examining
|
|
|
|
* aSearchBound timers, return the highest seen expiration time as a
|
|
|
|
* best effort guess.
|
2017-05-25 04:12:55 +03:00
|
|
|
*
|
|
|
|
* Timers with either the type nsITimer::TYPE_ONE_SHOT_LOW_PRIORITY or
|
|
|
|
* nsITIMER::TYPE_REPEATING_SLACK_LOW_PRIORITY will be skipped when
|
|
|
|
* searching for the next expiration time. This enables timers to
|
|
|
|
* have lower priority than callbacks dispatched from
|
|
|
|
* nsIThread::IdleDispatch.
|
|
|
|
*/
|
2017-05-25 04:14:29 +03:00
|
|
|
extern mozilla::TimeStamp NS_GetTimerDeadlineHintOnCurrentThread(
|
|
|
|
mozilla::TimeStamp aDefault, uint32_t aSearchBound);
|
2017-05-25 04:12:55 +03:00
|
|
|
|
2019-09-25 07:03:28 +03:00
|
|
|
/**
|
|
|
|
* Dispatches the given event to a background thread. The primary benefit of
|
|
|
|
* this API is that you do not have to manage the lifetime of your own thread
|
|
|
|
* for running your own events; the thread manager will take care of the
|
|
|
|
* background thread's lifetime. Not having to manage your own thread also
|
|
|
|
* means less resource usage, as the underlying implementation here can manage
|
|
|
|
* spinning up and shutting down threads appropriately.
|
2019-12-12 18:09:19 +03:00
|
|
|
*
|
|
|
|
* NOTE: there is no guarantee that events dispatched via these APIs are run
|
|
|
|
* serially, in dispatch order; several dispatched events may run in parallel.
|
|
|
|
* If you depend on serial execution of dispatched events, you should use
|
|
|
|
* NS_CreateBackgroundTaskQueue instead, and dispatch events to the returned
|
|
|
|
* event target.
|
2019-09-25 07:03:28 +03:00
|
|
|
*/
|
2019-11-06 00:19:18 +03:00
|
|
|
extern nsresult NS_DispatchBackgroundTask(
|
2019-10-06 21:29:55 +03:00
|
|
|
already_AddRefed<nsIRunnable> aEvent,
|
|
|
|
uint32_t aDispatchFlags = NS_DISPATCH_NORMAL);
|
2019-11-06 00:19:18 +03:00
|
|
|
extern nsresult NS_DispatchBackgroundTask(
|
2019-10-06 21:29:55 +03:00
|
|
|
nsIRunnable* aEvent, uint32_t aDispatchFlags = NS_DISPATCH_NORMAL);
|
2019-09-25 07:03:28 +03:00
|
|
|
|
2019-12-12 18:09:19 +03:00
|
|
|
/**
|
|
|
|
* Obtain a new serial event target that dispatches runnables to a background
|
|
|
|
* thread. In many cases, this is a straight replacement for creating your
|
|
|
|
* own, private thread, and is generally preferred to creating your own,
|
|
|
|
* private thread.
|
|
|
|
*/
|
2020-03-14 00:15:51 +03:00
|
|
|
extern "C" nsresult NS_CreateBackgroundTaskQueue(
|
|
|
|
const char* aName, nsISerialEventTarget** aTarget);
|
2019-12-12 18:09:19 +03:00
|
|
|
|
2017-05-03 01:42:02 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
2017-05-23 01:12:43 +03:00
|
|
|
// These functions return event targets that can be used to dispatch to the
|
|
|
|
// current or main thread. They can also be used to test if you're on those
|
|
|
|
// threads (via IsOnCurrentThread). These functions should be used in preference
|
|
|
|
// to the nsIThread-based NS_Get{Current,Main}Thread functions since they will
|
|
|
|
// return more useful answers in the case of threads sharing an event loop.
|
|
|
|
|
|
|
|
nsIEventTarget* GetCurrentThreadEventTarget();
|
|
|
|
|
|
|
|
nsIEventTarget* GetMainThreadEventTarget();
|
|
|
|
|
|
|
|
// These variants of the above functions assert that the given thread has a
|
|
|
|
// serial event target (i.e., that it's not part of a thread pool) and returns
|
|
|
|
// that.
|
|
|
|
|
|
|
|
nsISerialEventTarget* GetCurrentThreadSerialEventTarget();
|
|
|
|
|
|
|
|
nsISerialEventTarget* GetMainThreadSerialEventTarget();
|
|
|
|
|
2019-02-25 19:20:50 +03:00
|
|
|
// Returns the number of CPUs, like PR_GetNumberOfProcessors, except
|
|
|
|
// that it can return a cached value on platforms where sandboxing
|
|
|
|
// would prevent reading the current value (currently Linux). CPU
|
|
|
|
// hotplugging is uncommon, so this is unlikely to make a difference
|
|
|
|
// in practice.
|
|
|
|
size_t GetNumberOfProcessors();
|
|
|
|
|
2017-05-03 01:42:02 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
#endif // nsThreadUtils_h__
|