Nebula
Loading...
Searching...
No Matches
Posix::PosixThread Class Reference

#include <posixthread.h>

Detailed Description

Posix implementation of thread class.

(C) 2006 Radon Labs GmbH (C) 2013-2018 Individual contributors, see AUTHORS file

Inherits Core::RefCounted.

Public Types

enum  Priority { Low , Normal , High }
 thread priorities More...
 

Public Member Functions

 PosixThread ()
 constructor
 
virtual ~PosixThread ()
 destructor
 
void SetPriority (Priority p)
 set the thread priority
 
Priority GetPriority () const
 get the thread priority
 
void SetThreadAffinity (uint mask)
 set thread affinity
 
uint GetThreadAffinity ()
 get thread affinity
 
void SetStackSize (unsigned int s)
 set stack size in bytes (default is 4 KByte)
 
unsigned int GetStackSize () const
 get stack size
 
void SetName (const Util::String &n)
 set thread name
 
const Util::StringGetName () const
 get thread name
 
void Start ()
 start executing the thread code, returns when thread has actually started
 
void Stop ()
 request threading code to stop, returns when thread has actually finished
 
bool IsRunning () const
 return true if thread has been started
 
- Public Member Functions inherited from Core::RefCounted
 RefCounted ()
 constructor
 
int GetRefCount () const
 get the current refcount
 
void AddRef ()
 increment refcount by one
 
void Release ()
 decrement refcount and destroy object if refcount is zero
 
bool IsInstanceOf (const Rtti &rtti) const
 return true if this object is instance of given class
 
bool IsInstanceOf (const Util::String &className) const
 return true if this object is instance of given class by string
 
bool IsInstanceOf (const Util::FourCC &classFourCC) const
 return true if this object is instance of given class by fourcc
 
bool IsA (const Rtti &rtti) const
 return true if this object is instance of given class, or a derived class
 
bool IsA (const Util::String &rttiName) const
 return true if this object is instance of given class, or a derived class, by string
 
bool IsA (const Util::FourCC &rttiFourCC) const
 return true if this object is instance of given class, or a derived class, by fourcc
 
const Util::StringGetClassName () const
 get the class name
 
Util::FourCC GetClassFourCC () const
 get the class FourCC code
 

Static Public Member Functions

static void YieldThread ()
 yield the thread (gives up current time slice)
 
static const char * GetMyThreadName ()
 obtain name of thread from within thread code
 
static Threading::ThreadId GetMyThreadId ()
 get the thread ID of this thread
 
- Static Public Member Functions inherited from Core::RefCounted
static void DumpRefCountingLeaks ()
 dump refcounting leaks, call at end of application (NEBULA_DEBUG builds only!)
 

Protected Member Functions

virtual void EmitWakeupSignal ()
 override this method if your thread loop needs a wakeup call before stopping
 
virtual void DoWork ()
 this method runs in the thread context
 
bool ThreadStopRequested ()
 check if stop is requested, call from DoWork() to see if the thread proc should quit
 
- Protected Member Functions inherited from Core::RefCounted
virtual ~RefCounted ()
 destructor (called when refcount reaches zero)
 

Private Member Functions

 __DeclareClass (PosixThread)
 

Static Private Member Functions

static void * ThreadProc (void *self)
 internal thread proc helper function
 

Private Attributes

pthread_t threadHandle
 
cpu_set_t affinity
 
PosixEvent stopRequestEvent
 
bool running
 
Priority priority
 
unsigned int stackSize
 
Util::String name
 

Static Private Attributes

static ThreadLocal const char * ThreadName
 

Member Enumeration Documentation

◆ Priority

thread priorities

Enumerator
Low 
Normal 
High 

Constructor & Destructor Documentation

◆ PosixThread()

Posix::PosixThread::PosixThread ( )

constructor

◆ ~PosixThread()

Posix::PosixThread::~PosixThread ( )
virtual

destructor

Member Function Documentation

◆ __DeclareClass()

Posix::PosixThread::__DeclareClass ( PosixThread )
private

◆ DoWork()

void Posix::PosixThread::DoWork ( )
protectedvirtual

this method runs in the thread context

This method should be derived in a Thread subclass and contains the actual code which is run in the thread.

