-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #825 from CesiumGS/wait-in-main-thread
Add AsyncSystem::waitInMainThread
- Loading branch information
Showing
7 changed files
with
260 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,66 @@ | ||
#include "CesiumAsync/Impl/QueuedScheduler.h" | ||
|
||
using namespace CesiumAsync::CesiumImpl; | ||
#include <condition_variable> | ||
#include <mutex> | ||
|
||
// Hackily use Async++'s internal fifo_queue. We could copy it instead - it's | ||
// not much code - but why create the duplication? However, we are assuming that | ||
// Async++'s source (not just headers) are available while building | ||
// cesium-native. If that's a problem in some context, we'll need to do that | ||
// duplication of fifo_queue after all. | ||
#include <async++/../../src/fifo_queue.h> | ||
|
||
namespace CesiumAsync::CesiumImpl { | ||
|
||
struct QueuedScheduler::Impl { | ||
async::detail::fifo_queue queue; | ||
std::mutex mutex; | ||
std::condition_variable conditionVariable; | ||
}; | ||
|
||
QueuedScheduler::QueuedScheduler() : _pImpl(std::make_unique<Impl>()) {} | ||
QueuedScheduler::~QueuedScheduler() = default; | ||
|
||
void QueuedScheduler::schedule(async::task_run_handle t) { | ||
this->_scheduler.schedule(std::move(t)); | ||
std::unique_lock<std::mutex> guard(this->_pImpl->mutex); | ||
this->_pImpl->queue.push(std::move(t)); | ||
|
||
// Notify listeners that there is new work. | ||
this->_pImpl->conditionVariable.notify_all(); | ||
} | ||
|
||
void QueuedScheduler::dispatchQueuedContinuations() { | ||
auto scope = this->immediate.scope(); | ||
this->_scheduler.run_all_tasks(); | ||
while (this->dispatchZeroOrOneContinuation()) { | ||
} | ||
} | ||
|
||
bool QueuedScheduler::dispatchZeroOrOneContinuation() { | ||
auto scope = this->immediate.scope(); | ||
return this->_scheduler.try_run_one_task(); | ||
return this->dispatchInternal(false); | ||
} | ||
|
||
bool QueuedScheduler::dispatchInternal(bool blockIfNoTasks) { | ||
async::task_run_handle t; | ||
|
||
{ | ||
std::unique_lock<std::mutex> guard(this->_pImpl->mutex); | ||
t = this->_pImpl->queue.pop(); | ||
if (blockIfNoTasks && !t) { | ||
this->_pImpl->conditionVariable.wait(guard); | ||
} | ||
} | ||
|
||
if (t) { | ||
auto scope = this->immediate.scope(); | ||
t.run(); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
void QueuedScheduler::unblock() { | ||
std::unique_lock<std::mutex> guard(this->_pImpl->mutex); | ||
this->_pImpl->conditionVariable.notify_all(); | ||
} | ||
|
||
} // namespace CesiumAsync::CesiumImpl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters