Nebula
Loading...
Searching...
No Matches
linuxthread.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
11#include "core/refcounted.h"
12#include "threading/threadid.h"
13#include "threading/event.h"
14#include "system/cpu.h"
15
16//------------------------------------------------------------------------------
17namespace Linux
18{
20{
22public:
25 {
29 };
30
34 virtual ~LinuxThread();
36 void SetPriority(Priority p);
38 Priority GetPriority() const;
40 void SetThreadAffinity(uint mask);
44 void SetStackSize(SizeT s);
46 SizeT GetStackSize() const;
48 void SetMaxStackSize(SizeT s);
50 void SetName(const Util::String& n);
52 const Util::String& GetName() const;
54 void Start();
56 void Stop();
58 bool IsRunning() const;
59
61 static void YieldThread();
63 static const char* GetMyThreadName();
67 static bool GetMyThreadStopRequested();
69 static int GetMyThreadPriority();
70
71 #if NEBULA_DEBUG
72 struct ThreadDebugInfo
73 {
74 Util::String threadName;
75 LinuxThread::Priority threadPriority;
76 System::Cpu::CoreId threadCoreId;
77 SizeT threadStackSize;
78 };
80 static Util::Array<ThreadDebugInfo> GetRunningThreadDebugInfos();
81 #endif
82
83protected:
85 static void *ThreadProc(void *);
86
88 virtual void EmitWakeupSignal();
90 virtual void DoWork();
92 bool ThreadStopRequested() const;
93
94private:
96 static void SetMyThreadName(const Util::String& n);
97
105
106 pthread_t thread;
107 cpu_set_t affinity;
112
115
116#if NEBULA_DEBUG
117 static Threading::CriticalSection criticalSection;
118 static Util::List<LinuxThread*> ThreadList;
119 Util::List<LinuxThread*>::Iterator threadListIterator;
120#endif
121};
122
123//------------------------------------------------------------------------------
126inline void
128{
129 this->priority = p;
130}
131
132//------------------------------------------------------------------------------
137{
138 return this->priority;
139}
140
141//------------------------------------------------------------------------------
144inline void
146{
147 // empty
148}
149
150//------------------------------------------------------------------------------
153inline SizeT
155{
156 return this->stackSize;
157}
158
159//------------------------------------------------------------------------------
162inline void
164{
165 this->stackSize = s;
166}
167
168//------------------------------------------------------------------------------
175inline bool
177{
178 return this->stopRequestEvent.Peek();
179}
180
181//------------------------------------------------------------------------------
185inline void
187{
188 n_assert(n.IsValid());
189 this->name = n;
190}
191
192//------------------------------------------------------------------------------
197inline const Util::String&
199{
200 return this->name;
201}
202
203
204} // namespace Linux
205//------------------------------------------------------------------------------
The common base class of Nebula.
Definition refcounted.h:38
Linux implementation of Threading::Thread.
Definition linuxthread.h:20
ThreadState volatile threadState
Definition linuxthread.h:111
ThreadState
thread states
Definition linuxthread.h:100
@ Initial
Definition linuxthread.h:101
@ Stopped
Definition linuxthread.h:103
@ Running
Definition linuxthread.h:102
SizeT stackSize
Definition linuxthread.h:109
Util::String name
Definition linuxthread.h:110
static void SetMyThreadName(const Util::String &n)
set thread name
Definition linuxthread.cc:292
void SetName(const Util::String &n)
set thread name
Definition linuxthread.h:186
Threading::Event threadStartedEvent
Definition linuxthread.h:113
virtual void DoWork()
this method runs in the thread context
Definition linuxthread.cc:217
virtual ~LinuxThread()
destructor
Definition linuxthread.cc:53
SizeT GetStackSize() const
get stack size
Definition linuxthread.h:154
static void YieldThread()
yield the thread (gives up current time slice)
Definition linuxthread.cc:250
static void * ThreadProc(void *)
Internal static helper method.
Definition linuxthread.cc:171
const Util::String & GetName() const
get thread name
Definition linuxthread.h:198
bool IsRunning() const
return true if thread has been started
Definition linuxthread.cc:203
void Stop()
request threading code to stop, returns when thread has actually finished
Definition linuxthread.cc:148
void SetPriority(Priority p)
set the thread priority
Definition linuxthread.h:127
static Threading::ThreadId GetMyThreadId()
get the thread ID of this thread
Definition linuxthread.cc:227
LinuxThread()
constructor
Definition linuxthread.cc:36
pthread_t thread
Definition linuxthread.h:106
__DeclareClass(LinuxThread)
cpu_set_t affinity
Definition linuxthread.h:107
static const char * GetMyThreadName()
get name of thread
Definition linuxthread.cc:314
bool ThreadStopRequested() const
check if stop is requested, call from DoWork() to see if the thread proc should quit
Definition linuxthread.h:176
void Start()
start executing the thread code, returns when thread has actually started
Definition linuxthread.cc:74
static bool GetMyThreadStopRequested()
get the stop-requested state of this thread (not yet implemented in Linux)
Definition linuxthread.cc:239
void SetMaxStackSize(SizeT s)
set maximum thread size
Definition linuxthread.h:163
Priority GetPriority() const
get the thread priority
Definition linuxthread.h:136
Priority
thread priorities
Definition linuxthread.h:25
@ Normal
Definition linuxthread.h:27
@ High
Definition linuxthread.h:28
@ Low
Definition linuxthread.h:26
virtual void EmitWakeupSignal()
override this method if your thread loop needs a wakeup call before stopping
Definition linuxthread.cc:135
void SetStackSize(SizeT s)
set stack size in bytes
Definition linuxthread.h:145
void SetThreadAffinity(uint mask)
set thread affinity
Definition linuxthread.cc:342
Priority priority
Definition linuxthread.h:108
uint GetThreadAffinity()
set thread affinity
Definition linuxthread.cc:355
Threading::Event stopRequestEvent
Definition linuxthread.h:114
static int GetMyThreadPriority()
get the current actual thread-priority of this thread (platform specific!)
Definition linuxthread.cc:330
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
#define n_assert(exp)
Definition debug.h:50
Definition linuxcompletioncounter.h:15
pthread_t ThreadId
Definition linuxthreadid.h:15
Nebula's universal string class.
Definition string.h:50
bool IsValid() const
return true if string object is not empty
Definition string.h:682
int SizeT
Definition types.h:49
unsigned int uint
Definition types.h:31