AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
Acore::Thread Class Reference

#include "Threading.h"

Public Member Functions

 Thread ()
 
 Thread (Runnable *instance)
 
 ~Thread ()
 
bool wait ()
 
void destroy ()
 
void setPriority (Priority type)
 

Static Public Member Functions

static void Sleep (unsigned long msecs)
 
static std::thread::id currentId ()
 

Private Member Functions

 Thread (const Thread &)
 
Threadoperator= (const Thread &)
 

Static Private Member Functions

static void ThreadTask (void *param)
 

Private Attributes

Runnable *const m_task
 
std::thread::id m_iThreadId
 
std::thread m_ThreadImp
 

Detailed Description

Constructor & Destructor Documentation

◆ Thread() [1/3]

Thread::Thread ( )
29 : m_task(nullptr), m_iThreadId(), m_ThreadImp()
30{
31}
std::thread::id m_iThreadId
Definition: Threading.h:77
std::thread m_ThreadImp
Definition: Threading.h:78
Runnable *const m_task
Definition: Threading.h:76

◆ Thread() [2/3]

Thread::Thread ( Runnable instance)
explicit
33 : m_task(instance), m_ThreadImp(&Thread::ThreadTask, (void*)m_task)
34{
35 m_iThreadId = m_ThreadImp.get_id();
36
37 // register reference to m_task to prevent it deeltion until destructor
38 if (m_task)
39 {
41 }
42}
void incReference()
Definition: Threading.h:32
static void ThreadTask(void *param)
Definition: Threading.cpp:91

References Acore::Runnable::incReference(), m_iThreadId, m_task, and m_ThreadImp.

◆ ~Thread()

Thread::~Thread ( )
45{
46 // Wait();
47
48 // deleted runnable object (if no other references)
49 if (m_task)
50 {
52 }
53}
void decReference()
Definition: Threading.h:33

References Acore::Runnable::decReference(), and m_task.

◆ Thread() [3/3]

Acore::Thread::Thread ( const Thread )
private

Member Function Documentation

◆ currentId()

std::thread::id Thread::currentId ( )
static
98{
99 return std::this_thread::get_id();
100}

◆ destroy()

void Thread::destroy ( )
79{
80 if (m_iThreadId == std::thread::id() || !m_task)
81 {
82 return;
83 }
84
85 // FIXME: We need to make sure that all threads can be trusted to
86 // halt execution on their own as this is not an interrupt
87 m_ThreadImp.join();
88 m_iThreadId = std::thread::id();
89}

References m_iThreadId, m_task, and m_ThreadImp.

◆ operator=()

Thread & Acore::Thread::operator= ( const Thread )
private

◆ setPriority()

void Thread::setPriority ( Priority  type)
103{
104#ifdef WIN32
105 std::thread::native_handle_type handle = m_ThreadImp.native_handle();
106#endif
107
108 bool _ok = true;
109
110 switch (priority)
111 {
112#ifdef WIN32
114 _ok = SetThreadPriority(handle, THREAD_PRIORITY_TIME_CRITICAL);
115 break;
116 case Priority_Highest:
117 _ok = SetThreadPriority(handle, THREAD_PRIORITY_HIGHEST);
118 break;
119 case Priority_High:
120 _ok = SetThreadPriority(handle, THREAD_PRIORITY_ABOVE_NORMAL);
121 break;
122 case Priority_Normal:
123 _ok = SetThreadPriority(handle, THREAD_PRIORITY_NORMAL);
124 break;
125 case Priority_Low:
126 _ok = SetThreadPriority(handle, THREAD_PRIORITY_BELOW_NORMAL);
127 break;
128 case Priority_Lowest:
129 _ok = SetThreadPriority(handle, THREAD_PRIORITY_LOWEST);
130 break;
131 case Priority_Idle:
132 _ok = SetThreadPriority(handle, THREAD_PRIORITY_IDLE);
133 break;
134#endif
135 default:
136 break;
137 }
138
139 // remove this ASSERT in case you don't want to know is thread priority change was successful or not
140 ASSERT(_ok);
141}
#define ASSERT
Definition: Errors.h:68
@ Priority_Lowest
Definition: Threading.h:47
@ Priority_Idle
Definition: Threading.h:46
@ Priority_Low
Definition: Threading.h:48
@ Priority_Normal
Definition: Threading.h:49
@ Priority_High
Definition: Threading.h:50
@ Priority_Realtime
Definition: Threading.h:52
@ Priority_Highest
Definition: Threading.h:51

References ASSERT, m_ThreadImp, Acore::Priority_High, Acore::Priority_Highest, Acore::Priority_Idle, Acore::Priority_Low, Acore::Priority_Lowest, Acore::Priority_Normal, and Acore::Priority_Realtime.

◆ Sleep()

void Thread::Sleep ( unsigned long  msecs)
static
144{
145 std::this_thread::sleep_for(std::chrono::milliseconds(msecs));
146}

◆ ThreadTask()

void Thread::ThreadTask ( void *  param)
staticprivate
92{
93 Runnable* _task = (Runnable*)param;
94 _task->run();
95}
Definition: Threading.h:27
virtual void run()=0

References Acore::Runnable::run().

◆ wait()

bool Thread::wait ( )
56{
57 if (m_iThreadId == std::thread::id() || !m_task)
58 {
59 return false;
60 }
61
62 bool res = true;
63
64 try
65 {
66 m_ThreadImp.join();
67 }
68 catch (std::system_error&)
69 {
70 res = false;
71 }
72
73 m_iThreadId = std::thread::id();
74
75 return res;
76}

References m_iThreadId, m_task, and m_ThreadImp.

Member Data Documentation

◆ m_iThreadId

std::thread::id Acore::Thread::m_iThreadId
private

Referenced by destroy(), Thread(), and wait().

◆ m_task

Runnable* const Acore::Thread::m_task
private

Referenced by destroy(), Thread(), wait(), and ~Thread().

◆ m_ThreadImp

std::thread Acore::Thread::m_ThreadImp
private

Referenced by destroy(), setPriority(), Thread(), and wait().