text: /*
  
  Hork Engine Source Code
  
  MIT License
  
  Copyright (C) 2017-2022 Alexander Samusev.
  
  This file is part of the Hork Engine Source Code.
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
  
  */
  
  #pragma once
  
  #include <Containers/PodVector.h>
  #include <Core/Ref.h>
  
  //#define HK_ACTIVE_THREADS_COUNTERS
  
  /** Job for job list */
  struct SAsyncJob
  {
      /** Callback for the job */
      void (*Callback)(void*);
      /** Data that will be passed for the job */
      void* Data;
      /** Pointer to the next job in job list */
      SAsyncJob* Next;
  };
  
  class AAsyncJobManager;
  
  /** Job list */
  class AAsyncJobList final
  {
      HK_FORBID_COPY(AAsyncJobList)
  
      friend class AAsyncJobManager;
  
  public:
      /** Set job pool size (max jobs for the list) */
      void SetMaxParallelJobs(int _MaxParallelJobs);
  
      /** Get job pool size */
      int GetMaxParallelJobs() const;
  
      /** Add job to the list */
      void AddJob(void (*_Callback)(void*), void* _Data);
  
      /** Submit jobs to worker threads */
      void Submit();
  
      /** Block current thread while jobs are in working threads */
      void Wait();
  
      /** Submit jobs to worker threads and block current thread while jobs are in working threads */
      void SubmitAndWait();
  
  private:
      AAsyncJobList();
      ~AAsyncJobList();
  
      AAsyncJobManager* JobManager{nullptr};
  
      TPodVector<SAsyncJob, 1024> JobPool;
      SAsyncJob*                  JobList{nullptr};
      int                         NumPendingJobs{0};
  
      SAsyncJob* SubmittedJobs{nullptr};
      AMutex     SubmitSync;
  
      AAtomicInt SubmittedJobsCount{0};
      AAtomicInt FetchCount{0};
      //AAtomicInt FetchLock{ 0 };
  
      ASyncEvent EventDone;
      bool       bSignalled{false}; // FIXME: must be atomic?
  };
  
  HK_FORCEINLINE int AAsyncJobList::GetMaxParallelJobs() const
  {
      return JobPool.Capacity();
  }
  
  /** Job manager */
  class AAsyncJobManager final : public ARefCounted
  {
      HK_FORBID_COPY(AAsyncJobManager)
  
  public:
      static constexpr int MAX_WORKER_THREADS = 4;
      static constexpr int MAX_JOB_LISTS      = 4;
  
      /** Initialize job manager. Set worker threads count and create job lists */
      AAsyncJobManager(int _NumWorkerThreads, int _NumJobLists);
  
      ~AAsyncJobManager();
  
      void SubmitJobList(AAsyncJobList* InJobList);
  
      /** Wakeup worker threads for the new jobs */
      void NotifyThreads();
  
      /** Get job list by the index */
      AAsyncJobList* GetAsyncJobList(int _Index)
      {
          HK_ASSERT(_Index >= 0 && _Index < NumJobLists);
          return &JobList[_Index];
      }
  
      /** Get worker threads count */
      int GetNumWorkerThreads() const { return NumWorkerThreads; }
  
  #ifdef HK_ACTIVE_THREADS_COUNTERS
      int GetNumActiveThreads() const
      {
          return NumActiveThreads.Load();
      }
  #endif
  
  private:
      static void _WorkerThreadRoutine(void* _Data);
  
      void WorkerThreadRoutine(int _ThreadId);
  
      AThread WorkerThread[MAX_WORKER_THREADS];
      int     NumWorkerThreads{0};
  
  #ifdef HK_ACTIVE_THREADS_COUNTERS
      AAtomicInt NumActiveThreads{0};
  #endif
  
      ASyncEvent EventNotify[MAX_WORKER_THREADS];
  
      AAsyncJobList JobList[MAX_JOB_LISTS];
      int           NumJobLists{0};
  
      AAtomicInt TotalJobs{0};
  
      struct SContext
      {
          AAsyncJobManager* JobManager;
          int               ThreadId;
      };
  
      SContext Contexts[MAX_WORKER_THREADS];
  
      bool bTerminated{false};
  };
  