| 390 | } |
| 391 | |
| 392 | void PleReader::close() { |
| 393 | Impl& m = *impl_; |
| 394 | if (m.worker.joinable()) { |
| 395 | { |
| 396 | std::lock_guard<std::mutex> lk(m.mu); |
| 397 | m.stop = true; |
| 398 | m.queue.clear(); // unsubmitted work is dropped; in-flight reads still drain |
| 399 | } |
| 400 | m.cv_work.notify_all(); |
| 401 | m.file.wake(); |
| 402 | m.worker.join(); |
| 403 | } |
| 404 | // Outstanding reads must finish before their buffers are released (caller-thread mode, or a worker that |
| 405 | // stopped on an error). |
| 406 | if (m.file.is_open()) { |
| 407 | while (m.free_slots.size() < m.max_inflight) { |
| 408 | Completion c[64]; |
| 409 | const int n = m.file.wait(c, 64, -1); |
| 410 | if (n == 0) break; |
| 411 | for (int i = 0; i < n; ++i) |
| 412 | if (c[i].tag != DirectFile::WAKE_TAG) m.free_slots.push_back((uint32_t) c[i].tag); |
| 413 | } |
| 414 | } |
| 415 | m.file.close(); |
| 416 | DirectFile::free_aligned(m.slab); |
| 417 | m.slab = nullptr; |
| 418 | m.queue.clear(); |
| 419 | m.tickets.clear(); |
| 420 | m.delayed.clear(); |
| 421 | m.inflight.clear(); |
| 422 | m.free_slots.clear(); |
| 423 | m.cache.init(0); |
| 424 | m.threaded = false; |
| 425 | m.stop = false; |
| 426 | m.keep_us = 0; |
| 427 | m.last_issue_us = m.last_read_us = 0; |
| 428 | } |
| 429 | |
| 430 | bool PleReader::is_open() const { return impl_->file.is_open(); } |
| 431 | uint32_t PleReader::row_bytes() const { return impl_->row_bytes; } |