Start to add the thread between the interface and the system

This commit is contained in:
Edouard Dupin 2012-01-26 18:13:15 +01:00
parent 4b3456b28c
commit 37098ebad7
8 changed files with 347 additions and 17 deletions

View File

@ -26,6 +26,10 @@
#include <sys/time.h>
#include <time.h>
#include <stdint.h>
#include <pthread.h>
#include <tools_debug.h>
#include <ewol/threadMsg.h>
// declaration of the ewol android abstraction ...
void EWOL_NativeInit(void);
@ -39,62 +43,163 @@ void EWOL_NativeApplicationInit(void);
void EWOL_NativeApplicationUnInit(void);
void EWOL_NativeRender(void);
static ewol::threadMsg::threadMsg_ts androidJniMsg;
static pthread_t androidJniThread;
static pthread_attr_t androidJniThreadAttr;
enum {
JNI_NONE,
JNI_INIT,
JNI_DONE,
JNI_RESIZE,
JNI_INPUT_MOTION,
JNI_INPUT_STATE,
JNI_DATA_ARCHIVE_DIR,
JNI_APP_INIT,
JNI_APP_UN_INIT,
JNI_APP_RENDERER,
};
#include <unistd.h>
static void* BaseAppEntry(void* param)
{
EDN_DEBUG("start Ewol Basic thread ...");
ewol::threadMsg::tmppp5656("start Ewol Basic thread ...");
while(1) {
ewol::threadMsg::threadMsgContent_ts data;
ewol::threadMsg::tmppp5656("start waiting");
ewol::threadMsg::WaitMessage(androidJniMsg, data);
ewol::threadMsg::tmppp5656("end waiting");
switch (data.type) {
case JNI_NONE:
ewol::threadMsg::tmppp5656("JNI_NONE");
break;
case JNI_INIT:
ewol::threadMsg::tmppp5656("JNI_INIT");
break;
case JNI_DONE:
ewol::threadMsg::tmppp5656("JNI_DONE");
break;
case JNI_RESIZE:
ewol::threadMsg::tmppp5656("JNI_RESIZE");
break;
case JNI_INPUT_MOTION:
ewol::threadMsg::tmppp5656("JNI_INPUT_MOTION");
break;
case JNI_INPUT_STATE:
ewol::threadMsg::tmppp5656("JNI_INPUT_STATE");
break;
case JNI_DATA_ARCHIVE_DIR:
ewol::threadMsg::tmppp5656("JNI_DATA_ARCHIVE_DIR");
break;
case JNI_APP_INIT:
ewol::threadMsg::tmppp5656("JNI_APP_INIT");
break;
case JNI_APP_UN_INIT:
ewol::threadMsg::tmppp5656("JNI_APP_UN_INIT");
break;
case JNI_APP_RENDERER:
ewol::threadMsg::tmppp5656("JNI_APP_RENDERER");
break;
default:
ewol::threadMsg::tmppp5656("UNKNOW");
break;
}
}
EDN_DEBUG("End Ewol Basic thread ...");
pthread_exit(NULL);
}
void BaseInit(void)
{
static bool isInit = false;
if (false == isInit) {
// create interface mutex :
ewol::threadMsg::Init(androidJniMsg);
// init the thread :
//pthread_attr_init(&androidJniThreadAttr);
//pthread_attr_setdetachstate(&androidJniThreadAttr, PTHREAD_CREATE_DETACHED);
//pthread_create(&androidJniThread, &androidJniThreadAttr, BaseAppEntry, NULL);
pthread_create(&androidJniThread, NULL, BaseAppEntry, NULL);
isInit = true;
}
}
extern "C"
{
/* Call to initialize the graphics state */
void Java_com___PROJECT_VENDOR_____PROJECT_PACKAGE___EwolRenderer_nativeInit( JNIEnv* env )
{
EWOL_NativeInit();
BaseInit();
ewol::threadMsg::SendMessage(androidJniMsg, JNI_INIT);
//EWOL_NativeInit();
}
void Java_com___PROJECT_VENDOR_____PROJECT_PACKAGE___EwolRenderer_nativeResize( JNIEnv* env, jobject thiz, jint w, jint h )
{
EWOL_NativeResize(w, h);
ewol::threadMsg::SendMessage(androidJniMsg, JNI_RESIZE);
//EWOL_NativeResize(w, h);
}
void Java_com___PROJECT_VENDOR_____PROJECT_PACKAGE___EwolRenderer_nativeDone( JNIEnv* env )
{
EWOL_NativeDone();
ewol::threadMsg::SendMessage(androidJniMsg, JNI_DONE);
//EWOL_NativeDone();
}
void Java_com___PROJECT_VENDOR_____PROJECT_PACKAGE___EwolGLSurfaceView_nativeEventInputMotion( JNIEnv* env, jobject thiz, jint pointerID, jfloat x, jfloat y )
{
EWOL_NativeEventInputMotion(pointerID, x, y);
ewol::threadMsg::SendMessage(androidJniMsg, JNI_INPUT_MOTION);
//EWOL_NativeEventInputMotion(pointerID, x, y);
}
void Java_com___PROJECT_VENDOR_____PROJECT_PACKAGE___EwolGLSurfaceView_nativeEventInputState( JNIEnv* env, jobject thiz, jint pointerID, jboolean isUp, jfloat x, jfloat y )
{
EWOL_NativeEventInputState(pointerID, isUp, x, y);
ewol::threadMsg::SendMessage(androidJniMsg, JNI_INPUT_STATE);
//EWOL_NativeEventInputState(pointerID, isUp, x, y);
}
void Java_com___PROJECT_VENDOR_____PROJECT_PACKAGE___EwolGLSurfaceView_nativeEventUnknow( JNIEnv* env, jobject thiz, jint ID)
{
EWOL_NativeEventUnknow(ID);
ewol::threadMsg::SendMessage(androidJniMsg, JNI_DATA_ARCHIVE_DIR);
//EWOL_NativeEventUnknow(ID);
}
void Java_com___PROJECT_VENDOR_____PROJECT_PACKAGE___EwolGLSurfaceView_nativeParamSetArchiveDir( JNIEnv* env, jobject thiz, jint mode, jstring myString)
{
ewol::threadMsg::SendMessage(androidJniMsg, 0);
const char* str = env->GetStringUTFChars(myString,0);
EWOL_NativeParamSetArchiveDir(mode, str);
//EWOL_NativeParamSetArchiveDir(mode, str);
//env->ReleaseStringUTFChars(str,myString,0);
}
void Java_com___PROJECT_VENDOR_____PROJECT_PACKAGE___EwolGLSurfaceView_nativeApplicationInit( JNIEnv* env)
{
EWOL_NativeApplicationInit();
ewol::threadMsg::SendMessage(androidJniMsg, JNI_APP_INIT);
//EWOL_NativeApplicationInit();
}
void Java_com___PROJECT_VENDOR_____PROJECT_PACKAGE___EwolGLSurfaceView_nativeApplicationUnInit( JNIEnv* env)
{
EWOL_NativeApplicationUnInit();
ewol::threadMsg::SendMessage(androidJniMsg, JNI_APP_UN_INIT);
//EWOL_NativeApplicationUnInit();
}
void Java_com___PROJECT_VENDOR_____PROJECT_PACKAGE___EwolRenderer_nativeRender( JNIEnv* env )
{
EWOL_NativeRender();
//pthread_mutex_lock(&androidJniMutex);
//EWOL_NativeRender();
//messageID = JNI_APP_RENDERER;
//pthread_cond_signal(&androidJniCond);
//pthread_mutex_unlock(&androidJniMutex);
}
}

