Skip to content

Commit ec36e5a

Browse files
committed
Added v8 locking
1 parent 0a36d41 commit ec36e5a

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

test-app/app/src/debug/java/com/tns/NativeScriptSyncServiceSocketImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,14 @@ private void createOrOverrideFile(String fileName, byte[] content) throws IOExce
356356

357357
fileToCreate.getParentFile().mkdirs();
358358
FileOutputStream fos = new FileOutputStream(fileToCreate.getCanonicalPath());
359+
runtime.lock();
359360
fos.write(content);
360361
fos.close();
361362

362363
} catch (Exception e) {
363364
throw new IOException(String.format("\nLiveSync: failed to write file: %s\nOriginal Exception: %s", fileName, e.toString()));
365+
} finally {
366+
runtime.unlock();
364367
}
365368
}
366369

test-app/runtime/src/main/cpp/Runtime.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ void Runtime::Init(jstring filesPath, jstring nativeLibDir, bool verboseLoggingE
188188
s_isolate2RuntimesCache.insert(make_pair(m_isolate, this));
189189
}
190190

191+
void Runtime::Lock() {
192+
m_locker.reset(new v8::Locker(m_isolate));
193+
}
194+
195+
void Runtime::Unlock() {
196+
m_locker.reset(nullptr);
197+
}
198+
191199
void Runtime::RunModule(JNIEnv* _env, jobject obj, jstring scriptFile) {
192200
JEnv env(_env);
193201

test-app/runtime/src/main/cpp/Runtime.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ class Runtime {
5656
void ClearStartupData(JNIEnv* env, jobject obj);
5757
void DestroyRuntime();
5858

59+
void Lock();
60+
void Unlock();
61+
5962
static v8::Platform* platform;
63+
std::auto_ptr<v8::Locker> m_locker;
6064

6165
private:
6266
Runtime(JNIEnv* env, jobject runtime, int id);

test-app/runtime/src/main/cpp/com_tns_Runtime.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,20 @@ extern "C" JNIEXPORT jboolean Java_com_tns_Runtime_notifyGc(JNIEnv* env, jobject
231231
return success;
232232
}
233233

234+
extern "C" JNIEXPORT void Java_com_tns_Runtime_lock(JNIEnv* env, jobject obj, jint runtimeId) {
235+
auto runtime = TryGetRuntime(runtimeId);
236+
if (runtime != nullptr) {
237+
runtime->Lock();
238+
}
239+
}
240+
241+
extern "C" JNIEXPORT void Java_com_tns_Runtime_unlock(JNIEnv* env, jobject obj, jint runtimeId) {
242+
auto runtime = TryGetRuntime(runtimeId);
243+
if (runtime != nullptr) {
244+
runtime->Unlock();
245+
}
246+
}
247+
234248
extern "C" JNIEXPORT void Java_com_tns_Runtime_passUncaughtExceptionToJsNative(JNIEnv* env, jobject obj, jint runtimeId, jthrowable exception, jstring stackTrace) {
235249
auto runtime = TryGetRuntime(runtimeId);
236250
if (runtime == nullptr) {

test-app/runtime/src/main/java/com/tns/Runtime.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ private native void initNativeScript(int runtimeId, String filesPath, String nat
4848
private native int generateNewObjectId(int runtimeId);
4949

5050
private native boolean notifyGc(int runtimeId);
51+
private native void lock(int runtimeId);
52+
private native void unlock(int runtimeId);
5153

5254
private native void passUncaughtExceptionToJsNative(int runtimeId, Throwable ex, String stackTrace);
5355

@@ -602,9 +604,9 @@ private long getUsedMemory() {
602604
return usedMemory;
603605
}
604606

605-
public void notifyGc() {
606-
notifyGc(runtimeId);
607-
}
607+
public void notifyGc() { notifyGc(runtimeId); }
608+
public void lock() { lock(runtimeId); }
609+
public void unlock() { unlock(runtimeId); }
608610

609611
public static void initInstance(Object instance) {
610612
ManualInstrumentation.Frame frame = ManualInstrumentation.start("Runtime.initInstance");

0 commit comments

Comments
 (0)