| 1437 | } |
| 1438 | |
| 1439 | bool FlaxStorage::CloseFileHandles() |
| 1440 | { |
| 1441 | // Guard the whole process so if new thread wants to lock the chunks will need to wait for this to end |
| 1442 | Platform::InterlockedIncrement(&_isUnloadingData); |
| 1443 | SCOPE_EXIT{ Platform::InterlockedDecrement(&_isUnloadingData); }; |
| 1444 | |
| 1445 | if (Platform::AtomicRead(&_chunksLock) == 0 && Platform::AtomicRead(&_files) == 0) |
| 1446 | return false; // Early out when no files are opened |
| 1447 | PROFILE_CPU(); |
| 1448 | PROFILE_MEM(ContentFiles); |
| 1449 | |
| 1450 | // Note: this is usually called by the content manager when this file is not used or on exit |
| 1451 | // In those situations all the async tasks using this storage should be cancelled externally |
| 1452 | |
| 1453 | // Ensure that no one is using this resource |
| 1454 | int32 waitTime = 100; |
| 1455 | while (Platform::AtomicRead(&_chunksLock) != 0 && waitTime-- > 0) |
| 1456 | Platform::Sleep(1); |
| 1457 | if (Platform::AtomicRead(&_chunksLock) != 0) |
| 1458 | { |
| 1459 | // File can be locked by some streaming tasks (eg. AudioClip::StreamingTask or StreamModelLODTask) |
| 1460 | Entry e; |
| 1461 | for (int32 i = 0; i < GetEntriesCount(); i++) |
| 1462 | { |
| 1463 | GetEntry(i, e); |
| 1464 | Asset* asset = Content::GetAsset(e.ID); |
| 1465 | if (asset) |
| 1466 | { |
| 1467 | LOG(Info, "Canceling streaming for asset {0}", asset->ToString()); |
| 1468 | asset->CancelStreaming(); |
| 1469 | } |
| 1470 | } |
| 1471 | } |
| 1472 | waitTime = 100; |
| 1473 | while (Platform::AtomicRead(&_chunksLock) != 0 && waitTime-- > 0) |
| 1474 | Platform::Sleep(1); |
| 1475 | if (Platform::AtomicRead(&_chunksLock) != 0) |
| 1476 | return true; // Failed, someone is still accessing the file |
| 1477 | |
| 1478 | // Close file handles (from all threads) |
| 1479 | Array<FileReadStream*, InlinedAllocation<8>> streams; |
| 1480 | _file.GetValues(streams); |
| 1481 | for (FileReadStream* stream : streams) |
| 1482 | { |
| 1483 | if (stream) |
| 1484 | Delete(stream); |
| 1485 | } |
| 1486 | _file.Clear(); |
| 1487 | Platform::AtomicStore(&_files, 0); |
| 1488 | return false; |
| 1489 | } |
| 1490 | |
| 1491 | void FlaxStorage::Dispose() |
| 1492 | { |
no test coverage detected