Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mmkvWithID Lock Opt #1384

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 39 additions & 23 deletions Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKV.java
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,9 @@ public static MMKV mmkvWithID(String mmapID) throws RuntimeException {
if (rootDir == null) {
throw new IllegalStateException("You should Call MMKV.initialize() first.");
}

long handle = getMMKVWithID(mmapID, SINGLE_PROCESS_MODE, null, null, 0);
synchronized (mmapID.intern()) {
long handle = getMMKVWithID(mmapID, SINGLE_PROCESS_MODE, null, null, 0);
}
return checkProcessMode(handle, mmapID, SINGLE_PROCESS_MODE);
}

Expand All @@ -362,8 +363,9 @@ public static MMKV mmkvWithID(String mmapID, int mode) throws RuntimeException {
if (rootDir == null) {
throw new IllegalStateException("You should Call MMKV.initialize() first.");
}

long handle = getMMKVWithID(mmapID, mode, null, null, 0);
synchronized (mmapID.intern()) {
long handle = getMMKVWithID(mmapID, mode, null, null, 0);
}
return checkProcessMode(handle, mmapID, mode);
}

Expand All @@ -380,8 +382,9 @@ public static MMKV mmkvWithID(String mmapID, int mode, long expectedCapacity) th
if (rootDir == null) {
throw new IllegalStateException("You should Call MMKV.initialize() first.");
}

long handle = getMMKVWithID(mmapID, mode, null, null, expectedCapacity);
synchronized (mmapID.intern()) {
long handle = getMMKVWithID(mmapID, mode, null, null, expectedCapacity);
}
return checkProcessMode(handle, mmapID, mode);
}

Expand All @@ -398,8 +401,9 @@ public static MMKV mmkvWithID(String mmapID, int mode, @Nullable String cryptKey
if (rootDir == null) {
throw new IllegalStateException("You should Call MMKV.initialize() first.");
}

long handle = getMMKVWithID(mmapID, mode, cryptKey, null, 0);
synchronized (mmapID.intern()) {
long handle = getMMKVWithID(mmapID, mode, cryptKey, null, 0);
}
return checkProcessMode(handle, mmapID, mode);
}

Expand All @@ -415,8 +419,9 @@ public static MMKV mmkvWithID(String mmapID, String rootPath) throws RuntimeExce
if (rootDir == null) {
throw new IllegalStateException("You should Call MMKV.initialize() first.");
}

long handle = getMMKVWithID(mmapID, SINGLE_PROCESS_MODE, null, rootPath, 0);
synchronized (mmapID.intern()) {
long handle = getMMKVWithID(mmapID, SINGLE_PROCESS_MODE, null, rootPath, 0);
}
return checkProcessMode(handle, mmapID, SINGLE_PROCESS_MODE);
}

Expand All @@ -433,8 +438,9 @@ public static MMKV mmkvWithID(String mmapID, String rootPath, long expectedCapac
if (rootDir == null) {
throw new IllegalStateException("You should Call MMKV.initialize() first.");
}

long handle = getMMKVWithID(mmapID, SINGLE_PROCESS_MODE, null, rootPath, expectedCapacity);
synchronized (mmapID.intern()) {
long handle = getMMKVWithID(mmapID, SINGLE_PROCESS_MODE, null, rootPath, expectedCapacity);
}
return checkProcessMode(handle, mmapID, SINGLE_PROCESS_MODE);
}

Expand All @@ -454,8 +460,9 @@ public static MMKV mmkvWithID(String mmapID, int mode, @Nullable String cryptKey
if (rootDir == null) {
throw new IllegalStateException("You should Call MMKV.initialize() first.");
}

long handle = getMMKVWithID(mmapID, mode, cryptKey, rootPath, expectedCapacity);
synchronized (mmapID.intern()) {
long handle = getMMKVWithID(mmapID, mode, cryptKey, rootPath, expectedCapacity);
}
return checkProcessMode(handle, mmapID, mode);
}

Expand All @@ -474,8 +481,9 @@ public static MMKV mmkvWithID(String mmapID, int mode, @Nullable String cryptKey
if (rootDir == null) {
throw new IllegalStateException("You should Call MMKV.initialize() first.");
}

long handle = getMMKVWithID(mmapID, mode, cryptKey, rootPath, 0);
synchronized (mmapID.intern()) {
long handle = getMMKVWithID(mmapID, mode, cryptKey, rootPath, 0);
}
return checkProcessMode(handle, mmapID, mode);
}

Expand All @@ -496,7 +504,9 @@ public static MMKV backedUpMMKVWithID(String mmapID, int mode, @Nullable String
}

