js回调使用主线程
This commit is contained in:
parent
3b9a43d622
commit
36f030fba4
@ -26,6 +26,7 @@ NS_CC_BEGIN
|
|||||||
|
|
||||||
cocos2d::Application *g_app = nullptr;
|
cocos2d::Application *g_app = nullptr;
|
||||||
JcWallet *JcWallet::_instance = nullptr;
|
JcWallet *JcWallet::_instance = nullptr;
|
||||||
|
uv_loop_t *loop = nullptr;
|
||||||
bool _isStarted = false;
|
bool _isStarted = false;
|
||||||
uv_async_t gasync = {0};
|
uv_async_t gasync = {0};
|
||||||
|
|
||||||
@ -50,7 +51,7 @@ NS_CC_BEGIN
|
|||||||
void init_libuv_async_handle(uv_async_t *async) {
|
void init_libuv_async_handle(uv_async_t *async) {
|
||||||
memset(async, 0, sizeof(uv_async_t));
|
memset(async, 0, sizeof(uv_async_t));
|
||||||
// uv_loop_t *loop = uv_default_loop();
|
// uv_loop_t *loop = uv_default_loop();
|
||||||
uv_loop_t* loop = (uv_loop_t*)malloc(sizeof(uv_loop_t));
|
loop = (uv_loop_t*)malloc(sizeof(uv_loop_t));
|
||||||
uv_loop_init(loop);
|
uv_loop_init(loop);
|
||||||
uv_async_init(loop, async, cocos2d::flush_tasks_in_server_loop_cb);
|
uv_async_init(loop, async, cocos2d::flush_tasks_in_server_loop_cb);
|
||||||
async->data = new AsyncTaskData();
|
async->data = new AsyncTaskData();
|
||||||
@ -68,7 +69,12 @@ NS_CC_BEGIN
|
|||||||
//notify server thread to invoke `flush_tasks_in_server_loop_cb()`
|
//notify server thread to invoke `flush_tasks_in_server_loop_cb()`
|
||||||
return uv_async_send(asyn);
|
return uv_async_send(asyn);
|
||||||
}
|
}
|
||||||
|
#define RUN_IN_GAMETHREAD(task) \
|
||||||
|
do { \
|
||||||
|
JcWallet::getInstance()->performFunctionInCocosThread([=]() { \
|
||||||
|
task; \
|
||||||
|
});\
|
||||||
|
} while(0)
|
||||||
#define RUN_IN_SERVERTHREAD(task) do { \
|
#define RUN_IN_SERVERTHREAD(task) do { \
|
||||||
schedule_task_into_server_thread_task_queue(&gasync, [=](){ \
|
schedule_task_into_server_thread_task_queue(&gasync, [=](){ \
|
||||||
task; \
|
task; \
|
||||||
@ -97,6 +103,8 @@ NS_CC_BEGIN
|
|||||||
|
|
||||||
JcWallet::JcWallet() {
|
JcWallet::JcWallet() {
|
||||||
JcWallet::_instance = this;
|
JcWallet::_instance = this;
|
||||||
|
_functionsToPerform.reserve(30);
|
||||||
|
std::unique_lock<std::mutex> lock(_performMutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void JcWallet::initEnv() {
|
void JcWallet::initEnv() {
|
||||||
@ -110,6 +118,7 @@ NS_CC_BEGIN
|
|||||||
g_app->start();
|
g_app->start();
|
||||||
_isStarted = true;
|
_isStarted = true;
|
||||||
WalletEvent::Emit("wallet_init_event", "{}");
|
WalletEvent::Emit("wallet_init_event", "{}");
|
||||||
|
// RUN_IN_GAMETHREAD(JcWallet::getInstance()->jsToUnity("wallet_init_event", "{}"));
|
||||||
cocos2d::init_libuv_async_handle(&gasync);
|
cocos2d::init_libuv_async_handle(&gasync);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -126,7 +135,7 @@ NS_CC_BEGIN
|
|||||||
JcWallet::~JcWallet() {
|
JcWallet::~JcWallet() {
|
||||||
EventDispatcher::destroy();
|
EventDispatcher::destroy();
|
||||||
se::ScriptEngine::destroyInstance();
|
se::ScriptEngine::destroyInstance();
|
||||||
uv_loop_close(uv_default_loop());
|
uv_loop_close(loop);
|
||||||
JcWallet::_instance = nullptr;
|
JcWallet::_instance = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,14 +151,40 @@ NS_CC_BEGIN
|
|||||||
result = "";
|
result = "";
|
||||||
}
|
}
|
||||||
cocos2d::log("method: %s ::result: %s", data->methodName.c_str(), result.c_str());
|
cocos2d::log("method: %s ::result: %s", data->methodName.c_str(), result.c_str());
|
||||||
WalletEvent::Emit(data->funId.c_str(), result.c_str());
|
RUN_IN_GAMETHREAD(JcWallet::getInstance()->jsToUnity(data->funId, result));
|
||||||
|
// WalletEvent::Emit(data->funId.c_str(), result.c_str());
|
||||||
}
|
}
|
||||||
return const_cast<char *>(result.c_str());
|
return const_cast<char *>(result.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void JcWallet::performFunctionInCocosThread( const std::function<void()> &function) {
|
||||||
|
_performMutex.lock();
|
||||||
|
_functionsToPerform.push_back(function);
|
||||||
|
_performMutex.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
void JcWallet::tickRun() {
|
||||||
|
if( !_functionsToPerform.empty() ) {
|
||||||
|
_performMutex.lock();
|
||||||
|
// fixed #4123: Save the callback functions, they must be invoked after '_performMutex.unlock()', otherwise if new functions are added in callback, it will cause thread deadlock.
|
||||||
|
auto temp = _functionsToPerform;
|
||||||
|
_functionsToPerform.clear();
|
||||||
|
_performMutex.unlock();
|
||||||
|
for( const auto &function : temp ) {
|
||||||
|
function();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void JcWallet::jsToUnity(std::string funId, std::string msg) {
|
||||||
|
WalletEvent::Emit(funId.c_str(), msg.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
void initEnv() {
|
void initEnv() {
|
||||||
if (!_isStarted) {
|
if (!_isStarted) {
|
||||||
|
new JcWallet();
|
||||||
std::shared_ptr<JcWallet> wallet(JcWallet::getInstance());
|
std::shared_ptr<JcWallet> wallet(JcWallet::getInstance());
|
||||||
std::thread([=]() {
|
std::thread([=]() {
|
||||||
JcWallet::initJSThread(wallet);
|
JcWallet::initJSThread(wallet);
|
||||||
@ -161,6 +196,7 @@ NS_CC_BEGIN
|
|||||||
schedule_task_into_server_thread_task_queue(&gasync, [=](){
|
schedule_task_into_server_thread_task_queue(&gasync, [=](){
|
||||||
JcWallet::getInstance()->tick(dt);
|
JcWallet::getInstance()->tick(dt);
|
||||||
});
|
});
|
||||||
|
JcWallet::getInstance()->tickRun();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,7 +215,6 @@ NS_CC_BEGIN
|
|||||||
int result = schedule_task_into_server_thread_task_queue(&gasync, [=](){
|
int result = schedule_task_into_server_thread_task_queue(&gasync, [=](){
|
||||||
JcWallet::getInstance()->runJsMethod(params);
|
JcWallet::getInstance()->runJsMethod(params);
|
||||||
});
|
});
|
||||||
// RUN_IN_SERVERTHREAD(JcWallet::getInstance()->runJsMethod(params));
|
|
||||||
return result == 0 ? 1 : 0;
|
return result == 0 ? 1 : 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -215,7 +250,8 @@ bool jsb_wallet_callback(se::State& s) {
|
|||||||
std::string msg;
|
std::string msg;
|
||||||
ok = seval_to_std_string(args[1], &msg);
|
ok = seval_to_std_string(args[1], &msg);
|
||||||
SE_PRECONDITION2(ok, false, "Error processing arguments");
|
SE_PRECONDITION2(ok, false, "Error processing arguments");
|
||||||
WalletEvent::Emit(funId.c_str(), msg.c_str());
|
// WalletEvent::Emit(funId.c_str(), msg.c_str());
|
||||||
|
RUN_IN_GAMETHREAD(JcWallet::getInstance()->jsToUnity(funId, msg));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -36,6 +36,10 @@ NS_CC_BEGIN
|
|||||||
|
|
||||||
void performFunctionInCocosThread( const std::function<void()> &function);
|
void performFunctionInCocosThread( const std::function<void()> &function);
|
||||||
|
|
||||||
|
void tickRun();
|
||||||
|
|
||||||
|
void jsToUnity(std::string funId, std::string msg);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static JcWallet *_instance;
|
static JcWallet *_instance;
|
||||||
std::vector<std::function<void()>> _functionsToPerform;
|
std::vector<std::function<void()>> _functionsToPerform;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user