AGX Dynamics 2.41.3.2
Loading...
Searching...
No Matches
agx::Thread Class Reference

agx::Thread is a representation of an OS specific implementation of a computational thread. More...

#include <Thread.h>

+ Inheritance diagram for agx::Thread:

Public Types

typedef std::mt19937 RandomGenerator
 
typedef HashTable< Thread *, IndexThreadIdTable
 
typedef void * ThreadStorageData
 
typedef pthread_key_t ThreadStorageKey
 
- Public Types inherited from agx::BasicThread
enum  ThreadState {
  THREAD_NEW = 0 , THREAD_RUNNING = 1 , THREAD_DETACHED = 2 , THREAD_DONE = 3 ,
  THREAD_JOINED = 4 , THREAD_CANCELLED = 5
}
 States for the thread. More...
 

Public Member Functions

agx::Uuid generateUuid ()
 Generates a unique universal identifier.
 
agxData::EntityStoragegetDefaultStorage (agxData::EntityModel *entity)
 
Index getId () const
 Returns the thread's AGX thread ID, a value between 0 and N-1, for AGX's internal threads, i.e.
 
Index getIndex () const
 Get an index suitable for use when storing per-thread data in e.g.
 
Real getOverheadTime () const
 
RandomGeneratorgetRandomGenerator ()
 Return a reference to the mersienne twister used for generating random numbers.
 
agxData::EntityStorageRef popTimelineEntryStorage ()
 
void reportSystemJob (UInt64 startTick, UInt64 endTick, const char *description, const char *extraDataTitle=nullptr, agx::Real64 extraData=0.0)
 Add an entry to the job duration log.
 
void resetOverheadTime ()
 Reset the thread overhead time.
 
bool start ()
 Start the thread.
 
void stop ()
 Stop the thread.
 
- Public Member Functions inherited from agx::BasicThread
 BasicThread ()
 Default constructor.
 
 BasicThread (const BasicThread &other)=delete
 
virtual ~BasicThread ()=default
 Destructor.
 
void cancel ()
 Threads should normally not need to be killed.
 
void detach ()
 Detaches the thread to the background.
 
unsigned int getThreadState ()
 Returns the current thread state.
 
bool join ()
 Joins the thread.
 
bool joinable ()
 True if thread is joinable.
 
BasicThreadoperator= (const BasicThread &rhs)=delete
 
virtual void run ()
 This method is invoked by start.
 
bool setThreadAffinity (agx::UInt64 cpumask)
 Thread Affinity can be used to influence on which logical cores threads are scheduled and allowed to run.
 
bool start ()
 Launches the thread.
 
- Public Member Functions inherited from agxData::AttributeContainer
 AttributeContainer ()
 
 ~AttributeContainer ()
 
void * getAttribute (const Attribute *attribute)
 
const void * getAttribute (const Attribute *attribute) const
 
template<typename T >
T & getAttribute (const ScalarAttributeT< T > *attribute)
 
template<typename T >
const T & getAttribute (const ScalarAttributeT< T > *attribute) const
 
bool hasAttribute (const Attribute *attribute) const
 

Static Public Member Functions

static void addTask (Task *task)
 
static ThreadStorageKey allocatePerThreadStorage ()
 Allocate a storage location that is unique for each thread.
 
static void exportAllTimelines ()
 
static void flushLogs ()
 
static void flushTimelineLogs ()
 
static void freePerThreadStorage (ThreadStorageKey key)
 Deallocate the storage location.
 
static ThreadgetCurrentThread ()
 
static std::string getCurrentThreadDescription ()
 
static bool getEnableJobTimeline ()
 
static ThreadgetMainThread ()
 
static Thread::ThreadIdTablegetPromotedThreads ()
 
static ThreadgetThread (size_t id)
 
static void initThreadSystem ()
 
static bool isMainThread ()
 
static bool isShuttingDown ()
 
static int log (const char *format,...)
 
static void makeCurrentThreadMainThread ()
 Register current thread as main thread.
 
static ThreadStorageData readPerThreadStorage (ThreadStorageKey key)
 Read the thread-local value for the currently executing thread associated with the given key.
 
static ThreadregisterAsAgxThread ()
 Register the current thread as an AGX thread.
 
