#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2023 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License #ifndef NUCLEX_PLATFORM_TASKS_TASKCOORDINATOR_H #define NUCLEX_PLATFORM_TASKS_TASKCOORDINATOR_H #include "Nuclex/Platform/Config.h" #include "Nuclex/Platform/Tasks/ResourceType.h" #include // for std::string #include // for std::atomic, std::atomic_thread_fence #include // for std::shared_ptr namespace Nuclex { namespace Platform { namespace Tasks { // ------------------------------------------------------------------------------------------- // class Task; class TaskEnvironment; class ResourceManifest; // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Platform::Tasks namespace Nuclex { namespace Platform { namespace Tasks { // ------------------------------------------------------------------------------------------- // /// Coordinates background tasks based on their usage of system resouces class NUCLEX_PLATFORM_TYPE TaskCoordinator { /// Frees all resources owned by the task coordinator public: NUCLEX_PLATFORM_API virtual ~TaskCoordinator() = default; /// Queries the amount of a resource the system has in total /// Type of resource that will be queried /// The total amount of the queried resource in the system /// /// If there are multiple resource units, for example on a system with multiple GPUs, /// querying for video memory will return the highest amount of video memory available /// on any single GPU. The behavior is the same for all resource units. /// public: NUCLEX_PLATFORM_API virtual std::size_t QueryResourceMaximum( ResourceType resourceType ) const = 0; /// Schedules the specified task for execution /// Task that will be executed as soon as resources permit /// Resources that the task will occupy public: NUCLEX_PLATFORM_API virtual void Schedule(const std::shared_ptr &task) = 0; /// Schedules the specified task for execution /// /// Environment that needs to be active while the task executes /// /// Task that will be executed as soon as resources permit public: NUCLEX_PLATFORM_API virtual void Schedule( const std::shared_ptr &environment, const std::shared_ptr &task ) = 0; /// Schedules a task for execution with an alternative task /// /// Task that will be executed if the resources are available /// /// /// Task that can be executed instead of the preferred resources are not available /// public: NUCLEX_PLATFORM_API virtual void ScheduleWithAlternative( const std::shared_ptr &preferredTask, const std::shared_ptr &alternativeTask ) = 0; /// Schedules a task for execution with an alternative task /// /// Environment that needs to be active while the task executes /// /// /// Task that will be executed if the resources are available /// /// /// Task that can be executed instead of the preferred resources are not available /// public: NUCLEX_PLATFORM_API virtual void ScheduleWithAlternative( const std::shared_ptr &environment, const std::shared_ptr &preferredTask, const std::shared_ptr &alternativeTask ) = 0; /// Gives priority to the specified task /// Already scheduled task that will be given priority /// True if the task was found in the non-priority queue and prioritized /// /// This may be ignored but gives a hint of the task coordinator that the given task /// should be executed out of order as soon as possible. Normally used for tasks that /// set preconditions required to queue other tasks, i.e. extracting a batch of frames /// via ffmpeg so that it can be processed. /// public: NUCLEX_PLATFORM_API virtual bool Prioritize(const std::shared_ptr &task) { (void)task; return false; // By default, an implementation ignores prioritization } /// Cancels a waiting task /// Task that will be cancelled /// /// True if the task was still waiting and has been canceled, false if it wasn't found /// /// /// If the task has an alternative, that one will be cancelled, too. Specifying /// the alternative for cancellation is not allowed. /// public: NUCLEX_PLATFORM_API virtual bool Cancel(const std::shared_ptr &task) = 0; /// Cancels all waiting tasks /// Whether to cancel all future tasks, too /// /// Is usually called when the task coordinator shuts down to cancel all waiting tasks /// public: NUCLEX_PLATFORM_API virtual void CancelAll(bool forever = true) = 0; }; // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Platform::Tasks #endif // NUCLEX_PLATFORM_TASKS_TASKCOORDINATOR_H