mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-25 11:15:34 +00:00
35f0c7a7f9
This commit adds `ThreadPtr{Handle, Holder}` to wrap an `XpCom` object with thread-safe refcounting. These are analagous to `nsMainThreadPtr{Handle, Holder}`, but can hold references to objects from any thread, not just the main thread. `ThreadPtrHolder` is similar to `ThreadBoundRefPtr`. However, it's not possible to clone a `ThreadBoundRefPtr`, so it can't be shared among tasks. This is fine for objects that are only used once, like callbacks. However, `ThreadBoundRefPtr` doesn't work well for loggers or event emitters, which might need to be called multiple times on the owning thread. Unlike a `ThreadBoundRefPtr`, it's allowed and expected to clone and drop a `ThreadPtrHolder` on other threads. Internally, the holder keeps an atomic refcount, and releases the wrapped object on the owning thread once the count reaches zero. This commit also changes `TaskRunnable` to support dispatching from threads other than the main thread. Differential Revision: https://phabricator.services.mozilla.com/D20074 --HG-- extra : moz-landing-system : lando
31 lines
1.1 KiB
C++
31 lines
1.1 KiB
C++
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
/* 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/. */
|
|
|
|
#include "nsProxyRelease.h"
|
|
#include "nsThreadUtils.h"
|
|
|
|
namespace detail {
|
|
|
|
/* static */ void ProxyReleaseChooser<true>::ProxyReleaseISupports(
|
|
const char* aName, nsIEventTarget* aTarget, nsISupports* aDoomed,
|
|
bool aAlwaysProxy) {
|
|
::detail::ProxyRelease<nsISupports>(aName, aTarget, dont_AddRef(aDoomed),
|
|
aAlwaysProxy);
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
extern "C" {
|
|
|
|
// This function uses C linkage because it's exposed to Rust to support the
|
|
// `ThreadPtrHolder` wrapper in the `moz_task` crate.
|
|
void NS_ProxyReleaseISupports(const char* aName, nsIEventTarget* aTarget,
|
|
nsISupports* aDoomed, bool aAlwaysProxy) {
|
|
NS_ProxyRelease(aName, aTarget, dont_AddRef(aDoomed), aAlwaysProxy);
|
|
}
|
|
|
|
} // extern "C"
|