Skip to content

Commit f1b4847

Browse files
committed
Improve locking
1 parent 5270bb9 commit f1b4847

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "ManualInstrumentation.h"
2020
#include "Runtime.h"
2121
#include <sstream>
22+
#include <mutex>
2223
#include <libgen.h>
2324
#include <dlfcn.h>
2425

@@ -429,11 +430,11 @@ Local<Object> ModuleInternal::LoadData(Isolate* isolate, const string& path) {
429430

430431
Local<String> ModuleInternal::WrapModuleContent(const string& path) {
431432
TNSPERF();
432-
Runtime::GetRuntime(m_isolate)->Lock();
433-
string content = File::ReadText(path);
434-
Runtime::GetRuntime(m_isolate)->Unlock();
435433

436-
auto separatorIndex = path.find_last_of("/");
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);
437438

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

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

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

192+
#ifdef APPLICATION_IN_DEBUG
193+
std::mutex& Runtime::GetFileWriteMutex() {
194+
return m_fileWriteMutex;
195+
}
196+
#endif
197+
192198
void Runtime::Lock() {
193199
#ifdef APPLICATION_IN_DEBUG
194-
m_fileWriteLock.reset(new std::lock_guard<std::mutex>(m_fileWriteMutex));
200+
m_fileWriteMutex.lock();
195201
#endif
196202
}
197203

198204
void Runtime::Unlock() {
199205
#ifdef APPLICATION_IN_DEBUG
200-
m_fileWriteLock.reset(nullptr);
206+
m_fileWriteMutex.unlock();
201207
#endif
202208
}
203209

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ class Runtime {
6161
void Unlock();
6262

6363
static v8::Platform* platform;
64-
std::auto_ptr<std::lock_guard<std::mutex>> m_fileWriteLock;
65-
std::mutex m_fileWriteMutex;
64+
65+
#ifdef APPLICATION_IN_DEBUG
66+
std::mutex& GetFileWriteMutex();
67+
#endif
6668

6769
private:
6870
Runtime(JNIEnv* env, jobject runtime, int id);
@@ -104,6 +106,10 @@ class Runtime {
104106
static jmethodID GET_USED_MEMORY_METHOD_ID;
105107

106108
static bool s_mainThreadInitialized;
109+
110+
#ifdef APPLICATION_IN_DEBUG
111+
std::mutex m_fileWriteMutex;
112+
#endif
107113
};
108114
}
109115

0 commit comments

Comments
 (0)