Main Page   Class List   Class Members  

  • Main Page
  • User's Guide
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

sdk/extensions/physx/source/physics/NvBlastExtPxTaskImpl.h

Go to the documentation of this file.
00001 // This code contains NVIDIA Confidential Information and is disclosed to you
00002 // under a form of NVIDIA software license agreement provided separately to you.
00003 //
00004 // Notice
00005 // NVIDIA Corporation and its licensors retain all intellectual property and
00006 // proprietary rights in and to this software and related documentation and
00007 // any modifications thereto. Any use, reproduction, disclosure, or
00008 // distribution of this software and related documentation without an express
00009 // license agreement from NVIDIA Corporation is strictly prohibited.
00010 //
00011 // ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
00012 // NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
00013 // THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
00014 // MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
00015 //
00016 // Information and code furnished is believed to be accurate and reliable.
00017 // However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
00018 // information or for any infringement of patents or other rights of third parties that may
00019 // result from its use. No license is granted by implication or otherwise under any patent
00020 // or patent rights of NVIDIA Corporation. Details are subject to change without notice.
00021 // This code supersedes and replaces all information previously supplied.
00022 // NVIDIA Corporation products are not authorized for use as critical
00023 // components in life support devices or systems without express written approval of
00024 // NVIDIA Corporation.
00025 //
00026 // Copyright (c) 2016-2020 NVIDIA Corporation. All rights reserved.
00027 
00028 
00029 #ifndef NVBLASTEXTPXTASKIMPL_H
00030 #define NVBLASTEXTPXTASKIMPL_H
00031 
00032 #include "NvBlastExtPxTask.h"
00033 #include "PxTask.h"
00034 #include "NvBlastTkGroup.h"
00035 
00036 #include <atomic>
00037 #include <mutex>
00038 #include <condition_variable>
00039 
00040 namespace Nv
00041 {
00042 namespace Blast
00043 {
00044 
00048 class ExtTaskSync
00049 {
00050 public:
00054     ExtTaskSync(uint32_t count) : m_count(count) {}
00055 
00059     void wait()
00060     {
00061         std::unique_lock<std::mutex> lk(m_mutex);
00062         m_cv.wait(lk, [&] { return m_count == 0; });
00063     }
00064 
00068     void notify()
00069     {
00070         //PERF_SCOPE_H("TaskSync::notify");
00071         std::unique_lock<std::mutex> lk(m_mutex);
00072         if (m_count > 0)
00073         {
00074             m_count--;
00075         }
00076         if (m_count == 0)
00077         {
00078             lk.unlock();
00079             m_cv.notify_one();
00080         }
00081     }
00082 
00086     bool isDone()
00087     {
00088         std::unique_lock<std::mutex> lk(m_mutex);
00089         return m_count == 0;
00090     }
00091 
00095     void setCount(uint32_t count)
00096     {
00097         m_count = count;
00098     }
00099 
00100 private:
00101     std::mutex              m_mutex;
00102     std::condition_variable m_cv;
00103     uint32_t                m_count;
00104 };
00105 
00106 
00110 class ExtAtomicCounter
00111 {
00112 public:
00113     ExtAtomicCounter() : m_current(0), m_maxCount(0) {}
00114 
00115     bool isValid(uint32_t val)
00116     {
00117         return val < m_maxCount;
00118     }
00119 
00120     uint32_t next()
00121     {
00122         return m_current.fetch_add(1);
00123     }
00124 
00125     void reset(uint32_t maxCount)
00126     {
00127         m_maxCount = maxCount;
00128         m_current = 0;
00129     }
00130 private:
00131     std::atomic<uint32_t> m_current;
00132     uint32_t m_maxCount;
00133 };
00134 
00135 
00139 class ExtGroupWorkerTask : public physx::PxLightCpuTask
00140 {
00141 public:
00142     ExtGroupWorkerTask() : PxLightCpuTask(), m_group(nullptr), m_counter(nullptr), m_sync(nullptr)
00143     {
00144     }
00145 
00146     void setup(TkGroup* group, ExtAtomicCounter* counter, ExtTaskSync* sync)
00147     {
00148         m_group = group;
00149         m_counter = counter;
00150         m_sync = sync;
00151     }
00152 
00153     virtual void run() override
00154     {
00155         Nv::Blast::TkGroupWorker* worker = m_group->acquireWorker();
00156         uint32_t jobID = m_counter->next();
00157         while (m_counter->isValid(jobID))
00158         {
00159             worker->process(jobID);
00160             jobID = m_counter->next();
00161         }
00162         m_group->returnWorker(worker);
00163     }
00164 
00165     virtual void release() override
00166     {
00167         PxLightCpuTask::release();
00168 
00169         // release the sync last
00170         m_sync->notify();
00171     }
00172 
00173     virtual const char* getName() const override { return "BlastGroupWorkerTask"; }
00174 
00175 private:
00176     TkGroup* m_group;
00177     ExtAtomicCounter* m_counter;
00178     ExtTaskSync* m_sync;
00179 };
00180 
00181 
00185 class ExtGroupTaskManagerImpl : public ExtGroupTaskManager
00186 {
00187 public:
00188     ExtGroupTaskManagerImpl(physx::PxTaskManager& taskManager, TkGroup* group)
00189         : m_taskManager(taskManager), m_sync(0), m_group(group) {}
00190 
00191     // ExtGroupTaskManager API
00192     virtual void setGroup(TkGroup*) override;
00193     virtual uint32_t process(uint32_t) override;
00194     virtual void release() override;
00195     virtual bool wait(bool block) override;
00196 
00197 private:
00198     static const uint32_t       TASKS_MAX_COUNT = 16;
00199     physx::PxTaskManager&       m_taskManager;
00200     ExtAtomicCounter            m_counter;
00201     ExtGroupWorkerTask          m_tasks[TASKS_MAX_COUNT];
00202     ExtTaskSync                 m_sync;
00203     TkGroup*                    m_group;
00204 };
00205 
00206 } // namespace Blast
00207 } // namespace Nv
00208 
00209 #endif // NVBLASTEXTPXTASKIMPL_H
Copyright © 2015-2017 NVIDIA Corporation, 2701 San Tomas Expressway, Santa Clara, CA 95050 U.S.A. All rights reserved. www.nvidia.com