TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
G3D::GThread Class Referenceabstract

#include <GThread.h>

Public Types

enum  { NUM_CORES = -100 }
 
typedef shared_ptr< class GThreadRef
 

Public Member Functions

 GThread (const std::string &name)
 
virtual ~GThread ()
 
bool start (SpawnBehavior behavior=USE_NEW_THREAD)
 
void terminate ()
 
bool running () const
 
bool started () const
 
bool completed () const
 
void waitForCompletion ()
 
const std::string & name ()
 
- Public Member Functions inherited from G3D::ReferenceCountedObject
virtual ~ReferenceCountedObject ()
 

Static Public Member Functions

static int numCores ()
 
static GThreadRef create (const std::string &name, void(*proc)(void *), void *param=NULL)
 
template<class Class >
static void runConcurrently2D (const Vector2int32 &start, const Vector2int32 &upTo, Class *object, void(Class::*method)(int x, int y), int maxThreads=NUM_CORES)
 Iterates over a 2D region using multiple threads and blocks until all threads have completed. More...
 
template<class Class >
static void runConcurrently2D (const Vector2int32 &start, const Vector2int32 &upTo, Class *object, void(Class::*method)(int x, int y, int threadID), int maxThreads=NUM_CORES)
 

Static Public Attributes

static const SpawnBehavior USE_CURRENT_THREAD = G3D::USE_CURRENT_THREAD
 
static const SpawnBehavior USE_NEW_THREAD = G3D::USE_NEW_THREAD
 

Protected Member Functions

virtual void threadMain ()=0
 

Private Types

enum  GStatus { STATUS_CREATED, STATUS_STARTED, STATUS_RUNNING, STATUS_COMPLETED }
 

Private Member Functions

 GThread (const GThread &)
 
GThreadoperator= (const GThread &)
 
bool operator== (const GThread &)
 

Static Private Member Functions

static void * internalThreadProc (void *param)
 

Private Attributes

volatile GStatus m_status
 
pthread_t m_handle
 
std::string m_name
 

Detailed Description

Platform independent thread implementation. You can either subclass and override GThread::threadMain or call the create method with a method.

Beware of reference counting and threads. If circular references exist between GThread subclasses then neither class will ever be deallocated. Also, dropping all pointers (and causing deallocation) of a GThread does NOT stop the underlying process.

See also
G3D::GMutex, G3D::Spinlock, G3D::AtomicInt32, G3D::ThreadSet

Member Typedef Documentation

typedef shared_ptr<class GThread> G3D::GThread::Ref

Member Enumeration Documentation

anonymous enum
Enumerator
NUM_CORES 

Tells GThread::runConcurrently() and GThread::runConcurrently2D() to use System::numCores() threads.

130  {
133  NUM_CORES = -100
134  };
Definition: GThread.h:133
enum G3D::GThread::GStatus
private
Enumerator
STATUS_CREATED 
STATUS_STARTED 
STATUS_RUNNING 
STATUS_COMPLETED 
Definition: GThread.h:45
Definition: GThread.h:45
Definition: GThread.h:45
Definition: GThread.h:45

Constructor & Destructor Documentation

G3D::GThread::GThread ( const GThread )
private
G3D::GThread::GThread ( const std::string &  name)
37  :
39  m_name(name) {
40 
41 #ifdef G3D_WINDOWS
42  m_event = NULL;
43 #endif
44 
45  // system-independent clear of handle
46  System::memset(&m_handle, 0, sizeof(m_handle));
47 }
std::string m_name
Definition: GThread.h:68
static void memset(void *dst, uint8 value, size_t numBytes)
Definition: System.cpp:695
volatile GStatus m_status
Definition: GThread.h:58
pthread_t m_handle
Definition: GThread.h:65
arena_t NULL
Definition: jemalloc_internal.h:624
const std::string & name()
Definition: GThread.h:121
Definition: GThread.h:45

+ Here is the call graph for this function:

G3D::GThread::~GThread ( )
virtual
49  {
50 #ifdef _MSC_VER
51 # pragma warning( push )
52 # pragma warning( disable : 4127 )
53 #endif
54  alwaysAssertM(m_status != STATUS_RUNNING, "Deleting thread while running.");
55 #ifdef _MSC_VER
56 # pragma warning( pop )
57 #endif
58 
59 #ifdef G3D_WINDOWS
60  if (m_event) {
61  ::CloseHandle(m_event);
62  }
63 #endif
64 }
volatile GStatus m_status
Definition: GThread.h:58
Definition: GThread.h:45
#define alwaysAssertM(exp, message)
Definition: debugAssert.h:165

