Commit Graph
95 Commits
Author SHA1 Message Date
Nika Layzell 325ec7d64a Bug 1983309 - Reduce lock contention in TaskQueue. r=xpcom-reviewers,jstutte
This patch aims to reduce lock contention when dispatching to TaskQueue,
by reducing the frequency with which the runner thread needs to acquire
the TaskQueue monitor.

Prior to this change, if there is a backlog of queued runnables,
TaskQueue::Runner would acquire mQueueMonitor 3 times per event. After
this change, mQueueMonitor will only be acquired when the set of tasks
read the last time the monitor was held have all been processed.

TaskQueue would need to change to use an unbounded lock-free MPSC
queue in order to further reduce contention when dispatching. Doing so
is a much larger project, and would require more substantial changes
to the TaskQueue type to make state management (especially around
shutdown) avoid locking.

Differential Revision: https://phabricator.services.mozilla.com/D261278
2026-05-21 12:38:58 +00:00
Sylvestre Ledru 6149f49299 Bug 2023419 - Remove Emacs/Vim modelines from xpcom/ r=emilio
replaced by .editorconfig
# ignore-this-changeset

Differential Revision: https://phabricator.services.mozilla.com/D287878
2026-03-16 17:22:07 +00:00
Jens Stutte cf6c4de4d7 Bug 1999508 - Let TaskQueue register a shutdown task for itself on its target when needed and possible. r=xpcom-reviewers,nika
In addition, remove the TaskQueueTracker from the BackgroundEventTarget and let it just support shutdown tasks, too.

On a target that SUPPORTS_SHUTDOWN_TASK_DISPATCH, TaskQueue's runner already ensures to drain the queue before the target can fully shutdown. However, eventual TaskQueue shutdown tasks were not yet executed in this case.
We thus register a shutdown task that shuts the TaskQueue down on the target if we have at least one own shutdown task. Note that this will keep the TaskQueue alive until target shutdown if there is no earlier, explicit shutdown on behalf of the consumer.

Note that this means that a TaskQueue may or may not fully enter its internal shutdown state when the target dies, depending on consumers having registered a shutdown task to it. But subsequent dispatches will always fail in the same way, and there is no way to query the shutdown state, anyways.

Differential Revision: https://phabricator.services.mozilla.com/D272151
2026-01-28 07:24:10 +00:00
Jens Stutte 6829afc661 Bug 1999508 - Introduce nsIEventTarget::GetFeatures. r=xpcom-reviewers,necko-reviewers,dom-worker-reviewers,nika,asuth,valentin
Introduces SUPPORTS_SHUTDOWN_TASKS and SUPPORTS_SHUTDOWN_TASK_DISPATCH flags.

This just wires the function through all event targets without ever being used.

Differential Revision: https://phabricator.services.mozilla.com/D272150
2026-01-28 07:24:07 +00:00
Jens Stutte 69091519a2 Bug 2011161 - Enforce TaskQueue names to be literal with StaticString. r=xpcom-reviewers,dom-storage-reviewers,nika,asuth
By now, nobody really uses TaskQueue::mName. Bug 1999508 wants to introduce logging and in order to avoid copying strings just for that, we enforce names to be literals.

In a few cases this reduces the level of detail provided in the name (WebRTC, IndexedDB), but given it never showed up (except maybe while debugging) we can probably live without it.

Differential Revision: https://phabricator.services.mozilla.com/D279500
2026-01-26 15:15:47 +00:00
Sandor Molnar 64ab8b77b5 Revert "Bug 2011161 - Enforce TaskQueue names to be literal with StaticString. r=xpcom-reviewers,dom-storage-reviewers,nika,asuth" for causing build bustages @ TestFileSystemDataManagerVersions.cpp
This reverts commit 1030b77bd4.
2026-01-26 12:34:32 +00:00
Jens Stutte 1030b77bd4 Bug 2011161 - Enforce TaskQueue names to be literal with StaticString. r=xpcom-reviewers,dom-storage-reviewers,nika,asuth
By now, nobody really uses TaskQueue::mName. Bug 1999508 wants to introduce logging and in order to avoid copying strings just for that, we enforce names to be literals.

