Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/ps/Future.h
/* Copyright (C) 2021 Wildfire Games. | /* Copyright (C) 2022 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
▲ Show 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | |||||
* The shared state between futures and packaged state. | * The shared state between futures and packaged state. | ||||
* Holds all relevant data. | * Holds all relevant data. | ||||
*/ | */ | ||||
template<typename ResultType> | template<typename ResultType> | ||||
class SharedState : public SharedStateResult<ResultType> | class SharedState : public SharedStateResult<ResultType> | ||||
{ | { | ||||
static constexpr bool VoidResult = std::is_same_v<ResultType, void>; | static constexpr bool VoidResult = std::is_same_v<ResultType, void>; | ||||
public: | public: | ||||
SharedState(std::function<ResultType()>&& func) : m_Func(func) {} | SharedState(std::function<ResultType()>&& func) : m_Func(std::move(func)) {} | ||||
~SharedState() | ~SharedState() | ||||
{ | { | ||||
// For safety, wait on started task completion, but not on pending ones (auto-cancelled). | // For safety, wait on started task completion, but not on pending ones (auto-cancelled). | ||||
if (!Cancel()) | if (!Cancel()) | ||||
{ | { | ||||
Wait(); | Wait(); | ||||
Cancel(); | Cancel(); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 81 Lines • ▼ Show 20 Lines | class Future | ||||
template<typename T> | template<typename T> | ||||
friend class PackagedTask; | friend class PackagedTask; | ||||
static constexpr bool VoidResult = std::is_same_v<ResultType, void>; | static constexpr bool VoidResult = std::is_same_v<ResultType, void>; | ||||
using Status = FutureSharedStateDetail::Status; | using Status = FutureSharedStateDetail::Status; | ||||
using SharedState = FutureSharedStateDetail::SharedState<ResultType>; | using SharedState = FutureSharedStateDetail::SharedState<ResultType>; | ||||
public: | public: | ||||
Future() {}; | Future() = default; | ||||
Future(const Future& o) = delete; | Future(const Future& o) = delete; | ||||
Future(Future&&) = default; | Future(Future&&) = default; | ||||
Future& operator=(Future&&) = default; | Future& operator=(Future&&) = default; | ||||
~Future() {} | ~Future() = default; | ||||
/** | /** | ||||
* Make the future wait for the result of @a func. | * Make the future wait for the result of @a func. | ||||
*/ | */ | ||||
template<typename T> | template<typename T> | ||||
PackagedTask<ResultType> Wrap(T&& func); | PackagedTask<ResultType> Wrap(T&& func); | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | |||||
* handling the promise & continuation logic. | * handling the promise & continuation logic. | ||||
*/ | */ | ||||
template<typename ResultType> | template<typename ResultType> | ||||
class PackagedTask | class PackagedTask | ||||
{ | { | ||||
static constexpr bool VoidResult = std::is_same_v<ResultType, void>; | static constexpr bool VoidResult = std::is_same_v<ResultType, void>; | ||||
public: | public: | ||||
PackagedTask() = delete; | PackagedTask() = delete; | ||||
PackagedTask(const std::shared_ptr<typename Future<ResultType>::SharedState>& ss) : m_SharedState(ss) {} | PackagedTask(std::shared_ptr<typename Future<ResultType>::SharedState> ss) : m_SharedState(std::move(ss)) {} | ||||
void operator()() | void operator()() | ||||
{ | { | ||||
typename Future<ResultType>::Status expected = Future<ResultType>::Status::PENDING; | typename Future<ResultType>::Status expected = Future<ResultType>::Status::PENDING; | ||||
if (!m_SharedState->m_Status.compare_exchange_strong(expected, Future<ResultType>::Status::STARTED)) | if (!m_SharedState->m_Status.compare_exchange_strong(expected, Future<ResultType>::Status::STARTED)) | ||||
return; | return; | ||||
if constexpr (VoidResult) | if constexpr (VoidResult) | ||||
▲ Show 20 Lines • Show All 43 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator