Nebula
Loading...
Searching...
No Matches
win32thread.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
12#include "core/refcounted.h"
14#include "threading/threadid.h"
15#include "system/cpu.h"
17
18//------------------------------------------------------------------------------
19namespace Win32
20{
22{
24public:
27 {
31 };
32
36 virtual ~Win32Thread();
38 void SetPriority(Priority p);
40 Priority GetPriority() const;
42 void SetStackSize(SizeT s);
44 SizeT GetStackSize() const;
46 void SetName(const Util::String& n);
48 const Util::String& GetName() const;
50 void SetThreadAffinity(const uint mask);
53
55 void Start();
57 void Stop();
59 bool IsRunning() const;
60
62 static void YieldThread();
64 static void SetMyThreadName(const char* n);
66 static const char* GetMyThreadName();
69
70 #if NEBULA_DEBUG
71 struct ThreadDebugInfo
72 {
73 Util::String threadName;
74 Win32Thread::Priority threadPriority;
75 System::Cpu::CoreId threadCoreId;
76 SizeT threadStackSize;
77 };
79 static Util::Array<ThreadDebugInfo> GetRunningThreadDebugInfos();
80 #endif
81
82protected:
84 virtual void EmitWakeupSignal();
86 virtual void DoWork();
88 bool ThreadStopRequested() const;
89
90private:
92 static DWORD WINAPI ThreadProc(LPVOID self);
93
101 ThreadLocal static const char* ThreadName;
102
103 #if NEBULA_DEBUG
104 static Threading::CriticalSection criticalSection;
105 static Util::List<Win32Thread*> ThreadList;
106 Util::List<Win32Thread*>::Iterator threadListIterator;
107 #endif
108};
109
110//------------------------------------------------------------------------------
113inline void
115{
116 this->priority = p;
117}
118
119//------------------------------------------------------------------------------
124{
125 return this->priority;
126}
127
128//------------------------------------------------------------------------------
131inline void
133{
134 this->stackSize = s;
135}
136
137//------------------------------------------------------------------------------
140inline SizeT
142{
143 return this->stackSize;
144}
145
146//------------------------------------------------------------------------------
153inline bool
155{
156 return this->stopRequestEvent.Peek();
157}
158
159//------------------------------------------------------------------------------
165inline void
167{
168 n_assert(n.IsValid());
169 this->name = n;
170}
171
172//------------------------------------------------------------------------------
179inline const Util::String&
181{
182 return this->name;
183}
184
185//------------------------------------------------------------------------------
188inline void
190{
191 this->affinityMask = mask;
192}
193
194//------------------------------------------------------------------------------
197inline uint
199{
200 return this->affinityMask;
201}
202
203}; // namespace Win32
204//------------------------------------------------------------------------------
The common base class of Nebula.
Definition refcounted.h:38
CoreId
Definition cpu.h:19
Critical section objects are used to protect a portion of code from parallel execution.
Nebula's dynamic array class.
Definition array.h:60
the list iterator
Definition list.h:76
Implements a doubly linked list.
Definition list.h:20
Win32 implementation of an event synchronization object.
Definition win32event.h:18
bool Peek() const
check if event is signalled
Definition win32event.h:128
Win32 implementation of thread class.
Definition win32thread.h:22
Priority
thread priorities
Definition win32thread.h:27
@ High
Definition win32thread.h:30
@ Low
Definition win32thread.h:28
@ Normal
Definition win32thread.h:29
void SetStackSize(SizeT s)
set stack size in bytes (default is 4 KByte)
Definition win32thread.h:132
SizeT GetStackSize() const
get stack size
Definition win32thread.h:141
Win32Thread()
constructor
Definition win32thread.cc:26
virtual void DoWork()
this method runs in the thread context
Definition win32thread.cc:189
bool IsRunning() const
return true if thread has been started
Definition win32thread.cc:163
Win32Event threadStartedEvent
Definition win32thread.h:95
void Start()
start executing the thread code, returns when thread has actually started
Definition win32thread.cc:67
Util::String name
Definition win32thread.h:100
static DWORD WINAPI ThreadProc(LPVOID self)
internal thread proc helper function
Definition win32thread.cc:200
Win32Event stopRequestEvent
Definition win32thread.h:96
SizeT stackSize
Definition win32thread.h:99
const Util::String & GetName() const
get thread name
Definition win32thread.h:180
Priority GetPriority() const
get the thread priority
Definition win32thread.h:123
HANDLE threadHandle
Definition win32thread.h:94
void SetName(const Util::String &n)
set thread name
Definition win32thread.h:166
static ThreadLocal const char * ThreadName
Definition win32thread.h:101
void Stop()
request threading code to stop, returns when thread has actually finished
Definition win32thread.cc:139
virtual ~Win32Thread()
destructor
Definition win32thread.cc:43
static void SetMyThreadName(const char *n)
set thread name from within thread context
Definition win32thread.cc:224
static const char * GetMyThreadName()
obtain name of thread from within thread context
Definition win32thread.cc:259
void SetThreadAffinity(const uint mask)
set the thread affinity
Definition win32thread.h:189
__DeclareClass(Win32Thread)
bool ThreadStopRequested() const
check if stop is requested, call from DoWork() to see if the thread proc should quit
Definition win32thread.h:154
uint affinityMask
Definition win32thread.h:98
Priority priority
Definition win32thread.h:97
static void YieldThread()
yield the thread (gives up current time slice)
Definition win32thread.cc:279
uint GetThreadAffinity()
get the thread affinity
Definition win32thread.h:198
void SetPriority(Priority p)
set the thread priority
Definition win32thread.h:114
static Threading::ThreadId GetMyThreadId()
get the thread ID of this thread
Definition win32thread.cc:269
virtual void EmitWakeupSignal()
override this method if your thread loop needs a wakeup call before stopping
Definition win32thread.cc:126
#define n_assert(exp)
Definition debug.h:50
pthread_t ThreadId
Definition linuxthreadid.h:15
[TODO: Describe Win32 subsystem]
Nebula's universal string class.
Definition string.h:50
bool IsValid() const
return true if string object is not empty
Definition string.h:684
int SizeT
Definition types.h:49
unsigned int uint
Definition types.h:31