In a few cases this reduces the level of detail provided in the name (WebRTC, IndexedDB), but given it never showed up (except maybe while debugging) we can probably live without it.

Differential Revision: https://phabricator.services.mozilla.com/D279500
2026-01-26 11:30:22 +00:00
Jens Stutte 6a6b74df8e Bug 1979268 - part 1: Have a common TargetShutdownTaskSet to manage a set of nsITargetShutdownTask. r=xpcom-reviewers,nika,dom-worker-reviewers,smaug
This is just a refactoring to have less copy&pasted code instead of more once we enable more targets for shutdown tasks.

Differential Revision: https://phabricator.services.mozilla.com/D258804
2025-09-26 17:39:50 +00:00
Cristina Horotan 9e34621d53 Revert "Bug 1983309 - Reduce lock contention in TaskQueue, r=xpcom-reviewers,jstutte" for causing multiple failures
This reverts commit deb16dbc30.
2025-08-29 17:49:39 +00:00
Nika Layzell deb16dbc30 Bug 1983309 - Reduce lock contention in TaskQueue, r=xpcom-reviewers,jstutte
This patch aims to reduce lock contention when dispatching to TaskQueue, by reducing the frequency with which the runner thread needs to acquire the TaskQueue monitor.
Prior to this change, if there is a backlog of queued runnables, TaskQueue::Runner would acquire mQueueMonitor 3 times per event. After this change, mQueueMonitor will only be acquired when the set of tasks read the last time the monitor was held have all been processed.
TaskQueue would need to change to use an unbounded lock-free MPSC queue in order to further reduce contention when dispatching. Doing so is a much larger project, and would require more substantial changes to the TaskQueue type to make state management (especially around shutdown) avoid locking.

Differential Revision: https://phabricator.services.mozilla.com/D261278
2025-08-29 16:23:36 +00:00
Nika Layzell b66d532c27 Bug 1824189 - Add a NS_DISPATCH_FALLIBLE flag to specify that events should not be leaked on error, r=xpcom-reviewers,necko-reviewers,geckoview-reviewers,media-playback-reviewers,webrtc-reviewers,bwc,padenot,emilio,jstutte,dom-worker-reviewers,asuth,kershaw,ohall
Differential Revision: https://phabricator.services.mozilla.com/D258327
2025-08-26 16:47:30 +00:00
Atila Butkovits a906e9e172 Revert "Bug 1824189 - Add a NS_DISPATCH_FALLIBLE flag to specify that events should not be leaked on error, r=xpcom-reviewers,necko-reviewers,geckoview-reviewers,media-playback-reviewers,webrtc-reviewers,bwc,padenot,emilio,jstutte,dom-worker-reviewers,asuth,kershaw,ohall" for causing leaks complaining about MediaTransportHandlerSTS.
This reverts commit 381d5dd6f0.
2025-08-26 00:43:59 +00:00
Nika Layzell 381d5dd6f0 Bug 1824189 - Add a NS_DISPATCH_FALLIBLE flag to specify that events should not be leaked on error, r=xpcom-reviewers,necko-reviewers,geckoview-reviewers,media-playback-reviewers,webrtc-reviewers,bwc,padenot,emilio,jstutte,dom-worker-reviewers,asuth,kershaw,ohall
Differential Revision: https://phabricator.services.mozilla.com/D258327
2025-08-25 23:20:08 +00:00
Andreas Pehrson 794b587e3c Bug 1968812 - Add an Observer to TaskQueue. r=xpcom-reviewers,nika
This way our WebrtcTaskQueueWrapper can natively set up the RAII class
webrtc::CurrentTaskQueueSetter on the thread the TaskQueue runs on.
Then all tasks running on the TaskQueue can identify as being on the correct
webrtc::TaskQueueBase, regardless of how they were dispatched.

