linuxsampler 2.3.1
LinuxSampler::Thread Class Referenceabstract

Abstract base class for classes that need to run in an own thread. More...

#include <Thread.h>

Inheritance diagram for LinuxSampler::Thread:
LinuxSampler::InstrumentEditor

Public Member Functions

 Thread (bool LockMemory, bool RealTime, int PriorityMax, int PriorityDelta)
 
virtual ~Thread ()
 
virtual int StartThread ()
 
virtual int StopThread ()
 
virtual int SignalStopThread ()
 
virtual bool IsRunning ()
 
std::string name ()
 

Static Public Member Functions

static voidallocAlignedMem (size_t boundary, size_t size)
 Allocates an aligned block of memory.
 
static void freeAlignedMem (void *ptr)
 Frees an aligned block of memory allocated with allocAlignedMem()
 
static bool lockMemory (void *addr, size_t size)
 Locks a region of memory in physical RAM.
 
static bool unlockMemory (void *addr, size_t size)
 Unlocks a region of memory in physical RAM.
 
static void pushCancelable (bool cancel)
 
static void popCancelable ()
 
static std::string nameOfCaller ()
 
static void setNameOfCaller (std::string name)
 

Protected Member Functions

virtual int Main ()=0
 This method needs to be implemented by the descending class and is the entry point for the new thread.
 
void TestCancel ()
 Synchronization point for potentially terminating the thread.
 
virtual int SignalStartThread ()
 
virtual int SetSchedulingPriority ()
 
virtual int LockMemory ()
 
virtual void EnableDestructor ()
 
virtual int onThreadEnd ()
 

Detailed Description

Abstract base class for classes that need to run in an own thread.

The deriving class needs to implement the abstract method Main() for the actual task of the thread to be implemented.

IMPORTANT: You MUST call StopThread() before destructing a running Thread object! Destructing the Thread object without stopping it first can lead to undefined behavior! The destructor will detect if the thread is still running and will stop it automatically in this case, however once the destructor is entered, this Thread object's vpointer is already been modified, so if the thread is still running at this point this would cause a data race condition since the active thread is calling virtual methods and thus this can lead to undefined behavior.

Definition at line 64 of file Thread.h.

Constructor & Destructor Documentation

◆ Thread()

LinuxSampler::Thread::Thread ( bool  LockMemory,
bool  RealTime,
int  PriorityMax,
int  PriorityDelta 
)

◆ ~Thread()

virtual LinuxSampler::Thread::~Thread ( )
virtual

Member Function Documentation

◆ allocAlignedMem()

static void * LinuxSampler::Thread::allocAlignedMem ( size_t  boundary,
size_t  size 
)
inlinestatic

Allocates an aligned block of memory.

Allocated memory blocks need to be freed using freeAlignedMem().

Parameters
boundary- the alignement boundary, usually a power of 2 e.g. 4 but it can be an arbitrary number between 1 and 128
size- size in bytes to be allocated
Returns
pointer to the allocated memory block

Definition at line 83 of file Thread.h.

Referenced by LinuxSampler::AudioChannel::AudioChannel().

◆ EnableDestructor()

virtual void LinuxSampler::Thread::EnableDestructor ( )
protectedvirtual

◆ freeAlignedMem()

static void LinuxSampler::Thread::freeAlignedMem ( void ptr)
inlinestatic

Frees an aligned block of memory allocated with allocAlignedMem()

Parameters
ptr- pointer to the memory block

Definition at line 95 of file Thread.h.

Referenced by LinuxSampler::AudioChannel::~AudioChannel().

◆ IsRunning()

virtual bool LinuxSampler::Thread::IsRunning ( )
virtual

◆ LockMemory()

virtual int LinuxSampler::Thread::LockMemory ( )
protectedvirtual

◆ lockMemory()

static bool LinuxSampler::Thread::lockMemory ( void addr,
size_t  size 
)
inlinestatic

Locks a region of memory in physical RAM.

Parameters
addr- address of the memory block
size- size of the memory block
Returns
true if the locking succeded, otherwise false

Definition at line 108 of file Thread.h.

◆ Main()

virtual int LinuxSampler::Thread::Main ( )
protectedpure virtual

This method needs to be implemented by the descending class and is the entry point for the new thread.

NOTE: If your thread runs for a longer time, i.e. if it is running a loop, then you should explicitly call TestCancel() once in a while in your Main() implementation, especially if your implementation does not use any system calls.

Implemented in LinuxSampler::InstrumentEditor.

◆ name()

std::string LinuxSampler::Thread::name ( )

◆ nameOfCaller()

static std::string LinuxSampler::Thread::nameOfCaller ( )
static

◆ onThreadEnd()

virtual int LinuxSampler::Thread::onThreadEnd ( )
protectedvirtual

◆ popCancelable()

static void LinuxSampler::Thread::popCancelable ( )
static

◆ pushCancelable()

static void LinuxSampler::Thread::pushCancelable ( bool  cancel)
static

◆ setNameOfCaller()

static void LinuxSampler::Thread::setNameOfCaller ( std::string  name)
static

◆ SetSchedulingPriority()

virtual int LinuxSampler::Thread::SetSchedulingPriority ( )
protectedvirtual

◆ SignalStartThread()

virtual int LinuxSampler::Thread::SignalStartThread ( )
protectedvirtual

◆ SignalStopThread()

virtual int LinuxSampler::Thread::SignalStopThread ( )
virtual

◆ StartThread()

virtual int LinuxSampler::Thread::StartThread ( )
virtual

◆ StopThread()

virtual int LinuxSampler::Thread::StopThread ( )
virtual

◆ TestCancel()

void LinuxSampler::Thread::TestCancel ( )
protected

Synchronization point for potentially terminating the thread.

Like already described in Main() you should call TestCancel() in your Main() implementation once in a while to provide the system a chance to perform a clean termination of your thread. Depending on the underlying OS, and also depending on whether your are using any system call in your Main() implementation, it might otherwise be possible that the thread cannot be terminated at all! And even if the underlying OS supports terminating busy threads which do not call TestCancel(), this might still cause undefined behavior on such OSes!

◆ unlockMemory()

static bool LinuxSampler::Thread::unlockMemory ( void addr,
size_t  size 
)
inlinestatic

Unlocks a region of memory in physical RAM.

Parameters
addr- address of the memory block
size- size of the memory block
Returns
true if the unlocking succeded, otherwise false

Definition at line 123 of file Thread.h.


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