Skip to content

Commit 8660812

Browse files
committed
refactor: add lock
1 parent 5603b05 commit 8660812

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,25 @@ extern "C"
2424
/// key is jobject, value is pai which contain jclass and reference count
2525
static std::map<jobject, std::pair<jclass, int> > objectGlobalReference;
2626

27+
/// protect objectGlobalReference
28+
std::mutex globalReferenceMtx;
29+
2730
void _addGlobalObject(jobject globalObject, jclass globalClass)
2831
{
32+
globalReferenceMtx.lock();
33+
std::lock_guard<std::mutex> lk(globalReferenceMtx);
2934
std::pair<jclass, int> objPair = std::make_pair(globalClass, 0);
3035
objectGlobalReference[globalObject] = objPair;
36+
globalReferenceMtx.unlock();
3137
}
3238

3339
jclass _getGlobalClass(jobject globalObject)
3440
{
35-
if (objectGlobalReference.find(globalObject) != objectGlobalReference.end())
36-
{
37-
std::pair<jclass, int> objPair = objectGlobalReference[globalObject];
38-
return objPair.first;
39-
}
40-
return nullptr;
41+
globalReferenceMtx.lock();
42+
auto it = objectGlobalReference.find(globalObject);
43+
auto cls = it != objectGlobalReference.end() ? it->second.first : nullptr;
44+
globalReferenceMtx.unlock();
45+
return cls;
4146
}
4247

4348
void _detachThreadDestructor(void *arg)
@@ -195,7 +200,8 @@ extern "C"
195200
jclass cls = _getGlobalClass(object);
196201
if (cls == nullptr)
197202
{
198-
DNError("invokeNativeMethod not find class");
203+
/// maybe use cache pointer but jobject is release
204+
DNError("invokeNativeMethod not find class, check pointer and jobject lifecycle is same");
199205
return nullptr;
200206
}
201207

@@ -209,7 +215,7 @@ extern "C"
209215
}
210216

211217
char *methodSignature = generateSignature(dataTypes, argumentCount, returnType);
212-
DNDebug("call method %s %s", methodName, methodSignature);
218+
// DNDebug("call method %s %s", methodName, methodSignature);
213219
jmethodID method = env->GetMethodID(cls, methodName, methodSignature);
214220

215221
auto it = methodCallerMap.find(*returnType);
@@ -285,11 +291,13 @@ extern "C"
285291
/// reference counter
286292
void _updateObjectReference(jobject globalObject, bool isRetain)
287293
{
294+
globalReferenceMtx.lock();
288295
DNDebug("_updateObjectReference %s", isRetain ? "retain" : "release");
289296
auto it = objectGlobalReference.find(globalObject);
290297
if (it == objectGlobalReference.end())
291298
{
292299
DNError("_updateObjectReference %s error not contain this object!!!", isRetain ? "retain" : "release");
300+
globalReferenceMtx.unlock();
293301
return;
294302
}
295303

@@ -298,6 +306,7 @@ extern "C"
298306
/// dart object retain this dart object
299307
/// reference++
300308
it->second.second += 1;
309+
globalReferenceMtx.unlock();
301310
return;
302311
}
303312

@@ -311,8 +320,8 @@ extern "C"
311320
env->DeleteGlobalRef(it->second.first);
312321
objectGlobalReference.erase(it);
313322
env->DeleteGlobalRef(globalObject);
314-
return;
315323
}
324+
globalReferenceMtx.unlock();
316325
}
317326

318327
/// release native object from cache
@@ -387,7 +396,7 @@ extern "C"
387396
auto argTypeString = (jstring)env->GetObjectArrayElement(argumentTypes, i);
388397
auto argument = env->GetObjectArrayElement(argumentsArray, i);
389398
dataTypes[i] = (char *)env->GetStringUTFChars(argTypeString, 0);
390-
399+
DNError("not register this dart port! %s", dataTypes[i]);
391400
if (strcmp(dataTypes[i], "java.lang.String") == 0)
392401
{
393402
arguments[i] = (jstring)argument == nullptr ? reinterpret_cast<uint16_t *>((char *)"")
@@ -401,6 +410,7 @@ extern "C"
401410
arguments[i] = gObj;
402411

403412
env->DeleteLocalRef(objCls);
413+
DNError("over %s", dataTypes[i]);
404414
}
405415

406416
env->DeleteLocalRef(argTypeString);

0 commit comments

Comments
 (0)