UCommon
ucommon::Thread Class Referenceabstract

An abstract class for defining classes that operate as a thread. More...

#include <thread.h>

Inheritance diagram for ucommon::Thread:

Public Member Functions

virtual void exit (void)
 Exit the thread context.
 
bool isRunning (void) const
 
 operator bool () const
 
bool operator! () const
 
virtual void run (void)=0
 Abstract interface for thread context run method.
 
void setPriority (void)
 Set thread priority without disrupting scheduling if possible.
 
virtual ~Thread ()
 Destroy thread object, thread-specific data, and execution context.
 

Static Public Member Functions

static size_t cache (void)
 Get cache line size.
 
static void concurrency (int level)
 Set concurrency level of process.
 
static bool equal (pthread_t thread1, pthread_t thread2)
 Determine if two thread identifiers refer to the same thread.
 
static Threadget (void)
 Get mapped thread object.
 
static void init (void)
 Used to initialize threading library.
 
static void policy (int polid)
 Used to specify scheduling policy for threads above priority "0".
 
static void release (void)
 
static pthread_t self (void)
 Get current thread id.
 
static void sleep (timeout_t timeout)
 Sleep current thread for a specified time period.
 
static void yield (void)
 Yield execution context of the current thread.
 

Protected Types

enum  { R_UNUSED }
 

Protected Member Functions

virtual bool is_active (void) const
 Check if running.
 
void map (void)
 Map thread for get method.
 
 Thread (size_t stack=0)
 Create a thread object that will have a preset stack size.
 

Protected Attributes

void * cancellor
 
int priority
 
enum ucommon::Thread:: { ... }  reserved
 
stacksize_t stack
 
pthread_t tid
 

Detailed Description

An abstract class for defining classes that operate as a thread.

A derived thread class has a run method that is invoked with the newly created thread context, and can use the derived object to store all member data that needs to be associated with that context. This means the derived object can safely hold thread-specific data that is managed with the life of the object, rather than having to use the clumsy thread-specific data management and access functions found in thread support libraries.

Author
David Sugar dyfet.nosp@m.@gnu.nosp@m.telep.nosp@m.hony.nosp@m..org

Definition at line 645 of file thread.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
protected

Definition at line 658 of file thread.h.

Constructor & Destructor Documentation

◆ Thread()

ucommon::Thread::Thread ( size_t stack = 0)
protected

Create a thread object that will have a preset stack size.

If 0 is used, then the stack size is os defined/default.

Parameters
stacksize to use or 0 for default.

Member Function Documentation

◆ concurrency()

static void ucommon::Thread::concurrency ( int level)
static

Set concurrency level of process.

This is essentially a portable wrapper for pthread_setconcurrency.

◆ equal()

static bool ucommon::Thread::equal ( pthread_t thread1,
pthread_t thread2 )
static

Determine if two thread identifiers refer to the same thread.

Parameters
thread1to test.
thread2to test.
Returns
true if both are the same context.

◆ exit()

virtual void ucommon::Thread::exit ( void )
virtual

Exit the thread context.

This function should NO LONGER be called directly to exit a running thread. Instead this method will only be used to modify the behavior of the thread context at thread exit, including detached threads which by default delete themselves. This documented usage was changed to support Mozilla NSPR exit behavior in case we support NSPR as an alternate thread runtime in the future.

Reimplemented in ucommon::DetachedThread.

◆ get()

static Thread * ucommon::Thread::get ( void )
static

Get mapped thread object.

This returns the mapped base class of the thread object of the current executing context. You will need to cast to the correct derived class to access derived thread-specific storage. If the current thread context is not mapped NULL is returned.

◆ init()

static void ucommon::Thread::init ( void )
static

Used to initialize threading library.

May be needed for some platforms.

◆ is_active()

virtual bool ucommon::Thread::is_active ( void ) const
protectedvirtual

Check if running.

Reimplemented in ucommon::DetachedThread, and ucommon::JoinableThread.

◆ isRunning()

bool ucommon::Thread::isRunning ( void ) const
inline

Definition at line 806 of file thread.h.

◆ map()

void ucommon::Thread::map ( void )
protected

Map thread for get method.

This should be called from start of the run() method of a derived class.

◆ operator bool()

ucommon::Thread::operator bool ( ) const
inline

Definition at line 798 of file thread.h.

◆ operator!()

bool ucommon::Thread::operator! ( ) const
inline

Definition at line 802 of file thread.h.

◆ policy()

static void ucommon::Thread::policy ( int polid)
static

Used to specify scheduling policy for threads above priority "0".

Normally we apply static realtime policy SCHED_FIFO (default) or SCHED_RR. However, we could apply SCHED_OTHER, etc.

◆ run()

virtual void ucommon::Thread::run ( void )
pure virtual

Abstract interface for thread context run method.

Implemented in ucommon::DetachedThread, and ucommon::JoinableThread.

◆ self()

static pthread_t ucommon::Thread::self ( void )
static

Get current thread id.

Returns
thread id.

◆ setPriority()

void ucommon::Thread::setPriority ( void )

Set thread priority without disrupting scheduling if possible.

Based on scheduling policy. It is recommended that the process is set for realtime scheduling, and this method is actually for internal use.

◆ sleep()

static void ucommon::Thread::sleep ( timeout_t timeout)
static

Sleep current thread for a specified time period.

Parameters
timeoutto sleep for in milliseconds.

◆ yield()

static void ucommon::Thread::yield ( void )
static

Yield execution context of the current thread.

This is a static and may be used anywhere.

Field Documentation

◆ cancellor

void* ucommon::Thread::cancellor
protected

Definition at line 655 of file thread.h.

◆ priority

int ucommon::Thread::priority
protected

Definition at line 661 of file thread.h.

◆ stack

stacksize_t ucommon::Thread::stack
protected

Definition at line 660 of file thread.h.

◆ tid

pthread_t ucommon::Thread::tid
protected

Definition at line 659 of file thread.h.


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