non-blocking http
This commit is contained in:
@@ -50,6 +50,8 @@ void RuntimeLayerController::Stop()
|
|||||||
|
|
||||||
ControlActionResult RuntimeLayerController::HandleAddLayer(const std::string& body)
|
ControlActionResult RuntimeLayerController::HandleAddLayer(const std::string& body)
|
||||||
{
|
{
|
||||||
|
CleanupRetiredShaderBuilds();
|
||||||
|
|
||||||
std::string shaderId;
|
std::string shaderId;
|
||||||
std::string error;
|
std::string error;
|
||||||
if (!ExtractStringField(body, "shaderId", shaderId, error))
|
if (!ExtractStringField(body, "shaderId", shaderId, error))
|
||||||
@@ -69,6 +71,8 @@ ControlActionResult RuntimeLayerController::HandleAddLayer(const std::string& bo
|
|||||||
|
|
||||||
ControlActionResult RuntimeLayerController::HandleRemoveLayer(const std::string& body)
|
ControlActionResult RuntimeLayerController::HandleRemoveLayer(const std::string& body)
|
||||||
{
|
{
|
||||||
|
CleanupRetiredShaderBuilds();
|
||||||
|
|
||||||
std::string layerId;
|
std::string layerId;
|
||||||
std::string error;
|
std::string error;
|
||||||
if (!ExtractStringField(body, "layerId", layerId, error))
|
if (!ExtractStringField(body, "layerId", layerId, error))
|
||||||
@@ -81,7 +85,7 @@ ControlActionResult RuntimeLayerController::HandleRemoveLayer(const std::string&
|
|||||||
}
|
}
|
||||||
|
|
||||||
Log("runtime-shader", "Layer removed: " + layerId);
|
Log("runtime-shader", "Layer removed: " + layerId);
|
||||||
StopLayerShaderBuild(layerId);
|
RetireLayerShaderBuild(layerId);
|
||||||
PublishRuntimeRenderLayers();
|
PublishRuntimeRenderLayers();
|
||||||
return { true, std::string() };
|
return { true, std::string() };
|
||||||
}
|
}
|
||||||
@@ -125,6 +129,9 @@ void RuntimeLayerController::InitializeLayerModel(std::string& runtimeShaderId)
|
|||||||
|
|
||||||
void RuntimeLayerController::StartLayerShaderBuild(const std::string& layerId, const std::string& shaderId)
|
void RuntimeLayerController::StartLayerShaderBuild(const std::string& layerId, const std::string& shaderId)
|
||||||
{
|
{
|
||||||
|
CleanupRetiredShaderBuilds();
|
||||||
|
RetireLayerShaderBuild(layerId);
|
||||||
|
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mRuntimeLayerMutex);
|
std::lock_guard<std::mutex> lock(mRuntimeLayerMutex);
|
||||||
std::string error;
|
std::string error;
|
||||||
@@ -135,9 +142,6 @@ void RuntimeLayerController::StartLayerShaderBuild(const std::string& layerId, c
|
|||||||
RuntimeShaderBridge* bridgePtr = bridge.get();
|
RuntimeShaderBridge* bridgePtr = bridge.get();
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mShaderBuildMutex);
|
std::lock_guard<std::mutex> lock(mShaderBuildMutex);
|
||||||
auto existingIt = mShaderBuilds.find(layerId);
|
|
||||||
if (existingIt != mShaderBuilds.end())
|
|
||||||
existingIt->second->Stop();
|
|
||||||
mShaderBuilds[layerId] = std::move(bridge);
|
mShaderBuilds[layerId] = std::move(bridge);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,7 +158,7 @@ void RuntimeLayerController::StartLayerShaderBuild(const std::string& layerId, c
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void RuntimeLayerController::StopLayerShaderBuild(const std::string& layerId)
|
void RuntimeLayerController::RetireLayerShaderBuild(const std::string& layerId)
|
||||||
{
|
{
|
||||||
std::unique_ptr<RuntimeShaderBridge> bridge;
|
std::unique_ptr<RuntimeShaderBridge> bridge;
|
||||||
{
|
{
|
||||||
@@ -164,19 +168,45 @@ void RuntimeLayerController::StopLayerShaderBuild(const std::string& layerId)
|
|||||||
return;
|
return;
|
||||||
bridge = std::move(bridgeIt->second);
|
bridge = std::move(bridgeIt->second);
|
||||||
mShaderBuilds.erase(bridgeIt);
|
mShaderBuilds.erase(bridgeIt);
|
||||||
|
bridge->RequestStop();
|
||||||
|
mRetiredShaderBuilds.push_back(std::move(bridge));
|
||||||
}
|
}
|
||||||
bridge->Stop();
|
}
|
||||||
|
|
||||||
|
void RuntimeLayerController::CleanupRetiredShaderBuilds()
|
||||||
|
{
|
||||||
|
std::vector<std::unique_ptr<RuntimeShaderBridge>> readyToStop;
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mShaderBuildMutex);
|
||||||
|
for (auto it = mRetiredShaderBuilds.begin(); it != mRetiredShaderBuilds.end();)
|
||||||
|
{
|
||||||
|
if ((*it)->CanStopWithoutWaiting())
|
||||||
|
{
|
||||||
|
readyToStop.push_back(std::move(*it));
|
||||||
|
it = mRetiredShaderBuilds.erase(it);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (std::unique_ptr<RuntimeShaderBridge>& bridge : readyToStop)
|
||||||
|
bridge->Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
void RuntimeLayerController::StopAllRuntimeShaderBuilds()
|
void RuntimeLayerController::StopAllRuntimeShaderBuilds()
|
||||||
{
|
{
|
||||||
std::map<std::string, std::unique_ptr<RuntimeShaderBridge>> builds;
|
std::map<std::string, std::unique_ptr<RuntimeShaderBridge>> builds;
|
||||||
|
std::vector<std::unique_ptr<RuntimeShaderBridge>> retiredBuilds;
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mShaderBuildMutex);
|
std::lock_guard<std::mutex> lock(mShaderBuildMutex);
|
||||||
builds.swap(mShaderBuilds);
|
builds.swap(mShaderBuilds);
|
||||||
|
retiredBuilds.swap(mRetiredShaderBuilds);
|
||||||
}
|
}
|
||||||
for (auto& entry : builds)
|
for (auto& entry : builds)
|
||||||
entry.second->Stop();
|
entry.second->Stop();
|
||||||
|
for (auto& bridge : retiredBuilds)
|
||||||
|
bridge->Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
void RuntimeLayerController::PublishRuntimeRenderLayers()
|
void RuntimeLayerController::PublishRuntimeRenderLayers()
|
||||||
|
|||||||
@@ -40,7 +40,8 @@ private:
|
|||||||
void LoadSupportedShaderCatalog(const std::string& shaderLibrary, unsigned maxTemporalHistoryFrames);
|
void LoadSupportedShaderCatalog(const std::string& shaderLibrary, unsigned maxTemporalHistoryFrames);
|
||||||
void InitializeLayerModel(std::string& runtimeShaderId);
|
void InitializeLayerModel(std::string& runtimeShaderId);
|
||||||
void StartLayerShaderBuild(const std::string& layerId, const std::string& shaderId);
|
void StartLayerShaderBuild(const std::string& layerId, const std::string& shaderId);
|
||||||
void StopLayerShaderBuild(const std::string& layerId);
|
void RetireLayerShaderBuild(const std::string& layerId);
|
||||||
|
void CleanupRetiredShaderBuilds();
|
||||||
void StopAllRuntimeShaderBuilds();
|
void StopAllRuntimeShaderBuilds();
|
||||||
void PublishRuntimeRenderLayers();
|
void PublishRuntimeRenderLayers();
|
||||||
bool MarkRuntimeBuildReady(const RuntimeShaderArtifact& artifact);
|
bool MarkRuntimeBuildReady(const RuntimeShaderArtifact& artifact);
|
||||||
@@ -55,5 +56,6 @@ private:
|
|||||||
RuntimeLayerModel mRuntimeLayerModel;
|
RuntimeLayerModel mRuntimeLayerModel;
|
||||||
std::mutex mShaderBuildMutex;
|
std::mutex mShaderBuildMutex;
|
||||||
std::map<std::string, std::unique_ptr<RuntimeShaderBridge>> mShaderBuilds;
|
std::map<std::string, std::unique_ptr<RuntimeShaderBridge>> mShaderBuilds;
|
||||||
|
std::vector<std::unique_ptr<RuntimeShaderBridge>> mRetiredShaderBuilds;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,19 +22,31 @@ void RuntimeShaderBridge::Start(const std::string& layerId, const std::string& s
|
|||||||
mOnArtifactReady = std::move(onArtifactReady);
|
mOnArtifactReady = std::move(onArtifactReady);
|
||||||
mOnError = std::move(onError);
|
mOnError = std::move(onError);
|
||||||
mStopping.store(false, std::memory_order_release);
|
mStopping.store(false, std::memory_order_release);
|
||||||
|
mFinished.store(false, std::memory_order_release);
|
||||||
mCompiler.StartShaderBuild(shaderId);
|
mCompiler.StartShaderBuild(shaderId);
|
||||||
mThread = std::thread([this]() { ThreadMain(); });
|
mThread = std::thread([this]() { ThreadMain(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
void RuntimeShaderBridge::Stop()
|
void RuntimeShaderBridge::RequestStop()
|
||||||
{
|
{
|
||||||
mStopping.store(true, std::memory_order_release);
|
mStopping.store(true, std::memory_order_release);
|
||||||
|
}
|
||||||
|
|
||||||
|
void RuntimeShaderBridge::Stop()
|
||||||
|
{
|
||||||
|
RequestStop();
|
||||||
if (mThread.joinable())
|
if (mThread.joinable())
|
||||||
mThread.join();
|
mThread.join();
|
||||||
mCompiler.Stop();
|
mCompiler.Stop();
|
||||||
mLayerId.clear();
|
mLayerId.clear();
|
||||||
mOnArtifactReady = ArtifactCallback();
|
mOnArtifactReady = ArtifactCallback();
|
||||||
mOnError = ErrorCallback();
|
mOnError = ErrorCallback();
|
||||||
|
mFinished.store(true, std::memory_order_release);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RuntimeShaderBridge::CanStopWithoutWaiting() const
|
||||||
|
{
|
||||||
|
return mFinished.load(std::memory_order_acquire) && !mCompiler.Running();
|
||||||
}
|
}
|
||||||
|
|
||||||
void RuntimeShaderBridge::ThreadMain()
|
void RuntimeShaderBridge::ThreadMain()
|
||||||
@@ -54,8 +66,10 @@ void RuntimeShaderBridge::ThreadMain()
|
|||||||
{
|
{
|
||||||
mOnError(build.message);
|
mOnError(build.message);
|
||||||
}
|
}
|
||||||
|
mFinished.store(true, std::memory_order_release);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||||
}
|
}
|
||||||
|
mFinished.store(true, std::memory_order_release);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,9 @@ public:
|
|||||||
|
|
||||||
void Start(const std::string& shaderId, ArtifactCallback onArtifactReady, ErrorCallback onError);
|
void Start(const std::string& shaderId, ArtifactCallback onArtifactReady, ErrorCallback onError);
|
||||||
void Start(const std::string& layerId, const std::string& shaderId, ArtifactCallback onArtifactReady, ErrorCallback onError);
|
void Start(const std::string& layerId, const std::string& shaderId, ArtifactCallback onArtifactReady, ErrorCallback onError);
|
||||||
|
void RequestStop();
|
||||||
void Stop();
|
void Stop();
|
||||||
|
bool CanStopWithoutWaiting() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void ThreadMain();
|
void ThreadMain();
|
||||||
@@ -29,6 +31,7 @@ private:
|
|||||||
RuntimeSlangShaderCompiler mCompiler;
|
RuntimeSlangShaderCompiler mCompiler;
|
||||||
std::thread mThread;
|
std::thread mThread;
|
||||||
std::atomic<bool> mStopping{ false };
|
std::atomic<bool> mStopping{ false };
|
||||||
|
std::atomic<bool> mFinished{ true };
|
||||||
std::string mLayerId;
|
std::string mLayerId;
|
||||||
ArtifactCallback mOnArtifactReady;
|
ArtifactCallback mOnArtifactReady;
|
||||||
ErrorCallback mOnError;
|
ErrorCallback mOnError;
|
||||||
|
|||||||
Reference in New Issue
Block a user