Back to index

nux  3.0.0
Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Private Member Functions
nux::AbstractThread Class Reference

#include <AbstractThread.h>

Inheritance diagram for nux::AbstractThread:
Inheritance graph
[legend]
Collaboration diagram for nux::AbstractThread:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 AbstractThread (AbstractThread *Parent)
virtual ~AbstractThread ()
virtual ThreadState Start (void *arg=NULL)
 Info: Starts the thread.
virtual ThreadState Stop (bool bForceKill=false)
 Info: Stops the thread.
ThreadState Suspend ()
ThreadState Resume ()
ThreadState ResumeStart ()
ThreadState ResumeExit ()
unsigned int GetExitCode () const
 Info: Starts the thread.
void Attach (ThreadRoutineFunc lpThreadFunc)
 Info: Attaches a Thread Function.
void Detach (void)
 Info: Detaches the Attached Thread Function.
pthread_t GetPThread ()
ThreadState GetThreadState () const
void SetThreadState (ThreadState state)

Static Public Member Functions

static ThreadWaitResult JoinThread (NThread *thread, unsigned int milliseconds)
 Wait for a thread to complete.

Protected Member Functions

virtual int Run (void *)=0
 Info: Override this method.
virtual ThreadState StartChildThread (AbstractThread *thread, bool Modal)=0
virtual void AddChildThread (AbstractThread *)=0
virtual void RemoveChildThread (AbstractThread *)=0
virtual void ChildHasFinished (AbstractThread *app)=0
virtual void TerminateChildThreads ()=0
void TerminateChildWindows ()
void JoinChildThreads ()
virtual bool ThreadCtor ()
 Info: Constructor-like function.
virtual bool ThreadDtor ()
 Info: Destructor-like function.

Static Protected Member Functions

static void * EntryPoint (void *)
 Info: DONT override this method.

Protected Attributes

std::list< AbstractThread * > children_thread_list_
AbstractThreadparent_
ThreadUserInitFunc user_init_func_
ThreadUserExitFunc user_exit_func_
void * initialization_data_
 This pointer maybe set by the user in ThreadInitFunc and reused in ThreadExitFunc.
std::list< NThread * > m_ThreadList
volatile ThreadState m_ThreadState
NThreadContext m_ThreadCtx
 Attributes Section.
ThreadRoutineFunc m_pThreadFunc

Private Member Functions

 NUX_DECLARE_OBJECT_TYPE (AbstractThread, NThread)
 AbstractThread (const AbstractThread &)
AbstractThreadoperator= (const AbstractThread &)
AbstractThreadoperator& ()

Detailed Description

Definition at line 33 of file AbstractThread.h.


Constructor & Destructor Documentation

Definition at line 32 of file AbstractThread.cpp.

    :   parent_(Parent)
    ,   user_init_func_(NULL)
    ,   user_exit_func_(NULL)
  {

  }

Definition at line 40 of file AbstractThread.cpp.

  {

  }

Member Function Documentation

virtual void nux::AbstractThread::AddChildThread ( AbstractThread ) [protected, pure virtual]

Implemented in nux::WindowThread, and nux::SystemThread.

void nux::NThread::Attach ( ThreadRoutineFunc  lpThreadFunc) [inline, inherited]

Info: Attaches a Thread Function.

Used primarily for porting but can serve in developing generic thread objects

Definition at line 352 of file ThreadGNU.h.

    {
      m_pThreadFunc = lpThreadFunc;
    }

Here is the caller graph for this function:

virtual void nux::AbstractThread::ChildHasFinished ( AbstractThread app) [protected, pure virtual]

Implemented in nux::WindowThread, and nux::SystemThread.

void nux::NThread::Detach ( void  ) [inline, inherited]

Info: Detaches the Attached Thread Function.

Detaches the Attached Thread Function, If any. by resetting the thread function pointer to EntryPoint1

Definition at line 363 of file ThreadGNU.h.

Here is the call graph for this function:

void * nux::NThread::EntryPoint ( void *  pArg) [static, protected, inherited]

Info: DONT override this method.

This function is like a standard template. Override if you are sure of what you are doing.

In C++ the entry function of a thread cannot be a normal member function of a class. However, it can be a static member function of a class. This is what we will use as the entry point. There is a gotcha here though. Static member functions do not have access to the this pointer of a C++ object. They can only access static data. Fortunately, there is way to do it. Thread entry point functions take a void * as a parameter so that the caller can typecast any data and pass in to the thread. We will use this to pass this to the static function. The static function will then typecast the void * and use it to call a non static member function

Definition at line 199 of file ThreadGNU.cpp.

  {
    NThread *pParent = reinterpret_cast<NThread *> (pArg);

    if (pParent == 0)
    {
      nuxDebugMsg (TEXT ("[NThread::EntryPoint] Invalid pointer. The thread will exit.") );
      return 0;
    }

    if (!pParent->ThreadCtor() )
    {
      // return another message saying the thread could not execute due to error in ThreadCtor;
    }

    pParent->Run ( pParent->m_ThreadCtx.m_pUserData );

    pParent->ThreadDtor();
    return 0;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned int nux::NThread::GetExitCode ( ) const [inherited]

Info: Starts the thread.

This function starts the thread pointed by m_pThreadFunc with default attributes

Definition at line 220 of file ThreadGNU.cpp.

pthread_t nux::NThread::GetPThread ( ) [inherited]

Definition at line 225 of file ThreadGNU.cpp.

  {
    return m_ThreadCtx.m_dwTID;
  }

Here is the caller graph for this function:

Definition at line 230 of file ThreadGNU.cpp.

  {
    return m_ThreadState;
  }

Here is the caller graph for this function:

Definition at line 59 of file AbstractThread.cpp.

  {
#if defined(NUX_OS_WINDOWS)
    size_t sz = children_thread_list_.size();

    if (sz == 0)
    {
      return;
    }

    HANDLE *hdl = new HANDLE[sz];

    int i = 0;
    std::list<AbstractThread*>::iterator it;
    for (it = children_thread_list_.begin(); it != children_thread_list_.end(); ++it, ++i)
    {
      hdl[i] = (*it)->GetThreadHandle();
    }

    unsigned int result = WaitForMultipleObjects(sz, hdl, FALSE, INFINITE);
#else
  //#error WindowThread::JoinChildThreads has not been implemented for this platform.
  // Todo
#endif

    return;
  }

Here is the caller graph for this function:

ThreadWaitResult nux::NThread::JoinThread ( NThread thread,
unsigned int  milliseconds 
) [static, inherited]

Wait for a thread to complete.

The second parameters to this call specifies a how long to wait for the thread to complete. The following options are available: ThreadWaitTimeout::THREAD_WAIT_TIMEOUT_NONE: The function returns immediately if the thread exits. ThreadWaitTimeout::THREAD_WAIT_TIMEOUT_FOREVER: The function waits until the thread exits.

Parameters:
threadPointer to a valid NThread.
millisecondsTime to wait for the thread to complete.

Definition at line 240 of file ThreadGNU.cpp.

  {
    if (thread == NULL)
    {
      return THREAD_WAIT_RESULT_FAILED;
    }

    void *value_ptr = NULL;
    int result = pthread_join(thread->GetPThread(), &value_ptr);

    switch(result)
    {
    case 0:
      return THREAD_WAIT_RESULT_COMPLETED;
    default:
      return THREAD_WAIT_RESULT_FAILED;
    }
  }

Here is the call graph for this function:

AbstractThread* nux::AbstractThread::operator& ( ) [private]

Reimplemented in nux::WindowThread, and nux::SystemThread.

AbstractThread& nux::AbstractThread::operator= ( const AbstractThread ) [private]
virtual void nux::AbstractThread::RemoveChildThread ( AbstractThread ) [protected, pure virtual]

Implemented in nux::WindowThread, and nux::SystemThread.

Definition at line 180 of file ThreadGNU.cpp.

  {
    return m_ThreadState;
  }

Definition at line 193 of file ThreadGNU.cpp.

Definition at line 186 of file ThreadGNU.cpp.

virtual int nux::AbstractThread::Run ( void *  ) [protected, pure virtual]

Info: Override this method.

This function should contain the body/code of your thread. Notice the signature is similar to that of any worker thread function except for the calling convention.

Reimplemented from nux::NThread.

Implemented in nux::WindowThread, and nux::SystemThread.

void nux::NThread::SetThreadState ( ThreadState  state) [inherited]

Definition at line 235 of file ThreadGNU.cpp.

  {
    m_ThreadState = state;
  }

Here is the caller graph for this function:

ThreadState nux::NThread::Start ( void *  arg = NULL) [virtual, inherited]

Info: Starts the thread.

This function starts the thread pointed by m_pThreadFunc with default attributes

Reimplemented in nux::WindowThread, and nux::SystemThread.

Definition at line 141 of file ThreadGNU.cpp.

  {
    m_ThreadCtx.m_pUserData = arg;
    int ret = pthread_create (&m_ThreadCtx.m_dwTID,
                              NULL,
                              m_pThreadFunc,
                              this);

    if (ret != 0)
    {
      nuxDebugMsg (TEXT ("[NThread::Start] Cannot start thread.") );
      m_ThreadState = THREAD_START_ERROR;
      return m_ThreadState;
    }

    return m_ThreadState;
  }

Here is the caller graph for this function:

virtual ThreadState nux::AbstractThread::StartChildThread ( AbstractThread thread,
bool  Modal 
) [protected, pure virtual]

Implemented in nux::WindowThread, and nux::SystemThread.

ThreadState nux::NThread::Stop ( bool  bForceKill = false) [virtual, inherited]

Info: Stops the thread.

This function stops the current thread. We can force kill a thread which results in a TerminateThread.

Definition at line 159 of file ThreadGNU.cpp.

  {
    int ret = pthread_detach (m_ThreadCtx.m_dwTID);

    if (ret != 0)
    {
      nuxDebugMsg (TEXT ("[NThread::Stop] Cannot detach thread.") );
      m_ThreadState = THREAD_STOP_ERROR;
      return m_ThreadState;
    }

    m_ThreadState = THREADSTOP;
    return m_ThreadState;
  }

Here is the caller graph for this function:

Definition at line 174 of file ThreadGNU.cpp.

virtual void nux::AbstractThread::TerminateChildThreads ( ) [protected, pure virtual]

Implemented in nux::WindowThread, and nux::SystemThread.

Definition at line 45 of file AbstractThread.cpp.

  {
    std::list<AbstractThread*>::iterator it;

    for (it = children_thread_list_.begin(); it != children_thread_list_.end(); it++)
    {
      if ((*it)->Type().IsObjectType(WindowThread::StaticObjectType))
      {
        // Exit the main loop of a window.
        static_cast<WindowThread*>(*it)->ExitMainLoop();
      }
    }
  }

Here is the call graph for this function:

Here is the caller graph for this function:

virtual bool nux::NThread::ThreadCtor ( ) [inline, protected, virtual, inherited]

Info: Constructor-like function.

Will be called by EntryPoint before executing the thread body. Override this function to provide your extra initialization.

NOTE: do not confuse it with the classes constructor

Returns:
TRUE if the thread can continue running the program. If FALSE is returned, the thread won't execute the main body Run() and will exit without calling ThreadDtor.

Reimplemented in nux::WindowThread, and nux::SystemThread.

Definition at line 424 of file ThreadGNU.h.

    {
      return true;
    }

Here is the caller graph for this function:

virtual bool nux::NThread::ThreadDtor ( ) [inline, protected, virtual, inherited]

Info: Destructor-like function.

Will be called by EntryPoint after executing the thread body. Override this function to provide your extra destruction.

NOTE: do not confuse it with the classes constructor

Returns:
TRUE if this function executed without problems.

Reimplemented in nux::WindowThread, and nux::SystemThread.

Definition at line 438 of file ThreadGNU.h.

    {
      return true;
    }

Here is the caller graph for this function:


Member Data Documentation

Definition at line 52 of file AbstractThread.h.

This pointer maybe set by the user in ThreadInitFunc and reused in ThreadExitFunc.

Definition at line 60 of file AbstractThread.h.

Definition at line 486 of file ThreadGNU.h.

NThreadContext nux::NThread::m_ThreadCtx [protected, inherited]

Attributes Section.

Info: Members of NThread

Definition at line 485 of file ThreadGNU.h.

std::list<NThread *> nux::AbstractThread::m_ThreadList [protected]

Definition at line 62 of file AbstractThread.h.

volatile ThreadState nux::NThread::m_ThreadState [protected, inherited]

Definition at line 386 of file ThreadGNU.h.

Definition at line 53 of file AbstractThread.h.

Definition at line 55 of file AbstractThread.h.

Definition at line 54 of file AbstractThread.h.


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