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

#include <ThreadSet.h>

Public Types

typedef GThread Thread
 
typedef shared_ptr< ThreadThreadRef
 
typedef shared_ptr< ThreadSetRef
 
typedef Array< ThreadRef >
::Iterator 
Iterator
 
typedef Array< ThreadRef >
::ConstIterator 
ConstIterator
 

Public Member Functions

int size () const
 
int numStarted () const
 
void start (SpawnBehavior lastThreadBehavior=USE_NEW_THREAD) const
 
void terminate () const
 
void waitForCompletion () const
 
void clear ()
 
int removeCompleted ()
 
int insert (const ThreadRef &t)
 
bool remove (const ThreadRef &t)
 
bool contains (const ThreadRef &t) const
 
Iterator begin ()
 
Iterator end ()
 
ConstIterator begin () const
 
ConstIterator end () const
 
- Public Member Functions inherited from G3D::ReferenceCountedObject
virtual ~ReferenceCountedObject ()
 

Private Attributes

GMutex m_lock
 
Array< ThreadRefm_thread
 

Detailed Description

Manages a set of threads. All methods are threadsafe except for the iterator begin/end.

Member Typedef Documentation

typedef shared_ptr<ThreadSet> G3D::ThreadSet::Ref

Intended to allow future use with a template parameter.

typedef shared_ptr<Thread> G3D::ThreadSet::ThreadRef

Member Function Documentation

ThreadSet::Iterator G3D::ThreadSet::begin ( )

It is an error to mutate the ThreadSet while iterating through it.

150  {
151  return m_thread.begin();
152 }
Array< ThreadRef > m_thread
Definition: ThreadSet.h:34
Iterator begin()
Definition: Array.h:233

+ Here is the call graph for this function:

ThreadSet::ConstIterator G3D::ThreadSet::begin ( ) const
160  {
161  return m_thread.begin();
162 }
Array< ThreadRef > m_thread
Definition: ThreadSet.h:34
Iterator begin()
Definition: Array.h:233

+ Here is the call graph for this function:

void G3D::ThreadSet::clear ( )

Remove all (not stopping them)

102  {
103  m_lock.lock();
104  m_thread.clear();
105  m_lock.unlock();
106 }
Array< ThreadRef > m_thread
Definition: ThreadSet.h:34
GMutex m_lock
Definition: ThreadSet.h:31
void lock()
Definition: GThread.cpp:248
void unlock()
Definition: GThread.cpp:256
void clear(bool shrink=true)
Definition: Array.h:407

+ Here is the call graph for this function:

bool G3D::ThreadSet::contains ( const ThreadRef t) const
138  {
139  ThreadSet* me = const_cast<ThreadSet*>(this);
140  me->m_lock.lock();
141  bool found = false;
142  for (int i = 0; i < m_thread.size() && ! found; ++i) {
143  found = (m_thread[i] == t);
144  }
145  me->m_lock.unlock();
146  return found;
147 }
Array< ThreadRef > m_thread
Definition: ThreadSet.h:34
int size() const
Definition: Array.h:430

+ Here is the call graph for this function:

ThreadSet::Iterator G3D::ThreadSet::end ( )
155  {
156  return m_thread.end();
157 }
Array< ThreadRef > m_thread
Definition: ThreadSet.h:34
ConstIterator end() const
Definition: Array.h:244

+ Here is the call graph for this function:

ThreadSet::ConstIterator G3D::ThreadSet::end ( ) const
165  {
166  return m_thread.end();
167 }
Array< ThreadRef > m_thread
Definition: ThreadSet.h:34
ConstIterator end() const
Definition: Array.h:244

+ Here is the call graph for this function:

int G3D::ThreadSet::insert ( const ThreadRef t)

Inserts a new thread, if it is not already present, and returns the new number of threads.

109  {
110  m_lock.lock();
111  bool found = false;
112  for (int i = 0; i < m_thread.size() && ! found; ++i) {
113  found = (m_thread[i] == t);
114  }
115  if (! found) {
116  m_thread.append(t);
117  }
118  int s = m_thread.size();
119  m_lock.unlock();
120  return s;
121 }
Array< ThreadRef > m_thread
Definition: ThreadSet.h:34
GMutex m_lock
Definition: ThreadSet.h:31
void lock()
Definition: GThread.cpp:248
void unlock()
Definition: GThread.cpp:256
int size() const
Definition: Array.h:430
void append(const T &value)
Definition: Array.h:583

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int G3D::ThreadSet::numStarted ( ) const

Number of threads that have been started

15  {
16  ThreadSet* me = const_cast<ThreadSet*>(this);
17  me->m_lock.lock();
18  int count = 0;
19  for (int i = 0; i < m_thread.size(); ++i) {
20  if (m_thread[i]->started()) {
21  ++count;
22  }
23  }
24  me->m_lock.unlock();
25  return count;
26 }
Array< ThreadRef > m_thread
Definition: ThreadSet.h:34
int size() const
Definition: Array.h:430

+ Here is the call graph for this function:

bool G3D::ThreadSet::remove ( const ThreadRef t)

Removes a thread. Returns true if the thread was present and removed.

124  {
125  m_lock.lock();
126  bool found = false;
127  for (int i = 0; i < m_thread.size() && ! found; ++i) {
128  found = (m_thread[i] == t);
129  if (found) {
130  m_thread.fastRemove(i);
131  }
132  }
133  m_lock.unlock();
134  return found;
135 }
Array< ThreadRef > m_thread
Definition: ThreadSet.h:34
GMutex m_lock
Definition: ThreadSet.h:31
void lock()
Definition: GThread.cpp:248
void unlock()
Definition: GThread.cpp:256
void fastRemove(int index, bool shrinkIfNecessary=false)
Definition: Array.h:446
int size() const
Definition: Array.h:430

+ Here is the call graph for this function:

int G3D::ThreadSet::removeCompleted ( )

Removes completed threads and returns the new size.

87  {
88  m_lock.lock();
89  for (int i = 0; i < m_thread.size(); ++i) {
90  if (m_thread[i]->completed()) {
92  --i;
93  }
94  }
95 
96  int s = m_thread.size();
97  m_lock.unlock();
98  return s;
99 }
Array< ThreadRef > m_thread
Definition: ThreadSet.h:34
GMutex m_lock
Definition: ThreadSet.h:31
void lock()
Definition: GThread.cpp:248
void unlock()
Definition: GThread.cpp:256
void fastRemove(int index, bool shrinkIfNecessary=false)
Definition: Array.h:446
int size() const
Definition: Array.h:430

+ Here is the call graph for this function:

int G3D::ThreadSet::size ( ) const

Total number of threads (some of which may be completed).

6  {
7  ThreadSet* me = const_cast<ThreadSet*>(this);
8  me->m_lock.lock();
9  int s = m_thread.size();
10  me->m_lock.unlock();
11  return s;
12 }
Array< ThreadRef > m_thread
Definition: ThreadSet.h:34
int size() const
Definition: Array.h:430

+ Here is the call graph for this function:

void G3D::ThreadSet::start ( SpawnBehavior  lastThreadBehavior = USE_NEW_THREAD) const

Start all threads that are not currently started.

Parameters
lastThreadBehaviorIf USE_CURRENT_THREAD, takes the last unstarted thread and executes it manually on the current thread. This helps to take full advantage of the machine when running a large number of jobs and avoids the overhead of a thread start for single-thread groups. Note that this forces start() to block until that thread is complete.
29  {
30  ThreadSet* me = const_cast<ThreadSet*>(this);
31 
32  Array<GThreadRef> unstarted;
33  me->m_lock.lock();
34  // Find the unstarted threads
35  for (int i = 0; i < m_thread.size(); ++i) {
36  if (! m_thread[i]->started()) {
37  unstarted.append(m_thread[i]);
38  }
39  }
40 
41  int last = unstarted.size();
42  if (lastBehavior == USE_CURRENT_THREAD) {
43  // Save the last unstarted for the current thread
44  --last;
45  }
46 
47  // Start all threads
48  for (int i = 0; i < last; ++i) {
49  unstarted[i]->start(USE_NEW_THREAD);
50  }
51 
52  me->m_lock.unlock();
53 
54  // Start the last one on my thread
55  if ((unstarted.size() > 0) && (lastBehavior == USE_CURRENT_THREAD)) {
56  unstarted.last()->start(USE_CURRENT_THREAD);
57  debugAssert(unstarted.last()->completed());
58  }
59 }
Array< ThreadRef > m_thread
Definition: ThreadSet.h:34
#define debugAssert(exp)
Definition: debugAssert.h:160
int size() const
Definition: Array.h:430
Definition: SpawnBehavior.h:4
Definition: SpawnBehavior.h:4

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void G3D::ThreadSet::terminate ( ) const

Terminate all threads that are currently started

62  {
63  ThreadSet* me = const_cast<ThreadSet*>(this);
64  me->m_lock.lock();
65  for (int i = 0; i < m_thread.size(); ++i) {
66  if (m_thread[i]->started()) {
67  m_thread[i]->terminate();
68  }
69  }
70  me->m_lock.unlock();
71 }
Array< ThreadRef > m_thread
Definition: ThreadSet.h:34
int size() const
Definition: Array.h:430

+ Here is the call graph for this function:

void G3D::ThreadSet::waitForCompletion ( ) const

Waits until all started threads have completed.

74  {
75  ThreadSet* me = const_cast<ThreadSet*>(this);
76 
77  me->m_lock.lock();
78  for (int i = 0; i < m_thread.size(); ++i) {
79  if (m_thread[i]->started()) {
80  m_thread[i]->waitForCompletion();
81  }
82  }
83  me->m_lock.unlock();
84 }
Array< ThreadRef > m_thread
Definition: ThreadSet.h:34
int size() const
Definition: Array.h:430

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

GMutex G3D::ThreadSet::m_lock
private

Protects m_thread

Array<ThreadRef> G3D::ThreadSet::m_thread
private

Threads in the set


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