static void resetStartTick ()
 
static void setEnableJobTimeline (bool flag)
 Enable or disable job timeline statistics.
 
static void shutdown ()
 Shutdown the threading system.
 
static void unregisterAsAgxThread ()
 Remove agx attributes from current thread.
 
static void writePerThreadStorage (ThreadStorageKey key, ThreadStorageData data)
 Write to the thread-local location owned by the currently executing thread.
 
- Static Public Member Functions inherited from agx::BasicThread
static std::thread::native_handle_type getCurrentThreadHandle ()
 Return a native_handle for the current executing thread.
 

Static Public Attributes

static bool immediateLogging
 

Protected Member Functions

 Thread (const Thread &)=delete
 
 Thread (Index id)
 
virtual ~Thread ()
 

Additional Inherited Members

- Protected Attributes inherited from agx::BasicThread
std::thread m_handle
 
std::atomic< unsigned int > m_state
 

Detailed Description

agx::Thread is a representation of an OS specific implementation of a computational thread.

Threads can execute generic tasks (agx::Task) and jobs (agx::Job).

Definition at line 199 of file Thread.h.

Member Typedef Documentation

◆ RandomGenerator

typedef std::mt19937 agx::Thread::RandomGenerator

Definition at line 214 of file Thread.h.

◆ ThreadIdTable

Definition at line 382 of file Thread.h.

◆ ThreadStorageData

Definition at line 211 of file Thread.h.

◆ ThreadStorageKey

typedef pthread_key_t agx::Thread::ThreadStorageKey

Definition at line 210 of file Thread.h.

Constructor & Destructor Documentation

◆ Thread() [1/2]

agx::Thread::Thread ( Index  id)
protected

◆ Thread() [2/2]

agx::Thread::Thread ( const Thread )
protecteddelete

◆ ~Thread()

virtual agx::Thread::~Thread ( )
protectedvirtual

Member Function Documentation

◆ addTask()

static void agx::Thread::addTask ( Task task)
static

◆ allocatePerThreadStorage()

static ThreadStorageKey agx::Thread::allocatePerThreadStorage ( )
static

Allocate a storage location that is unique for each thread.

This key will be usable from all threads.

◆ exportAllTimelines()

static void agx::Thread::exportAllTimelines ( )
static

◆ flushLogs()

static void agx::Thread::flushLogs ( )
static

◆ flushTimelineLogs()

static void agx::Thread::flushTimelineLogs ( )
static

◆ freePerThreadStorage()

static void agx::Thread::freePerThreadStorage ( ThreadStorageKey  key)
static

Deallocate the storage location.

No thread can use 'key' after this method has been called.

◆ generateUuid()

agx::Uuid agx::Thread::generateUuid ( )

Generates a unique universal identifier.

Returns
a Unique Universal identifier

◆ getCurrentThread()

static Thread * agx::Thread::getCurrentThread ( )
static
Returns
the current thread, or nullptr if not a registered AgxThread (see registerAsAgxThread).

Referenced by agxTerrain::ScopeReportSystemJob::~ScopeReportSystemJob(), agx::Block::block(), agx::Barrier::block(), isMainThread(), and log().

◆ getCurrentThreadDescription()

static std::string agx::Thread::getCurrentThreadDescription ( )
static
Returns
a string describing the current thread. The format is "os, agx" where 'os' is an OS specific identifier (typically a number) and 'agx' is the agx thread id, or null if this is not an agx thread

◆ getDefaultStorage()

agxData::EntityStorage * agx::Thread::getDefaultStorage ( agxData::EntityModel entity)

◆ getEnableJobTimeline()

static bool agx::Thread::getEnableJobTimeline ( )
static

◆ getId()

Index agx::Thread::getId ( ) const

Returns the thread's AGX thread ID, a value between 0 and N-1, for AGX's internal threads, i.e.

threads created in response to a call to agx::setNumThreads. For threads created using Thread::registerAsAgxThread agx::InvalidIndex is returned.

Definition at line 650 of file Thread.h.

◆ getIndex()

Index agx::Thread::getIndex ( ) const

Get an index suitable for use when storing per-thread data in e.g.

