#include <jni.h>
#include <string>
#include <atomic>
#include <android/log.h>
#define _LOG_TO_CONSOLE_(x) ((void)__android_log_print(ANDROID_LOG_INFO, "JNI", x))
typedef void* VPtr;
typedef __int64_t TInt64;
#define SYNC_EXCL_MASK ((TInt64) (0x8000000000000001))
VPtr
SetV (volatile VPtr * pDest, VPtr pValToSet)
{
__android_log_print(ANDROID_LOG_INFO,"JNI","Vptr Set");
return (VPtr) __sync_lock_test_and_set ((volatile TInt64 *) pDest, (TInt64) pValToSet);
}
TInt64
SetI (volatile TInt64 * pDest, TInt64 pValToSet)
{
__android_log_print(ANDROID_LOG_INFO,"JNI","Int64 set = %u", pValToSet);
return __sync_lock_test_and_set(pDest, pValToSet);
}
typedef int (*FuncPtr) ();
int testFunct()
{
return 0;
}
void TestAtomics()
{
TInt64 *dest;
__android_log_print(ANDROID_LOG_INFO,"JNI","Testing Set with int = %d", sizeof(dest));
SetI(dest, SYNC_EXCL_MASK);
__android_log_print(ANDROID_LOG_INFO,"JNI","Testing lock text and set - value set = %u ", *dest);
FuncPtr function;
__android_log_print(ANDROID_LOG_INFO,"JNI","Testing Set with function pointer");
SetV((volatile VPtr *)&function, (VPtr) testFunct);
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_tally_atomictest_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
TestAtomics();
return env->NewStringUTF(hello.c_str());
}
=======================
Note:
If I change :
typedef __int64_t TInt64;to:typedef __int32_t TInt64;It works fine! So definitely the issue with H/W support of atomic on 64 bit values ....Please help, how to make it 64 bit atomic set for ARM devices ....
TInt64 *dest;
__android_log_print(ANDROID_LOG_INFO,"JNI","Testing Set with int = %d", sizeof(dest));
SetI(dest, SYNC_EXCL_MASK);
TInt64 dest;
SetI(&dest, SYNC_EXCL_MASK);
--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/d1e8aac4-4336-4570-956a-217317106cf4%40googlegroups.com.
VPtr
SetV (volatile VPtr * pDest, VPtr pValToSet)
{
__android_log_print(ANDROID_LOG_INFO,"JNI","Vptr Set");
//return (VPtr) __sync_lock_test_and_set ((volatile TInt64 *) pDest, (TInt64) pValToSet);
return (VPtr)__atomic_exchange_n(pDest, pValToSet, __ATOMIC_SEQ_CST);
}
TInt64
SetI (volatile TInt64 * pDest, TInt64 pValToSet)
{
__android_log_print(ANDROID_LOG_INFO,"JNI","Int64 set = %u", pValToSet);
//return __sync_lock_test_and_set(pDest, pValToSet);
return __atomic_exchange_n(pDest, pValToSet, __ATOMIC_SEQ_CST);
}
This code worked perfectly on x86_64. Anyway, I changed the pointer to stack variable.void TestAtomics()
{
TInt64 dest;
__android_log_print(ANDROID_LOG_INFO,"JNI","Testing Set with int = %d", sizeof(dest));
SetI(&dest, SYNC_EXCL_MASK);
__android_log_print(ANDROID_LOG_INFO,"JNI","Testing lock text and set - value set = %u ", dest);
FuncPtr function;
__android_log_print(ANDROID_LOG_INFO,"JNI","Testing Set with function pointer");
SetV((volatile VPtr *)&function, (VPtr) testFunct);
}
Worked perfectly again on x86_64.Crashed on ARM architecture:2020-03-31 15:49:11.096 5762-5762/com.tally.atomictest I/JNI: Testing Set with int = 8
2020-03-31 15:49:11.096 5762-5762/com.tally.atomictest I/JNI: Int64 set = 2519096968
2020-03-31 15:49:11.096 5762-5762/com.tally.atomictest I/JNI: Testing lock text and set - value set = 4
2020-03-31 15:49:11.096 5762-5762/com.tally.atomictest I/JNI: Testing Set with function pointer
2020-03-31 15:49:11.096 5762-5762/com.tally.atomictest I/JNI: Vptr Set
2020-03-31 15:49:11.096 5762-5762/com.tally.atomictest A/libc: Fatal signal 7 (SIGBUS), code 1 (BUS_ADRALN), fault addr 0xbefa2cc4 in tid 5762 (ally.atomictest), pid 5762 (ally.atomictest)Let me try the std::atomic -> I am using C++17
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CALgsJz%3Do7dyaYqgXXURdMUTbnJ7RGp%2B5AXFBVmWzWcT4Fj8PKA%40mail.gmail.com.
void TestAtomics()
{
TInt64 dest;
__android_log_print(ANDROID_LOG_INFO,"JNI","Testing Set with int = %d", sizeof(dest));
SetI(&dest, SYNC_EXCL_MASK);
__android_log_print(ANDROID_LOG_INFO,"JNI","Testing lock text and set - value set = %u ", dest);
FuncPtr function;
__android_log_print(ANDROID_LOG_INFO,"JNI","Testing Set with function pointer");
SetV((volatile VPtr *)&function, (VPtr) testFunct);
}
Worked perfectly again on x86_64.
Crashed on ARM architecture:
2020-03-31 15:49:11.096 5762-5762/com.tally.atomictest I/JNI: Testing Set with int = 8
2020-03-31 15:49:11.096 5762-5762/com.tally.atomictest I/JNI: Int64 set = 2519096968
2020-03-31 15:49:11.096 5762-5762/com.tally.atomictest I/JNI: Testing lock text and set - value set = 4
2020-03-31 15:49:11.096 5762-5762/com.tally.atomictest I/JNI: Testing Set with function pointer
2020-03-31 15:49:11.096 5762-5762/com.tally.atomictest I/JNI: Vptr Set
2020-03-31 15:49:11.096 5762-5762/com.tally.atomictest A/libc: Fatal signal 7 (SIGBUS), code 1 (BUS_ADRALN), fault addr 0xbefa2cc4 in tid 5762 (ally.atomictest), pid 5762 (ally.atomictest)
Let me try the std::atomic -> I am using C++17
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CALgsJz%3Do7dyaYqgXXURdMUTbnJ7RGp%2B5AXFBVmWzWcT4Fj8PKA%40mail.gmail.com.
typedef void* VPtr;typedef __int64_t TInt64;
...
VPtrSetV (volatile VPtr * pDest, VPtr pValToSet){__android_log_print(ANDROID_LOG_INFO,"JNI","Vptr Set");return (VPtr) __sync_lock_test_and_set ((volatile TInt64 *) pDest, (TInt64) pValToSet);}
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAKS5LG4FZDLNcomJ1CPM7ch6cZT4dH2-SbCFpd-4ooDDCVW_HQ%40mail.gmail.com.