diff --git a/gale/Thread.cpp b/gale/Thread.cpp index 45b0934..ef4bee6 100644 --- a/gale/Thread.cpp +++ b/gale/Thread.cpp @@ -26,13 +26,18 @@ void gale::Thread::start() { if (m_state == state_stop) { GALE_DEBUG("Allocate std11::thread [START]"); m_state = state_starting; - m_thread = new std11::thread(&gale::Thread::threadCall, this); + m_thread = new std11::thread(&gale::Thread::threadCall, this, &gale::getContext()); + if (m_thread == nullptr) { + GALE_ERROR("Can not create thread ..."); + return; + } + m_thread->detach(); GALE_DEBUG("Allocate std11::thread [Set priority]"); // set priority GALE_DEBUG("Allocate std11::thread [Register context]"); // set association with the gale context ... - gale::contextRegisterThread(m_thread); + //gale::contextRegisterThread(m_thread); GALE_DEBUG("Allocate std11::thread [set State]"); m_state = state_running; @@ -52,7 +57,7 @@ void gale::Thread::stop() { } GALE_DEBUG("stop std11::thread [START]"); m_thread->join(); - gale::contextUnRegisterThread(m_thread); + //gale::contextUnRegisterThread(m_thread); GALE_DEBUG("stop std11::thread [delete]"); delete m_thread; m_thread = nullptr; @@ -61,8 +66,70 @@ void gale::Thread::stop() { GALE_DEBUG("stop std11::thread [STOP]"); } -void gale::Thread::threadCall() { - GALE_DEBUG("THREAD MAIN [START]"); +void gale::Thread::threadCall(gale::Context* _context) { + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + GALE_ERROR("THREAD MAIN [START]"); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + gale::setContext(_context); while (m_state != state_stopping) { if (m_state == state_starting) { GALE_DEBUG("run std11::thread [NOTHING to do]"); diff --git a/gale/Thread.h b/gale/Thread.h index 947e89e..084e367 100644 --- a/gale/Thread.h +++ b/gale/Thread.h @@ -11,6 +11,7 @@ #include #include +#include namespace gale { /** @@ -39,7 +40,7 @@ namespace gale { void start(); void stop(); private: - void threadCall(); + void threadCall(gale::Context* _context); protected: virtual bool onThreadCall() { return true; }; }; diff --git a/gale/context/Context.cpp b/gale/context/Context.cpp index a4c9818..6dfb3f4 100644 --- a/gale/context/Context.cpp +++ b/gale/context/Context.cpp @@ -53,6 +53,7 @@ gale::Context& gale::getContext() { if (it != list.end()) { out = it->second; } + g_lockContextMap.unlock(); #if DEBUG_LEVEL > 2 if(out ==nullptr){ @@ -62,8 +63,9 @@ gale::Context& gale::getContext() { return *out; } -static void setContext(gale::Context* _context) { +void gale::setContext(gale::Context* _context) { std::map& list = getContextList(); + GALE_ERROR("Set context : " << std11::this_thread::get_id() << " context pointer : " << uint64_t(_context)); g_lockContextMap.lock(); std::map::iterator it = list.find(std11::this_thread::get_id()); if (it == list.end()) { @@ -80,6 +82,7 @@ void gale::contextRegisterThread(std11::thread* _thread) { } gale::Context* context = &gale::getContext(); std::map& list = getContextList(); + GALE_ERROR("REGISTER Thread : " << _thread->get_id() << " context pointer : " << uint64_t(context)); g_lockContextMap.lock(); std::map::iterator it = list.find(_thread->get_id()); if (it == list.end()) { @@ -551,7 +554,9 @@ bool gale::Context::OS_Draw(bool _displayEveryTime) { } if( needRedraw == true || _displayEveryTime == true) { + lockContext(); m_resourceManager.updateContext(); + unLockContext(); if (m_displayFps == true) { m_FpsSystemContext.incrementCounter(); } diff --git a/gale/context/Context.h b/gale/context/Context.h index 7b84040..35c247b 100644 --- a/gale/context/Context.h +++ b/gale/context/Context.h @@ -301,6 +301,11 @@ namespace gale { * @return current reference on the instance. */ Context& getContext(); + /** + * @brief Set a context of the current thread. + * @param[in] _context Current context to associate. + */ + void setContext(gale::Context* _context); /** * @brief When a new thread is created, it is needed to register it in the gale context interface to permit to get the context associated on it ... * @param[in] _thread generic C++11 thread handle