mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-08 12:31:51 +00:00
Merge branch 'main' into volume-renderer
This commit is contained in:
commit
d9a7afad05
42 changed files with 539 additions and 196 deletions
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <optional>
|
||||
#include <condition_variable>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
|
|
@ -48,6 +49,7 @@ namespace util {
|
|||
std::atomic<int> busyWorkers = 0;
|
||||
std::atomic<uint> jobsDone = 0;
|
||||
std::atomic<bool> working = true;
|
||||
supplier<std::optional<T>> jobsSource = nullptr;
|
||||
bool failed = false;
|
||||
bool standaloneResults = true;
|
||||
bool stopOnFail = true;
|
||||
|
|
@ -151,6 +153,10 @@ namespace util {
|
|||
return working;
|
||||
}
|
||||
|
||||
void setJobsSource(supplier<std::optional<T>>&& source) {
|
||||
jobsSource = std::move(source);
|
||||
}
|
||||
|
||||
void terminate() override {
|
||||
if (!working) {
|
||||
return;
|
||||
|
|
@ -227,6 +233,22 @@ namespace util {
|
|||
}
|
||||
}
|
||||
}
|
||||
if (jobsSource) {
|
||||
bool jobsAdded = false;
|
||||
std::lock_guard<std::mutex> jobsLock(jobsMutex);
|
||||
while (true) {
|
||||
auto job = jobsSource();
|
||||
if (job.has_value()) {
|
||||
jobs.push(std::move(job.value()));
|
||||
jobsAdded = true;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (jobsAdded) {
|
||||
jobsMutexCondition.notify_one();
|
||||
}
|
||||
}
|
||||
if (failed) {
|
||||
throw std::runtime_error("some job failed");
|
||||
}
|
||||
|
|
|
|||
76
src/util/array_queue.hpp
Normal file
76
src/util/array_queue.hpp
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace util {
|
||||
template <typename T>
|
||||
class array_queue {
|
||||
public:
|
||||
array_queue(size_t initCapacity = 8)
|
||||
: _capacity(initCapacity),
|
||||
_size(0),
|
||||
_front(0),
|
||||
_back(0),
|
||||
_data(std::make_unique<T[]>(_capacity)) {
|
||||
if (initCapacity == 0 || (initCapacity & (initCapacity - 1)) != 0) {
|
||||
throw std::invalid_argument("initCapacity must be positive power of 2");
|
||||
}
|
||||
}
|
||||
|
||||
~array_queue() {
|
||||
while (!empty()) {
|
||||
pop();
|
||||
}
|
||||
}
|
||||
|
||||
void push(T&& value) {
|
||||
if (_back == _front && _size) {
|
||||
grow();
|
||||
}
|
||||
_data[_back] = std::forward<T>(value);
|
||||
_back = (_back + 1) & (_capacity - 1);
|
||||
++_size;
|
||||
}
|
||||
|
||||
T& front() {
|
||||
return _data[_front];
|
||||
}
|
||||
|
||||
void pop() {
|
||||
_data[_front].~T();
|
||||
_front = (_front + 1) & (_capacity - 1);
|
||||
--_size;
|
||||
}
|
||||
|
||||
bool empty() const {
|
||||
return _size == 0;
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
return _size;
|
||||
}
|
||||
|
||||
private:
|
||||
size_t _capacity;
|
||||
size_t _size;
|
||||
size_t _front;
|
||||
size_t _back;
|
||||
std::unique_ptr<T[]> _data;
|
||||
|
||||
void grow() {
|
||||
size_t newCapacity = _capacity * 2;
|
||||
auto newData = std::make_unique<T[]>(newCapacity);
|
||||
|
||||
std::move(_data.get() + _front, _data.get() + _capacity, newData.get());
|
||||
std::move(_data.get(), _data.get() + _front, newData.get() + (_capacity - _front));
|
||||
|
||||
_back = _size;
|
||||
_front = 0;
|
||||
|
||||
_data.reset(newData.release());
|
||||
_capacity = newCapacity;
|
||||
}
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue