mirror of
https://github.com/enduro2d/enduro2d.git
synced 2025-12-13 15:48:11 +07:00
update modules
This commit is contained in:
399
headers/3rdparty/promise.hpp/jobber.hpp
vendored
Normal file
399
headers/3rdparty/promise.hpp/jobber.hpp
vendored
Normal file
@@ -0,0 +1,399 @@
|
||||
/*******************************************************************************
|
||||
* This file is part of the "promise.hpp"
|
||||
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
||||
* Copyright (C) 2018 Matvey Cherevko
|
||||
******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cassert>
|
||||
|
||||
#include <tuple>
|
||||
#include <mutex>
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <exception>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
#include <condition_variable>
|
||||
|
||||
#include "promise.hpp"
|
||||
|
||||
namespace jobber_hpp
|
||||
{
|
||||
using namespace promise_hpp;
|
||||
|
||||
enum class jobber_priority {
|
||||
lowest,
|
||||
below_normal,
|
||||
normal,
|
||||
above_normal,
|
||||
highest
|
||||
};
|
||||
|
||||
enum class jobber_wait_status {
|
||||
no_timeout,
|
||||
cancelled,
|
||||
timeout
|
||||
};
|
||||
|
||||
class jobber_cancelled_exception : public std::runtime_error {
|
||||
public:
|
||||
jobber_cancelled_exception()
|
||||
: std::runtime_error("jobber has stopped working") {}
|
||||
};
|
||||
|
||||
class jobber final : private detail::noncopyable {
|
||||
public:
|
||||
explicit jobber(std::size_t threads);
|
||||
~jobber() noexcept;
|
||||
|
||||
template < typename F, typename... Args >
|
||||
using async_invoke_result_t = invoke_hpp::invoke_result_t<
|
||||
std::decay_t<F>,
|
||||
std::decay_t<Args>...>;
|
||||
|
||||
template < typename F, typename... Args
|
||||
, typename R = async_invoke_result_t<F, Args...> >
|
||||
promise<R> async(F&& f, Args&&... args);
|
||||
|
||||
template < typename F, typename... Args
|
||||
, typename R = async_invoke_result_t<F, Args...> >
|
||||
promise<R> async(jobber_priority priority, F&& f, Args&&... args);
|
||||
|
||||
void pause() noexcept;
|
||||
void resume() noexcept;
|
||||
bool is_paused() const noexcept;
|
||||
|
||||
jobber_wait_status wait_all() const noexcept;
|
||||
jobber_wait_status active_wait_all() noexcept;
|
||||
|
||||
template < typename Rep, typename Period >
|
||||
jobber_wait_status wait_all_for(
|
||||
const std::chrono::duration<Rep, Period>& timeout_duration) const;
|
||||
|
||||
template < typename Clock, typename Duration >
|
||||
jobber_wait_status wait_all_until(
|
||||
const std::chrono::time_point<Clock, Duration>& timeout_time) const;
|
||||
|
||||
template < typename Rep, typename Period >
|
||||
jobber_wait_status active_wait_all_for(
|
||||
const std::chrono::duration<Rep, Period>& timeout_duration);
|
||||
|
||||
template < typename Clock, typename Duration >
|
||||
jobber_wait_status active_wait_all_until(
|
||||
const std::chrono::time_point<Clock, Duration>& timeout_time);
|
||||
private:
|
||||
class task;
|
||||
using task_ptr = std::unique_ptr<task>;
|
||||
template < typename R, typename F, typename... Args >
|
||||
class concrete_task;
|
||||
private:
|
||||
void push_task_(jobber_priority priority, task_ptr task);
|
||||
task_ptr pop_task_() noexcept;
|
||||
void shutdown_() noexcept;
|
||||
void worker_main_() noexcept;
|
||||
void process_task_(std::unique_lock<std::mutex> lock) noexcept;
|
||||
private:
|
||||
std::vector<std::thread> threads_;
|
||||
std::vector<std::pair<jobber_priority, task_ptr>> tasks_;
|
||||
std::atomic<bool> paused_{false};
|
||||
std::atomic<bool> cancelled_{false};
|
||||
std::atomic<std::size_t> active_task_count_{0};
|
||||
mutable std::mutex tasks_mutex_;
|
||||
mutable std::condition_variable cond_var_;
|
||||
};
|
||||
|
||||
class jobber::task : private noncopyable {
|
||||
public:
|
||||
virtual ~task() noexcept = default;
|
||||
virtual void run() noexcept = 0;
|
||||
virtual void cancel() noexcept = 0;
|
||||
};
|
||||
|
||||
template < typename R, typename F, typename... Args >
|
||||
class jobber::concrete_task : public task {
|
||||
F f_;
|
||||
std::tuple<Args...> args_;
|
||||
promise<R> promise_;
|
||||
public:
|
||||
template < typename U >
|
||||
concrete_task(U&& u, std::tuple<Args...>&& args);
|
||||
void run() noexcept final;
|
||||
void cancel() noexcept final;
|
||||
promise<R> future() noexcept;
|
||||
};
|
||||
|
||||
template < typename F, typename... Args >
|
||||
class jobber::concrete_task<void, F, Args...> : public task {
|
||||
F f_;
|
||||
std::tuple<Args...> args_;
|
||||
promise<void> promise_;
|
||||
public:
|
||||
template < typename U >
|
||||
concrete_task(U&& u, std::tuple<Args...>&& args);
|
||||
void run() noexcept final;
|
||||
void cancel() noexcept final;
|
||||
promise<void> future() noexcept;
|
||||
};
|
||||
}
|
||||
|
||||
namespace jobber_hpp
|
||||
{
|
||||
inline jobber::jobber(std::size_t threads) {
|
||||
try {
|
||||
threads_.resize(threads);
|
||||
for ( std::thread& thread : threads_ ) {
|
||||
thread = std::thread(&jobber::worker_main_, this);
|
||||
}
|
||||
} catch (...) {
|
||||
shutdown_();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
inline jobber::~jobber() noexcept {
|
||||
shutdown_();
|
||||
}
|
||||
|
||||
template < typename F, typename... Args, typename R >
|
||||
promise<R> jobber::async(F&& f, Args&&... args) {
|
||||
return async(
|
||||
jobber_priority::normal,
|
||||
std::forward<F>(f),
|
||||
std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template < typename F, typename... Args, typename R >
|
||||
promise<R> jobber::async(jobber_priority priority, F&& f, Args&&... args) {
|
||||
using task_t = concrete_task<
|
||||
R,
|
||||
std::decay_t<F>,
|
||||
std::decay_t<Args>...>;
|
||||
std::unique_ptr<task_t> task = std::make_unique<task_t>(
|
||||
std::forward<F>(f),
|
||||
std::make_tuple(std::forward<Args>(args)...));
|
||||
promise<R> future = task->future();
|
||||
std::lock_guard<std::mutex> guard(tasks_mutex_);
|
||||
push_task_(priority, std::move(task));
|
||||
return future;
|
||||
}
|
||||
|
||||
inline void jobber::pause() noexcept {
|
||||
std::lock_guard<std::mutex> guard(tasks_mutex_);
|
||||
paused_.store(true);
|
||||
cond_var_.notify_all();
|
||||
}
|
||||
|
||||
inline void jobber::resume() noexcept {
|
||||
std::lock_guard<std::mutex> guard(tasks_mutex_);
|
||||
paused_.store(false);
|
||||
cond_var_.notify_all();
|
||||
}
|
||||
|
||||
inline bool jobber::is_paused() const noexcept {
|
||||
return paused_;
|
||||
}
|
||||
|
||||
inline jobber_wait_status jobber::wait_all() const noexcept {
|
||||
std::unique_lock<std::mutex> lock(tasks_mutex_);
|
||||
cond_var_.wait(lock, [this](){
|
||||
return cancelled_ || !active_task_count_;
|
||||
});
|
||||
return cancelled_
|
||||
? jobber_wait_status::cancelled
|
||||
: jobber_wait_status::no_timeout;
|
||||
}
|
||||
|
||||
inline jobber_wait_status jobber::active_wait_all() noexcept {
|
||||
while ( !cancelled_ && active_task_count_ ) {
|
||||
std::unique_lock<std::mutex> lock(tasks_mutex_);
|
||||
cond_var_.wait(lock, [this](){
|
||||
return cancelled_ || !active_task_count_ || !tasks_.empty();
|
||||
});
|
||||
if ( !tasks_.empty() ) {
|
||||
process_task_(std::move(lock));
|
||||
}
|
||||
}
|
||||
return cancelled_
|
||||
? jobber_wait_status::cancelled
|
||||
: jobber_wait_status::no_timeout;
|
||||
}
|
||||
|
||||
template < typename Rep, typename Period >
|
||||
jobber_wait_status jobber::wait_all_for(
|
||||
const std::chrono::duration<Rep, Period>& timeout_duration) const
|
||||
{
|
||||
return wait_all_until(
|
||||
std::chrono::steady_clock::now() + timeout_duration);
|
||||
}
|
||||
|
||||
template < typename Clock, typename Duration >
|
||||
jobber_wait_status jobber::wait_all_until(
|
||||
const std::chrono::time_point<Clock, Duration>& timeout_time) const
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(tasks_mutex_);
|
||||
return cond_var_.wait_until(lock, timeout_time, [this](){
|
||||
return cancelled_ || !active_task_count_;
|
||||
}) ? jobber_wait_status::no_timeout
|
||||
: jobber_wait_status::timeout;
|
||||
}
|
||||
|
||||
template < typename Rep, typename Period >
|
||||
jobber_wait_status jobber::active_wait_all_for(
|
||||
const std::chrono::duration<Rep, Period>& timeout_duration)
|
||||
{
|
||||
return active_wait_all_until(
|
||||
std::chrono::steady_clock::now() + timeout_duration);
|
||||
}
|
||||
|
||||
template < typename Clock, typename Duration >
|
||||
jobber_wait_status jobber::active_wait_all_until(
|
||||
const std::chrono::time_point<Clock, Duration>& timeout_time)
|
||||
{
|
||||
while ( !cancelled_ && active_task_count_ ) {
|
||||
if ( !(Clock::now() < timeout_time) ) {
|
||||
return jobber_wait_status::timeout;
|
||||
}
|
||||
std::unique_lock<std::mutex> lock(tasks_mutex_);
|
||||
cond_var_.wait_until(lock, timeout_time, [this](){
|
||||
return cancelled_ || !active_task_count_ || !tasks_.empty();
|
||||
});
|
||||
if ( !tasks_.empty() ) {
|
||||
process_task_(std::move(lock));
|
||||
}
|
||||
}
|
||||
return cancelled_
|
||||
? jobber_wait_status::cancelled
|
||||
: jobber_wait_status::no_timeout;
|
||||
}
|
||||
|
||||
inline void jobber::push_task_(jobber_priority priority, task_ptr task) {
|
||||
tasks_.emplace_back(priority, std::move(task));
|
||||
std::push_heap(tasks_.begin(), tasks_.end());
|
||||
++active_task_count_;
|
||||
cond_var_.notify_all();
|
||||
}
|
||||
|
||||
inline jobber::task_ptr jobber::pop_task_() noexcept {
|
||||
if ( !tasks_.empty() ) {
|
||||
std::pop_heap(tasks_.begin(), tasks_.end());
|
||||
task_ptr task = std::move(tasks_.back().second);
|
||||
tasks_.pop_back();
|
||||
return task;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
inline void jobber::shutdown_() noexcept {
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(tasks_mutex_);
|
||||
while ( !tasks_.empty() ) {
|
||||
task_ptr task = pop_task_();
|
||||
if ( task ) {
|
||||
task->cancel();
|
||||
--active_task_count_;
|
||||
}
|
||||
}
|
||||
cancelled_.store(true);
|
||||
cond_var_.notify_all();
|
||||
}
|
||||
for ( std::thread& thread : threads_ ) {
|
||||
if ( thread.joinable() ) {
|
||||
thread.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline void jobber::worker_main_() noexcept {
|
||||
while ( true ) {
|
||||
std::unique_lock<std::mutex> lock(tasks_mutex_);
|
||||
cond_var_.wait(lock, [this](){
|
||||
return cancelled_ || (!paused_ && !tasks_.empty());
|
||||
});
|
||||
if ( cancelled_ ) {
|
||||
break;
|
||||
}
|
||||
process_task_(std::move(lock));
|
||||
}
|
||||
}
|
||||
|
||||
inline void jobber::process_task_(std::unique_lock<std::mutex> lock) noexcept {
|
||||
assert(lock.owns_lock());
|
||||
task_ptr task = pop_task_();
|
||||
if ( task ) {
|
||||
lock.unlock();
|
||||
task->run();
|
||||
--active_task_count_;
|
||||
cond_var_.notify_all();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace jobber_hpp
|
||||
{
|
||||
//
|
||||
// concrete_task<R, F, Args...>
|
||||
//
|
||||
|
||||
template < typename R, typename F, typename... Args >
|
||||
template < typename U >
|
||||
jobber::concrete_task<R, F, Args...>::concrete_task(U&& u, std::tuple<Args...>&& args)
|
||||
: f_(std::forward<U>(u))
|
||||
, args_(std::move(args)) {}
|
||||
|
||||
template < typename R, typename F, typename... Args >
|
||||
void jobber::concrete_task<R, F, Args...>::run() noexcept {
|
||||
try {
|
||||
R value = invoke_hpp::apply(std::move(f_), std::move(args_));
|
||||
promise_.resolve(std::move(value));
|
||||
} catch (...) {
|
||||
promise_.reject(std::current_exception());
|
||||
}
|
||||
}
|
||||
|
||||
template < typename R, typename F, typename... Args >
|
||||
void jobber::concrete_task<R, F, Args...>::cancel() noexcept {
|
||||
promise_.reject(jobber_cancelled_exception());
|
||||
}
|
||||
|
||||
template < typename R, typename F, typename... Args >
|
||||
promise<R> jobber::concrete_task<R, F, Args...>::future() noexcept {
|
||||
return promise_;
|
||||
}
|
||||
|
||||
//
|
||||
// concrete_task<void, F, Args...>
|
||||
//
|
||||
|
||||
template < typename F, typename... Args >
|
||||
template < typename U >
|
||||
jobber::concrete_task<void, F, Args...>::concrete_task(U&& u, std::tuple<Args...>&& args)
|
||||
: f_(std::forward<U>(u))
|
||||
, args_(std::move(args)) {}
|
||||
|
||||
template < typename F, typename... Args >
|
||||
void jobber::concrete_task<void, F, Args...>::run() noexcept {
|
||||
try {
|
||||
invoke_hpp::apply(std::move(f_), std::move(args_));
|
||||
promise_.resolve();
|
||||
} catch (...) {
|
||||
promise_.reject(std::current_exception());
|
||||
}
|
||||
}
|
||||
|
||||
template < typename F, typename... Args >
|
||||
void jobber::concrete_task<void, F, Args...>::cancel() noexcept {
|
||||
promise_.reject(jobber_cancelled_exception());
|
||||
}
|
||||
|
||||
template < typename F, typename... Args >
|
||||
promise<void> jobber::concrete_task<void, F, Args...>::future() noexcept {
|
||||
return promise_;
|
||||
}
|
||||
}
|
||||
180
headers/3rdparty/promise.hpp/promise.hpp
vendored
180
headers/3rdparty/promise.hpp/promise.hpp
vendored
@@ -12,6 +12,7 @@
|
||||
#include <new>
|
||||
#include <mutex>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
@@ -20,6 +21,7 @@
|
||||
#include <stdexcept>
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
#include <condition_variable>
|
||||
|
||||
//
|
||||
// invoke.hpp
|
||||
@@ -368,6 +370,15 @@ namespace promise_hpp
|
||||
};
|
||||
}
|
||||
|
||||
//
|
||||
// promise_wait_status
|
||||
//
|
||||
|
||||
enum class promise_wait_status {
|
||||
no_timeout,
|
||||
timeout
|
||||
};
|
||||
|
||||
//
|
||||
// promise<T>
|
||||
//
|
||||
@@ -376,12 +387,6 @@ namespace promise_hpp
|
||||
class promise final {
|
||||
public:
|
||||
using value_type = T;
|
||||
|
||||
enum class status : std::uint8_t {
|
||||
pending,
|
||||
resolved,
|
||||
rejected
|
||||
};
|
||||
public:
|
||||
promise()
|
||||
: state_(std::make_shared<state>()) {}
|
||||
@@ -535,6 +540,28 @@ namespace promise_hpp
|
||||
return state_->reject(
|
||||
std::make_exception_ptr(std::forward<E>(e)));
|
||||
}
|
||||
|
||||
const T& get() const {
|
||||
return state_->get();
|
||||
}
|
||||
|
||||
void wait() const noexcept {
|
||||
state_->wait();
|
||||
}
|
||||
|
||||
template < typename Rep, typename Period >
|
||||
promise_wait_status wait_for(
|
||||
const std::chrono::duration<Rep, Period>& timeout_duration) const
|
||||
{
|
||||
return state_->wait_for(timeout_duration);
|
||||
}
|
||||
|
||||
template < typename Clock, typename Duration >
|
||||
promise_wait_status wait_until(
|
||||
const std::chrono::time_point<Clock, Duration>& timeout_time) const
|
||||
{
|
||||
return state_->wait_until(timeout_time);
|
||||
}
|
||||
private:
|
||||
class state;
|
||||
std::shared_ptr<state> state_;
|
||||
@@ -552,6 +579,7 @@ namespace promise_hpp
|
||||
storage_.set(std::forward<U>(value));
|
||||
status_ = status::resolved;
|
||||
invoke_resolve_handlers_();
|
||||
cond_var_.notify_all();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -563,9 +591,51 @@ namespace promise_hpp
|
||||
exception_ = e;
|
||||
status_ = status::rejected;
|
||||
invoke_reject_handlers_();
|
||||
cond_var_.notify_all();
|
||||
return true;
|
||||
}
|
||||
|
||||
const T& get() {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
cond_var_.wait(lock, [this](){
|
||||
return status_ != status::pending;
|
||||
});
|
||||
if ( status_ == status::rejected ) {
|
||||
std::rethrow_exception(exception_);
|
||||
}
|
||||
assert(status_ == status::resolved);
|
||||
return storage_.value();
|
||||
}
|
||||
|
||||
void wait() const noexcept {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
cond_var_.wait(lock, [this](){
|
||||
return status_ != status::pending;
|
||||
});
|
||||
}
|
||||
|
||||
template < typename Rep, typename Period >
|
||||
promise_wait_status wait_for(
|
||||
const std::chrono::duration<Rep, Period>& timeout_duration) const
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
return cond_var_.wait_for(lock, timeout_duration, [this](){
|
||||
return status_ != status::pending;
|
||||
}) ? promise_wait_status::no_timeout
|
||||
: promise_wait_status::timeout;
|
||||
}
|
||||
|
||||
template < typename Clock, typename Duration >
|
||||
promise_wait_status wait_until(
|
||||
const std::chrono::time_point<Clock, Duration>& timeout_time) const
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
return cond_var_.wait_until(lock, timeout_time, [this](){
|
||||
return status_ != status::pending;
|
||||
}) ? promise_wait_status::no_timeout
|
||||
: promise_wait_status::timeout;
|
||||
}
|
||||
|
||||
template < typename U, typename ResolveF, typename RejectF >
|
||||
std::enable_if_t<std::is_void<U>::value, void>
|
||||
attach(promise<U>& next, ResolveF&& resolve, RejectF&& reject) {
|
||||
@@ -674,11 +744,17 @@ namespace promise_hpp
|
||||
handlers_.clear();
|
||||
}
|
||||
private:
|
||||
detail::storage<T> storage_;
|
||||
status status_ = status::pending;
|
||||
std::exception_ptr exception_ = nullptr;
|
||||
enum class status {
|
||||
pending,
|
||||
resolved,
|
||||
rejected
|
||||
};
|
||||
|
||||
std::mutex mutex_;
|
||||
status status_{status::pending};
|
||||
std::exception_ptr exception_{nullptr};
|
||||
|
||||
mutable std::mutex mutex_;
|
||||
mutable std::condition_variable cond_var_;
|
||||
|
||||
struct handler {
|
||||
using resolve_t = std::function<void(const T&)>;
|
||||
@@ -694,6 +770,7 @@ namespace promise_hpp
|
||||
};
|
||||
|
||||
std::vector<handler> handlers_;
|
||||
detail::storage<T> storage_;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -705,12 +782,6 @@ namespace promise_hpp
|
||||
class promise<void> final {
|
||||
public:
|
||||
using value_type = void;
|
||||
|
||||
enum class status : std::uint8_t {
|
||||
pending,
|
||||
resolved,
|
||||
rejected
|
||||
};
|
||||
public:
|
||||
promise()
|
||||
: state_(std::make_shared<state>()) {}
|
||||
@@ -858,6 +929,28 @@ namespace promise_hpp
|
||||
return state_->reject(
|
||||
std::make_exception_ptr(std::forward<E>(e)));
|
||||
}
|
||||
|
||||
void get() const {
|
||||
state_->get();
|
||||
}
|
||||
|
||||
void wait() const noexcept {
|
||||
state_->wait();
|
||||
}
|
||||
|
||||
template < typename Rep, typename Period >
|
||||
promise_wait_status wait_for(
|
||||
const std::chrono::duration<Rep, Period>& timeout_duration) const
|
||||
{
|
||||
return state_->wait_for(timeout_duration);
|
||||
}
|
||||
|
||||
template < typename Clock, typename Duration >
|
||||
promise_wait_status wait_until(
|
||||
const std::chrono::time_point<Clock, Duration>& timeout_time) const
|
||||
{
|
||||
return state_->wait_until(timeout_time);
|
||||
}
|
||||
private:
|
||||
class state;
|
||||
std::shared_ptr<state> state_;
|
||||
@@ -873,6 +966,7 @@ namespace promise_hpp
|
||||
}
|
||||
status_ = status::resolved;
|
||||
invoke_resolve_handlers_();
|
||||
cond_var_.notify_all();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -884,9 +978,50 @@ namespace promise_hpp
|
||||
exception_ = e;
|
||||
status_ = status::rejected;
|
||||
invoke_reject_handlers_();
|
||||
cond_var_.notify_all();
|
||||
return true;
|
||||
}
|
||||
|
||||
void get() {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
cond_var_.wait(lock, [this](){
|
||||
return status_ != status::pending;
|
||||
});
|
||||
if ( status_ == status::rejected ) {
|
||||
std::rethrow_exception(exception_);
|
||||
}
|
||||
assert(status_ == status::resolved);
|
||||
}
|
||||
|
||||
void wait() const noexcept {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
cond_var_.wait(lock, [this](){
|
||||
return status_ != status::pending;
|
||||
});
|
||||
}
|
||||
|
||||
template < typename Rep, typename Period >
|
||||
promise_wait_status wait_for(
|
||||
const std::chrono::duration<Rep, Period>& timeout_duration) const
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
return cond_var_.wait_for(lock, timeout_duration, [this](){
|
||||
return status_ != status::pending;
|
||||
}) ? promise_wait_status::no_timeout
|
||||
: promise_wait_status::timeout;
|
||||
}
|
||||
|
||||
template < typename Clock, typename Duration >
|
||||
promise_wait_status wait_until(
|
||||
const std::chrono::time_point<Clock, Duration>& timeout_time) const
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
return cond_var_.wait_until(lock, timeout_time, [this](){
|
||||
return status_ != status::pending;
|
||||
}) ? promise_wait_status::no_timeout
|
||||
: promise_wait_status::timeout;
|
||||
}
|
||||
|
||||
template < typename U, typename ResolveF, typename RejectF >
|
||||
std::enable_if_t<std::is_void<U>::value, void>
|
||||
attach(promise<U>& next, ResolveF&& resolve, RejectF&& reject) {
|
||||
@@ -991,10 +1126,17 @@ namespace promise_hpp
|
||||
handlers_.clear();
|
||||
}
|
||||
private:
|
||||
status status_ = status::pending;
|
||||
std::exception_ptr exception_ = nullptr;
|
||||
enum class status {
|
||||
pending,
|
||||
resolved,
|
||||
rejected
|
||||
};
|
||||
|
||||
std::mutex mutex_;
|
||||
status status_{status::pending};
|
||||
std::exception_ptr exception_{nullptr};
|
||||
|
||||
mutable std::mutex mutex_;
|
||||
mutable std::condition_variable cond_var_;
|
||||
|
||||
struct handler {
|
||||
using resolve_t = std::function<void()>;
|
||||
|
||||
307
headers/3rdparty/promise.hpp/scheduler.hpp
vendored
Normal file
307
headers/3rdparty/promise.hpp/scheduler.hpp
vendored
Normal file
@@ -0,0 +1,307 @@
|
||||
/*******************************************************************************
|
||||
* This file is part of the "promise.hpp"
|
||||
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
||||
* Copyright (C) 2018 Matvey Cherevko
|
||||
******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cassert>
|
||||
|
||||
#include <tuple>
|
||||
#include <mutex>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <exception>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
#include <condition_variable>
|
||||
|
||||
#include "promise.hpp"
|
||||
|
||||
namespace scheduler_hpp
|
||||
{
|
||||
using namespace promise_hpp;
|
||||
|
||||
enum class scheduler_priority {
|
||||
lowest,
|
||||
below_normal,
|
||||
normal,
|
||||
above_normal,
|
||||
highest
|
||||
};
|
||||
|
||||
enum class scheduler_wait_status {
|
||||
no_timeout,
|
||||
cancelled,
|
||||
timeout
|
||||
};
|
||||
|
||||
class scheduler_cancelled_exception : public std::runtime_error {
|
||||
public:
|
||||
scheduler_cancelled_exception()
|
||||
: std::runtime_error("scheduler has stopped working") {}
|
||||
};
|
||||
|
||||
class scheduler final : private detail::noncopyable {
|
||||
public:
|
||||
scheduler();
|
||||
~scheduler() noexcept;
|
||||
|
||||
template < typename F, typename... Args >
|
||||
using schedule_invoke_result_t = invoke_hpp::invoke_result_t<
|
||||
std::decay_t<F>,
|
||||
std::decay_t<Args>...>;
|
||||
|
||||
template < typename F, typename... Args
|
||||
, typename R = schedule_invoke_result_t<F, Args...> >
|
||||
promise<R> schedule(F&& f, Args&&... args);
|
||||
|
||||
template < typename F, typename... Args
|
||||
, typename R = schedule_invoke_result_t<F, Args...> >
|
||||
promise<R> schedule(scheduler_priority scheduler_priority, F&& f, Args&&... args);
|
||||
|
||||
scheduler_wait_status process_all_tasks() noexcept;
|
||||
|
||||
template < typename Rep, typename Period >
|
||||
scheduler_wait_status process_tasks_for(
|
||||
const std::chrono::duration<Rep, Period>& timeout_duration) noexcept;
|
||||
|
||||
template < typename Clock, typename Duration >
|
||||
scheduler_wait_status process_tasks_until(
|
||||
const std::chrono::time_point<Clock, Duration>& timeout_time) noexcept;
|
||||
private:
|
||||
class task;
|
||||
using task_ptr = std::unique_ptr<task>;
|
||||
template < typename R, typename F, typename... Args >
|
||||
class concrete_task;
|
||||
private:
|
||||
void push_task_(scheduler_priority scheduler_priority, task_ptr task);
|
||||
task_ptr pop_task_() noexcept;
|
||||
void shutdown_() noexcept;
|
||||
void process_task_(std::unique_lock<std::mutex> lock) noexcept;
|
||||
private:
|
||||
std::vector<std::pair<scheduler_priority, task_ptr>> tasks_;
|
||||
std::atomic<bool> cancelled_{false};
|
||||
std::atomic<std::size_t> active_task_count_{0};
|
||||
mutable std::mutex tasks_mutex_;
|
||||
mutable std::condition_variable cond_var_;
|
||||
};
|
||||
|
||||
class scheduler::task : private noncopyable {
|
||||
public:
|
||||
virtual ~task() noexcept = default;
|
||||
virtual void run() noexcept = 0;
|
||||
virtual void cancel() noexcept = 0;
|
||||
};
|
||||
|
||||
template < typename R, typename F, typename... Args >
|
||||
class scheduler::concrete_task : public task {
|
||||
F f_;
|
||||
std::tuple<Args...> args_;
|
||||
promise<R> promise_;
|
||||
public:
|
||||
template < typename U >
|
||||
concrete_task(U&& u, std::tuple<Args...>&& args);
|
||||
void run() noexcept final;
|
||||
void cancel() noexcept final;
|
||||
promise<R> future() noexcept;
|
||||
};
|
||||
|
||||
template < typename F, typename... Args >
|
||||
class scheduler::concrete_task<void, F, Args...> : public task {
|
||||
F f_;
|
||||
std::tuple<Args...> args_;
|
||||
promise<void> promise_;
|
||||
public:
|
||||
template < typename U >
|
||||
concrete_task(U&& u, std::tuple<Args...>&& args);
|
||||
void run() noexcept final;
|
||||
void cancel() noexcept final;
|
||||
promise<void> future() noexcept;
|
||||
};
|
||||
}
|
||||
|
||||
namespace scheduler_hpp
|
||||
{
|
||||
inline scheduler::scheduler() = default;
|
||||
|
||||
inline scheduler::~scheduler() noexcept {
|
||||
shutdown_();
|
||||
}
|
||||
|
||||
template < typename F, typename... Args, typename R >
|
||||
promise<R> scheduler::schedule(F&& f, Args&&... args) {
|
||||
return schedule(
|
||||
scheduler_priority::normal,
|
||||
std::forward<F>(f),
|
||||
std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template < typename F, typename... Args, typename R >
|
||||
promise<R> scheduler::schedule(scheduler_priority priority, F&& f, Args&&... args) {
|
||||
using task_t = concrete_task<
|
||||
R,
|
||||
std::decay_t<F>,
|
||||
std::decay_t<Args>...>;
|
||||
std::unique_ptr<task_t> task = std::make_unique<task_t>(
|
||||
std::forward<F>(f),
|
||||
std::make_tuple(std::forward<Args>(args)...));
|
||||
promise<R> future = task->future();
|
||||
std::lock_guard<std::mutex> guard(tasks_mutex_);
|
||||
push_task_(priority, std::move(task));
|
||||
return future;
|
||||
}
|
||||
|
||||
inline scheduler_wait_status scheduler::process_all_tasks() noexcept {
|
||||
while ( !cancelled_ && active_task_count_ ) {
|
||||
std::unique_lock<std::mutex> lock(tasks_mutex_);
|
||||
cond_var_.wait(lock, [this](){
|
||||
return cancelled_ || !active_task_count_ || !tasks_.empty();
|
||||
});
|
||||
if ( !tasks_.empty() ) {
|
||||
process_task_(std::move(lock));
|
||||
}
|
||||
}
|
||||
return cancelled_
|
||||
? scheduler_wait_status::cancelled
|
||||
: scheduler_wait_status::no_timeout;
|
||||
}
|
||||
|
||||
template < typename Rep, typename Period >
|
||||
scheduler_wait_status scheduler::process_tasks_for(
|
||||
const std::chrono::duration<Rep, Period>& timeout_duration) noexcept
|
||||
{
|
||||
return process_tasks_until(
|
||||
std::chrono::steady_clock::now() + timeout_duration);
|
||||
}
|
||||
|
||||
template < typename Clock, typename Duration >
|
||||
scheduler_wait_status scheduler::process_tasks_until(
|
||||
const std::chrono::time_point<Clock, Duration>& timeout_time) noexcept
|
||||
{
|
||||
while ( !cancelled_ && active_task_count_ ) {
|
||||
if ( !(Clock::now() < timeout_time) ) {
|
||||
return scheduler_wait_status::timeout;
|
||||
}
|
||||
std::unique_lock<std::mutex> lock(tasks_mutex_);
|
||||
cond_var_.wait_until(lock, timeout_time, [this](){
|
||||
return cancelled_ || !active_task_count_ || !tasks_.empty();
|
||||
});
|
||||
if ( !tasks_.empty() ) {
|
||||
process_task_(std::move(lock));
|
||||
}
|
||||
}
|
||||
return cancelled_
|
||||
? scheduler_wait_status::cancelled
|
||||
: scheduler_wait_status::no_timeout;
|
||||
}
|
||||
|
||||
inline void scheduler::push_task_(scheduler_priority priority, task_ptr task) {
|
||||
tasks_.emplace_back(priority, std::move(task));
|
||||
std::push_heap(tasks_.begin(), tasks_.end());
|
||||
++active_task_count_;
|
||||
cond_var_.notify_all();
|
||||
}
|
||||
|
||||
inline scheduler::task_ptr scheduler::pop_task_() noexcept {
|
||||
if ( !tasks_.empty() ) {
|
||||
std::pop_heap(tasks_.begin(), tasks_.end());
|
||||
task_ptr task = std::move(tasks_.back().second);
|
||||
tasks_.pop_back();
|
||||
return task;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
inline void scheduler::shutdown_() noexcept {
|
||||
std::lock_guard<std::mutex> guard(tasks_mutex_);
|
||||
while ( !tasks_.empty() ) {
|
||||
task_ptr task = pop_task_();
|
||||
if ( task ) {
|
||||
task->cancel();
|
||||
--active_task_count_;
|
||||
}
|
||||
}
|
||||
cancelled_.store(true);
|
||||
cond_var_.notify_all();
|
||||
}
|
||||
|
||||
inline void scheduler::process_task_(std::unique_lock<std::mutex> lock) noexcept {
|
||||
assert(lock.owns_lock());
|
||||
task_ptr task = pop_task_();
|
||||
if ( task ) {
|
||||
lock.unlock();
|
||||
task->run();
|
||||
--active_task_count_;
|
||||
cond_var_.notify_all();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace scheduler_hpp
|
||||
{
|
||||
//
|
||||
// concrete_task<R, F, Args...>
|
||||
//
|
||||
|
||||
template < typename R, typename F, typename... Args >
|
||||
template < typename U >
|
||||
scheduler::concrete_task<R, F, Args...>::concrete_task(U&& u, std::tuple<Args...>&& args)
|
||||
: f_(std::forward<U>(u))
|
||||
, args_(std::move(args)) {}
|
||||
|
||||
template < typename R, typename F, typename... Args >
|
||||
void scheduler::concrete_task<R, F, Args...>::run() noexcept {
|
||||
try {
|
||||
R value = invoke_hpp::apply(std::move(f_), std::move(args_));
|
||||
promise_.resolve(std::move(value));
|
||||
} catch (...) {
|
||||
promise_.reject(std::current_exception());
|
||||
}
|
||||
}
|
||||
|
||||
template < typename R, typename F, typename... Args >
|
||||
void scheduler::concrete_task<R, F, Args...>::cancel() noexcept {
|
||||
promise_.reject(scheduler_cancelled_exception());
|
||||
}
|
||||
|
||||
template < typename R, typename F, typename... Args >
|
||||
promise<R> scheduler::concrete_task<R, F, Args...>::future() noexcept {
|
||||
return promise_;
|
||||
}
|
||||
|
||||
//
|
||||
// concrete_task<void, F, Args...>
|
||||
//
|
||||
|
||||
template < typename F, typename... Args >
|
||||
template < typename U >
|
||||
scheduler::concrete_task<void, F, Args...>::concrete_task(U&& u, std::tuple<Args...>&& args)
|
||||
: f_(std::forward<U>(u))
|
||||
, args_(std::move(args)) {}
|
||||
|
||||
template < typename F, typename... Args >
|
||||
void scheduler::concrete_task<void, F, Args...>::run() noexcept {
|
||||
try {
|
||||
invoke_hpp::apply(std::move(f_), std::move(args_));
|
||||
promise_.resolve();
|
||||
} catch (...) {
|
||||
promise_.reject(std::current_exception());
|
||||
}
|
||||
}
|
||||
|
||||
template < typename F, typename... Args >
|
||||
void scheduler::concrete_task<void, F, Args...>::cancel() noexcept {
|
||||
promise_.reject(scheduler_cancelled_exception());
|
||||
}
|
||||
|
||||
template < typename F, typename... Args >
|
||||
promise<void> scheduler::concrete_task<void, F, Args...>::future() noexcept {
|
||||
return promise_;
|
||||
}
|
||||
}
|
||||
7
headers/3rdparty/variant/lib.hpp
vendored
7
headers/3rdparty/variant/lib.hpp
vendored
@@ -207,15 +207,8 @@ namespace mpark {
|
||||
|
||||
template <typename T, bool = is_swappable<T>::value>
|
||||
struct is_nothrow_swappable {
|
||||
#if defined(__GNUC__) && !defined(__clang__)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wnoexcept"
|
||||
#endif
|
||||
static constexpr bool value =
|
||||
noexcept(swap(std::declval<T &>(), std::declval<T &>()));
|
||||
#if defined(__GNUC__) && !defined(__clang__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
|
||||
7
headers/3rdparty/variant/variant.hpp
vendored
7
headers/3rdparty/variant/variant.hpp
vendored
@@ -1814,19 +1814,12 @@ namespace mpark {
|
||||
lib::forward<Vs>(vs)...))
|
||||
#endif
|
||||
|
||||
#if defined(__GNUC__) && !defined(__clang__)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wnoexcept"
|
||||
#endif
|
||||
template <typename... Ts>
|
||||
inline auto swap(variant<Ts...> &lhs,
|
||||
variant<Ts...> &rhs) noexcept(noexcept(lhs.swap(rhs)))
|
||||
-> decltype(lhs.swap(rhs)) {
|
||||
lhs.swap(rhs);
|
||||
}
|
||||
#if defined(__GNUC__) && !defined(__clang__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
namespace detail {
|
||||
|
||||
|
||||
Submodule modules/catch2 updated: 4902cd7215...461843b1f0
Submodule modules/promise.hpp updated: 8491a7a2ef...4fb77fb49d
Submodule modules/pugixml updated: 7664bbf9af...b3db08ffcc
Submodule modules/rapidjson updated: 30d92a6399...66eb6067b1
Submodule modules/variant updated: 8e46e0941f...7dd1e48c33
@@ -26,7 +26,9 @@ mkdir -p $HEADERS_RDPARTY_DIR/invoke.hpp
|
||||
cp -rfv $MODULES_DIR/invoke.hpp/invoke.hpp $HEADERS_RDPARTY_DIR/invoke.hpp/invoke.hpp
|
||||
|
||||
mkdir -p $HEADERS_RDPARTY_DIR/promise.hpp
|
||||
cp -rfv $MODULES_DIR/promise.hpp/jobber.hpp $HEADERS_RDPARTY_DIR/promise.hpp/jobber.hpp
|
||||
cp -rfv $MODULES_DIR/promise.hpp/promise.hpp $HEADERS_RDPARTY_DIR/promise.hpp/promise.hpp
|
||||
cp -rfv $MODULES_DIR/promise.hpp/scheduler.hpp $HEADERS_RDPARTY_DIR/promise.hpp/scheduler.hpp
|
||||
|
||||
mkdir -p $SOURCES_RDPARTY_DIR/pugixml
|
||||
cp -rfv $MODULES_DIR/pugixml/src/. $SOURCES_RDPARTY_DIR/pugixml/
|
||||
|
||||
2
sources/3rdparty/rapidjson/filereadstream.h
vendored
2
sources/3rdparty/rapidjson/filereadstream.h
vendored
@@ -59,7 +59,7 @@ public:
|
||||
|
||||
// For encoding detection only.
|
||||
const Ch* Peek4() const {
|
||||
return (current_ + 4 <= bufferLast_) ? current_ : 0;
|
||||
return (current_ + 4 - !eof_ <= bufferLast_) ? current_ : 0;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
7
sources/3rdparty/rapidjson/internal/regex.h
vendored
7
sources/3rdparty/rapidjson/internal/regex.h
vendored
@@ -395,8 +395,7 @@ private:
|
||||
}
|
||||
return false;
|
||||
|
||||
default:
|
||||
RAPIDJSON_ASSERT(op == kOneOrMore);
|
||||
case kOneOrMore:
|
||||
if (operandStack.GetSize() >= sizeof(Frag)) {
|
||||
Frag e = *operandStack.template Pop<Frag>(1);
|
||||
SizeType s = NewState(kRegexInvalidState, e.start, 0);
|
||||
@@ -405,6 +404,10 @@ private:
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
default:
|
||||
// syntax error (e.g. unclosed kLeftParenthesis)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
82
sources/3rdparty/rapidjson/istreamwrapper.h
vendored
82
sources/3rdparty/rapidjson/istreamwrapper.h
vendored
@@ -48,57 +48,71 @@ template <typename StreamType>
|
||||
class BasicIStreamWrapper {
|
||||
public:
|
||||
typedef typename StreamType::char_type Ch;
|
||||
BasicIStreamWrapper(StreamType& stream) : stream_(stream), count_(), peekBuffer_() {}
|
||||
|
||||
Ch Peek() const {
|
||||
typename StreamType::int_type c = stream_.peek();
|
||||
return RAPIDJSON_LIKELY(c != StreamType::traits_type::eof()) ? static_cast<Ch>(c) : static_cast<Ch>('\0');
|
||||
//! Constructor.
|
||||
/*!
|
||||
\param stream stream opened for read.
|
||||
*/
|
||||
BasicIStreamWrapper(StreamType &stream) : stream_(stream), buffer_(peekBuffer_), bufferSize_(4), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) {
|
||||
Read();
|
||||
}
|
||||
|
||||
Ch Take() {
|
||||
typename StreamType::int_type c = stream_.get();
|
||||
if (RAPIDJSON_LIKELY(c != StreamType::traits_type::eof())) {
|
||||
count_++;
|
||||
return static_cast<Ch>(c);
|
||||
}
|
||||
else
|
||||
return '\0';
|
||||
//! Constructor.
|
||||
/*!
|
||||
\param stream stream opened for read.
|
||||
\param buffer user-supplied buffer.
|
||||
\param bufferSize size of buffer in bytes. Must >=4 bytes.
|
||||
*/
|
||||
BasicIStreamWrapper(StreamType &stream, char* buffer, size_t bufferSize) : stream_(stream), buffer_(buffer), bufferSize_(bufferSize), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) {
|
||||
RAPIDJSON_ASSERT(bufferSize >= 4);
|
||||
Read();
|
||||
}
|
||||
|
||||
// tellg() may return -1 when failed. So we count by ourself.
|
||||
size_t Tell() const { return count_; }
|
||||
Ch Peek() const { return *current_; }
|
||||
Ch Take() { Ch c = *current_; Read(); return c; }
|
||||
size_t Tell() const { return count_ + static_cast<size_t>(current_ - buffer_); }
|
||||
|
||||
Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
|
||||
// Not implemented
|
||||
void Put(Ch) { RAPIDJSON_ASSERT(false); }
|
||||
void Flush() { RAPIDJSON_ASSERT(false); }
|
||||
void Flush() { RAPIDJSON_ASSERT(false); }
|
||||
Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
|
||||
size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
|
||||
|
||||
// For encoding detection only.
|
||||
const Ch* Peek4() const {
|
||||
RAPIDJSON_ASSERT(sizeof(Ch) == 1); // Only usable for byte stream.
|
||||
int i;
|
||||
bool hasError = false;
|
||||
for (i = 0; i < 4; ++i) {
|
||||
typename StreamType::int_type c = stream_.get();
|
||||
if (c == StreamType::traits_type::eof()) {
|
||||
hasError = true;
|
||||
stream_.clear();
|
||||
break;
|
||||
}
|
||||
peekBuffer_[i] = static_cast<Ch>(c);
|
||||
}
|
||||
for (--i; i >= 0; --i)
|
||||
stream_.putback(peekBuffer_[i]);
|
||||
return !hasError ? peekBuffer_ : 0;
|
||||
return (current_ + 4 - !eof_ <= bufferLast_) ? current_ : 0;
|
||||
}
|
||||
|
||||
private:
|
||||
BasicIStreamWrapper();
|
||||
BasicIStreamWrapper(const BasicIStreamWrapper&);
|
||||
BasicIStreamWrapper& operator=(const BasicIStreamWrapper&);
|
||||
|
||||
StreamType& stream_;
|
||||
size_t count_; //!< Number of characters read. Note:
|
||||
mutable Ch peekBuffer_[4];
|
||||
void Read() {
|
||||
if (current_ < bufferLast_)
|
||||
++current_;
|
||||
else if (!eof_) {
|
||||
count_ += readCount_;
|
||||
readCount_ = bufferSize_;
|
||||
bufferLast_ = buffer_ + readCount_ - 1;
|
||||
current_ = buffer_;
|
||||
|
||||
if (!stream_.read(buffer_, static_cast<std::streamsize>(bufferSize_))) {
|
||||
readCount_ = static_cast<size_t>(stream_.gcount());
|
||||
*(bufferLast_ = buffer_ + readCount_) = '\0';
|
||||
eof_ = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
StreamType &stream_;
|
||||
Ch peekBuffer_[4], *buffer_;
|
||||
size_t bufferSize_;
|
||||
Ch *bufferLast_;
|
||||
Ch *current_;
|
||||
size_t readCount_;
|
||||
size_t count_; //!< Number of characters read
|
||||
bool eof_;
|
||||
};
|
||||
|
||||
typedef BasicIStreamWrapper<std::istream> IStreamWrapper;
|
||||
|
||||
Reference in New Issue
Block a user