mode |= BACKUP_MODE;
long handle = getMMKVWithID(mmapID, mode, cryptKey, rootPath, 0);
synchronized (mmapID.intern()) {
long handle = getMMKVWithID(mmapID, mode, cryptKey, rootPath, 0);
}
return checkProcessMode(handle, mmapID, mode);
}

Expand Down Expand Up @@ -557,7 +567,9 @@ public static MMKV mmkvWithAshmemID(Context context, String mmapID, int size, in
simpleLog(MMKVLogLevel.LevelInfo, "getting mmkv in main process");

mode = mode | ASHMEM_MODE;
long handle = getMMKVWithIDAndSize(mmapID, size, mode, cryptKey);
synchronized (mmapID.intern()) {
long handle = getMMKVWithIDAndSize(mmapID, size, mode, cryptKey);
}
if (handle != 0) {
return new MMKV(handle);
}
Expand All @@ -574,8 +586,9 @@ public static MMKV defaultMMKV() throws RuntimeException {
if (rootDir == null) {
throw new IllegalStateException("You should Call MMKV.initialize() first.");
}

long handle = getDefaultMMKV(SINGLE_PROCESS_MODE, null);
synchronized {
long handle = getDefaultMMKV(SINGLE_PROCESS_MODE, null);
}
return checkProcessMode(handle, "DefaultMMKV", SINGLE_PROCESS_MODE);
}

Expand All @@ -591,8 +604,9 @@ public static MMKV defaultMMKV(int mode, @Nullable String cryptKey) throws Runti
if (rootDir == null) {
throw new IllegalStateException("You should Call MMKV.initialize() first.");
}

long handle = getDefaultMMKV(mode, cryptKey);
synchronized {
long handle = getDefaultMMKV(mode, cryptKey);
}
return checkProcessMode(handle, "DefaultMMKV", mode);
}

Expand Down Expand Up @@ -1483,7 +1497,9 @@ public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeL
@NonNull
@Contract("_, _, _, _ -> new")
public static MMKV mmkvWithAshmemFD(String mmapID, int fd, int metaFD, String cryptKey) throws RuntimeException {
long handle = getMMKVWithAshmemFD(mmapID, fd, metaFD, cryptKey);
synchronized (mmapID.intern()) {
long handle = getMMKVWithAshmemFD(mmapID, fd, metaFD, cryptKey);
}
if (handle == 0) {
throw new RuntimeException("Fail to create an ashmem MMKV instance [" + mmapID + "] in JNI");
}
Expand Down
21 changes: 19 additions & 2 deletions Core/MMKV_Android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,18 @@ MMKV *MMKV::mmkvWithID(const string &mmapID, int size, MMKVMode mode, string *cr
if (mmapID.empty()) {
return nullptr;
}
SCOPED_LOCK(g_instanceLock);
//SCOPED_LOCK(g_instanceLock);

g_instanceLock->lock();
auto mmapKey = mmapedKVKey(mmapID, rootPath);
auto itr = g_instanceDic->find(mmapKey);
if (itr != g_instanceDic->end()) {
MMKV *kv = itr->second;
g_instanceLock->unlock();
return kv;
}
g_instanceLock->unlock();

if (rootPath) {
if (!isFileExist(*rootPath)) {
if (!mkPath(*rootPath)) {
Expand All @@ -157,8 +161,13 @@ MMKV *MMKV::mmkvWithID(const string &mmapID, int size, MMKVMode mode, string *cr
}
MMKVInfo("prepare to load %s (id %s) from rootPath %zu", mmapID.c_str(), mmapKey.c_str(), rootPath->c_str());
}

auto kv = new MMKV(mmapID, size, mode, cryptKey, rootPath, expectedCapacity);

g_instanceLock->lock();
(*g_instanceDic)[mmapKey] = kv;
g_instanceLock->unlock();

return kv;
}

Expand All @@ -167,18 +176,26 @@ MMKV *MMKV::mmkvWithAshmemFD(const string &mmapID, int fd, int metaFD, string *c
if (fd < 0) {
return nullptr;
}
SCOPED_LOCK(g_instanceLock);
//SCOPED_LOCK(g_instanceLock);

g_instanceLock->lock();
auto itr = g_instanceDic->find(mmapID);
if (itr != g_instanceDic->end()) {
MMKV *kv = itr->second;
# ifndef MMKV_DISABLE_CRYPT
kv->checkReSetCryptKey(fd, metaFD, cryptKey);
# endif
g_instanceLock->unlock();
return kv;
}
g_instanceLock->unlock();

auto kv = new MMKV(mmapID, fd, metaFD, cryptKey);

g_instanceLock->lock();
(*g_instanceDic)[mmapID] = kv;
g_instanceLock->unlock();

return kv;
}

Expand Down