View File

@ -68,7 +68,7 @@ void TOOLS_DisplayTime(void);
#define __class__ (NULL)
#define ETK_DBG_COMMON(libName, color, info, data) do { \
etk::cout << color; \
etk::cout << etk::cstart << color; \
TOOLS_DisplayTime(); \
TOOLS_DisplayFuncName(__LINE__, __class__, __func__, libName); \
etk::cout << "[" << info << "] " << data; \

View File

@ -28,4 +28,5 @@
etk::CCout etk::cout;
etk::CEndl etk::endl;
etk::CHex etk::hex;
etk::CStart etk::cstart;

View File

@ -29,6 +29,7 @@
#include <cstdio>
#include <typeinfo>
#include <string.h>
#include <pthread.h>
#if defined(__PLATFORM__Android)
# include <android/log.h>
@ -43,19 +44,25 @@
namespace etk{
class CEndl{};
class CHex{};
class CStart{};
class CCout{
private:
bool hex;
public:
private:
char m_tmpChar[MAX_LOG_SIZE+1];
char tmp[MAX_LOG_SIZE_TMP];
char m_tmpChar[MAX_LOG_SIZE+1];
char tmp[MAX_LOG_SIZE_TMP];
pthread_mutex_t m_mutex;
public:
CCout(){
hex=false;
memset(m_tmpChar, 0, (MAX_LOG_SIZE+1)*sizeof(char));
pthread_mutex_init(&m_mutex, NULL);
};
~CCout() { };
~CCout() {
pthread_mutex_destroy(&m_mutex);
};
CCout& operator << (int t) {
snprintf(tmp, MAX_LOG_SIZE_TMP, "%d", t);
strncat(m_tmpChar, tmp, MAX_LOG_SIZE);
@ -101,6 +108,10 @@ namespace etk{
strncat(m_tmpChar, tmp, MAX_LOG_SIZE);
return *this;
}
CCout& operator << (CStart ccc) {
pthread_mutex_lock(&m_mutex);
return *this;
}
CCout& operator << (etk::CEndl t) {
strncat(m_tmpChar, "\n", MAX_LOG_SIZE);
m_tmpChar[MAX_LOG_SIZE] = '\0';
@ -110,12 +121,14 @@ namespace etk{
printf("%s", m_tmpChar);
#endif
memset(m_tmpChar, 0, (MAX_LOG_SIZE+1)*sizeof(char));
pthread_mutex_unlock(&m_mutex);
return *this;
}
};
extern etk::CCout cout;
extern etk::CEndl endl;
extern etk::CHex hex;
extern etk::CStart cstart;
}
#endif

View File

@ -255,10 +255,10 @@ template<typename MY_TYPE=int32_t> class VectorType
if (NULL!=m_data) {
ETK_FREE(m_data);
m_data = NULL;
m_allocated = 0;
m_size = 0;
m_increment = 0;
}
m_allocated = 0;
m_size = 0;
m_increment = 0;
}
/**

View File

@ -0,0 +1,139 @@
/**
*******************************************************************************
* @file threadMsg.cpp
* @brief User abstraction for message to a specific thread (Sources)
* @author Edouard DUPIN
* @date 26/01/2012
* @par Project
* ewol
*
* @par Copyright
* Copyright 2011 Edouard DUPIN, all right reserved
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY.
*
* Licence summary :
* You can modify and redistribute the sources code and binaries.
* You can send me the bug-fix
*
* Term of the licence in in the file licence.txt.
*
*******************************************************************************
*/
#include <ewol/ewol.h>
#include <ewol/Debug.h>
#include <ewol/threadMsg.h>
void ewol::threadMsg::Init(ewol::threadMsg::threadMsg_ts& messageData)
{
// create interface mutex :
int ret = pthread_mutex_init(&messageData.mutex, NULL);
EWOL_ASSERT(ret == 0, "Error creating Mutex ...");
// create contition :
ret = pthread_cond_init(&messageData.condition, NULL);
EWOL_ASSERT(ret == 0, "Error creating Condition ...");
if (ret != 0) {
ret = pthread_mutex_destroy(&messageData.mutex);
EWOL_ASSERT(ret == 0, "Error destroying Mutex ...");
} else {
for (int32_t iii=0; MSG_PRIO_NUMBER>iii; iii++) {
messageData.nbMessages[iii] = 0;
for (int32_t jjj=0; NUMBER_OF_ELEMENT_IN_THE_FIFO>jjj; jjj++) {
messageData.listOfMessages[iii][jjj].isActive = false;
}
}
}
messageData.isInit = true;
}
void ewol::threadMsg::UnInit(ewol::threadMsg::threadMsg_ts& messageData)
{
if (true == messageData.isInit) {
// Remove Mutex
int ret = pthread_cond_destroy(&messageData.condition);
EWOL_ASSERT(ret == 0, "Error destroying Condition ...");
// Remove condition
ret = pthread_mutex_destroy(&messageData.mutex);
EWOL_ASSERT(ret == 0, "Error destroying Mutex ...");
// remove data ????
messageData.isInit = false;
}
}
bool ewol::threadMsg::WaitMessage(ewol::threadMsg::threadMsg_ts& messageData, ewol::threadMsg::threadMsgContent_ts &data)
{
if (false == messageData.isInit) {
return false;
}
pthread_mutex_lock(&messageData.mutex);
pthread_cond_wait(&messageData.condition, &messageData.mutex);
// find the message :
for (int32_t iii=0; MSG_PRIO_NUMBER>iii; iii++) {
if (0 < messageData.nbMessages[iii]) {
// find a message :
for (int32_t jjj=0; NUMBER_OF_ELEMENT_IN_THE_FIFO>jjj; jjj++) {
if (true == messageData.listOfMessages[iii][jjj].isActive) {
// copy the data :
data = messageData.listOfMessages[iii][jjj];
// disable the current message :
messageData.listOfMessages[iii][jjj].isActive = false;
}
}
// decrement the number of message :
messageData.nbMessages[iii]--;
if (0>messageData.nbMessages[iii]) {
messageData.nbMessages[iii] = 0;
}
// exit the waiting system ...
break;
}
}
pthread_mutex_unlock(&messageData.mutex);
return true;
}
bool ewol::threadMsg::SendMessage(ewol::threadMsg::threadMsg_ts& messageData, uint32_t type, char * data, uint32_t size, ewol::threadMsg::msgPriority_te prio)
{
if (false == messageData.isInit) {
return false;
}
if ( 0>prio || MSG_PRIO_NUMBER <= prio) {
EWOL_ERROR("Send message with an unknown priority ... " << prio);
return false;
}
if (size > MAX_MSG_DATA_SIZE) {
EWOL_ERROR("Send message with an biger size than predictible " << size << " > " << MAX_MSG_DATA_SIZE);
}
EWOL_DEBUG("Try to send Message");
int32_t lastNbMessage = messageData.nbMessages[prio];
for (int32_t jjj=0; NUMBER_OF_ELEMENT_IN_THE_FIFO>jjj; jjj++) {
if (messageData.listOfMessages[prio][jjj].isActive == false) {
// we find a slot ...
messageData.listOfMessages[prio][jjj].isActive = true;
messageData.listOfMessages[prio][jjj].type = type;
memset(messageData.listOfMessages[prio][jjj].data, 0, MAX_MSG_DATA_SIZE*sizeof(char) );
if (data!=NULL) {
memcpy(messageData.listOfMessages[prio][jjj].data, data, size);
}
messageData.nbMessages[prio]++;
}
}
pthread_cond_broadcast(&messageData.condition);
if (lastNbMessage != messageData.nbMessages[prio]) {
return true;
} else {
EWOL_ERROR("Send message Add error");
return false;
}
}
void ewol::threadMsg::tmppp5656(const char * plop)
{
EWOL_CRITICAL(plop);
}

View File

@ -0,0 +1,71 @@
/**
*******************************************************************************
* @file threadMsg.h
* @brief User abstraction for message to a specific thread (Header)
* @author Edouard DUPIN
* @date 26/01/2012
* @par Project
* ewol
*
* @par Copyright
* Copyright 2011 Edouard DUPIN, all right reserved
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY.
*
* Licence summary :
* You can modify and redistribute the sources code and binaries.
* You can send me the bug-fix
*
* Term of the licence in in the file licence.txt.
*
*******************************************************************************
*/
#ifndef __EWOL_TREAD_MSG_H__
#define __EWOL_TREAD_MSG_H__
#include <pthread.h>
namespace ewol {
namespace threadMsg {
typedef enum {
MSG_PRIO_REAL_TIME,
MSG_PRIO_HIGH,
MSG_PRIO_MEDIUM,
MSG_PRIO_LOW,
MSG_PRIO_NONE,
MSG_PRIO_NUMBER,
} msgPriority_te;
#define NUMBER_OF_ELEMENT_IN_THE_FIFO (1024)
#define MAX_MSG_DATA_SIZE (128)
typedef struct {
bool isActive;
uint32_t type;
char data[MAX_MSG_DATA_SIZE];
} threadMsgContent_ts;
typedef struct {
bool isInit;
pthread_mutex_t mutex;
pthread_cond_t condition;
threadMsgContent_ts listOfMessages[MSG_PRIO_NUMBER][NUMBER_OF_ELEMENT_IN_THE_FIFO];
int32_t nbMessages[MSG_PRIO_NUMBER];
} threadMsg_ts;
void Init(threadMsg_ts& messageData);
void UnInit(threadMsg_ts& messageData);
bool WaitMessage(threadMsg_ts& messageData, threadMsgContent_ts &data);
bool SendMessage(threadMsg_ts& messageData, uint32_t type, char * data = NULL, uint32_t size = 0, msgPriority_te prio = MSG_PRIO_NONE);
void tmppp5656(const char * plop);
};
};
#endif

View File

@ -2,6 +2,7 @@
FILE_LIST = ewol/ewol.cpp \
ewol/ewolInterne.cpp \
ewol/threadMsg.cpp \
ewol/Debug.cpp \
ewol/OObject.cpp \
ewol/OObject/2DText.cpp \