| 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 21 matching lines...) Expand all Loading... |
| 32 #include "platform/TaskSynchronizer.h" | 32 #include "platform/TaskSynchronizer.h" |
| 33 #include "public/platform/Platform.h" | 33 #include "public/platform/Platform.h" |
| 34 #include "public/platform/WebTaskRunner.h" | 34 #include "public/platform/WebTaskRunner.h" |
| 35 #include "public/platform/WebTraceLocation.h" | 35 #include "public/platform/WebTraceLocation.h" |
| 36 #include "wtf/PtrUtil.h" | 36 #include "wtf/PtrUtil.h" |
| 37 | 37 |
| 38 namespace blink { | 38 namespace blink { |
| 39 | 39 |
| 40 using LoaderMap = HashMap<double, HRTFDatabaseLoader*>; | 40 using LoaderMap = HashMap<double, HRTFDatabaseLoader*>; |
| 41 | 41 |
| 42 // loaderMap() returns the static hash map that contains the mapping between the |
| 43 // sample rate and the corresponding HRTF database. |
| 42 static LoaderMap& loaderMap() | 44 static LoaderMap& loaderMap() |
| 43 { | 45 { |
| 44 DEFINE_STATIC_LOCAL(LoaderMap*, map, (new LoaderMap)); | 46 DEFINE_STATIC_LOCAL(LoaderMap*, map, (new LoaderMap)); |
| 45 return *map; | 47 return *map; |
| 46 } | 48 } |
| 47 | 49 |
| 48 PassRefPtr<HRTFDatabaseLoader> HRTFDatabaseLoader::createAndLoadAsynchronouslyIf
Necessary(float sampleRate) | 50 PassRefPtr<HRTFDatabaseLoader> HRTFDatabaseLoader::createAndLoadAsynchronouslyIf
Necessary(float sampleRate) |
| 49 { | 51 { |
| 50 ASSERT(isMainThread()); | 52 ASSERT(isMainThread()); |
| 51 | 53 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 69 | 71 |
| 70 HRTFDatabaseLoader::~HRTFDatabaseLoader() | 72 HRTFDatabaseLoader::~HRTFDatabaseLoader() |
| 71 { | 73 { |
| 72 ASSERT(isMainThread()); | 74 ASSERT(isMainThread()); |
| 73 ASSERT(!m_thread); | 75 ASSERT(!m_thread); |
| 74 loaderMap().remove(m_databaseSampleRate); | 76 loaderMap().remove(m_databaseSampleRate); |
| 75 } | 77 } |
| 76 | 78 |
| 77 void HRTFDatabaseLoader::loadTask() | 79 void HRTFDatabaseLoader::loadTask() |
| 78 { | 80 { |
| 79 ASSERT(!isMainThread()); | 81 DCHECK(!isMainThread()); |
| 82 DCHECK(!m_hrtfDatabase); |
| 80 | 83 |
| 81 { | 84 // Protect access to m_hrtfDatabase, which can be accessed from the audio |
| 82 MutexLocker locker(m_lock); | 85 // thread. |
| 83 if (!m_hrtfDatabase) { | 86 MutexLocker locker(m_lock); |
| 84 // Load the default HRTF database. | 87 // Load the default HRTF database. |
| 85 m_hrtfDatabase = HRTFDatabase::create(m_databaseSampleRate); | 88 m_hrtfDatabase = HRTFDatabase::create(m_databaseSampleRate); |
| 86 } | |
| 87 } | |
| 88 } | 89 } |
| 89 | 90 |
| 90 void HRTFDatabaseLoader::loadAsynchronously() | 91 void HRTFDatabaseLoader::loadAsynchronously() |
| 91 { | 92 { |
| 92 ASSERT(isMainThread()); | 93 ASSERT(isMainThread()); |
| 93 | 94 |
| 94 MutexLocker locker(m_lock); | 95 // m_hrtfDatabase and m_thread should both be unset because this should be a |
| 95 if (!m_hrtfDatabase && !m_thread) { | 96 // new HRTFDatabaseLoader object that was just created by |
| 96 // Start the asynchronous database loading process. | 97 // createAndLoadAsynchronouslyIfNecessary and because we haven't started |
| 97 m_thread = wrapUnique(Platform::current()->createThread("HRTF database l
oader")); | 98 // loadTask yet for this object. |
| 98 // TODO(alexclarke): Should this be posted as a loading task? | 99 DCHECK(!m_hrtfDatabase); |
| 99 m_thread->getWebTaskRunner()->postTask(BLINK_FROM_HERE, crossThreadBind(
&HRTFDatabaseLoader::loadTask, crossThreadUnretained(this))); | 100 DCHECK(!m_thread); |
| 100 } | 101 |
| 102 // Start the asynchronous database loading process. |
| 103 m_thread = wrapUnique(Platform::current()->createThread("HRTF database loade
r")); |
| 104 // TODO(alexclarke): Should this be posted as a loading task? |
| 105 m_thread->getWebTaskRunner()->postTask(BLINK_FROM_HERE, crossThreadBind(&HRT
FDatabaseLoader::loadTask, crossThreadUnretained(this))); |
| 101 } | 106 } |
| 102 | 107 |
| 103 bool HRTFDatabaseLoader::isLoaded() | 108 HRTFDatabase* HRTFDatabaseLoader::database() |
| 104 { | 109 { |
| 105 MutexLocker locker(m_lock); | 110 DCHECK(!isMainThread()); |
| 111 |
| 112 // Seeing that this is only called from the audio thread, we can't block. |
| 113 // It's ok to return nullptr if we can't get the lock. |
| 114 MutexTryLocker tryLocker(m_lock); |
| 115 |
| 116 if (!tryLocker.locked()) |
| 117 return nullptr; |
| 118 |
| 106 return m_hrtfDatabase.get(); | 119 return m_hrtfDatabase.get(); |
| 107 } | 120 } |
| 108 | 121 |
| 109 // This cleanup task is needed just to make sure that the loader thread finishes | 122 // This cleanup task is needed just to make sure that the loader thread finishes |
| 110 // the load task and thus the loader thread doesn't touch m_thread any more. | 123 // the load task and thus the loader thread doesn't touch m_thread any more. |
| 111 void HRTFDatabaseLoader::cleanupTask(TaskSynchronizer* sync) | 124 void HRTFDatabaseLoader::cleanupTask(TaskSynchronizer* sync) |
| 112 { | 125 { |
| 113 sync->taskCompleted(); | 126 sync->taskCompleted(); |
| 114 } | 127 } |
| 115 | 128 |
| 116 void HRTFDatabaseLoader::waitForLoaderThreadCompletion() | 129 void HRTFDatabaseLoader::waitForLoaderThreadCompletion() |
| 117 { | 130 { |
| 118 if (!m_thread) | 131 if (!m_thread) |
| 119 return; | 132 return; |
| 120 | 133 |
| 121 TaskSynchronizer sync; | 134 TaskSynchronizer sync; |
| 122 // TODO(alexclarke): Should this be posted as a loading task? | 135 // TODO(alexclarke): Should this be posted as a loading task? |
| 123 m_thread->getWebTaskRunner()->postTask(BLINK_FROM_HERE, crossThreadBind(&HRT
FDatabaseLoader::cleanupTask, crossThreadUnretained(this), crossThreadUnretained
(&sync))); | 136 m_thread->getWebTaskRunner()->postTask(BLINK_FROM_HERE, crossThreadBind(&HRT
FDatabaseLoader::cleanupTask, crossThreadUnretained(this), crossThreadUnretained
(&sync))); |
| 124 sync.waitForTaskCompletion(); | 137 sync.waitForTaskCompletion(); |
| 125 m_thread.reset(); | 138 m_thread.reset(); |
| 126 } | 139 } |
| 127 | 140 |
| 128 } // namespace blink | 141 } // namespace blink |
| OLD | NEW |