| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 */ | 27 */ |
| 28 | 28 |
| 29 #include "config.h" | 29 #include "config.h" |
| 30 | 30 |
| 31 #if ENABLE(WEB_AUDIO) | 31 #if ENABLE(WEB_AUDIO) |
| 32 | 32 |
| 33 #include "platform/audio/HRTFDatabaseLoader.h" | 33 #include "platform/audio/HRTFDatabaseLoader.h" |
| 34 | 34 |
| 35 #include "platform/Task.h" | 35 #include "platform/Task.h" |
| 36 #include "platform/TaskSynchronizer.h" |
| 36 #include "public/platform/Platform.h" | 37 #include "public/platform/Platform.h" |
| 37 #include "wtf/MainThread.h" | 38 #include "wtf/MainThread.h" |
| 38 | 39 |
| 39 namespace blink { | 40 namespace blink { |
| 40 | 41 |
| 41 typedef HeapHashMap<double, WeakMember<HRTFDatabaseLoader> > LoaderMap; | 42 typedef HeapHashMap<double, WeakMember<HRTFDatabaseLoader> > LoaderMap; |
| 42 | 43 |
| 43 static LoaderMap& loaderMap() | 44 static LoaderMap& loaderMap() |
| 44 { | 45 { |
| 45 DEFINE_STATIC_LOCAL(Persistent<LoaderMap>, map, (new LoaderMap)); | 46 DEFINE_STATIC_LOCAL(Persistent<LoaderMap>, map, (new LoaderMap)); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 64 | 65 |
| 65 HRTFDatabaseLoader::HRTFDatabaseLoader(float sampleRate) | 66 HRTFDatabaseLoader::HRTFDatabaseLoader(float sampleRate) |
| 66 : m_databaseSampleRate(sampleRate) | 67 : m_databaseSampleRate(sampleRate) |
| 67 { | 68 { |
| 68 ASSERT(isMainThread()); | 69 ASSERT(isMainThread()); |
| 69 } | 70 } |
| 70 | 71 |
| 71 HRTFDatabaseLoader::~HRTFDatabaseLoader() | 72 HRTFDatabaseLoader::~HRTFDatabaseLoader() |
| 72 { | 73 { |
| 73 ASSERT(isMainThread()); | 74 ASSERT(isMainThread()); |
| 74 waitForLoaderThreadCompletion(); | 75 ASSERT(!m_thread); |
| 75 } | 76 } |
| 76 | 77 |
| 77 void HRTFDatabaseLoader::load() | 78 void HRTFDatabaseLoader::loadTask() |
| 78 { | 79 { |
| 79 ASSERT(!isMainThread()); | 80 ASSERT(!isMainThread()); |
| 80 m_thread->attachGC(); | 81 m_thread->attachGC(); |
| 81 | 82 |
| 82 { | 83 { |
| 83 MutexLocker locker(m_lock); | 84 MutexLocker locker(m_lock); |
| 84 if (!m_hrtfDatabase) { | 85 if (!m_hrtfDatabase) { |
| 85 // Load the default HRTF database. | 86 // Load the default HRTF database. |
| 86 m_hrtfDatabase = HRTFDatabase::create(m_databaseSampleRate); | 87 m_hrtfDatabase = HRTFDatabase::create(m_databaseSampleRate); |
| 87 } | 88 } |
| 88 } | 89 } |
| 89 | 90 |
| 90 m_thread->detachGC(); | 91 m_thread->detachGC(); |
| 91 } | 92 } |
| 92 | 93 |
| 93 void HRTFDatabaseLoader::loadAsynchronously() | 94 void HRTFDatabaseLoader::loadAsynchronously() |
| 94 { | 95 { |
| 95 ASSERT(isMainThread()); | 96 ASSERT(isMainThread()); |
| 96 | 97 |
| 97 MutexLocker locker(m_lock); | 98 MutexLocker locker(m_lock); |
| 98 if (!m_hrtfDatabase && !m_thread) { | 99 if (!m_hrtfDatabase && !m_thread) { |
| 99 // Start the asynchronous database loading process. | 100 // Start the asynchronous database loading process. |
| 100 m_thread = WebThreadSupportingGC::create("HRTF database loader"); | 101 m_thread = WebThreadSupportingGC::create("HRTF database loader"); |
| 101 m_thread->postTask(new Task(WTF::bind(&HRTFDatabaseLoader::load, this)))
; | 102 m_thread->postTask(new Task(WTF::bind(&HRTFDatabaseLoader::loadTask, thi
s))); |
| 102 } | 103 } |
| 103 } | 104 } |
| 104 | 105 |
| 105 bool HRTFDatabaseLoader::isLoaded() | 106 bool HRTFDatabaseLoader::isLoaded() |
| 106 { | 107 { |
| 107 MutexLocker locker(m_lock); | 108 MutexLocker locker(m_lock); |
| 108 return m_hrtfDatabase; | 109 return m_hrtfDatabase; |
| 109 } | 110 } |
| 110 | 111 |
| 112 // This cleanup task is needed just to make sure that the loader thread finishes |
| 113 // the load task and thus the loader thread doesn't touch m_thread any more. |
| 114 void HRTFDatabaseLoader::cleanupTask(TaskSynchronizer* sync) |
| 115 { |
| 116 sync->taskCompleted(); |
| 117 } |
| 118 |
| 111 void HRTFDatabaseLoader::waitForLoaderThreadCompletion() | 119 void HRTFDatabaseLoader::waitForLoaderThreadCompletion() |
| 112 { | 120 { |
| 121 if (!m_thread) |
| 122 return; |
| 123 |
| 124 TaskSynchronizer sync; |
| 125 m_thread->postTask(new Task(WTF::bind(&HRTFDatabaseLoader::cleanupTask, this
, &sync))); |
| 126 sync.waitForTaskCompletion(); |
| 113 m_thread.clear(); | 127 m_thread.clear(); |
| 114 } | 128 } |
| 115 | 129 |
| 116 } // namespace blink | 130 } // namespace blink |
| 117 | 131 |
| 118 #endif // ENABLE(WEB_AUDIO) | 132 #endif // ENABLE(WEB_AUDIO) |
| OLD | NEW |