2016-05-25 00:45:44 +03: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: */
|
2014-01-23 08:21:05 +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/. */
|
|
|
|
|
|
|
|
#ifndef SharedThreadPool_h_
|
|
|
|
#define SharedThreadPool_h_
|
|
|
|
|
|
|
|
#include <queue>
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2014-01-23 08:21:05 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2015-08-03 21:39:45 +03:00
|
|
|
#include "nsIThreadManager.h"
|
2014-01-23 08:21:05 +04:00
|
|
|
#include "nsIThreadPool.h"
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
// Wrapper that makes an nsIThreadPool a singleton, and provides a
|
|
|
|
// consistent threadsafe interface to get instances. Callers simply get a
|
|
|
|
// SharedThreadPool by the name of its nsIThreadPool. All get requests of
|
|
|
|
// the same name get the same SharedThreadPool. Users must store a reference
|
|
|
|
// to the pool, and when the last reference to a SharedThreadPool is dropped
|
|
|
|
// the pool is shutdown and deleted. Users aren't required to manually
|
2015-08-05 02:18:17 +03:00
|
|
|
// shutdown the pool, and can release references on any thread. This can make
|
|
|
|
// it significantly easier to use thread pools, because the caller doesn't need
|
|
|
|
// to worry about joining and tearing it down.
|
2015-08-03 21:49:42 +03:00
|
|
|
//
|
|
|
|
// On Windows all threads in the pool have MSCOM initialized with
|
|
|
|
// COINIT_MULTITHREADED. Note that not all users of MSCOM use this mode see [1],
|
|
|
|
// and mixing MSCOM objects between the two is terrible for performance, and can
|
|
|
|
// cause some functions to fail. So be careful when using Win32 APIs on a
|
|
|
|
// SharedThreadPool, and avoid sharing objects if at all possible.
|
|
|
|
//
|
2016-08-16 03:28:05 +03:00
|
|
|
// [1] https://dxr.mozilla.org/mozilla-central/search?q=coinitialize&redirect=false
|
2014-02-18 02:53:53 +04:00
|
|
|
class SharedThreadPool : public nsIThreadPool
|
|
|
|
{
|
2014-01-23 08:21:05 +04:00
|
|
|
public:
|
|
|
|
|
|
|
|
// Gets (possibly creating) the shared thread pool singleton instance with
|
|
|
|
// thread pool named aName.
|
2015-06-17 17:00:52 +03:00
|
|
|
static already_AddRefed<SharedThreadPool> Get(const nsCString& aName,
|
2014-02-18 02:53:52 +04:00
|
|
|
uint32_t aThreadLimit = 4);
|
2014-01-23 08:21:05 +04:00
|
|
|
|
|
|
|
// We implement custom threadsafe AddRef/Release pair, that destroys the
|
|
|
|
// the shared pool singleton when the refcount drops to 0. The addref/release
|
|
|
|
// are implemented using locking, so it's not recommended that you use them
|
|
|
|
// in a tight loop.
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
|
|
|
|
NS_IMETHOD_(MozExternalRefCountType) AddRef(void) override;
|
|
|
|
NS_IMETHOD_(MozExternalRefCountType) Release(void) override;
|
2014-01-23 08:21:05 +04:00
|
|
|
|
|
|
|
// Forward behaviour to wrapped thread pool implementation.
|
|
|
|
NS_FORWARD_SAFE_NSITHREADPOOL(mPool);
|
2015-07-10 06:21:46 +03:00
|
|
|
|
2016-01-05 06:35:17 +03:00
|
|
|
// Call this when dispatching from an event on the same
|
|
|
|
// threadpool that is about to complete. We should not create a new thread
|
|
|
|
// in that case since a thread is about to become idle.
|
2016-09-14 06:12:15 +03:00
|
|
|
nsresult DispatchFromEndOfTaskInThisPool(nsIRunnable *event)
|
|
|
|
{
|
|
|
|
return Dispatch(event, NS_DISPATCH_AT_END);
|
|
|
|
}
|
2016-01-05 06:35:17 +03:00
|
|
|
|
2015-07-10 06:21:46 +03:00
|
|
|
NS_IMETHOD DispatchFromScript(nsIRunnable *event, uint32_t flags) override {
|
|
|
|
return Dispatch(event, flags);
|
|
|
|
}
|
|
|
|
|
2017-05-20 06:58:07 +03:00
|
|
|
NS_IMETHOD Dispatch(already_AddRefed<nsIRunnable> event, uint32_t flags = NS_DISPATCH_NORMAL) override
|
2015-07-10 06:21:46 +03:00
|
|
|
{ return !mEventTarget ? NS_ERROR_NULL_POINTER : mEventTarget->Dispatch(Move(event), flags); }
|
|
|
|
|
2016-06-01 03:04:54 +03:00
|
|
|
NS_IMETHOD DelayedDispatch(already_AddRefed<nsIRunnable>, uint32_t) override
|
2016-05-13 01:15:43 +03:00
|
|
|
{ return NS_ERROR_NOT_IMPLEMENTED; }
|
|
|
|
|
2016-01-12 07:18:47 +03:00
|
|
|
using nsIEventTarget::Dispatch;
|
|
|
|
|
2015-07-10 06:21:46 +03:00
|
|
|
NS_IMETHOD IsOnCurrentThread(bool *_retval) override { return !mEventTarget ? NS_ERROR_NULL_POINTER : mEventTarget->IsOnCurrentThread(_retval); }
|
2014-01-23 08:21:05 +04:00
|
|
|
|
2017-05-22 21:26:39 +03:00
|
|
|
NS_IMETHOD_(bool) IsOnCurrentThreadInfallible() override { return mEventTarget && mEventTarget->IsOnCurrentThread(); }
|
|
|
|
|
2015-06-25 02:01:15 +03:00
|
|
|
// Creates necessary statics. Called once at startup.
|
|
|
|
static void InitStatics();
|
|
|
|
|
|
|
|
// Spins the event loop until all thread pools are shutdown.
|
|
|
|
// *Must* be called on the main thread.
|
|
|
|
static void SpinUntilEmpty();
|
|
|
|
|
2015-08-03 21:39:45 +03:00
|
|
|
#if defined(MOZ_ASAN)
|
|
|
|
// Use the system default in ASAN builds, because the default is assumed to be
|
|
|
|
// larger than the size we want to use and is hopefully sufficient for ASAN.
|
|
|
|
static const uint32_t kStackSize = nsIThreadManager::DEFAULT_STACK_SIZE;
|
|
|
|
#elif defined(XP_WIN) || defined(XP_MACOSX) || defined(LINUX)
|
|
|
|
static const uint32_t kStackSize = (256 * 1024);
|
|
|
|
#else
|
|
|
|
// All other platforms use their system defaults.
|
|
|
|
static const uint32_t kStackSize = nsIThreadManager::DEFAULT_STACK_SIZE;
|
|
|
|
#endif
|
|
|
|
|
2014-01-23 08:21:05 +04:00
|
|
|
private:
|
|
|
|
|
2015-06-25 02:01:15 +03:00
|
|
|
// Returns whether there are no pools in existence at the moment.
|
|
|
|
static bool IsEmpty();
|
2014-01-23 08:21:05 +04:00
|
|
|
|
|
|
|
// Creates a singleton SharedThreadPool wrapper around aPool.
|
|
|
|
// aName is the name of the aPool, and is used to lookup the
|
|
|
|
// SharedThreadPool in the hash table of all created pools.
|
2014-02-18 02:53:52 +04:00
|
|
|
SharedThreadPool(const nsCString& aName,
|
|
|
|
nsIThreadPool* aPool);
|
2014-01-23 08:21:05 +04:00
|
|
|
virtual ~SharedThreadPool();
|
|
|
|
|
2014-02-18 02:53:52 +04:00
|
|
|
nsresult EnsureThreadLimitIsAtLeast(uint32_t aThreadLimit);
|
|
|
|
|
2014-01-23 08:21:05 +04:00
|
|
|
// Name of mPool.
|
|
|
|
const nsCString mName;
|
|
|
|
|
|
|
|
// Thread pool being wrapped.
|
|
|
|
nsCOMPtr<nsIThreadPool> mPool;
|
|
|
|
|
|
|
|
// Refcount. We implement custom ref counting so that the thread pool is
|
|
|
|
// shutdown in a threadsafe manner and singletonness is preserved.
|
|
|
|
nsrefcnt mRefCnt;
|
|
|
|
|
|
|
|
// mPool QI'd to nsIEventTarget. We cache this, so that we can use
|
|
|
|
// NS_FORWARD_SAFE_NSIEVENTTARGET above.
|
|
|
|
nsCOMPtr<nsIEventTarget> mEventTarget;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
#endif // SharedThreadPool_h_
|