mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Reviewed By: lexs Differential Revision: D2982364 fb-gh-sync-id: 25f099377acca75298768072d38d77dcb9c1bff3 shipit-source-id: 25f099377acca75298768072d38d77dcb9c1bff3
40 lines
949 B
C++
40 lines
949 B
C++
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
#pragma once
|
|
|
|
#include <condition_variable>
|
|
#include <functional>
|
|
#include <mutex>
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
class MessageQueueThread {
|
|
public:
|
|
virtual ~MessageQueueThread() {}
|
|
virtual void runOnQueue(std::function<void()>&&) = 0;
|
|
virtual bool isOnThread() = 0;
|
|
// quitSynchronous() should synchronously ensure that no further tasks will run on the queue.
|
|
virtual void quitSynchronous() = 0;
|
|
|
|
void runOnQueueSync(std::function<void()>&& runnable) {
|
|
std::mutex signalMutex;
|
|
std::condition_variable signalCv;
|
|
bool runnableComplete = false;
|
|
|
|
runOnQueue([&] () mutable {
|
|
std::lock_guard<std::mutex> lock(signalMutex);
|
|
|
|
runnable();
|
|
runnableComplete = true;
|
|
|
|
signalCv.notify_one();
|
|
});
|
|
|
|
std::unique_lock<std::mutex> lock(signalMutex);
|
|
signalCv.wait(lock, [&runnableComplete] { return runnableComplete; });
|
|
}
|
|
};
|
|
|
|
}}
|