File indexing completed on 2023-03-17 11:01:50
0001 #ifndef FWCore_Concurrency_WaitingTask_h
0002 #define FWCore_Concurrency_WaitingTask_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #include <atomic>
0023 #include <exception>
0024 #include <memory>
0025
0026
0027 #include "FWCore/Concurrency/interface/TaskBase.h"
0028
0029
0030
0031 namespace edm {
0032 class WaitingTaskList;
0033 class WaitingTaskHolder;
0034 class WaitingTaskWithArenaHolder;
0035
0036 class WaitingTask : public TaskBase {
0037 public:
0038 friend class WaitingTaskList;
0039 friend class WaitingTaskHolder;
0040 friend class WaitingTaskWithArenaHolder;
0041
0042
0043 WaitingTask() : m_ptr{} {}
0044 ~WaitingTask() override{};
0045
0046
0047
0048
0049
0050
0051 std::exception_ptr exceptionPtr() const {
0052 if (m_ptrSet == static_cast<unsigned char>(State::kSet)) {
0053 return m_ptr;
0054 }
0055 return std::exception_ptr{};
0056 }
0057
0058 protected:
0059 std::exception_ptr const& uncheckedExceptionPtr() const { return m_ptr; }
0060
0061 private:
0062 enum class State : unsigned char { kUnset = 0, kSetting = 1, kSet = 2 };
0063
0064
0065
0066
0067
0068 void dependentTaskFailed(std::exception_ptr iPtr) {
0069 unsigned char isSet = static_cast<unsigned char>(State::kUnset);
0070 if (iPtr and m_ptrSet.compare_exchange_strong(isSet, static_cast<unsigned char>(State::kSetting))) {
0071 m_ptr = iPtr;
0072 m_ptrSet = static_cast<unsigned char>(State::kSet);
0073 }
0074 }
0075
0076 std::exception_ptr m_ptr;
0077 std::atomic<unsigned char> m_ptrSet = static_cast<unsigned char>(State::kUnset);
0078 };
0079
0080 template <typename F>
0081 class FunctorWaitingTask : public WaitingTask {
0082 public:
0083 explicit FunctorWaitingTask(F f) : func_(std::move(f)) {}
0084
0085 void execute() final { func_(uncheckedExceptionPtr() ? &uncheckedExceptionPtr() : nullptr); };
0086
0087 private:
0088 F func_;
0089 };
0090
0091 template <typename F>
0092 FunctorWaitingTask<F>* make_waiting_task(F f) {
0093 return new FunctorWaitingTask<F>(std::move(f));
0094 }
0095
0096 }
0097
0098 #endif