content/media/SharedThreadPool.h

Fri, 16 Jan 2015 04:50:19 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 04:50:19 +0100
branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
permissions
-rw-r--r--

Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef SharedThreadPool_h_
michael@0 8 #define SharedThreadPool_h_
michael@0 9
michael@0 10 #include <queue>
michael@0 11 #include "mozilla/RefPtr.h"
michael@0 12 #include "nsThreadUtils.h"
michael@0 13 #include "nsIThreadPool.h"
michael@0 14 #include "nsISupports.h"
michael@0 15 #include "nsISupportsImpl.h"
michael@0 16 #include "nsCOMPtr.h"
michael@0 17
michael@0 18 namespace mozilla {
michael@0 19
michael@0 20 // Wrapper that makes an nsIThreadPool a singleton, and provides a
michael@0 21 // consistent threadsafe interface to get instances. Callers simply get a
michael@0 22 // SharedThreadPool by the name of its nsIThreadPool. All get requests of
michael@0 23 // the same name get the same SharedThreadPool. Users must store a reference
michael@0 24 // to the pool, and when the last reference to a SharedThreadPool is dropped
michael@0 25 // the pool is shutdown and deleted. Users aren't required to manually
michael@0 26 // shutdown the pool, and can release references on any thread. On Windows
michael@0 27 // all threads in the pool have MSCOM initialized with COINIT_MULTITHREADED.
michael@0 28 class SharedThreadPool : public nsIThreadPool
michael@0 29 {
michael@0 30 public:
michael@0 31
michael@0 32 // Gets (possibly creating) the shared thread pool singleton instance with
michael@0 33 // thread pool named aName.
michael@0 34 // *Must* be called on the main thread.
michael@0 35 static TemporaryRef<SharedThreadPool> Get(const nsCString& aName,
michael@0 36 uint32_t aThreadLimit = 4);
michael@0 37
michael@0 38 // Spins the event loop until all thread pools are shutdown.
michael@0 39 // *Must* be called on the main thread.
michael@0 40 static void SpinUntilShutdown();
michael@0 41
michael@0 42 // We implement custom threadsafe AddRef/Release pair, that destroys the
michael@0 43 // the shared pool singleton when the refcount drops to 0. The addref/release
michael@0 44 // are implemented using locking, so it's not recommended that you use them
michael@0 45 // in a tight loop.
michael@0 46 NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr);
michael@0 47 NS_IMETHOD_(MozExternalRefCountType) AddRef(void);
michael@0 48 NS_IMETHOD_(MozExternalRefCountType) Release(void);
michael@0 49
michael@0 50 // Forward behaviour to wrapped thread pool implementation.
michael@0 51 NS_FORWARD_SAFE_NSITHREADPOOL(mPool);
michael@0 52 NS_FORWARD_SAFE_NSIEVENTTARGET(GetEventTarget());
michael@0 53
michael@0 54 nsIEventTarget* GetEventTarget() {
michael@0 55 return mEventTarget;
michael@0 56 }
michael@0 57
michael@0 58 private:
michael@0 59
michael@0 60 // Creates necessary statics.
michael@0 61 // Main thread only.
michael@0 62 static void EnsureInitialized();
michael@0 63
michael@0 64 // Creates a singleton SharedThreadPool wrapper around aPool.
michael@0 65 // aName is the name of the aPool, and is used to lookup the
michael@0 66 // SharedThreadPool in the hash table of all created pools.
michael@0 67 SharedThreadPool(const nsCString& aName,
michael@0 68 nsIThreadPool* aPool);
michael@0 69 virtual ~SharedThreadPool();
michael@0 70
michael@0 71 nsresult EnsureThreadLimitIsAtLeast(uint32_t aThreadLimit);
michael@0 72
michael@0 73 // Name of mPool.
michael@0 74 const nsCString mName;
michael@0 75
michael@0 76 // Thread pool being wrapped.
michael@0 77 nsCOMPtr<nsIThreadPool> mPool;
michael@0 78
michael@0 79 // Refcount. We implement custom ref counting so that the thread pool is
michael@0 80 // shutdown in a threadsafe manner and singletonness is preserved.
michael@0 81 nsrefcnt mRefCnt;
michael@0 82
michael@0 83 // mPool QI'd to nsIEventTarget. We cache this, so that we can use
michael@0 84 // NS_FORWARD_SAFE_NSIEVENTTARGET above.
michael@0 85 nsCOMPtr<nsIEventTarget> mEventTarget;
michael@0 86 };
michael@0 87
michael@0 88 } // namespace mozilla
michael@0 89
michael@0 90 #endif // SharedThreadPool_h_

mercurial