Member Function Documentation

bool G3D::GThread::completed ( ) const

Returns true if the thread has exited.

171  {
172  return (m_status == STATUS_COMPLETED);
173 }
Definition: GThread.h:45
volatile GStatus m_status
Definition: GThread.h:58
GThreadRef G3D::GThread::create ( const std::string &  name,
void(*)(void *)  proc,
void *  param = NULL 
)
static

Constructs a basic GThread without requiring a subclass.

Parameters
procThe global or static function for the threadMain()
67  {
68  return shared_ptr<GThread>(new _internal::BasicThread(name, proc, param));
69 }
const std::string & name()
Definition: GThread.h:121
void * G3D::GThread::internalThreadProc ( void *  param)
staticprivate
203  {
204  GThread* current = reinterpret_cast<GThread*>(param);
205  current->m_status = STATUS_RUNNING;
206  current->threadMain();
207  current->m_status = STATUS_COMPLETED;
208  return (void*)NULL;
209 }
Definition: GThread.h:45
arena_t NULL
Definition: jemalloc_internal.h:624
GThread(const GThread &)
Definition: GThread.h:45

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

const std::string& G3D::GThread::name ( )
inline

Returns thread name

121  {
122  return m_name;
123  }
std::string m_name
Definition: GThread.h:68
int G3D::GThread::numCores ( )
static

Returns System::numCores(); put here to break a dependence on System.h

77  {
78  return System::numCores();
79 }
static int numCores()
Definition: System.h:180

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

