Back to index

nux  3.0.0
SystemThread.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "Nux.h"
00024 
00025 #include "Layout.h"
00026 #if defined(NUX_OS_WINDOWS)
00027 #include "NuxGraphics/GraphicsDisplay.h"
00028 #elif defined(NUX_OS_LINUX)
00029 #include "NuxGraphics/GraphicsDisplay.h"
00030 #endif
00031 #include "NuxGraphics/GraphicsEngine.h"
00032 #include "ClientArea.h"
00033 #include "WindowCompositor.h"
00034 #include "TimerProc.h"
00035 #include "WindowCompositor.h"
00036 #include "WindowThread.h"
00037 #include "SystemThread.h"
00038 
00039 namespace nux
00040 {
00041 
00042   NUX_IMPLEMENT_OBJECT_TYPE(SystemThread);
00043 
00044 
00045   SystemThread::SystemThread(AbstractThread *Parent/* = 0*/)
00046     :   AbstractThread(Parent)
00047   {
00048 
00049   }
00050 
00051   SystemThread::~SystemThread()
00052   {
00053   }
00054 
00055   ThreadState SystemThread::Start(void *arg)
00056   {
00057     if (!parent_)
00058     {
00059       return NThread::Start();
00060     }
00061     else
00062     {
00063       if (parent_->Type().IsObjectType(SystemThread::StaticObjectType))
00064         return static_cast<SystemThread *> (parent_)->StartChildThread(this, true);
00065 
00066       if (parent_->Type().IsObjectType(WindowThread::StaticObjectType))
00067         return static_cast<WindowThread *> (parent_)->StartChildThread(this, true);
00068 
00069       nuxAssertMsg(0, "[WindowThread::Start] This should not happen.");
00070       return THREAD_START_ERROR;
00071     }
00072   }
00073 
00074   int SystemThread::Run(void *arg)
00075   {
00076     if (user_init_func_)
00077     {
00078       (*user_init_func_) (this, initialization_data_);
00079     }
00080 
00081     if (parent_)
00082     {
00083       if (parent_->Type().IsObjectType(SystemThread::StaticObjectType))
00084         static_cast<SystemThread *> (parent_)->ChildHasFinished(this);
00085 
00086       if (parent_->Type().IsObjectType(WindowThread::StaticObjectType))
00087         static_cast<WindowThread *> (parent_)->ChildHasFinished(this);
00088     }
00089 
00090     SetThreadState(THREADSTOP);
00091     TerminateChildThreads();
00092     return 0;
00093   }
00094 
00095   ThreadState SystemThread::StartChildThread(AbstractThread *thread, bool Modal)
00096   {
00097     ThreadState state = thread->NThread::Start();
00098     //if(state == THREADRUNNING)
00099     AddChildThread(thread);
00100     return state;
00101   }
00102 
00103   void SystemThread::AddChildThread(AbstractThread *thread)
00104   {
00105     nuxAssert(thread);
00106     std::list<AbstractThread*>::iterator it;
00107     it = find(children_thread_list_.begin(), children_thread_list_.end(), thread);
00108 
00109     if (it == children_thread_list_.end())
00110     {
00111       children_thread_list_.push_back(thread);
00112     }
00113   }
00114 
00115   void SystemThread::RemoveChildThread(AbstractThread *window)
00116   {
00117     nuxAssert(window);
00118     std::list<AbstractThread*>::iterator it;
00119     it = find(children_thread_list_.begin(), children_thread_list_.end(), window);
00120 
00121     if (it != children_thread_list_.end())
00122     {
00123       children_thread_list_.erase(it);
00124     }
00125   }
00126 
00127   void SystemThread::ChildHasFinished(AbstractThread *thread)
00128   {
00129     RemoveChildThread(thread);
00130 
00131     if (thread->Type().IsObjectType(WindowThread::StaticObjectType))
00132     {
00133       //SuspendChildGraphics(static_cast<WindowThread*>(thread));
00134     }
00135 
00136     thread->SetThreadState(THREADSTOP);
00137   }
00138 
00139   void SystemThread::TerminateChildThreads()
00140   {
00141     std::list<AbstractThread*>::iterator it;
00142 
00143     for (it = children_thread_list_.begin(); it != children_thread_list_.end(); it++)
00144     {
00145       (*it)->SetThreadState(THREADSTOP);
00146     }
00147 
00148     children_thread_list_.clear();
00149   }
00150 
00151   bool SystemThread::ThreadCtor()
00152   {
00153     SetThreadState(THREADRUNNING);
00154 
00155     return true;
00156   }
00157 
00158   bool SystemThread::ThreadDtor()
00159   {
00160     return true;
00161   }
00162 
00163 }