Skip to content

Commit 7feebdb

Browse files
committed
Improve locking to be used on all ReadText files
1 parent f1b4847 commit 7feebdb

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ Local<Object> ModuleInternal::LoadData(Isolate* isolate, const string& path) {
399399
tns::instrumentation::Frame frame(frameName.c_str());
400400
Local<Object> json;
401401

402-
auto jsonData = File::ReadText(path);
402+
auto jsonData = Runtime::GetRuntime(m_isolate)->ReadFileText(path);
403403

404404
TryCatch tc(isolate);
405405

@@ -431,10 +431,7 @@ Local<Object> ModuleInternal::LoadData(Isolate* isolate, const string& path) {
431431
Local<String> ModuleInternal::WrapModuleContent(const string& path) {
432432
TNSPERF();
433433

434-
#ifdef APPLICATION_IN_DEBUG
435-
std::lock_guard<std::mutex> lock(Runtime::GetRuntime(m_isolate)->GetFileWriteMutex());
436-
#endif
437-
string content = File::ReadText(path);
434+
string content = Runtime::GetRuntime(m_isolate)->ReadFileText(path);
438435

439436
// TODO: Use statically allocated buffer for better performance
440437
string result(MODULE_PROLOGUE);

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,13 @@ void Runtime::Init(jstring filesPath, jstring nativeLibDir, bool verboseLoggingE
189189
s_isolate2RuntimesCache.insert(make_pair(m_isolate, this));
190190
}
191191

192+
std::string Runtime::ReadFileText(const std::string& filePath) {
192193
#ifdef APPLICATION_IN_DEBUG
193-
std::mutex& Runtime::GetFileWriteMutex() {
194-
return m_fileWriteMutex;
195-
}
194+
std::lock_guard<std::mutex> lock(m_fileWriteMutex);
196195
#endif
196+
return File::ReadText(filePath);
197+
}
198+
197199

198200
void Runtime::Lock() {
199201
#ifdef APPLICATION_IN_DEBUG
@@ -228,7 +230,8 @@ jobject Runtime::RunScript(JNIEnv* _env, jobject obj, jstring scriptFile) {
228230
auto context = isolate->GetCurrentContext();
229231

230232
auto filename = ArgConverter::jstringToString(scriptFile);
231-
auto src = File::ReadText(filename);
233+
auto src = ReadFileText(filename);
234+
232235
auto source = ArgConverter::ConvertToV8String(isolate, src);
233236

234237
TryCatch tc(isolate);
@@ -495,7 +498,7 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, const string& native
495498

496499
// check for custom script to include in the snapshot
497500
if (Constants::V8_HEAP_SNAPSHOT_SCRIPT.size() > 0 && File::Exists(Constants::V8_HEAP_SNAPSHOT_SCRIPT)) {
498-
customScript = File::ReadText(Constants::V8_HEAP_SNAPSHOT_SCRIPT);
501+
customScript = ReadFileText(Constants::V8_HEAP_SNAPSHOT_SCRIPT);
499502
}
500503

501504
DEBUG_WRITE_FORCE("Creating heap snapshot");

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ class Runtime {
6262

6363
static v8::Platform* platform;
6464

65-
#ifdef APPLICATION_IN_DEBUG
66-
std::mutex& GetFileWriteMutex();
67-
#endif
65+
std::string ReadFileText(const std::string& filePath);
6866

6967
private:
7068
Runtime(JNIEnv* env, jobject runtime, int id);

0 commit comments

Comments
 (0)