a Vector. The returned index is the thread ID for regular AGX worker threads and a value larger than the largest thread ID for non-AGX threads that has called registerAsAgxThread.

The value returned may change when the number of AGX threads is changed, and when other threads are registered or unregistered as AGX threads. It is therefor not recommended to store this value for longer than a single time step.

◆ getMainThread()

Thread * agx::Thread::getMainThread ( )
static
Returns
the main thread.

Definition at line 661 of file Thread.h.

References agxAssert.

◆ getOverheadTime()

Real agx::Thread::getOverheadTime ( ) const
Returns
The overhead time for the thread, including jobpool stalls and job administration.

Definition at line 675 of file Thread.h.

◆ getPromotedThreads()

static Thread::ThreadIdTable * agx::Thread::getPromotedThreads ( )
static

◆ getRandomGenerator()

Thread::RandomGenerator & agx::Thread::getRandomGenerator ( )

Return a reference to the mersienne twister used for generating random numbers.

This is local to the thread and can be used safely from the current thread

Returns
reference to a mt19937 random generator

Definition at line 670 of file Thread.h.

◆ getThread()

Thread * agx::Thread::getThread ( size_t  id)
static
Returns
a specific thread.

Definition at line 655 of file Thread.h.

References agxAssert.

◆ initThreadSystem()

static void agx::Thread::initThreadSystem ( )
static

◆ isMainThread()

bool agx::Thread::isMainThread ( )
static
Returns
True if main thread.

Definition at line 667 of file Thread.h.

References getCurrentThread().

◆ isShuttingDown()

static bool agx::Thread::isShuttingDown ( )
static

◆ log()

int agx::Thread::log ( const char *  format,
  ... 
)
inlinestatic

Definition at line 678 of file Thread.h.

References getCurrentThread().

◆ makeCurrentThreadMainThread()

static void agx::Thread::makeCurrentThreadMainThread ( )
static

Register current thread as main thread.

NOTE: The old main thread will also think it is the main thread!

◆ popTimelineEntryStorage()

agxData::EntityStorageRef agx::Thread::popTimelineEntryStorage ( )

◆ readPerThreadStorage()

static ThreadStorageData agx::Thread::readPerThreadStorage ( ThreadStorageKey  key)
static

Read the thread-local value for the currently executing thread associated with the given key.

◆ registerAsAgxThread()

static Thread * agx::Thread::registerAsAgxThread ( )
static

Register the current thread as an AGX thread.

Necessary to execute agx::Task instances. This is normally handled automatically.

◆ reportSystemJob()

void agx::Thread::reportSystemJob ( UInt64  startTick,
UInt64  endTick,
const char *  description,
const char *  extraDataTitle = nullptr,
agx::Real64  extraData = 0.0 
)

Add an entry to the job duration log.

Intended to be used when the system performs some lengthy operation without using the job system.

This will NOT copy the content of 'description' and 'datatitle', so don't pass pointers to anything that will be deallocated before the next disk writing.

Referenced by agxTerrain::ScopeReportSystemJob::~ScopeReportSystemJob().

◆ resetOverheadTime()

void agx::Thread::resetOverheadTime ( )

Reset the thread overhead time.

◆ resetStartTick()

static void agx::Thread::resetStartTick ( )
static

◆ setEnableJobTimeline()

static void agx::Thread::setEnableJobTimeline ( bool  flag)
static

Enable or disable job timeline statistics.

◆ shutdown()

static void agx::Thread::shutdown ( )
static

Shutdown the threading system.

After this no threads will be available.

◆ start()

bool agx::Thread::start ( )

Start the thread.

◆ stop()

void agx::Thread::stop ( )

Stop the thread.

◆ unregisterAsAgxThread()

static void agx::Thread::unregisterAsAgxThread ( )
static

Remove agx attributes from current thread.

This is normally handled automatically.

◆ writePerThreadStorage()

static void agx::Thread::writePerThreadStorage ( ThreadStorageKey  key,
ThreadStorageData  data 
)
static

Write to the thread-local location owned by the currently executing thread.

Member Data Documentation

◆ immediateLogging

bool agx::Thread::immediateLogging
static

Definition at line 369 of file Thread.h.


The documentation for this class was generated from the following file: