|
| 1 | +/* |
| 2 | + * Copyright 2024 Diligent Graphics LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * In no event and under no legal theory, whether in tort (including negligence), |
| 17 | + * contract, or otherwise, unless required by applicable law (such as deliberate |
| 18 | + * and grossly negligent acts) or agreed to in writing, shall any Contributor be |
| 19 | + * liable for any damages, including any direct, indirect, special, incidental, |
| 20 | + * or consequential damages of any character arising as a result of this License or |
| 21 | + * out of the use or inability to use the software (including but not limited to damages |
| 22 | + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and |
| 23 | + * all other commercial damages or losses), even if such Contributor has been advised |
| 24 | + * of the possibility of such damages. |
| 25 | + */ |
| 26 | + |
| 27 | +#pragma once |
| 28 | + |
| 29 | +#include <atomic> |
| 30 | +#include <memory> |
| 31 | + |
| 32 | +#include "SpinLock.hpp" |
| 33 | +#include "ThreadPool.hpp" |
| 34 | + |
| 35 | +namespace Diligent |
| 36 | +{ |
| 37 | + |
| 38 | +class AsyncInitializer |
| 39 | +{ |
| 40 | +public: |
| 41 | + static_assert((ASYNC_TASK_STATUS_UNKNOWN < ASYNC_TASK_STATUS_NOT_STARTED && |
| 42 | + ASYNC_TASK_STATUS_NOT_STARTED < ASYNC_TASK_STATUS_RUNNING && |
| 43 | + ASYNC_TASK_STATUS_RUNNING < ASYNC_TASK_STATUS_CANCELLED && |
| 44 | + ASYNC_TASK_STATUS_CANCELLED < ASYNC_TASK_STATUS_COMPLETE), |
| 45 | + "ASYNC_TASK_STATUS enum values are not ordered correctly"); |
| 46 | + |
| 47 | + ASYNC_TASK_STATUS Update(bool WaitForCompletion) |
| 48 | + { |
| 49 | + ASYNC_TASK_STATUS CurrStatus = m_Status.load(); |
| 50 | + VERIFY_EXPR(CurrStatus != ASYNC_TASK_STATUS_UNKNOWN); |
| 51 | + if (CurrStatus <= ASYNC_TASK_STATUS_RUNNING) |
| 52 | + { |
| 53 | + RefCntAutoPtr<IAsyncTask> pTask; |
| 54 | + { |
| 55 | + Threading::SpinLockGuard Guard{m_TaskLock}; |
| 56 | + pTask = m_wpTask.Lock(); |
| 57 | + } |
| 58 | + |
| 59 | + ASYNC_TASK_STATUS NewStatus = ASYNC_TASK_STATUS_UNKNOWN; |
| 60 | + if (pTask) |
| 61 | + { |
| 62 | + if (WaitForCompletion) |
| 63 | + { |
| 64 | + pTask->WaitForCompletion(); |
| 65 | + } |
| 66 | + NewStatus = pTask->GetStatus(); |
| 67 | + } |
| 68 | + |
| 69 | + if (NewStatus == ASYNC_TASK_STATUS_CANCELLED || NewStatus == ASYNC_TASK_STATUS_COMPLETE) |
| 70 | + { |
| 71 | + Threading::SpinLockGuard Guard{m_TaskLock}; |
| 72 | + m_wpTask.Release(); |
| 73 | + } |
| 74 | + |
| 75 | + if (NewStatus > CurrStatus) |
| 76 | + { |
| 77 | + while (!m_Status.compare_exchange_weak(CurrStatus, std::max(CurrStatus, NewStatus))) |
| 78 | + { |
| 79 | + // If exchange fails, CurrStatus will hold the actual value of m_Status. |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return m_Status.load(); |
| 85 | + } |
| 86 | + |
| 87 | + ASYNC_TASK_STATUS GetStatus() const |
| 88 | + { |
| 89 | + return m_Status.load(); |
| 90 | + } |
| 91 | + |
| 92 | + RefCntAutoPtr<IAsyncTask> GetCompileTask() const |
| 93 | + { |
| 94 | + Threading::SpinLockGuard Guard{m_TaskLock}; |
| 95 | + return m_wpTask.Lock(); |
| 96 | + } |
| 97 | + |
| 98 | + template <typename HanlderType> |
| 99 | + static std::unique_ptr<AsyncInitializer> Start(IThreadPool* pThreadPool, |
| 100 | + IAsyncTask** ppPrerequisites, |
| 101 | + Uint32 NumPrerequisites, |
| 102 | + HanlderType&& Handler) |
| 103 | + { |
| 104 | + VERIFY_EXPR(pThreadPool != nullptr); |
| 105 | + return std::unique_ptr<AsyncInitializer>{ |
| 106 | + new AsyncInitializer{EnqueueAsyncWork(pThreadPool, ppPrerequisites, NumPrerequisites, std::forward<HanlderType>(Handler))}, |
| 107 | + }; |
| 108 | + } |
| 109 | + |
| 110 | + template <typename HanlderType> |
| 111 | + static std::unique_ptr<AsyncInitializer> Start(IThreadPool* pThreadPool, |
| 112 | + HanlderType&& Handler) |
| 113 | + { |
| 114 | + return Start(pThreadPool, nullptr, 0, std::forward<HanlderType>(Handler)); |
| 115 | + } |
| 116 | + |
| 117 | + static ASYNC_TASK_STATUS Update(std::unique_ptr<AsyncInitializer>& Initializer, bool WaitForCompletion) |
| 118 | + { |
| 119 | + return Initializer ? Initializer->Update(WaitForCompletion) : ASYNC_TASK_STATUS_UNKNOWN; |
| 120 | + } |
| 121 | + |
| 122 | + static RefCntAutoPtr<IAsyncTask> GetAsyncTask(const std::unique_ptr<AsyncInitializer>& Initializer) |
| 123 | + { |
| 124 | + return Initializer ? Initializer->GetCompileTask() : RefCntAutoPtr<IAsyncTask>{}; |
| 125 | + } |
| 126 | + |
| 127 | +private: |
| 128 | + AsyncInitializer(RefCntAutoPtr<IAsyncTask> pTask) : |
| 129 | + m_wpTask{pTask} |
| 130 | + { |
| 131 | + // Do not get the actual status from the task since it needs to be done in Update(). |
| 132 | + // The task may happen to be complete by the time the initializer is created. |
| 133 | + } |
| 134 | + |
| 135 | +private: |
| 136 | + // It is important to set the task status to a non-unknown value before the task is started. |
| 137 | + std::atomic<ASYNC_TASK_STATUS> m_Status{ASYNC_TASK_STATUS_NOT_STARTED}; |
| 138 | + |
| 139 | + // Note that while RefCntAutoPtr/RefCntWeakPtr allow safely accessing the same object |
| 140 | + // from multiple threads using different pointers, they are not thread-safe by themselves |
| 141 | + // (e.g. it is not safe to call Lock() or Release() on the same pointer from multiple threads). |
| 142 | + // Therefore, we need to use a lock to protect access to m_wpTask. |
| 143 | + mutable Threading::SpinLock m_TaskLock; |
| 144 | + mutable RefCntWeakPtr<IAsyncTask> m_wpTask; |
| 145 | +}; |
| 146 | + |
| 147 | +} // namespace Diligent |
0 commit comments