Skip to content

Commit 7f5fbbc

Browse files
committed
refactor: use lock guard
1 parent bc27c28 commit 7f5fbbc

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

dart_native/android/src/main/jni/dart_native.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,16 @@ extern "C"
2929

3030
void _addGlobalObject(jobject globalObject, jclass globalClass)
3131
{
32-
globalReferenceMtx.lock();
32+
std::lock_guard<std::mutex> lockGuard(globalReferenceMtx);
3333
std::pair<jclass, int> objPair = std::make_pair(globalClass, 0);
3434
objectGlobalReference[globalObject] = objPair;
35-
globalReferenceMtx.unlock();
3635
}
3736

3837
jclass _getGlobalClass(jobject globalObject)
3938
{
40-
globalReferenceMtx.lock();
39+
std::lock_guard<std::mutex> lockGuard(globalReferenceMtx);
4140
auto it = objectGlobalReference.find(globalObject);
4241
auto cls = it != objectGlobalReference.end() ? it->second.first : nullptr;
43-
globalReferenceMtx.unlock();
4442
return cls;
4543
}
4644

@@ -290,13 +288,12 @@ extern "C"
290288
/// reference counter
291289
void _updateObjectReference(jobject globalObject, bool isRetain)
292290
{
293-
globalReferenceMtx.lock();
291+
std::lock_guard<std::mutex> lockGuard(globalReferenceMtx);
294292
DNDebug("_updateObjectReference %s", isRetain ? "retain" : "release");
295293
auto it = objectGlobalReference.find(globalObject);
296294
if (it == objectGlobalReference.end())
297295
{
298296
DNError("_updateObjectReference %s error not contain this object!!!", isRetain ? "retain" : "release");
299-
globalReferenceMtx.unlock();
300297
return;
301298
}
302299

@@ -305,7 +302,6 @@ extern "C"
305302
/// dart object retain this dart object
306303
/// reference++
307304
it->second.second += 1;
308-
globalReferenceMtx.unlock();
309305
return;
310306
}
311307

@@ -320,7 +316,6 @@ extern "C"
320316
objectGlobalReference.erase(it);
321317
env->DeleteGlobalRef(globalObject);
322318
}
323-
globalReferenceMtx.unlock();
324319
}
325320

326321
/// release native object from cache

0 commit comments

Comments
 (0)