atomic can be suitble for more platform.

This commit is contained in:
shuxin   zheng 2020-06-28 15:52:24 +08:00
parent bc8c05bd11
commit 6676fa22d1
3 changed files with 31 additions and 60 deletions

View File

@ -28,8 +28,9 @@ android {
externalNativeBuild {
cmake {
//arguments "-DANDROID_STL=stl_shared"
//arguments "-DANDROID_STL=gnustl_shared" // for ndk16b
arguments "-DANDROID_STL=c++_shared" // for ndk20 or above
//arguments "-DANDROID_STL=gnustl_shared" // for ndk16b
arguments "-DANDROID_STL=c++_shared" // for ndk20 or above
arguments "-DHAS_ATOMIC=YES" // for ndk20 or above
arguments "-DCMAKE_BUILD_TYPE=RELEASE"
arguments "-DCMAKE_CXX_FLAGS_RELEASE=-O3"

View File

@ -15,6 +15,9 @@ if (CMAKE_SYSTEM_NAME MATCHES "Android")
add_definitions("-Oz -g -DHAVE_NO_ATEXIT")
else()
add_definitions("-O3 -flto")
if (HAS_ATOMIC MATCHES "YES")
add_definitions("-DHAS_ATOMIC")
endif()
endif()
add_definitions("-DANDROID")
add_definitions("-DACL_CLIENT_ONLY")

View File

@ -9,12 +9,26 @@
#endif
#if defined(ACL_ANDROID)
# undef HAS_ATOMIC
#elif defined(ACL_WINDOWS) || defined(ACL_LINUX)
/* In lower NDK, atomic not support for lower Android, see:
* https://android.googlesource.com/toolchain/gcc/+/refs/heads/master/gcc-4.9/libgcc/config/arm/linux-atomic-64bit.c#53
* So, we must exclude atomic for lower Android version.
*/
#if !defined(ACL_ANDROID) && defined(ACL_LINUX)
# if defined(__GNUC__) && (__GNUC__ >= 4)
# define HAS_ATOMIC
# endif
#endif
#if defined(ACL_MACOSX) || defined(ACL_FREEBSD)
# define HAS_ATOMIC
#else
# undef HAS_ATOMIC
#endif
#if defined(ACL_WINDOWS)
# define HAS_ATOMIC
#endif
#if !defined(HAS_ATOMIC)
# pragma message "Atomic not support, using thread mutex instead!"
#endif
struct ACL_ATOMIC {
@ -52,15 +66,8 @@ void acl_atomic_set(ACL_ATOMIC *self, void *value)
acl_pthread_mutex_unlock(&self->lock);
#elif defined(ACL_WINDOWS)
InterlockedExchangePointer((volatile PVOID*) &self->value, value);
#elif defined(ACL_LINUX)
# if defined(__GNUC__) && (__GNUC__ >= 4)
#else
(void) __sync_lock_test_and_set(&self->value, value);
# else
(void) self;
(void) value;
acl_msg_error("%s(%d), %s: not support!",
__FILE__, __LINE__, __FUNCTION__);
# endif
#endif
}
@ -79,17 +86,8 @@ void *acl_atomic_cas(ACL_ATOMIC *self, void *cmp, void *value)
#elif defined(ACL_WINDOWS)
return InterlockedCompareExchangePointer(
(volatile PVOID*)&self->value, value, cmp);
#elif defined(ACL_LINUX)
# if defined(__GNUC__) && (__GNUC__ >= 4)
#else
return __sync_val_compare_and_swap(&self->value, cmp, value);
# else
(void) self;
(void) cmp;
(void) value;
acl_msg_error("%s(%d), %s: not support!",
__FILE__, __LINE__, __FUNCTION__);
return NULL;
# endif
#endif
}
@ -106,16 +104,8 @@ void *acl_atomic_xchg(ACL_ATOMIC *self, void *value)
return old;
#elif defined(ACL_WINDOWS)
return InterlockedExchangePointer((volatile PVOID*)&self->value, value);
#elif defined(ACL_LINUX)
# if defined(__GNUC__) && (__GNUC__ >= 4)
#else
return __sync_lock_test_and_set(&self->value, value);
# else
(void) self;
(void) value;
acl_msg_error("%s(%d), %s: not support!",
__FILE__, __LINE__, __FUNCTION__);
return NULL;
# endif
#endif
}
@ -127,15 +117,8 @@ void acl_atomic_int64_set(ACL_ATOMIC *self, long long n)
acl_pthread_mutex_unlock(&self->lock);
#elif defined(ACL_WINDOWS)
InterlockedExchangePointer((volatile PVOID*) self->value, (PVOID) n);
#elif defined(ACL_LINUX)
# if defined(__GNUC__) && (__GNUC__ >= 4)
#else
(void) __sync_lock_test_and_set((long long *) self->value, n);
# else
(void) self;
(void) value;
acl_msg_error("%s(%d), %s: not support!",
__FILE__, __LINE__, __FUNCTION__);
# endif
#endif
}
@ -149,16 +132,8 @@ long long acl_atomic_int64_fetch_add(ACL_ATOMIC *self, long long n)
return v;
#elif defined(ACL_WINDOWS)
return InterlockedExchangeAdd64((volatile LONGLONG*) self->value, n);
#elif defined(ACL_LINUX)
# if defined(__GNUC__) && (__GNUC__ >= 4)
#else
return (long long) __sync_fetch_and_add((long long *) self->value, n);
# else
(void) self;
(void) n;
acl_msg_error("%s(%d), %s: not support!",
__FILE__, __LINE__, __FUNCTION__);
return -1;
# endif
#endif
}
@ -172,16 +147,8 @@ long long acl_atomic_int64_add_fetch(ACL_ATOMIC *self, long long n)
return v;
#elif defined(ACL_WINDOWS)
return n + InterlockedExchangeAdd64((volatile LONGLONG*) self->value, n);
#elif defined(ACL_LINUX)
# if defined(__GNUC__) && (__GNUC__ >= 4)
#else
return (long long) __sync_add_and_fetch((long long *) self->value, n);
# else
(void) self;
(void) n;
acl_msg_error("%s(%d), %s: not support!",
__FILE__, __LINE__, __FUNCTION__);
return -1;
# endif
#endif
}