GThread& G3D::GThread::operator= ( const GThread )
private
bool G3D::GThread::operator== ( const GThread )
private
template<class Class >
static void G3D::GThread::runConcurrently2D ( const Vector2int32 start,
const Vector2int32 upTo,
Class *  object,
void(Class::*)(int x, int y method,
int  maxThreads = NUM_CORES 
)
inlinestatic

Iterates over a 2D region using multiple threads and blocks until all threads have completed.

Evaluates object->method(x, y) for every start.x <= x < upTo.x and start.y <= y < upTo.y. Iteration is row major, so each thread can expect to see successive x values.

Parameters
maxThreadsMaximum number of threads to use. By default at most one thread per processor core will be used.

Example:

class RayTracer {
public:
void trace(const Vector2int32& pixel) {
...
}
void traceAll() {
GThread::runConcurrently2D(Point2int32(0,0), Point2int32(w,h), this, &RayTracer::trace);
}
};
168  {
169  _internal_runConcurrently2DHelper(start, upTo, object, method, static_cast<void (Class::*)(int, int, int)>(NULL), maxThreads);
170  }
void _internal_runConcurrently2DHelper(const Vector2int32 &start, const Vector2int32 &upTo, Class *object, void(Class::*method1)(int x, int y), void(Class::*method2)(int x, int y, int threadID), int maxThreads)
Definition: GThread.h:241
arena_t NULL
Definition: jemalloc_internal.h:624

+ Here is the call graph for this function:

template<class Class >
static void G3D::GThread::runConcurrently2D ( const Vector2int32 start,
const Vector2int32 upTo,
Class *  object,
void(Class::*)(int x, int y, int threadID)  method,
int  maxThreads = NUM_CORES 
)
inlinestatic

Like the other version of runConcurrently2D, but tells the method the thread index that it is running on. That enables the caller to manage per-thread state.

182  {
183  _internal_runConcurrently2DHelper(start, upTo, object, static_cast<void (Class::*)(int, int)>(NULL), method, maxThreads);
184  }
void _internal_runConcurrently2DHelper(const Vector2int32 &start, const Vector2int32 &upTo, Class *object, void(Class::*method1)(int x, int y), void(Class::*method2)(int x, int y, int threadID), int maxThreads)
Definition: GThread.h:241
arena_t NULL
Definition: jemalloc_internal.h:624

+ Here is the call graph for this function:

bool G3D::GThread::running ( ) const

Returns true if threadMain is currently executing. This will only be set when the thread is actually running and might not be set when start() returns.

166  {
167  return (m_status == STATUS_RUNNING);
168 }
volatile GStatus m_status
Definition: GThread.h:58
Definition: GThread.h:45
bool G3D::GThread::start ( SpawnBehavior  behavior = USE_NEW_THREAD)

Starts the thread and executes threadMain(). Returns false if the thread failed to start (either because it was already started or because the OS refused).

Parameters
behaviorIf USE_CURRENT_THREAD, rather than spawning a new thread, this routine runs threadMain on the current thread.
108  {
109 
110  debugAssertM(! started(), "Thread has already executed.");
111  if (started()) {
112  return false;
113  }
114 
116 
117  if (behavior == USE_CURRENT_THREAD) {
118  // Run on this thread
120  threadMain();
122  return true;
123  }
124 
125 # ifdef G3D_WINDOWS
126  DWORD threadId;
127 
128  m_event = ::CreateEvent(NULL, TRUE, FALSE, NULL);
129  debugAssert(m_event);
130 
131  m_handle = ::CreateThread(NULL, 0, &internalThreadProc, this, 0, &threadId);
132 
133  if (m_handle == NULL) {
134  ::CloseHandle(m_event);
135  m_event = NULL;
136  }
137 
138  SetThreadName(threadId, m_name.c_str());
139 
140  return (m_handle != NULL);
141 # else
142  if (!pthread_create(&m_handle, NULL, &internalThreadProc, this)) {
143  return true;
144  } else {
145  // system-independent clear of handle
146  System::memset(&m_handle, 0, sizeof(m_handle));
147 
148  return false;
149  }
150 # endif
151 }
Definition: GThread.h:45
std::string m_name
Definition: GThread.h:68
static const SpawnBehavior USE_CURRENT_THREAD
Definition: GThread.h:126
static void memset(void *dst, uint8 value, size_t numBytes)
Definition: System.cpp:695
Definition: GThread.h:45
volatile GStatus m_status
Definition: GThread.h:58
pthread_t m_handle
Definition: GThread.h:65
arena_t NULL
Definition: jemalloc_internal.h:624
#define debugAssertM(exp, message)
Definition: debugAssert.h:161
#define debugAssert(exp)
Definition: debugAssert.h:160
static void * internalThreadProc(void *param)
Definition: GThread.cpp:203
bool started() const
Definition: GThread.cpp:72
unsigned int DWORD
Definition: CascPort.h:139
virtual void threadMain()=0
Definition: GThread.h:45

+ Here is the call graph for this function:

bool G3D::GThread::started ( ) const

True after start() has been called, even through the thread may have already completed(), or be currently running().

72  {
73  return m_status != STATUS_CREATED;
74 }
volatile GStatus m_status
Definition: GThread.h:58
Definition: GThread.h:45

+ Here is the caller graph for this function:

void G3D::GThread::terminate ( )

Terminates the thread without notifying or waiting for a cancelation point.

153  {
154  if (m_handle) {
155 # ifdef G3D_WINDOWS
156  ::TerminateThread(m_handle, 0);
157 # else
158  pthread_kill(m_handle, SIGSTOP);
159 # endif
160  // system-independent clear of handle
161  System::memset(&m_handle, 0, sizeof(m_handle));
162  }
163 }
static void memset(void *dst, uint8 value, size_t numBytes)
Definition: System.cpp:695
pthread_t m_handle
Definition: GThread.h:65

+ Here is the call graph for this function:

virtual void G3D::GThread::threadMain ( )
protectedpure virtual

Overriden by the thread implementor

Implemented in G3D::_internalGThreadWorker< Class >, and G3D::_internal::BasicThread.

+ Here is the caller graph for this function:

void G3D::GThread::waitForCompletion ( )

Waits for the thread to finish executing.

176  {
177  if (m_status == STATUS_COMPLETED) {
178  // Must be done
179  return;
180  }
181 
182 # ifdef G3D_WINDOWS
183  debugAssert(m_event);
184  ::WaitForSingleObject(m_event, INFINITE);
185 # else
187  pthread_join(m_handle, NULL);
188 # endif
189 }
Definition: GThread.h:45
volatile GStatus m_status
Definition: GThread.h:58
pthread_t m_handle
Definition: GThread.h:65
arena_t NULL
Definition: jemalloc_internal.h:624
#define debugAssert(exp)
Definition: debugAssert.h:160

Member Data Documentation

pthread_t G3D::GThread::m_handle
private
std::string G3D::GThread::m_name
private
volatile GStatus G3D::GThread::m_status
private
const SpawnBehavior G3D::GThread::USE_CURRENT_THREAD = G3D::USE_CURRENT_THREAD
static

For backwards compatibility to G3D 8.xx

const SpawnBehavior G3D::GThread::USE_NEW_THREAD = G3D::USE_NEW_THREAD
static

For backwards compatibility to G3D 8.xx


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