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#include <sys/resource.h>
16
17//------------------------------------------------------------------------------
18namespace Linux
19{
21{
23public:
26 {
30 };
31
35 virtual ~LinuxThread();
37 void SetPriority(Priority p);
39 Priority GetPriority() const;
41 void SetThreadAffinity(uint mask);
45 void SetStackSize(SizeT s);
47 SizeT GetStackSize() const;
49 void SetMaxStackSize(SizeT s);
51 void SetName(const Util::String& n);
53 const Util::String& GetName() const;
55 void Start();
57 void Stop();
59 bool IsRunning() const;
60
62 static void YieldThread();
64 static const char* GetMyThreadName();
68 static bool GetMyThreadStopRequested();
70 static int GetMyThreadPriority();
71
72 #if NEBULA_DEBUG
73 struct ThreadDebugInfo
74 {
75 Util::String threadName;
76 LinuxThread::Priority threadPriority;
77 System::Cpu::CoreId threadCoreId;
78 SizeT threadStackSize;
79 };
81 static Util::Array<ThreadDebugInfo> GetRunningThreadDebugInfos();
82 #endif
83
84protected:
86 static void *ThreadProc(void *);
87
89 virtual void EmitWakeupSignal();
91 virtual void DoWork();
93 bool ThreadStopRequested() const;
94
95private:
97 static void SetMyThreadName(const Util::String& n);
98
106
107 pthread_t thread;
108 cpu_set_t affinity;
113
114 Threading::Event threadStartedEvent;
115 Threading::Event stopRequestEvent;
116
117#if NEBULA_DEBUG
118 static Threading::CriticalSection criticalSection;
119 static Util::List<LinuxThread*> ThreadList;
120 Util::List<LinuxThread*>::Iterator threadListIterator;
121#endif
122};
123
124//------------------------------------------------------------------------------
127inline void
129{
130 this->priority = p;
131}
132
133//------------------------------------------------------------------------------
138{
139 return this->priority;
140}
141
142//------------------------------------------------------------------------------
145inline void
147{
148 // empty
149}
150
151//------------------------------------------------------------------------------
154inline SizeT
156{
157 return this->stackSize;
158}
159
160//------------------------------------------------------------------------------
163inline void
165{
166 this->stackSize = s;
167}
168
169//------------------------------------------------------------------------------
176inline bool
178{
179 return this->stopRequestEvent.Peek();
180}
181
182//------------------------------------------------------------------------------
186inline void
188{
189 n_assert(n.IsValid());
190 this->name = n;
191}
192
193//------------------------------------------------------------------------------
198inline const Util::String&
200{
201 return this->name;
202}
203
204
205} // namespace Linux
206//------------------------------------------------------------------------------
The common base class of Nebula.
Definition refcounted.h:38
ThreadState volatile threadState
Definition linuxthread.h:112
ThreadState
thread states
Definition linuxthread.h:101
@ Initial
Definition linuxthread.h:102
@ Stopped
Definition linuxthread.h:104
@ Running
Definition linuxthread.h:103
SizeT stackSize
Definition linuxthread.h:110
Util::String name
Definition linuxthread.h:111
static void SetMyThreadName(const Util::String &n)
set thread name
Definition linuxthread.cc:295
void SetName(const Util::String &n)
set thread name
Definition linuxthread.h:187
Threading::Event threadStartedEvent
Definition linuxthread.h:114
virtual void DoWork()
this method runs in the thread context
Definition linuxthread.cc:220
virtual ~LinuxThread()
destructor
Definition linuxthread.cc:56
SizeT GetStackSize() const
get stack size
Definition linuxthread.h:155
static void YieldThread()
yield the thread (gives up current time slice)
Definition linuxthread.cc:253
static void * ThreadProc(void *)
Internal static helper method.
Definition linuxthread.cc:174
const Util::String & GetName() const
get thread name
Definition linuxthread.h:199
bool IsRunning() const
return true if thread has been started
Definition linuxthread.cc:206
void Stop()
request threading code to stop, returns when thread has actually finished
Definition linuxthread.cc:151
void SetPriority(Priority p)
set the thread priority
Definition linuxthread.h:128
static Threading::ThreadId GetMyThreadId()
get the thread ID of this thread
Definition linuxthread.cc:230
LinuxThread()
constructor
Definition linuxthread.cc:38
pthread_t thread
Definition linuxthread.h:107
__DeclareClass(LinuxThread)
cpu_set_t affinity
Definition linuxthread.h:108
static const char * GetMyThreadName()
get name of thread
Definition linuxthread.cc:317
bool ThreadStopRequested() const
check if stop is requested, call from DoWork() to see if the thread proc should quit
Definition linuxthread.h:177
void Start()
start executing the thread code, returns when thread has actually started
Definition linuxthread.cc:77
static bool GetMyThreadStopRequested()
get the stop-requested state of this thread (not yet implemented in Linux)
Definition linuxthread.cc:242
void SetMaxStackSize(SizeT s)
set maximum thread size
Definition linuxthread.h:164
Priority GetPriority() const
get the thread priority
Definition linuxthread.h:137
Priority
thread priorities
Definition linuxthread.h:26
@ Normal
Definition linuxthread.h:28
@ High
Definition linuxthread.h:29
@ Low
Definition linuxthread.h:27
virtual void EmitWakeupSignal()
override this method if your thread loop needs a wakeup call before stopping
Definition linuxthread.cc:138
void SetStackSize(SizeT s)
set stack size in bytes
Definition linuxthread.h:146
void SetThreadAffinity(uint mask)
set thread affinity
Definition linuxthread.cc:345
Priority priority
Definition linuxthread.h:109
uint GetThreadAffinity()
set thread affinity
Definition linuxthread.cc:358
Threading::Event stopRequestEvent
Definition linuxthread.h:115
static int GetMyThreadPriority()
get the current actual thread-priority of this thread (platform specific!)
Definition linuxthread.cc:333
CoreId
Definition cpu.h:19
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.cs:8
bool IsValid() const
return true if string object is not empty
Definition string.h:686
int SizeT
Definition types.h:42
unsigned int uint
Definition types.h:33