Merge pull request #544 from localvar/fix-jni-init-race

fix race in jni initialization
This commit is contained in:
haojun Liao 2019-10-09 18:06:51 +08:00 committed by GitHub
commit 8bf1054de0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 2 deletions

12
src/client/src/TSDBJNIConnector.c Normal file → Executable file
View File

@ -62,8 +62,15 @@ jmethodID g_rowdataSetByteArrayFp;
void jniGetGlobalMethod(JNIEnv *env) { void jniGetGlobalMethod(JNIEnv *env) {
// make sure init function executed once // make sure init function executed once
if (__sync_val_compare_and_swap_32(&__init, 0, 1) == 1) { switch (__sync_val_compare_and_swap_32(&__init, 0, 1)) {
return; case 0:
break;
case 1:
do {
taosMsleep(0);
} while (__atomic_load_n(&__init, __ATOMIC_ACQUIRE) == 1);
case 2:
return;
} }
if (g_vm == NULL) { if (g_vm == NULL) {
@ -101,6 +108,7 @@ void jniGetGlobalMethod(JNIEnv *env) {
g_rowdataSetByteArrayFp = (*env)->GetMethodID(env, g_rowdataClass, "setByteArray", "(I[B)V"); g_rowdataSetByteArrayFp = (*env)->GetMethodID(env, g_rowdataClass, "setByteArray", "(I[B)V");
(*env)->DeleteLocalRef(env, rowdataClass); (*env)->DeleteLocalRef(env, rowdataClass);
__atomic_store_n(&__init, 2, __ATOMIC_RELEASE);
jniTrace("native method register finished"); jniTrace("native method register finished");
} }