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 | Private Attributes | Friends
nux::SystemThread Class Reference

#include <SystemThread.h>

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

List of all members.

Public Member Functions

 SystemThread (AbstractThread *Parent=0)
 ~SystemThread ()
virtual ThreadState Start (void *arg)
 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 *arg)
 Info: Override this method.
virtual ThreadState StartChildThread (AbstractThread *thread, bool Modal)
virtual void AddChildThread (AbstractThread *)
virtual void RemoveChildThread (AbstractThread *)
virtual void ChildHasFinished (AbstractThread *app)
virtual void TerminateChildThreads ()
virtual bool ThreadCtor ()
 Info: Constructor-like function.
virtual bool ThreadDtor ()
 Info: Destructor-like function.
void TerminateChildWindows ()
void JoinChildThreads ()

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 (SystemThread, AbstractThread)
 SystemThread (const SystemThread &)
 This pointer maybe set by the user in ThreadInitFunc and reused in ThreadExitFunc.
SystemThreadoperator= (const SystemThread &)
SystemThreadoperator& ()

Private Attributes

NString m_ThreadName

Friends

class WindowThread
SystemThreadCreateSystemThread (AbstractThread *Parent, ThreadUserInitFunc UserInitFunc, void *InitData)

Detailed Description

Definition at line 32 of file SystemThread.h.


Constructor & Destructor Documentation

Definition at line 45 of file SystemThread.cpp.

    :   AbstractThread(Parent)
  {

  }

Definition at line 51 of file SystemThread.cpp.

  {
  }

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


Member Function Documentation

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

Implements nux::AbstractThread.

Definition at line 103 of file SystemThread.cpp.

  {
    nuxAssert(thread);
    std::list<AbstractThread*>::iterator it;
    it = find(children_thread_list_.begin(), children_thread_list_.end(), thread);

    if (it == children_thread_list_.end())
    {
      children_thread_list_.push_back(thread);
    }
  }

Here is the caller graph for this function:

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:

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

Implements nux::AbstractThread.

Definition at line 127 of file SystemThread.cpp.

  {
    RemoveChildThread(thread);

    if (thread->Type().IsObjectType(WindowThread::StaticObjectType))
    {
      //SuspendChildGraphics(static_cast<WindowThread*>(thread));
    }

    thread->SetThreadState(THREADSTOP);
  }

Here is the call graph for this function:

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:

void nux::AbstractThread::JoinChildThreads ( ) [protected, inherited]

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:

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

Reimplemented from nux::AbstractThread.

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

Implements nux::AbstractThread.

Definition at line 115 of file SystemThread.cpp.

  {
    nuxAssert(window);
    std::list<AbstractThread*>::iterator it;
    it = find(children_thread_list_.begin(), children_thread_list_.end(), window);

    if (it != children_thread_list_.end())
    {
      children_thread_list_.erase(it);
    }
  }

Here is the caller graph for this function:

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.

int nux::SystemThread::Run ( void *  ) [protected, 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.

Implements nux::AbstractThread.

Definition at line 74 of file SystemThread.cpp.

  {
    if (user_init_func_)
    {
      (*user_init_func_) (this, initialization_data_);
    }

    if (parent_)
    {
      if (parent_->Type().IsObjectType(SystemThread::StaticObjectType))
        static_cast<SystemThread *> (parent_)->ChildHasFinished(this);

      if (parent_->Type().IsObjectType(WindowThread::StaticObjectType))
        static_cast<WindowThread *> (parent_)->ChildHasFinished(this);
    }

    SetThreadState(THREADSTOP);
    TerminateChildThreads();
    return 0;
  }

Here is the call graph for this function:

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::SystemThread::Start ( void *  arg) [virtual]

Info: Starts the thread.

This function starts the thread pointed by m_pThreadFunc with default attributes

Reimplemented from nux::NThread.

Definition at line 55 of file SystemThread.cpp.

  {
    if (!parent_)
    {
      return NThread::Start();
    }
    else
    {
      if (parent_->Type().IsObjectType(SystemThread::StaticObjectType))
        return static_cast<SystemThread *> (parent_)->StartChildThread(this, true);

      if (parent_->Type().IsObjectType(WindowThread::StaticObjectType))
        return static_cast<WindowThread *> (parent_)->StartChildThread(this, true);

      nuxAssertMsg(0, "[WindowThread::Start] This should not happen.");
      return THREAD_START_ERROR;
    }
  }

Here is the caller graph for this function:

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

Implements nux::AbstractThread.

Definition at line 95 of file SystemThread.cpp.

  {
    ThreadState state = thread->NThread::Start();
    //if(state == THREADRUNNING)
    AddChildThread(thread);
    return state;
  }

Here is the call graph for this function:

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.

void nux::SystemThread::TerminateChildThreads ( ) [protected, virtual]

Implements nux::AbstractThread.

Definition at line 139 of file SystemThread.cpp.

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

    for (it = children_thread_list_.begin(); it != children_thread_list_.end(); it++)
    {
      (*it)->SetThreadState(THREADSTOP);
    }

    children_thread_list_.clear();
  }

Here is the caller graph for this function:

void nux::AbstractThread::TerminateChildWindows ( ) [protected, inherited]

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:

bool nux::SystemThread::ThreadCtor ( ) [protected, virtual]

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

Reimplemented from nux::NThread.

Definition at line 151 of file SystemThread.cpp.

  {
    SetThreadState(THREADRUNNING);

    return true;
  }

Here is the call graph for this function:

bool nux::SystemThread::ThreadDtor ( ) [protected, virtual]

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

Reimplemented from nux::NThread.

Definition at line 158 of file SystemThread.cpp.

  {
    return true;
  }

Friends And Related Function Documentation

SystemThread* CreateSystemThread ( AbstractThread Parent,
ThreadUserInitFunc  UserInitFunc,
void *  InitData 
) [friend]

Definition at line 285 of file Nux.cpp.

  {
    SystemThread *system_thread = new SystemThread(parent);
    
    if (system_thread == 0)
    {
      nuxDebugMsg("[CreateSimpleThread] SystemThread creation failed.");
      return NULL;
    }
    system_thread->user_init_func_ = user_init_func;
    system_thread->user_exit_func_ = 0;
    system_thread->initialization_data_ = data;
    return system_thread;
  }
friend class WindowThread [friend]

Definition at line 89 of file SystemThread.h.


Member Data Documentation

std::list<AbstractThread*> nux::AbstractThread::children_thread_list_ [protected, inherited]

Definition at line 52 of file AbstractThread.h.

void* nux::AbstractThread::initialization_data_ [protected, inherited]

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, inherited]

Definition at line 62 of file AbstractThread.h.

Definition at line 85 of file SystemThread.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: