[DEV] set pthread to have a basic version that work

This commit is contained in:
Edouard DUPIN 2015-09-28 21:24:00 +02:00
parent a26eb9c92a
commit 31dff9e69d
4 changed files with 89 additions and 78 deletions

View File

@ -10,9 +10,25 @@
#include <unistd.h>
#include <gale/context/Context.h>
#if defined(__TARGET_OS__Android)
static void* threadCallback(void* _userData) {
gale::Thread* threadHandle = static_cast<gale::Thread*>(_userData);
if (threadHandle != nullptr) {
threadHandle->threadCall();
}
return nullptr;
}
#endif
gale::Thread::Thread() :
m_state(state_stop),
m_thread(nullptr) {
#if !defined(__TARGET_OS__Android)
m_thread(nullptr),
#endif
m_context(nullptr) {
GALE_INFO("Create new Thread");
}
@ -26,12 +42,17 @@ 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, &gale::getContext());
if (m_thread == nullptr) {
GALE_ERROR("Can not create thread ...");
return;
}
m_thread->detach();
m_context = &gale::getContext();
#if defined(__TARGET_OS__Android)
pthread_create(&m_thread, nullptr, &threadCallback, this);
#else
m_thread = new std11::thread(&gale::Thread::threadCall, this);//, &gale::getContext());
if (m_thread == nullptr) {
GALE_ERROR("Can not create thread ...");
return;
}
#endif
//m_thread->detach();
GALE_DEBUG("Allocate std11::thread [Set priority]");
// set priority
@ -53,83 +74,30 @@ void gale::Thread::stop() {
|| m_state == state_starting) {
// requesting a stop ...
GALE_INFO("wait Thread stopping");
usleep(500000);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
GALE_DEBUG("stop std11::thread [START]");
m_thread->join();
#if defined(__TARGET_OS__Android)
//m_thread.join();
#else
m_thread->join();
#endif
//gale::contextUnRegisterThread(m_thread);
GALE_DEBUG("stop std11::thread [delete]");
delete m_thread;
m_thread = nullptr;
#if defined(__TARGET_OS__Android)
#else
delete m_thread;
m_thread = nullptr;
#endif
GALE_DEBUG("stop std11::thread [set state]");
m_state = state_stop;
GALE_DEBUG("stop std11::thread [STOP]");
}
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);
void gale::Thread::threadCall() {
GALE_DEBUG("THREAD MAIN [START]");
gale::setContext(m_context);
while (m_state != state_stopping) {
if (m_state == state_starting) {
GALE_DEBUG("run std11::thread [NOTHING to do]");

View File

@ -13,6 +13,12 @@
#include <etk/thread/tools.h>
#include <gale/context/Context.h>
#if defined(__TARGET_OS__Android)
#include <pthread.h>
#else
#include <thread>
#endif
namespace gale {
/**
* @brief in the dimention class we store the data as the more usefull unit (pixel)
@ -27,7 +33,12 @@ namespace gale {
state_stopping
};
enum state m_state;
std11::thread* m_thread;
#if defined(__TARGET_OS__Android)
pthread_t m_thread;
#else
std11::thread* m_thread;
#endif
gale::Context* m_context;
public:
/**
* @brief Constructor (default :0,0 mode pixel)
@ -39,8 +50,8 @@ namespace gale {
virtual ~Thread();
void start();
void stop();
private:
void threadCall(gale::Context* _context);
public:
void threadCall();
protected:
virtual bool onThreadCall() { return true; };
};

View File

@ -435,25 +435,32 @@ class AndroidContext : public gale::Context {
enum gale::key::status _status,
int32_t _pointerID,
const vec2& _pos) {
GALE_DEBUG("OS_SetInput [BEGIN]");
gale::Context::OS_SetInput(_type, _status, _pointerID, vec2(_pos.x(),m_currentHeight-_pos.y()));
GALE_DEBUG("OS_SetInput [END]");
}
void ANDROID_SetKeyboard(char32_t _myChar, bool _isDown, bool _isARepeateKey=false) {
GALE_DEBUG("ANDROID_SetKeyboard [BEGIN]");
OS_setKeyboard(m_guiKeyBoardSpecialKeyMode,
gale::key::keyboard_char,
(_isDown==true?gale::key::status_down:gale::key::status_up),
_isARepeateKey,
_myChar);
GALE_DEBUG("ANDROID_SetKeyboard [END]");
}
bool ANDROID_systemKeyboradEvent(enum gale::key::keyboard _key, bool _isDown) {
GALE_DEBUG("ANDROID_systemKeyboradEvent [BEGIN]");
OS_setKeyboard(m_guiKeyBoardSpecialKeyMode,
_key,
(_isDown==true?gale::key::status_down:gale::key::status_up));
GALE_DEBUG("ANDROID_systemKeyboradEvent [END]");
return false;
}
void ANDROID_SetKeyboardMove(int _move, bool _isDown, bool _isARepeateKey=false) {
GALE_DEBUG("ANDROID_SetKeyboardMove [BEGIN]");
// direct wrapping :
enum gale::key::keyboard move = (enum gale::key::keyboard)_move;
m_guiKeyBoardSpecialKeyMode.update(move, _isDown);
@ -461,11 +468,14 @@ class AndroidContext : public gale::Context {
move,
(_isDown==true?gale::key::status_down:gale::key::status_up),
_isARepeateKey);
GALE_DEBUG("ANDROID_SetKeyboardMove [END]");
}
void OS_Resize(const vec2& _size) {
GALE_DEBUG("OS_Resize [BEGIN]");
m_currentHeight = _size.y();
gale::Context::OS_Resize(_size);
GALE_DEBUG("OS_Resize [END]");
}
};
@ -878,6 +888,7 @@ extern "C" {
jobject _thiz,
jint _id) {
std::unique_lock<std::mutex> lock(g_interfaceMutex);
GALE_DEBUG("Java_org_gale_Gale_EWrenderInit [BEGIN]");
if( _id >= (int32_t)s_listInstance.size()
|| _id<0
|| nullptr == s_listInstance[_id] ) {
@ -885,6 +896,7 @@ extern "C" {
// TODO : generate error in java to stop the current instance
return;
}
GALE_DEBUG("Java_org_gale_Gale_EWrenderInit [END]");
}
void Java_org_gale_Gale_EWrenderResize(JNIEnv* _env,
@ -893,6 +905,7 @@ extern "C" {
jint _w,
jint _h) {
std::unique_lock<std::mutex> lock(g_interfaceMutex);
GALE_DEBUG("Java_org_gale_Gale_EWrenderResize [BEGIN]");
if( _id >= (int32_t)s_listInstance.size()
|| _id<0
|| nullptr == s_listInstance[_id] ) {
@ -901,6 +914,7 @@ extern "C" {
return;
}
s_listInstance[_id]->OS_Resize(vec2(_w, _h));
GALE_DEBUG("Java_org_gale_Gale_EWrenderResize [END]");
}
// TODO : Return true or false to not redraw when the under draw has not be done (processing gain of time)
@ -908,6 +922,7 @@ extern "C" {
jobject _thiz,
jint _id) {
std::unique_lock<std::mutex> lock(g_interfaceMutex);
GALE_DEBUG("Java_org_gale_Gale_EWrenderDraw [BEGIN]");
if( _id >= (int32_t)s_listInstance.size()
|| _id<0
|| nullptr == s_listInstance[_id] ) {
@ -916,6 +931,7 @@ extern "C" {
return;
}
s_listInstance[_id]->OS_Draw(true);
GALE_DEBUG("Java_org_gale_Gale_EWrenderDraw [END]");
}
};

View File

@ -506,6 +506,7 @@ void gale::Context::clipBoardSet(enum gale::context::clipBoard::clipboardListe _
}
bool gale::Context::OS_Draw(bool _displayEveryTime) {
GALE_INFO("plop 10");
gale::openGL::threadHasContext();
int64_t currentTime = gale::getTime();
// this is to prevent the multiple display at the a high frequency ...
@ -517,11 +518,13 @@ bool gale::Context::OS_Draw(bool _displayEveryTime) {
}
#endif
m_previousDisplayTime = currentTime;
GALE_INFO("plop 20");
// process the events
if (m_displayFps == true) {
m_FpsSystemEvent.tic();
}
GALE_INFO("plop 30");
bool needRedraw = false;
//! Event management section ...
{
@ -544,6 +547,7 @@ bool gale::Context::OS_Draw(bool _displayEveryTime) {
// release the curent interface :
unLockContext();
}
GALE_INFO("plop 40");
bool hasDisplayDone = false;
//! drawing section:
{
@ -552,6 +556,7 @@ bool gale::Context::OS_Draw(bool _displayEveryTime) {
if (m_displayFps == true) {
m_FpsSystemContext.tic();
}
GALE_INFO("plop 41");
if( needRedraw == true
|| _displayEveryTime == true) {
lockContext();
@ -561,10 +566,12 @@ bool gale::Context::OS_Draw(bool _displayEveryTime) {
m_FpsSystemContext.incrementCounter();
}
}
GALE_INFO("plop 42");
if (m_displayFps == true) {
m_FpsSystemContext.toc();
m_FpsSystem.tic();
}
GALE_INFO("plop 43");
if (m_application != nullptr) {
if( true == needRedraw
|| true == _displayEveryTime) {
@ -576,28 +583,35 @@ bool gale::Context::OS_Draw(bool _displayEveryTime) {
hasDisplayDone = true;
}
}
GALE_INFO("plop 44");
if (m_displayFps == true) {
m_FpsSystem.toc();
m_FpsFlush.tic();
}
GALE_INFO("plop 45");
if (hasDisplayDone == true) {
if (m_displayFps == true) {
m_FpsFlush.incrementCounter();
}
gale::openGL::flush();
}
GALE_INFO("plop 46");
if (m_displayFps == true) {
m_FpsFlush.toc();
}
GALE_INFO("plop 47");
// release open GL Context
gale::openGL::unLock();
GALE_INFO("plop 48");
}
GALE_INFO("plop 50");
if (m_displayFps == true) {
m_FpsSystemEvent.draw();
m_FpsSystemContext.draw();
m_FpsSystem.draw();
m_FpsFlush.draw();
}
GALE_INFO("plop 60");
{
// set the curent interface :
lockContext();
@ -612,7 +626,9 @@ bool gale::Context::OS_Draw(bool _displayEveryTime) {
// release the curent interface :
unLockContext();
}
GALE_INFO("plop 70");
gale::openGL::threadHasNoMoreContext();
GALE_INFO("plop 80");
return hasDisplayDone;
}