Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
MultiThread Init sequence (#2241)
Browse files Browse the repository at this point in the history
  • Loading branch information
Darin726 authored and YorkShen committed Mar 25, 2019
1 parent 720f73a commit 36dce32
Show file tree
Hide file tree
Showing 20 changed files with 206 additions and 55 deletions.
Binary file modified android/sdk/libs/armeabi-v7a/libweexcore.so
Binary file not shown.
Binary file modified android/sdk/libs/armeabi-v7a/libweexjss.so
Binary file not shown.
Binary file modified android/sdk/libs/armeabi/libweexcore.so
Binary file not shown.
Binary file modified android/sdk/libs/armeabi/libweexjss.so
Binary file not shown.
Binary file modified android/sdk/libs/x86/libweexcore.so
Binary file not shown.
Binary file modified android/sdk/libs/x86/libweexjss.so
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.taobao.weex.adapter;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

public interface IWXJscProcessManager {
boolean enableBackupThread();
boolean enableBackUpThreadCache();
boolean shouldReboot();
long rebootTimeout();
boolean withException(WXSDKInstance instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,25 @@ private void stop() {
private Message obtainAsyncMessage(int what) {
Message msg = Message.obtain();
msg.what = what;
if (mMessageMethodSetAsynchronous != null) {
// If invocation fails, assume this is indicative of future
// failures, and avoid log spam by nulling the reflected method.
try {
mMessageMethodSetAsynchronous.invoke(msg, true);
} catch (IllegalAccessException e) {
Log.e(TAG, "Illegal access to asynchronous message creation, disabling.");
mMessageMethodSetAsynchronous = null;
} catch (IllegalArgumentException e) {
Log.e(TAG, "Illegal argument for asynchronous message creation, disabling.");
mMessageMethodSetAsynchronous = null;
} catch (InvocationTargetException e) {
Log.e(TAG, "Invocation exception during asynchronous message creation, disabling.");
mMessageMethodSetAsynchronous = null;
} catch (RuntimeException e) {
Log.e(TAG, "Runtime exception during asynchronous message creation, disabling.");
mMessageMethodSetAsynchronous = null;
}
}
// if (mMessageMethodSetAsynchronous != null) {
// // If invocation fails, assume this is indicative of future
// // failures, and avoid log spam by nulling the reflected method.
// try {
// mMessageMethodSetAsynchronous.invoke(msg, true);
// } catch (IllegalAccessException e) {
// Log.e(TAG, "Illegal access to asynchronous message creation, disabling.");
// mMessageMethodSetAsynchronous = null;
// } catch (IllegalArgumentException e) {
// Log.e(TAG, "Illegal argument for asynchronous message creation, disabling.");
// mMessageMethodSetAsynchronous = null;
// } catch (InvocationTargetException e) {
// Log.e(TAG, "Invocation exception during asynchronous message creation, disabling.");
// mMessageMethodSetAsynchronous = null;
// } catch (RuntimeException e) {
// Log.e(TAG, "Runtime exception during asynchronous message creation, disabling.");
// mMessageMethodSetAsynchronous = null;
// }
// }
return msg;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2054,6 +2054,11 @@ private WXParams assembleDefaultOptions() {
wxParams.setDeviceHeight(TextUtils.isEmpty(config.get("deviceHeight")) ? String.valueOf(WXViewUtils.getScreenHeight(WXEnvironment.sApplication)) : config.get("deviceHeight"));
Map<String, String> customOptions = WXEnvironment.getCustomOptions();
customOptions.put("enableBackupThread", String.valueOf(jsEngineMultiThreadEnable()));
IWXJscProcessManager wxJscProcessManager = WXSDKManager.getInstance().getWXJscProcessManager();
if(wxJscProcessManager != null) {
customOptions.put("enableBackupThreadCache", String.valueOf(wxJscProcessManager.enableBackUpThreadCache()));
}

wxParams.setOptions(customOptions);
wxParams.setNeedInitV8(WXSDKManager.getInstance().needInitV8());
mInitParams = wxParams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ namespace weex {
auto enable = value == "true";
LOGE("enable backupThread %d",enable);
WeexEnv::getEnv()->setEnableBackupThread(enable);
} else if(type == "enableBackupThreadCache") {
auto enable = value == "true";
LOGE("enable backupThreadCache %d",enable);
WeexEnv::getEnv()->set_m_cache_task_(enable);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,21 @@ int ScriptSideInQueue::InitFramework(
weexTaskQueue_->init();

if (WeexEnv::getEnv()->enableBackupThread()) {
weexTaskQueue_bk_ = new WeexTaskQueue(weexTaskQueue_->isMultiProgress);
weexTaskQueue_bk_->addTask(new InitFrameworkTask(String::fromUTF8(script), params));
weexTaskQueue_bk_->init();
WeexEnv::getEnv()->locker()->lock();
while (!WeexEnv::getEnv()->is_jsc_init_finished()) {
WeexEnv::getEnv()->locker()->wait();
}
WeexEnv::getEnv()->locker()->unlock();

if(WeexEnv::getEnv()->can_m_cache_task_()) {
WeexEnv::getEnv()->m_task_cache_.push_back(new InitFrameworkTask(String::fromUTF8(script), params));
LOGE("cache initFramework %d", WeexEnv::getEnv()->m_task_cache_.size());
} else {
weexTaskQueue_bk_ = new WeexTaskQueue(weexTaskQueue_->isMultiProgress);
weexTaskQueue_bk_->addTask(new InitFrameworkTask(String::fromUTF8(script), params));
weexTaskQueue_bk_->init();
}

}

return 1;
Expand Down Expand Up @@ -119,8 +131,15 @@ int ScriptSideInQueue::ExecJsService(const char *source) {
LOGD("ScriptSideInQueue::ExecJsService");

weexTaskQueue_->addTask(new ExeJsServicesTask(String::fromUTF8(source)));
if (WeexEnv::getEnv()->enableBackupThread() && weexTaskQueue_bk_ != nullptr) {
weexTaskQueue_bk_->addTask(new ExeJsServicesTask(String::fromUTF8(source)));
if (WeexEnv::getEnv()->enableBackupThread()) {
ExeJsServicesTask *task = new ExeJsServicesTask(String::fromUTF8(source));
if(WeexEnv::getEnv()->can_m_cache_task_() && weexTaskQueue_bk_ == nullptr){
WeexEnv::getEnv()->m_task_cache_.push_back(task);
LOGE("cache ExecJsService %d", WeexEnv::getEnv()->m_task_cache_.size());
} else {
weexTaskQueue_bk_->addTask(task);
}

}

return 1;
Expand Down Expand Up @@ -148,8 +167,13 @@ int ScriptSideInQueue::ExecJS(const char *instanceId, const char *nameSpace,
task->addExtraArg(String::fromUTF8(func));

if (instanceId == nullptr || strlen(instanceId) == 0) {
if (WeexEnv::getEnv()->enableBackupThread() && weexTaskQueue_bk_ != nullptr) {
weexTaskQueue_bk_->addTask(task->clone());
if (WeexEnv::getEnv()->enableBackupThread()) {
if(WeexEnv::getEnv()->can_m_cache_task_() && weexTaskQueue_bk_ == nullptr){
WeexEnv::getEnv()->m_task_cache_.push_back(task->clone());
LOGE("cache ExecJS %d", WeexEnv::getEnv()->m_task_cache_.size());
} else {
weexTaskQueue_bk_->addTask(task->clone());
}
}

weexTaskQueue_->addTask(task);
Expand Down Expand Up @@ -198,19 +222,25 @@ int ScriptSideInQueue::CreateInstance(const char *instanceId,
"CreateInstance id = %s, func = %s, script = %s, opts = %s, initData = "
"%s, extendsApi = %s",
instanceId, func, script, opts, initData, extendsApi);
bool backUpThread = false;
if (WeexEnv::getEnv()->enableBackupThread()) {
for (int i = 0; i < params.size(); ++i) {
auto param = params[i];
auto type = String::fromUTF8(param->type->content);
auto value = String::fromUTF8(param->value->content);
if (type == "use_back_thread") {
if (value == "true") {
useBackUpWeexRuntime(instanceId);
backUpThread = true;
}
break;
}
}
}

if(backUpThread) {
useBackUpWeexRuntime(instanceId);
}

auto string = String::fromUTF8(script);
if (string.isEmpty()) {
return 0;
Expand Down Expand Up @@ -289,6 +319,12 @@ WeexTaskQueue *ScriptSideInQueue::taskQueue(const char *id, bool log) {
if (id != nullptr && shouldUseBackUpWeexRuntime(id)) {
if (weexTaskQueue_bk_ == nullptr) {
weexTaskQueue_bk_ = new WeexTaskQueue(weexTaskQueue_->isMultiProgress);
WeexEnv::getEnv()->set_m_cache_task_(false);
for (std::deque<WeexTask *>::iterator it = WeexEnv::getEnv()->m_task_cache_.begin(); it < WeexEnv::getEnv()->m_task_cache_.end(); ++it) {
auto reference = *it;
weexTaskQueue_bk_->addTask(std::move(reference));
}
WeexEnv::getEnv()->m_task_cache_.clear();
}
if (log) {
LOGE("dyyLog instance %s use back up thread time is %lld", id, microTime());
Expand Down
42 changes: 42 additions & 0 deletions weex_core/Source/android/jsengine/object/weex_env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,51 @@ void WeexEnv::setScriptBridge(WeexCore::ScriptBridge *scriptBridge) { scriptBrid

void WeexEnv::initIPC() {
// init IpcClient in io Thread
isMultiProcess = true;
m_ipc_client_.reset(new WeexIPCClient(ipcClientFd_));
}
WeexEnv::WeexEnv() {
this->enableBackupThread__ = false;
this->isUsingWson = true;
this->isJscInitOk_ = false;
this->m_cache_task_ = true;
}

void WeexEnv::initJSC(bool isMultiProgress) {
static std::once_flag initJSCFlag;
std::call_once(initJSCFlag, [isMultiProgress]{
if (!WEEXICU::initICUEnv(isMultiProgress)) {
LOGE("failed to init ICUEnv single process");
// return false;
}

Options::enableRestrictedOptions(true);
// Initialize JSC before getting VM.
WTF::initializeMainThread();
initHeapTimer();
JSC::initializeThreading();
#if ENABLE(WEBASSEMBLY)
JSC::Wasm::enableFastMemory();
#endif
});
}
void WeexEnv::init_crash_handler(std::string crashFileName) {
// initialize signal handler
isMultiProcess = true;
crashHandler.reset(new crash_handler::CrashHandlerInfo(crashFileName));
crashHandler->initializeCrashHandler();
}
bool WeexEnv::is_app_crashed() {
if(!isMultiProcess)
return false;
return crashHandler->is_crashed();
}
volatile bool WeexEnv::can_m_cache_task_() const {
return m_cache_task_;
}
void WeexEnv::set_m_cache_task_(volatile bool m_cache_task_) {
WeexEnv::m_cache_task_ = m_cache_task_;
}
WeexEnv::~WeexEnv() {
wson::destory();
}
38 changes: 33 additions & 5 deletions weex_core/Source/android/jsengine/object/weex_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define WEEXV8_WEEXENV_H

#include <mutex>
#include <task/weex_task.h>
#include "android/jsengine/task/back_to_weex_core_queue.h"

#include "android/jsengine/task/timer_queue.h"
Expand All @@ -41,6 +42,8 @@ class WeexEnv {

WeexEnv();

~WeexEnv();

bool useWson();

void setUseWson(bool useWson);
Expand Down Expand Up @@ -76,6 +79,8 @@ class WeexEnv {

void initIPC();

void initJSC(bool isMultiProgress);

void setEnableBackupThread(bool enable) {
this->enableBackupThread__ = enable;
}
Expand All @@ -84,16 +89,28 @@ class WeexEnv {
return enableBackupThread__;
}

public:
void jsc_init_finished() { isJscInitOk_ = true; };

bool is_jsc_init_finished() {return isJscInitOk_; }

ThreadLocker* locker() { return &thread_locker_; }

void init_crash_handler(std::string crashFileName);

bool is_app_crashed();
public:
std::unique_ptr<BackToWeexCoreQueue> m_back_to_weex_core_thread;
volatile bool isMultiProcess = true;
volatile bool isMultiProcess = false;
std::unique_ptr<WeexIPCClient> m_ipc_client_;
private:

std::deque<WeexTask *> m_task_cache_;

private:
static WeexEnv *env_;

volatile bool isUsingWson = true;
volatile bool isJscInitOk_ = false;

std::unique_ptr<TimerQueue> weexTimerQueue_;
volatile bool isUsingWson = true;

WeexCore::ScriptBridge *scriptBridge_;

Expand All @@ -102,6 +119,17 @@ class WeexEnv {
volatile int ipcClientFd_;
volatile int ipcServerFd_;
volatile bool enableTrace_;

volatile bool m_cache_task_;
public:
volatile bool can_m_cache_task_() const;
void set_m_cache_task_(volatile bool m_cache_task_);
private:

ThreadLocker thread_locker_;

std::unique_ptr<crash_handler::CrashHandlerInfo> crashHandler;

};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ void WeexObjectHolder::initFromParams(std::vector<INIT_FRAMEWORK_PARAMS *> &para
globalObject->timeQueue = this->timeQueue;
m_globalObject.set(vm, globalObject);
vm.heap.setGarbageCollectionTimerEnabled(true);
wson::init(&vm);
}

WeexObjectHolder::WeexObjectHolder(VM* vm, TimerQueue* timeQueue, bool isMultiProgress) {
Expand Down Expand Up @@ -98,7 +97,7 @@ WeexGlobalObject *WeexObjectHolder::cloneWeexObject(std::string page_id, bool in
}

WeexObjectHolder::~WeexObjectHolder() {
wson::destory();
// wson::destory();
JSLockHolder locker(this->m_globalVM);
this->m_globalVM->heap.collectAllGarbage();
}
Expand Down
8 changes: 8 additions & 0 deletions weex_core/Source/android/jsengine/task/weex_task_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#include "android/jsengine/object/weex_env.h"

void WeexTaskQueue::run(WeexTask *task) {
if(task == nullptr || WeexEnv::getEnv()->is_app_crashed()) {
return;
}
task->timeCalculator->set_task_name(task->taskName());
task->timeCalculator->taskStart();
task->run(weexRuntime);
Expand Down Expand Up @@ -60,6 +63,11 @@ WeexTask *WeexTaskQueue::getTask() {
continue;
}

if(WeexEnv::getEnv()->is_app_crashed()) {
threadLocker.unlock();
return nullptr;
}

assert(!taskQueue_.empty());
task = taskQueue_.front();
taskQueue_.pop_front();
Expand Down
6 changes: 1 addition & 5 deletions weex_core/Source/android/jsengine/weex_ipc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
using namespace JSC;
using namespace WTF;
using namespace WEEXICU;
using namespace crash_handler;


struct WeexJSServer::WeexJSServerImpl {
Expand All @@ -38,7 +37,6 @@ struct WeexJSServer::WeexJSServerImpl {
std::unique_ptr<IPCHandler> handler;
std::unique_ptr<IPCListener> listener;
std::unique_ptr<IPCSerializer> serializer;
std::unique_ptr<CrashHandlerInfo> crashHandler;
};

WeexJSServer::WeexJSServerImpl::WeexJSServerImpl(int serverFd, int clientFd, bool enableTrace, std::string crashFileName) {
Expand All @@ -60,9 +58,7 @@ WeexJSServer::WeexJSServerImpl::WeexJSServerImpl(int serverFd, int clientFd, boo
listener = std::move(createIPCListener(futexPageQueue.get(), handler.get()));
serializer = std::move(createIPCSerializer());

// initialize signal handler
crashHandler.reset(new CrashHandlerInfo(crashFileName));
crashHandler->initializeCrashHandler();
WeexEnv::getEnv()->init_crash_handler(crashFileName);

WeexEnv::getEnv()->m_back_to_weex_core_thread.reset(new BackToWeexCoreQueue());
WeexEnv::getEnv()->m_back_to_weex_core_thread.get()->init();
Expand Down
Loading

0 comments on commit 36dce32

Please sign in to comment.