Nebula
Loading...
Searching...
No Matches
posixthread.h
Go to the documentation of this file.
1#pragma once
2#ifndef POSIX_POSIXTHREAD_H
3#define POSIX_POSIXTHREAD_H
4//------------------------------------------------------------------------------
13#include "core/refcounted.h"
15#include "threading/threadid.h"
16#include "system/cpu.h"
17#include <sched.h>
18
19//------------------------------------------------------------------------------
20namespace Posix
21{
23{
25public:
28 {
32 };
36 virtual ~PosixThread();
38 void SetPriority(Priority p);
40 Priority GetPriority() const;
42 void SetThreadAffinity(uint mask);
46 void SetStackSize(unsigned int s);
48 unsigned int GetStackSize() const;
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();
66
67protected:
69 virtual void EmitWakeupSignal();
71 virtual void DoWork();
74
75private:
77 static void* ThreadProc(void* self);
78
79 pthread_t threadHandle;
80 cpu_set_t affinity;
82 bool running;
84 unsigned int stackSize;
86 static ThreadLocal const char* ThreadName;
87};
88
89//------------------------------------------------------------------------------
92inline bool
94{
95 return (0 != this->threadHandle);
96}
97
98//------------------------------------------------------------------------------
101inline void
103{
104 this->priority = p;
105}
106
107//------------------------------------------------------------------------------
112{
113 return this->priority;
114}
115
116//------------------------------------------------------------------------------
123inline bool
128
129//------------------------------------------------------------------------------
135inline void
137{
138 n_assert(n.IsValid());
139 this->name = n;
140}
141
142//------------------------------------------------------------------------------
149inline const Util::String&
151{
152 return this->name;
153}
154
155} // namespace Posix
156//------------------------------------------------------------------------------
157#endif
The common base class of Nebula.
Definition refcounted.h:38
Posix implmentation of an event synchronization object.
Definition posixevent.h:20
bool Peek() const
check if event is signalled
Definition posixevent.h:135
Posix implementation of thread class.
Definition posixthread.h:23
Util::String name
Definition posixthread.h:85
Priority
thread priorities
Definition posixthread.h:28
@ Low
Definition posixthread.h:29
@ High
Definition posixthread.h:31
@ Normal
Definition posixthread.h:30
void SetName(const Util::String &n)
set thread name
Definition posixthread.h:136
uint GetThreadAffinity()
get thread affinity
static void YieldThread()
yield the thread (gives up current time slice)
PosixEvent stopRequestEvent
Definition posixthread.h:81
void Start()
start executing the thread code, returns when thread has actually started
Definition posixthread.cc:49
bool running
Definition posixthread.h:82
bool IsRunning() const
return true if thread has been started
Definition posixthread.h:93
static const char * GetMyThreadName()
obtain name of thread from within thread code
Definition posixthread.cc:155
virtual void EmitWakeupSignal()
override this method if your thread loop needs a wakeup call before stopping
Definition posixthread.cc:87
unsigned int stackSize
Definition posixthread.h:84
Priority priority
Definition posixthread.h:83
void SetThreadAffinity(uint mask)
set thread affinity
Definition posixthread.cc:192
unsigned int GetStackSize() const
get stack size
static Threading::ThreadId GetMyThreadId()
get the thread ID of this thread
Definition posixthread.cc:168
virtual ~PosixThread()
destructor
Definition posixthread.cc:34
void Stop()
request threading code to stop, returns when thread has actually finished
Definition posixthread.cc:100
void SetPriority(Priority p)
set the thread priority
Definition posixthread.h:102
__DeclareClass(PosixThread)
void SetStackSize(unsigned int s)
set stack size in bytes (default is 4 KByte)
Priority GetPriority() const
get the thread priority
Definition posixthread.h:111
virtual void DoWork()
this method runs in the thread context
Definition posixthread.cc:127
static void * ThreadProc(void *self)
internal thread proc helper function
Definition posixthread.cc:138
PosixThread()
constructor
Definition posixthread.cc:21
const Util::String & GetName() const
get thread name
Definition posixthread.h:150
pthread_t threadHandle
Definition posixthread.h:79
static ThreadLocal const char * ThreadName
Definition posixthread.h:86
cpu_set_t affinity
Definition posixthread.h:80
bool ThreadStopRequested()
check if stop is requested, call from DoWork() to see if the thread proc should quit
Definition posixthread.h:124
#define n_assert(exp)
Definition debug.h:50
Posix implemention of a read-many write-few lock.
Definition posixsysfunc.cc:21
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
unsigned int uint
Definition types.h:31