michael@0: /* michael@0: * Copyright (C) 2010 Google Inc. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of michael@0: * its contributors may be used to endorse or promote products derived michael@0: * from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY michael@0: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED michael@0: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE michael@0: * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY michael@0: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES michael@0: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; michael@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND michael@0: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #ifndef HRTFDatabaseLoader_h michael@0: #define HRTFDatabaseLoader_h michael@0: michael@0: #include "nsHashKeys.h" michael@0: #include "mozilla/RefPtr.h" michael@0: #include "mozilla/MemoryReporting.h" michael@0: #include "mozilla/Mutex.h" michael@0: #include "HRTFDatabase.h" michael@0: michael@0: template class nsTHashtable; michael@0: template class nsAutoRef; michael@0: michael@0: namespace WebCore { michael@0: michael@0: // HRTFDatabaseLoader will asynchronously load the default HRTFDatabase in a new thread. michael@0: michael@0: class HRTFDatabaseLoader { michael@0: public: michael@0: // Lazily creates a HRTFDatabaseLoader (if not already created) for the given sample-rate michael@0: // and starts loading asynchronously (when created the first time). michael@0: // Returns the HRTFDatabaseLoader. michael@0: // Must be called from the main thread. michael@0: static mozilla::TemporaryRef createAndLoadAsynchronouslyIfNecessary(float sampleRate); michael@0: michael@0: // AddRef and Release may be called from any thread. michael@0: void AddRef() michael@0: { michael@0: #if defined(DEBUG) || defined(NS_BUILD_REFCNT_LOGGING) michael@0: int count = michael@0: #endif michael@0: ++m_refCnt; michael@0: MOZ_ASSERT(count > 0, "invalid ref count"); michael@0: NS_LOG_ADDREF(this, count, "HRTFDatabaseLoader", sizeof(*this)); michael@0: } michael@0: michael@0: void Release() michael@0: { michael@0: // The last reference can't be removed on a non-main thread because michael@0: // the object can be accessed on the main thread from the hash michael@0: // table via createAndLoadAsynchronouslyIfNecessary(). michael@0: int count = m_refCnt; michael@0: MOZ_ASSERT(count > 0, "extra release"); michael@0: // Optimization attempt to possibly skip proxying the release to the michael@0: // main thread. michael@0: if (count != 1 && m_refCnt.compareExchange(count, count - 1)) { michael@0: NS_LOG_RELEASE(this, count - 1, "HRTFDatabaseLoader"); michael@0: return; michael@0: } michael@0: michael@0: ProxyRelease(); michael@0: } michael@0: michael@0: // Returns true once the default database has been completely loaded. michael@0: bool isLoaded() const; michael@0: michael@0: // waitForLoaderThreadCompletion() may be called more than once, michael@0: // on any thread except m_databaseLoaderThread. michael@0: void waitForLoaderThreadCompletion(); michael@0: michael@0: HRTFDatabase* database() { return m_hrtfDatabase.get(); } michael@0: michael@0: float databaseSampleRate() const { return m_databaseSampleRate; } michael@0: michael@0: static void shutdown(); michael@0: michael@0: // Called in asynchronous loading thread. michael@0: void load(); michael@0: michael@0: size_t sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; michael@0: michael@0: private: michael@0: // Both constructor and destructor must be called from the main thread. michael@0: explicit HRTFDatabaseLoader(float sampleRate); michael@0: ~HRTFDatabaseLoader(); michael@0: michael@0: void ProxyRelease(); // any thread michael@0: void MainThreadRelease(); // main thread only michael@0: class ProxyReleaseEvent; michael@0: michael@0: // If it hasn't already been loaded, creates a new thread and initiates asynchronous loading of the default database. michael@0: // This must be called from the main thread. michael@0: void loadAsynchronously(); michael@0: michael@0: // Map from sample-rate to loader. michael@0: class LoaderByRateEntry : public nsFloatHashKey { michael@0: public: michael@0: LoaderByRateEntry(KeyTypePointer aKey) michael@0: : nsFloatHashKey(aKey) michael@0: , mLoader() // so PutEntry() will zero-initialize michael@0: { michael@0: } michael@0: HRTFDatabaseLoader* mLoader; michael@0: }; michael@0: michael@0: static PLDHashOperator shutdownEnumFunc(LoaderByRateEntry *entry, michael@0: void* unused); michael@0: michael@0: // Keeps track of loaders on a per-sample-rate basis. michael@0: static nsTHashtable *s_loaderMap; // singleton michael@0: michael@0: mozilla::Atomic m_refCnt; michael@0: michael@0: nsAutoRef m_hrtfDatabase; michael@0: michael@0: // Holding a m_threadLock is required when accessing m_databaseLoaderThread. michael@0: mozilla::Mutex m_threadLock; michael@0: PRThread* m_databaseLoaderThread; michael@0: michael@0: float m_databaseSampleRate; michael@0: }; michael@0: michael@0: } // namespace WebCore michael@0: michael@0: #endif // HRTFDatabaseLoader_h