2026-05-28 15:37:18 +10:00
|
|
|
/********************************************************************************
|
|
|
|
|
* *
|
|
|
|
|
* This file is part of IfcOpenShell. *
|
|
|
|
|
* *
|
|
|
|
|
* IfcOpenShell is free software: you can redistribute it and/or modify *
|
|
|
|
|
* it under the terms of the Lesser GNU General Public License as published by *
|
|
|
|
|
* the Free Software Foundation, either version 3.0 of the License, or *
|
|
|
|
|
* (at your option) any later version. *
|
|
|
|
|
* *
|
|
|
|
|
* IfcOpenShell is distributed in the hope that it will be useful, *
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
|
* Lesser GNU General Public License for more details. *
|
|
|
|
|
* *
|
|
|
|
|
* You should have received a copy of the Lesser GNU General Public License *
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
|
* *
|
|
|
|
|
********************************************************************************/
|
|
|
|
|
|
2026-06-04 11:11:14 +10:00
|
|
|
#include "StreamingThread.h"
|
2026-05-28 15:37:18 +10:00
|
|
|
|
2026-06-04 11:11:14 +10:00
|
|
|
#include "StreamingLoader.h"
|
2026-05-28 15:37:18 +10:00
|
|
|
|
2026-06-04 11:11:14 +10:00
|
|
|
StreamingThread::~StreamingThread() {
|
2026-05-28 15:37:18 +10:00
|
|
|
stop();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 11:11:14 +10:00
|
|
|
void StreamingThread::start() {
|
2026-07-03 08:38:57 +10:00
|
|
|
std::unique_lock lock(mu_);
|
2026-05-28 15:37:18 +10:00
|
|
|
if (running_) return;
|
|
|
|
|
shutdown_ = false;
|
|
|
|
|
running_ = true;
|
2026-07-03 08:38:57 +10:00
|
|
|
lock.unlock();
|
2026-06-04 11:11:14 +10:00
|
|
|
worker_ = std::thread(&StreamingThread::workerLoop, this);
|
2026-05-28 15:37:18 +10:00
|
|
|
}
|
|
|
|
|
|
2026-06-04 11:11:14 +10:00
|
|
|
void StreamingThread::stop() {
|
2026-05-28 15:37:18 +10:00
|
|
|
{
|
2026-07-03 08:38:57 +10:00
|
|
|
std::unique_lock lock(mu_);
|
2026-05-28 15:37:18 +10:00
|
|
|
if (!running_) return;
|
|
|
|
|
shutdown_ = true;
|
|
|
|
|
}
|
|
|
|
|
cv_.notify_all();
|
|
|
|
|
if (worker_.joinable()) worker_.join();
|
2026-07-03 08:38:57 +10:00
|
|
|
std::unique_lock lock(mu_);
|
2026-05-28 15:37:18 +10:00
|
|
|
running_ = false;
|
|
|
|
|
requests_.clear();
|
|
|
|
|
results_.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 11:11:14 +10:00
|
|
|
bool StreamingThread::enqueue(Request req) {
|
2026-05-28 15:37:18 +10:00
|
|
|
{
|
2026-07-03 08:38:57 +10:00
|
|
|
std::unique_lock lock(mu_);
|
2026-05-28 15:37:18 +10:00
|
|
|
if (!running_ || shutdown_) return false;
|
|
|
|
|
requests_.push_back(std::move(req));
|
|
|
|
|
}
|
|
|
|
|
cv_.notify_one();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 11:11:14 +10:00
|
|
|
std::vector<StreamingThread::Result> StreamingThread::drainResults() {
|
2026-07-03 08:38:57 +10:00
|
|
|
std::vector<Result> results;
|
2026-05-28 15:37:18 +10:00
|
|
|
{
|
2026-07-03 08:38:57 +10:00
|
|
|
std::unique_lock lock(mu_);
|
|
|
|
|
results.reserve(results_.size());
|
2026-05-28 15:37:18 +10:00
|
|
|
while (!results_.empty()) {
|
2026-07-03 08:38:57 +10:00
|
|
|
results.push_back(std::move(results_.front()));
|
2026-05-28 15:37:18 +10:00
|
|
|
results_.pop_front();
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-03 08:38:57 +10:00
|
|
|
return results;
|
2026-05-28 15:37:18 +10:00
|
|
|
}
|
|
|
|
|
|
2026-06-04 11:11:14 +10:00
|
|
|
std::size_t StreamingThread::inFlightApprox() const {
|
2026-07-03 08:38:57 +10:00
|
|
|
std::unique_lock lock(mu_);
|
2026-05-28 15:37:18 +10:00
|
|
|
return requests_.size() + (in_progress_ ? 1u : 0u);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 11:11:14 +10:00
|
|
|
void StreamingThread::workerLoop() {
|
2026-05-28 15:37:18 +10:00
|
|
|
for (;;) {
|
|
|
|
|
Request req;
|
|
|
|
|
{
|
2026-07-03 08:38:57 +10:00
|
|
|
std::unique_lock lock(mu_);
|
|
|
|
|
cv_.wait(lock, [this]() { return shutdown_ || !requests_.empty(); });
|
2026-05-28 15:37:18 +10:00
|
|
|
if (shutdown_ && requests_.empty()) return;
|
|
|
|
|
req = std::move(requests_.front());
|
|
|
|
|
requests_.pop_front();
|
|
|
|
|
in_progress_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Disk reads happen off-thread. Each Request carries everything
|
|
|
|
|
// the reader needs; the viewport keeps the corresponding chunk
|
|
|
|
|
// marked is_loading so eviction won't yank the slot underneath
|
|
|
|
|
// us. The vbytes / idx buffers are allocated here on the worker
|
|
|
|
|
// thread — they cross back to the main thread when the result
|
|
|
|
|
// is drained and applied (pool.alloc + queueWriteBuffer).
|
2026-07-03 08:38:57 +10:00
|
|
|
Result result;
|
|
|
|
|
result.model_id = req.model_id;
|
|
|
|
|
result.chunk_idx = req.chunk_idx;
|
|
|
|
|
result.success = readChunkGeometryCompressed(
|
2026-07-02 10:24:59 +10:00
|
|
|
req.file_path, req.geometry_section_offset,
|
|
|
|
|
req.v_comp_off, req.v_comp_size, req.v_raw_size,
|
|
|
|
|
req.i_comp_off, req.i_comp_size, req.i_raw_size,
|
2026-07-03 08:38:57 +10:00
|
|
|
result.vbytes, result.idx);
|
2026-05-28 15:37:18 +10:00
|
|
|
|
|
|
|
|
{
|
2026-07-03 08:38:57 +10:00
|
|
|
std::unique_lock lock(mu_);
|
|
|
|
|
results_.push_back(std::move(result));
|
2026-05-28 15:37:18 +10:00
|
|
|
in_progress_ = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|