AGXDynamics  2.31.1.0
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 *, Index > ThreadIdTable
 
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...
 
typedef void * ThreadStorageData
 
typedef pthread_key_t ThreadStorageKey
 

Public Member Functions

void cancel ()
 Threads should normally not need to be killed. More...
 
void detach ()
 Detaches the thread to the background. More...
 
agx::Uuid generateUuid ()
 Generates a unique universal identifier. More...
 
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
 
agxData::EntityStorage * getDefaultStorage (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. More...
 
Index getIndex () const
 Get an index suitable for use when storing per-thread data in e.g. More...
 
Real getOverheadTime () const
 
RandomGenerator & getRandomGenerator ()
 Return a reference to the mersienne twister used for generating random numbers. More...
 
unsigned int getThreadState ()
 Returns the current thread state. More...
 
bool hasAttribute (const Attribute *attribute) const
 
bool join ()
 Joins the thread. More...
 
bool joinable ()
 True if thread is joinable. More...
 
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. More...
 
void resetOverheadTime ()
 Reset the thread overhead time. More...
 
bool setThreadAffinity (agx::UInt64 cpumask)
 Thread Affinity can be used to influence on which logical cores threads are scheduled and allowed to run. More...
 
bool start ()
 Start the thread. More...
 
void stop ()
 Stop the thread. More...
 

Static Public Member Functions

static void addTask (Task *task)
 
static ThreadStorageKey allocatePerThreadStorage ()
 Allocate a storage location that is unique for each thread. More...
 
static void exportAllTimelines ()
 
static void flushLogs ()
 
static void flushTimelineLogs ()
 
static void freePerThreadStorage (ThreadStorageKey key)
 Deallocate the storage location. More...
 
static Thread * getCurrentThread ()
 
static std::string getCurrentThreadDescription ()
 
static std::thread::native_handle_type getCurrentThreadHandle ()
 Return a native_handle for the current executing thread. More...
 
static bool getEnableJobTimeline ()
 
static Thread * getMainThread ()
 
static Thread::ThreadIdTable * getPromotedThreads ()
 
static Thread * getThread (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. More...
 
static ThreadStorageData readPerThreadStorage (ThreadStorageKey key)
 Read the thread-local value for the currently executing thread associated with the given key. More...
 
static Thread * registerAsAgxThread ()
 Register the current thread as an AGX thread. More...
 
static void resetStartTick ()
 
static void setEnableJobTimeline (bool flag)
 Enable or disable job timeline statistics. More...
 
static void shutdown ()
 Shutdown the threading system. More...
 
static void unregisterAsAgxThread ()
 Remove agx attributes from current thread. More...
 
static void writePerThreadStorage (ThreadStorageKey key, ThreadStorageData data)
 Write to the thread-local location owned by the currently executing thread. More...
 

Static Public Attributes

static bool immediateLogging
 

Protected Member Functions

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

Protected Attributes

std::thread m_handle
 
std::atomic< unsigned int > m_state
 

Friends

void AGXCORE_EXPORT setNumThreads (size_t numThreads)
 Set the number of threads to use (including the main thread). More...
 
void AGXPHYSICS_EXPORT shutdown ()
 Shutdown agx. More...
 

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 197 of file Thread.h.

Member Typedef Documentation

◆ RandomGenerator

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

Definition at line 212 of file Thread.h.

◆ ThreadIdTable

Definition at line 386 of file Thread.h.

◆ ThreadStorageData

Definition at line 209 of file Thread.h.

◆ ThreadStorageKey

typedef pthread_key_t agx::Thread::ThreadStorageKey

Definition at line 208 of file Thread.h.

Member Enumeration Documentation

◆ ThreadState

States for the thread.

Enumerator
THREAD_NEW 
THREAD_RUNNING 
THREAD_DETACHED 
THREAD_DONE 
THREAD_JOINED 
THREAD_CANCELLED 

Definition at line 88 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.

◆ cancel()

void agx::BasicThread::cancel ( )
inherited

Threads should normally not need to be killed.

Killing a thread can have bad consequences and leak resources. This will forcefully terminate the thread.

The thread should still be joined even after cancel has been called.

◆ detach()

void agx::BasicThread::detach ( )
inherited

Detaches the thread to the background.

Will no longer be joinable.

◆ 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

◆ getAttribute() [1/4]

void* agxData::AttributeContainer::getAttribute ( const Attribute *  attribute)
inherited
Returns
The value corresponding to an attribute.

◆ getAttribute() [2/4]

const void* agxData::AttributeContainer::getAttribute ( const Attribute *  attribute) const
inherited

◆ getAttribute() [3/4]

template<typename T >
T & agxData::AttributeContainer::getAttribute ( const ScalarAttributeT< T > *  attribute)
inherited

Definition at line 65 of file AttributeContainer.h.

◆ getAttribute() [4/4]

template<typename T >
const T & agxData::AttributeContainer::getAttribute ( const ScalarAttributeT< T > *  attribute) const
inherited

Definition at line 71 of file AttributeContainer.h.

◆ getCurrentThread()

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

Definition at line 695 of file Thread.h.

Referenced by agx::Barrier::block(), and agx::Block::block().

◆ 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

◆ getCurrentThreadHandle()

static std::thread::native_handle_type agx::BasicThread::getCurrentThreadHandle ( )
staticinherited

Return a native_handle for the current executing 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 684 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 703 of file Thread.h.

References AGX_FORCE_INLINE, and agxAssert.

◆ getOverheadTime()

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

Definition at line 735 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 730 of file Thread.h.

◆ getThread()

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

Definition at line 689 of file Thread.h.

References agxAssert.

◆ getThreadState()

unsigned int agx::BasicThread::getThreadState ( )
inlineinherited

Returns the current thread state.

Definition at line 677 of file Thread.h.

◆ hasAttribute()

bool agxData::AttributeContainer::hasAttribute ( const Attribute *  attribute) const
inherited
Returns
True if an attribute is active/allocated.

◆ initThreadSystem()

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

◆ isMainThread()

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

Definition at line 716 of file Thread.h.

References AGX_FORCE_INLINE, and agx::getCurrentThread().

◆ isShuttingDown()

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

◆ join()

bool agx::BasicThread::join ( )
inherited

Joins the thread.

Will block until the thread is done executing.

◆ joinable()

bool agx::BasicThread::joinable ( )
inlineinherited

True if thread is joinable.

Definition at line 670 of file Thread.h.

◆ log()

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

◆ 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.

◆ 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.

◆ setThreadAffinity()

bool agx::BasicThread::setThreadAffinity ( agx::UInt64  cpumask)
inherited

Thread Affinity can be used to influence on which logical cores threads are scheduled and allowed to run.

On Windows and Linux, the input is a bitmask where the bits that are set are maps to logical cores.

On OS X, the input value is an affinity tag and threads with the same tag are scheduled so they should share and L2 cache.

Note: If the BasicThread has not yet been launched via start(), then this method will affect the current executing thread.

◆ 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.

Friends And Related Function Documentation

◆ setNumThreads

void AGXCORE_EXPORT setNumThreads ( size_t  numThreads)
friend

Set the number of threads to use (including the main thread).

Parameters
numThreads- The total available number of threads. If 0, then all CPU:s/Cores will be used.

◆ shutdown

void AGXPHYSICS_EXPORT shutdown ( )
friend

Shutdown agx.

Must be called before end of main()/unload of dll:s etc.

Member Data Documentation

◆ immediateLogging

bool agx::Thread::immediateLogging
static

Definition at line 373 of file Thread.h.

◆ m_handle

std::thread agx::BasicThread::m_handle
protectedinherited

Definition at line 185 of file Thread.h.

◆ m_state

std::atomic< unsigned int> agx::BasicThread::m_state
protectedinherited

Definition at line 186 of file Thread.h.


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