Differential Revision: https://phabricator.services.mozilla.com/D251427
2025-06-19 14:17:32 +00:00
Atila Butkovits 861d860e35 Revert "Bug 1968812 - Remove the CallWorker thread and use TaskQueue::Observer instead. r=bwc" for causing failures complaining about mutexLock.
This reverts commit d53f26b530.

Revert "Bug 1968812 - Copy WebrtcTaskQueueWrapper.h to WebrtcTaskQueueWrapper.cpp. r=bwc"

This reverts commit b0d97303dc.

Revert "Bug 1968812 - In WebrtcTaskQueueWrapper, inject the CurrentTaskQueueSetter through the TaskQueue::Observer interface instead of wrapping tasks. r=bwc"

This reverts commit 9b6c76197d.

Revert "Bug 1968812 - Add an Observer to TaskQueue. r=xpcom-reviewers,nika"

This reverts commit 6d63b62958.

Revert "Bug 1968812 - Rename TaskQueueWrapper to WebrtcTaskQueueWrapper to better indicate what it wraps. r=bwc"

This reverts commit 087b7fd11e.

Revert "Bug 1968812 - Add gtest. r=bwc"

This reverts commit 85928698b2.
2025-06-11 20:29:40 +00:00
Andreas Pehrson 6d63b62958 Bug 1968812 - Add an Observer to TaskQueue. r=xpcom-reviewers,nika
This way our WebrtcTaskQueueWrapper can natively set up the RAII class
webrtc::CurrentTaskQueueSetter on the thread the TaskQueue runs on.
Then all tasks running on the TaskQueue can identify as being on the correct
webrtc::TaskQueueBase, regardless of how they were dispatched.

Differential Revision: https://phabricator.services.mozilla.com/D251427
2025-06-11 13:39:46 +00:00
Andreas Pehrson 937787cce5 Bug 1971198 - Make GetCurrentSerialEventTarget() valid during TaskQueue direct tasks. r=xpcom-reviewers,nika
Differential Revision: https://phabricator.services.mozilla.com/D253124
2025-06-11 08:47:24 +00:00
Karl Tomlinson f8f446b788 Bug 1965992 Support do_QueryObject to TaskQueue r=xpcom-reviewers,emilio
This allows TaskQueue::AwaitIdle() to be used in GTests that identify the
TaskQueue via AbstractThread::GetCurrent().

Differential Revision: https://phabricator.services.mozilla.com/D249212
2025-05-15 04:58:34 +00:00
serge-sans-paille bf3516e81e Bug 1964489 - Avoid duplication in NS_DECLARE_STATIC_IID_ACCESSOR / NS_DEFINE_STATIC_IID_ACCESSOR r=nika,necko-reviewers,media-playback-reviewers,places-reviewers,win-reviewers,dom-storage-reviewers,xpcom-reviewers,gstoll,janv,emilio,padenot,valentin,asuth
In modern C++, static constexpr member variables are automatically
inline (aka weak) so the template trick is not needed. This also avoid
duplication and reduces the amount of parsed code. No impact on
generated binary (actually: smaller debuginfo, close to identical
binary).

Differential Revision: https://phabricator.services.mozilla.com/D247825
2025-05-08 08:05:51 +00:00
Karl Tomlinson 39b84e7e1a Bug 1840313 remove declaration of non-existent TaskQueue::CancelDelayedRunnables() r=emilio
Gone since https://hg.mozilla.org/mozilla-central/rev/ad682061ae5c#l26.116

Differential Revision: https://phabricator.services.mozilla.com/D181965
2023-06-26 21:52:50 +00:00
Fabrice Desré e29be44cb7 Bug 1761040 - Prefix thread safety macros with MOZ_ r=geckoview-reviewers,media-playback-reviewers,alwu,jesup,m_kato
Differential Revision: https://phabricator.services.mozilla.com/D152575
2022-08-03 16:39:41 +00:00
Andreea Pavel fc724b92fc Backed out changeset b9d2965591b9 (bug 1761040) for landing with wrong author CLOSED TREE DONTBUILD 2022-08-03 18:55:00 +03:00
Andreea Pavel eda47f3911 Bug 1761040 - Prefix thread safety macros with MOZ_ r=geckoview-reviewers,media-playback-reviewers,alwu,jesup,m_kato
Differential Revision: https://phabricator.services.mozilla.com/D152575
2022-08-03 15:27:43 +00:00
Andreea Pavel c2e97d2e06 Backed out changeset a907159a482f (bug 1761040) for causing build bustages on a CLOSED TREE 2022-08-02 04:59:08 +03:00
Fabrice Desré d2d02e8493 Bug 1761040 - Prefix thread safety macros with MOZ_ r=geckoview-reviewers,media-playback-reviewers,alwu,jesup,m_kato
Differential Revision: https://phabricator.services.mozilla.com/D152575
2022-08-02 00:49:41 +00:00
Nika Layzell bcebb32f5d Bug 1738106 - Part 5: Track TaskQueue lifetimes to avoid leaking TaskQueues, r=xpcom-reviewers,mccr8
Differential Revision: https://phabricator.services.mozilla.com/D142606
2022-05-02 20:37:35 +00:00
Nika Layzell 5d5537e48b Bug 1738106 - Part 4: Support ThreadSafeWeakPtr in TaskQueue, r=xpcom-reviewers,mccr8
This will be used to allow the BackgroundEventTarget to safely hold weak
references to active TaskQueues managed by it.

Differential Revision: https://phabricator.services.mozilla.com/D142605
2022-05-02 20:37:35 +00:00
Nika Layzell 260ebb1412 Bug 1738106 - Part 3: Switch to TaskQueue::Create, r=xpcom-reviewers,necko-reviewers,media-playback-reviewers,dragana,alwu,barret
In future parts, TaskQueue will require extra initialization to be performed
which cannot happen in a constructor, as it takes references to the TaskQueue
object itself, which will require the introduction of a helper method. This
patch switches all callers of the TaskQueue constructor to use the new method.

Differential Revision: https://phabricator.services.mozilla.com/D142604
2022-05-02 20:37:34 +00:00
Nika Layzell 532aa21353 Bug 1738106 - Part 1: TaskQueue thread safety annotations, r=xpcom-reviewers,mccr8
Differential Revision: https://phabricator.services.mozilla.com/D142602
2022-05-02 20:37:34 +00:00
Nika Layzell c320f56d5b Bug 1738103 - Part 4: Add XPCOM support for shutdown tasks, r=necko-reviewers,KrisWright
Tasks registered with shutdown tasks are called when the target
nsISerialEventTarget is about to be destroyed.

Differential Revision: https://phabricator.services.mozilla.com/D129840
2022-03-23 22:22:04 +00:00
Randell Jesup 855202c183 Bug 1207753 - Add MOZ_UNANNOTATED to all Mutexes/Monitors r=nika,kershaw
Differential Revision: https://phabricator.services.mozilla.com/D140849
2022-03-16 18:47:08 +00:00
Noemi Erli 16f5f5f2b9 Backed out changeset 12a59e5a50bf (bug 1207753) for causing build bustage CLOSED TREE 2022-03-16 18:32:51 +02:00
Randell Jesup a98b1f8218 Bug 1207753 - Add MOZ_UNANNOTATED to all Mutexes/Monitors r=nika,kershaw
Differential Revision: https://phabricator.services.mozilla.com/D140849
2022-03-16 16:16:14 +00:00
Butkovits Atila 01197944a0 Backed out changeset a68ee4b09f92 (bug 1207753) for causing Hazard bustages. CLOSED TREE 2022-03-16 14:38:14 +02:00
Randell Jesup fe15f2115d Bug 1207753 - Add MOZ_UNANNOTATED to all Mutexes/Monitors r=nika,kershaw
Differential Revision: https://phabricator.services.mozilla.com/D140849
2022-03-16 12:01:14 +00:00
Randell Jesup 8167318db4 Bug 1743895: Add names to all TaskQueues, remove no-name constructor r=nika,necko-reviewers
Differential Revision: https://phabricator.services.mozilla.com/D132638
2021-12-03 03:16:15 +00:00
kriswright eba3b4de71 Bug 1678149 - Use a mozilla::Queue in TaskQueue r=xpcom-reviewers,mccr8
It looks like we can do a mostly direct swap from `std::Queue` to `mozilla::Queue` without needing to expose any new API, so that's what I've done here.

Differential Revision: https://phabricator.services.mozilla.com/D123523
2021-08-25 14:10:06 +00:00
Andreas Pehrson 6b5a6f5a8a Bug 1695580 - In xpcom, cancel pending DelayedRunnable timers on shutdown. r=KrisWright
Because DelayedRunnables are fire-and-forget, there is no way for a targeted
EventTarget to clean them up on shutdown. Thus if a timer fires after
EventTarget shutdown it will fail to dispatch the timer event, and avoid
releasing the timer callback because it's not on the targeted thread. This
causes a leak as there is a ref-cycle between nsTimerImpl::mCallback and
DelayedRunnable::mTimer.

This patch adds nsIDelayedRunnableObserver for a target to observe which
DelayedRunnables are relying on their timer to run them. This allows the target
to schedule a shutdown task to cancel those timers and release the runnables on
the target thread.

Supported DelayedRunnable targets with this patch are TaskQueues,
eventqueue-based nsThreads and XPCOMThreadWrappers that wrap a supported
nsThread.

An assertion makes sure at runtime that future new uses of DelayedRunnable
target nsIDelayedRunnableObserver-supported event targets.

Differential Revision: https://phabricator.services.mozilla.com/D109781
2021-04-06 20:15:11 +00:00
Brindusan Cristian 13b2a5f0b2 Backed out 8 changesets (bug 1695580) for causing build bustages on DataMutex.h. CLOSED TREE
Backed out changeset ec0b0fcc8d88 (bug 1695580)
Backed out changeset 982a46056fcb (bug 1695580)
Backed out changeset e38e8b90f119 (bug 1695580)
Backed out changeset 6fdd154aa151 (bug 1695580)
Backed out changeset d04f7a7ec375 (bug 1695580)
Backed out changeset 2103cd9e58b7 (bug 1695580)
Backed out changeset de9a7dd7fc79 (bug 1695580)
Backed out changeset ae16f09be41b (bug 1695580)
2021-04-06 17:41:19 +03:00
Andreas Pehrson 42799c4a6e Bug 1695580 - In xpcom, cancel pending DelayedRunnable timers on shutdown. r=KrisWright
Because DelayedRunnables are fire-and-forget, there is no way for a targeted
EventTarget to clean them up on shutdown. Thus if a timer fires after
EventTarget shutdown it will fail to dispatch the timer event, and avoid
releasing the timer callback because it's not on the targeted thread. This
causes a leak as there is a ref-cycle between nsTimerImpl::mCallback and
DelayedRunnable::mTimer.

This patch adds nsIDelayedRunnableObserver for a target to observe which
DelayedRunnables are relying on their timer to run them. This allows the target
to schedule a shutdown task to cancel those timers and release the runnables on
the target thread.

Supported DelayedRunnable targets with this patch are TaskQueues,
eventqueue-based nsThreads and XPCOMThreadWrappers that wrap a supported
nsThread.

An assertion makes sure at runtime that future new uses of DelayedRunnable
target nsIDelayedRunnableObserver-supported event targets.

Differential Revision: https://phabricator.services.mozilla.com/D109781
2021-04-06 12:16:11 +00:00
Simon Giesecke 5efd1faf25 Bug 1678062 - Remove unnecessary includes. r=andi
Differential Revision: https://phabricator.services.mozilla.com/D97467

Depends on D96561
2020-11-23 16:12:02 +00:00
Nihanth Subramanya 73467920c6 Bug 1555557 - Do cert override file writes off the main thread. r=keeler
Differential Revision: https://phabricator.services.mozilla.com/D35375
2020-06-29 17:00:58 +00:00
Jean-Yves Avenard d28bc98c76 Bug 1646054 - P2. Always retain dispatch flags r=froydnj
When TaskQueue was first conceived; it was only used with AbstractThreads and with tail dispatch.
By default, AbstractThread::Dispatch dropped the flags , as it was dispatching all tasks via the tail dispatcher.

It was an oversight, there's no use-case where we wouldn't want the dispatch flags to be carried forward.

It also simplifies the code and TaskQueue's use.

Depends on D80351

Differential Revision: https://phabricator.services.mozilla.com/D80352
2020-06-22 13:55:23 +00:00
Jean-Yves Avenard a5c6bb8904 Bug 1646054 - P1. Remove EventTargetWrapper class. r=froydnj
We also establish the intent of bug 1602167 on the TaskQueue object

Differential Revision: https://phabricator.services.mozilla.com/D80351
2020-06-22 13:55:21 +00:00
Jean-Yves Avenard 12af745102 Bug 1644009 - P13. Don't have AutoTaskGuard inherit from AutoTaskDispatcher. r=bholley.
This prevent being able to assert in the AutoTaskDispatcher that we are using it from the right thread/taskqueue as the SimpleTaskQueue object isn't thread-safe.

We want to assert that all nsIDirectTaskDispatcher methods are only ever accessed on the underlying thread. To do so require that the scope of AutoTaskDispatcher to terminate prior the AutoTaskGuard one.

Differential Revision: https://phabricator.services.mozilla.com/D79096
2020-06-12 05:10:55 +00:00
Jean-Yves Avenard 962915402f Bug 1644009 - P9. Add nsIDirectTaskDispatcher interface. r=froydnj
We extract the dealing of direct tasks from the TailDispatcher object and move into to the existing nsThread and TaskQueue classes.
TaskQueue is made to work with do_QueryInterface.

We continue accessing the direct tasks dispatcher via the TailDispatcher for now, which itself will forward the dealing of such tasks to the underlying thread.

Differential Revision: https://phabricator.services.mozilla.com/D79093
2020-06-12 05:10:25 +00:00
Chris Peterson e6295e2bc7 Bug 1624776 - Replace MOZ_MUST_USE with [[nodiscard]] in xpcom. r=xpcom-reviewers,KrisWright
Also move MOZ_MUST_USE before function declarations' specifiers and return type. While clang and gcc's __attribute__((warn_unused_result)) can appear before, between, or after function specifiers and return types, the [[nodiscard]] attribute must precede the function specifiers.

Also remove MOZ_MUST_USE from nsIMemoryReporter's function pointer typedefs. [[nodiscard]] is an attribute of a function's declaration, not the function's type, and thus can't be applied to function pointers or lambdas.

Differential Revision: https://phabricator.services.mozilla.com/D68138
2020-03-27 17:21:48 +00:00
Sylvestre Ledru e2537c26d2 Bug 1617437 - TaskQueue.h: Fix a -Wnon-c-typedef-for-linkage warning r=froydnj
Depends on D63781

Differential Revision: https://phabricator.services.mozilla.com/D63783
2020-02-23 12:59:35 +00:00
Doug Thayer 9f70804193 Bug 1602646 - Remove vestigial references to cooperative scheduling r=froydnj
GetCurrentPhysicalThread and GetCurrentVirtualThread are, in practice,
identical, as the TLS override that GetCurrentVirtualThread depends on
is never actually set. This simply removes that and renames some things/
deletes some comments.

Rebased across https://hg.mozilla.org/mozilla-central/rev/3f0b4e206853
by Karl Tomlinson <[email protected]>.

Differential Revision: https://phabricator.services.mozilla.com/D41247
2019-12-12 00:56:53 +00:00
Kristen Wright 765b6e862e Bug 1602167 - Make TaskQueue capable of retaining its dispatch flags r=froydnj
Makes TaskQueue's queue hold a struct with each runnable and its flags. When specified, flags will be stored and passed with NS_DISPATCH_AT_END when TaskQueue::Runner dispatches itself back to its event target.

Differential Revision: https://phabricator.services.mozilla.com/D56802
2019-12-11 22:26:06 +00:00