Fix crash when existing engine while content streaming is active
This commit is contained in:
@@ -467,11 +467,13 @@ void Asset::CancelStreaming()
|
||||
{
|
||||
// Cancel loading task but go over asset locker to prevent case if other load threads still loads asset while it's reimported on other thread
|
||||
Locker.Lock();
|
||||
auto loadTask = (ContentLoadTask*)Platform::AtomicRead(&_loadingTask);
|
||||
auto loadingTask = (ContentLoadTask*)Platform::AtomicRead(&_loadingTask);
|
||||
Locker.Unlock();
|
||||
if (loadTask)
|
||||
if (loadingTask)
|
||||
{
|
||||
loadTask->Cancel();
|
||||
Platform::AtomicStore(&_loadingTask, 0);
|
||||
LOG(Warning, "Cancel loading task for \'{0}\'", ToString());
|
||||
loadingTask->Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -632,18 +634,11 @@ void Asset::onUnload_MainThread()
|
||||
|
||||
ASSERT(IsInMainThread());
|
||||
|
||||
// Cancel any streaming before calling OnUnloaded event
|
||||
CancelStreaming();
|
||||
|
||||
// Send event
|
||||
OnUnloaded(this);
|
||||
|
||||
// Check if is during loading
|
||||
auto loadingTask = (ContentLoadTask*)Platform::AtomicRead(&_loadingTask);
|
||||
if (loadingTask != nullptr)
|
||||
{
|
||||
// Cancel loading
|
||||
Platform::AtomicStore(&_loadingTask, 0);
|
||||
LOG(Warning, "Cancel loading task for \'{0}\'", ToString());
|
||||
loadingTask->Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
#if USE_EDITOR
|
||||
|
||||
@@ -34,6 +34,10 @@ public:
|
||||
|
||||
public:
|
||||
// [ContentLoadTask]
|
||||
String ToString() const override
|
||||
{
|
||||
return String::Format(TEXT("Load Asset Data Task ({}, {}, {})"), (int32)GetState(), _chunks, _asset ? _asset->GetPath() : String::Empty);
|
||||
}
|
||||
bool HasReference(Object* obj) const override
|
||||
{
|
||||
return obj == _asset;
|
||||
|
||||
@@ -322,15 +322,17 @@ class StreamTextureMipTask : public GPUUploadTextureMipTask
|
||||
{
|
||||
private:
|
||||
StreamingTexture* _streamingTexture;
|
||||
Task* _rootTask;
|
||||
FlaxStorage::LockData _dataLock;
|
||||
|
||||
public:
|
||||
StreamTextureMipTask(StreamingTexture* texture, int32 mipIndex)
|
||||
StreamTextureMipTask(StreamingTexture* texture, int32 mipIndex, Task* rootTask)
|
||||
: GPUUploadTextureMipTask(texture->GetTexture(), mipIndex, Span<byte>(nullptr, 0), 0, 0, false)
|
||||
, _streamingTexture(texture)
|
||||
, _rootTask(rootTask ? rootTask : this)
|
||||
, _dataLock(_streamingTexture->GetOwner()->LockData())
|
||||
{
|
||||
_streamingTexture->_streamingTasks.Add(this);
|
||||
_streamingTexture->_streamingTasks.Add(_rootTask);
|
||||
_texture.Released.Bind<StreamTextureMipTask, &StreamTextureMipTask::OnResourceReleased2>(this);
|
||||
}
|
||||
|
||||
@@ -341,7 +343,7 @@ private:
|
||||
if (_streamingTexture)
|
||||
{
|
||||
ScopeLock lock(_streamingTexture->GetOwner()->GetOwnerLocker());
|
||||
_streamingTexture->_streamingTasks.Remove(this);
|
||||
_streamingTexture->_streamingTasks.Remove(_rootTask);
|
||||
_streamingTexture = nullptr;
|
||||
}
|
||||
}
|
||||
@@ -393,7 +395,7 @@ protected:
|
||||
if (_streamingTexture)
|
||||
{
|
||||
ScopeLock lock(_streamingTexture->GetOwner()->GetOwnerLocker());
|
||||
_streamingTexture->_streamingTasks.Remove(this);
|
||||
_streamingTexture->_streamingTasks.Remove(_rootTask);
|
||||
_streamingTexture = nullptr;
|
||||
}
|
||||
|
||||
@@ -443,7 +445,7 @@ Task* StreamingTexture::CreateStreamingTask(int32 residency)
|
||||
|
||||
// Add upload data task
|
||||
const int32 allocatedMipIndex = TotalIndexToTextureMipIndex(mipIndex);
|
||||
task = New<StreamTextureMipTask>(this, allocatedMipIndex);
|
||||
task = New<StreamTextureMipTask>(this, allocatedMipIndex, result);
|
||||
if (result)
|
||||
result->ContinueWith(task);
|
||||
else
|
||||
|
||||
@@ -208,8 +208,8 @@ void Task::OnCancel()
|
||||
if (IsRunning())
|
||||
{
|
||||
// Wait for it a little bit
|
||||
const double timeout = 2000.0;
|
||||
LOG(Warning, "Cannot cancel \'{0}\' because it's still running, waiting for end with timeout: {1} ms", ToString(), timeout);
|
||||
constexpr double timeout = 10000.0; // 10s
|
||||
LOG(Warning, "Cannot cancel \'{0}\' because it's still running, waiting for end with timeout: {1}ms", ToString(), timeout);
|
||||
Wait(timeout);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user