The method must not call C-Lib functions under Posix. To terminate the thread, just return from this function. If DoWork() runs in an infinite loop, call ThreadStopRequested() to check whether the Thread object wants the thread code to quit.

◆ EmitWakeupSignal()

void Posix::PosixThread::EmitWakeupSignal ( )
protectedvirtual

override this method if your thread loop needs a wakeup call before stopping

This method is called by Thread::Stop() after setting the stopRequest event and before waiting for the thread to stop.

If your thread runs a loop and waits for jobs it may need an extra wakeup signal to stop waiting and check for the ThreadStopRequested() event. In this case, override this method and signal your event object.

◆ GetMyThreadId()

Threading::ThreadId Posix::PosixThread::GetMyThreadId ( )
static

get the thread ID of this thread

Static method which returns the ThreadId of this thread.

◆ GetMyThreadName()

const char * Posix::PosixThread::GetMyThreadName ( )
static

obtain name of thread from within thread code

Static method to obtain the current thread name from anywhere in the thread's code.

◆ GetName()

const Util::String & Posix::PosixThread::GetName ( ) const
inline

get thread name

Get the thread's name.

This is the vanilla method which returns the name member. To obtain the current thread's name from anywhere in the thread's execution context, call the static method Thread::GetMyThreadName().

◆ GetPriority()

PosixThread::Priority Posix::PosixThread::GetPriority ( ) const
inline

get the thread priority

◆ GetStackSize()

unsigned int Posix::PosixThread::GetStackSize ( ) const

get stack size

◆ GetThreadAffinity()

uint Posix::PosixThread::GetThreadAffinity ( )

get thread affinity

◆ IsRunning()

bool Posix::PosixThread::IsRunning ( ) const
inline

return true if thread has been started

◆ SetName()

void Posix::PosixThread::SetName ( const Util::String & n)
inline

set thread name

Set the thread's name.

To obtain the current thread's name from anywhere in the thread's execution context, call the static method Thread::GetMyThreadName().

◆ SetPriority()

void Posix::PosixThread::SetPriority ( Priority p)
inline

set the thread priority

◆ SetStackSize()

void Posix::PosixThread::SetStackSize ( unsigned int s)

set stack size in bytes (default is 4 KByte)

◆ SetThreadAffinity()

void Posix::PosixThread::SetThreadAffinity ( uint mask)

set thread affinity

◆ Start()

void Posix::PosixThread::Start ( )

start executing the thread code, returns when thread has actually started

Start the thread, this creates a Posix thread and calls the static ThreadProc, which in turn calls the virtual DoWork() class of this object.

The method returns immediately without waiting for the thread to start.

◆ Stop()

void Posix::PosixThread::Stop ( )

request threading code to stop, returns when thread has actually finished

This stops the thread by signalling the stopRequestEvent and waits for the thread to actually quit.

If the thread code runs in a loop it should use the IsStopRequested() method to see if the thread object wants it to shutdown. If so DoWork() should simply return.

◆ ThreadProc()

void * Posix::PosixThread::ThreadProc ( void * self)
staticprivate

internal thread proc helper function

Internal static helper method.

This is called by CreateThread() and simply calls the virtual DoWork() method on the thread object.

◆ ThreadStopRequested()

bool Posix::PosixThread::ThreadStopRequested ( )
inlineprotected

check if stop is requested, call from DoWork() to see if the thread proc should quit

If the derived DoWork() method is running in a loop it must regularly check if the process wants the thread to terminate by calling ThreadStopRequested() and simply return if the result is true.

This will cause the thread to shut down.

◆ YieldThread()

void Posix::PosixThread::YieldThread ( )
static

yield the thread (gives up current time slice)

Yields thread.

Member Data Documentation

◆ affinity

cpu_set_t Posix::PosixThread::affinity
private

◆ name

Util::String Posix::PosixThread::name
private

◆ priority

Priority Posix::PosixThread::priority
private

◆ running

bool Posix::PosixThread::running
private

◆ stackSize

unsigned int Posix::PosixThread::stackSize
private

◆ stopRequestEvent

PosixEvent Posix::PosixThread::stopRequestEvent
private

◆ threadHandle

pthread_t Posix::PosixThread::threadHandle
private

◆ ThreadName

ThreadLocal const char* Posix::PosixThread::ThreadName
staticprivate

The documentation for this class was generated from the following files: