mirror of
https://github.com/open5gs/open5gs.git
synced 2025-11-23 16:01:21 +00:00
atomic is added
This commit is contained in:
@@ -25,6 +25,17 @@
|
||||
* And please, make an effort to stub core.hw and core.hnw in the process.
|
||||
*/
|
||||
|
||||
#if !defined(__GNUC__) || __GNUC__ < 2 || \
|
||||
(__GNUC__ == 2 && __GNUC_MINOR__ < 7) ||\
|
||||
defined(NEXT)
|
||||
#ifndef __attribute__
|
||||
#define __attribute__(__x)
|
||||
#endif
|
||||
#define CORE_INLINE
|
||||
#else
|
||||
#define CORE_INLINE __inline__
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
115
lib/core/include/core_atomic.h
Normal file
115
lib/core/include/core_atomic.h
Normal file
@@ -0,0 +1,115 @@
|
||||
#ifndef __CORE_ATOMIC_H__
|
||||
#define __CORE_ATOMIC_H__
|
||||
|
||||
#include "core.h"
|
||||
#include "core_errno.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
/**
|
||||
* this function is required on some platforms to initialize the
|
||||
* atomic operation's internal structures
|
||||
* @param p pool
|
||||
* @return APR_SUCCESS on successful completion
|
||||
* @remark Programs do NOT need to call this directly. APR will call this
|
||||
* automatically from apr_initialize.
|
||||
* @internal
|
||||
*/
|
||||
CORE_DECLARE(status_t) atomic_init();
|
||||
|
||||
CORE_DECLARE(status_t) atomic_final();
|
||||
|
||||
/*
|
||||
* Atomic operations on 32-bit values
|
||||
* Note: Each of these functions internally implements a memory barrier
|
||||
* on platforms that require it
|
||||
*/
|
||||
|
||||
/**
|
||||
* atomically read an c_uint32_t from memory
|
||||
* @param mem the pointer
|
||||
*/
|
||||
CORE_DECLARE(c_uint32_t) atomic_read32(volatile c_uint32_t *mem);
|
||||
|
||||
/**
|
||||
* atomically set an c_uint32_t in memory
|
||||
* @param mem pointer to the object
|
||||
* @param val value that the object will assume
|
||||
*/
|
||||
CORE_DECLARE(void) atomic_set32(volatile c_uint32_t *mem, c_uint32_t val);
|
||||
|
||||
/**
|
||||
* atomically add 'val' to an c_uint32_t
|
||||
* @param mem pointer to the object
|
||||
* @param val amount to add
|
||||
* @return old value pointed to by mem
|
||||
*/
|
||||
CORE_DECLARE(c_uint32_t) atomic_add32(volatile c_uint32_t *mem, c_uint32_t val);
|
||||
|
||||
/**
|
||||
* atomically subtract 'val' from an c_uint32_t
|
||||
* @param mem pointer to the object
|
||||
* @param val amount to subtract
|
||||
*/
|
||||
CORE_DECLARE(void) atomic_sub32(volatile c_uint32_t *mem, c_uint32_t val);
|
||||
|
||||
/**
|
||||
* atomically increment an c_uint32_t by 1
|
||||
* @param mem pointer to the object
|
||||
* @return old value pointed to by mem
|
||||
*/
|
||||
CORE_DECLARE(c_uint32_t) atomic_inc32(volatile c_uint32_t *mem);
|
||||
|
||||
/**
|
||||
* atomically decrement an c_uint32_t by 1
|
||||
* @param mem pointer to the atomic value
|
||||
* @return zero if the value becomes zero on decrement, otherwise non-zero
|
||||
*/
|
||||
CORE_DECLARE(int) atomic_dec32(volatile c_uint32_t *mem);
|
||||
|
||||
/**
|
||||
* compare an c_uint32_t's value with 'cmp'.
|
||||
* If they are the same swap the value with 'with'
|
||||
* @param mem pointer to the value
|
||||
* @param with what to swap it with
|
||||
* @param cmp the value to compare it to
|
||||
* @return the old value of *mem
|
||||
*/
|
||||
CORE_DECLARE(c_uint32_t) atomic_cas32(volatile c_uint32_t *mem, c_uint32_t with,
|
||||
c_uint32_t cmp);
|
||||
|
||||
/**
|
||||
* exchange an c_uint32_t's value with 'val'.
|
||||
* @param mem pointer to the value
|
||||
* @param val what to swap it with
|
||||
* @return the old value of *mem
|
||||
*/
|
||||
CORE_DECLARE(c_uint32_t) atomic_xchg32(volatile c_uint32_t *mem,
|
||||
c_uint32_t val);
|
||||
|
||||
/**
|
||||
* compare the pointer's value with cmp.
|
||||
* If they are the same swap the value with 'with'
|
||||
* @param mem pointer to the pointer
|
||||
* @param with what to swap it with
|
||||
* @param cmp the value to compare it to
|
||||
* @return the old value of the pointer
|
||||
*/
|
||||
CORE_DECLARE(void*) atomic_casptr(void *volatile *mem, void *with,
|
||||
const void *cmp);
|
||||
|
||||
/**
|
||||
* exchange a pair of pointer values
|
||||
* @param mem pointer to the pointer
|
||||
* @param with what to swap it with
|
||||
* @return the old value of the pointer
|
||||
*/
|
||||
CORE_DECLARE(void*) atomic_xchgptr(void *volatile *mem, void *with);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /* !__CORE_ATOMIC_H__ */
|
||||
@@ -17,6 +17,7 @@ libcore_la_SOURCES = \
|
||||
../include/core_sha1_hmac.h ../include/core_sha2.h ../include/core_sha2_hmac.h \
|
||||
../include/core_signal.h ../include/core_thread.h ../include/core_time.h \
|
||||
../include/core_timer.h ../include/core_tlv.h ../include/core_tlv_msg.h \
|
||||
../include/core_atomic.h \
|
||||
../include/core_version.h
|
||||
|
||||
nodist_libcore_la_SOURCES = \
|
||||
|
||||
@@ -10,7 +10,7 @@ libcoreunix_la_SOURCES = \
|
||||
|
||||
nodist_libcoreunix_la_SOURCES = \
|
||||
cond.c file.c net_lib.c thread.c errorcodes.c mutex.c rwlock.c \
|
||||
start.c time.c semaphore.c signal.c pkbuf.c rand.c
|
||||
start.c time.c semaphore.c signal.c pkbuf.c rand.c atomic.c
|
||||
|
||||
AM_CPPFLAGS = \
|
||||
-I$(top_srcdir) \
|
||||
|
||||
168
lib/core/src/unix/atomic.c
Normal file
168
lib/core/src/unix/atomic.c
Normal file
@@ -0,0 +1,168 @@
|
||||
#define TRACE_MODULE _atomic
|
||||
|
||||
#include "core_atomic.h"
|
||||
#include "core_mutex.h"
|
||||
#include "core_debug.h"
|
||||
|
||||
#define DECLARE_MUTEX_LOCKED(name, mem) \
|
||||
mutex_id name = mutex_hash(mem)
|
||||
#define MUTEX_UNLOCK(name) \
|
||||
d_assert(mutex_unlock(name) == CORE_OK,,);
|
||||
|
||||
#define NUM_ATOMIC_HASH 7
|
||||
static mutex_id hash_mutex[NUM_ATOMIC_HASH];
|
||||
|
||||
/* shift by 2 to get rid of alignment issues */
|
||||
#define ATOMIC_HASH(x) (unsigned int)(((unsigned long)(x)>>2)%(unsigned int)NUM_ATOMIC_HASH)
|
||||
|
||||
status_t atomic_init()
|
||||
{
|
||||
int i;
|
||||
status_t rv;
|
||||
|
||||
for (i = 0; i < NUM_ATOMIC_HASH; i++)
|
||||
{
|
||||
rv = mutex_create(&hash_mutex[i], MUTEX_DEFAULT);
|
||||
if (rv != CORE_OK)
|
||||
{
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
return CORE_OK;
|
||||
}
|
||||
|
||||
status_t atomic_final()
|
||||
{
|
||||
status_t rv;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < NUM_ATOMIC_HASH; i++)
|
||||
{
|
||||
rv = mutex_delete(hash_mutex[i]);
|
||||
if (rv != CORE_OK)
|
||||
{
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
return CORE_OK;
|
||||
}
|
||||
|
||||
static CORE_INLINE mutex_id mutex_hash(volatile c_uint32_t *mem)
|
||||
{
|
||||
mutex_id id = hash_mutex[ATOMIC_HASH(mem)];
|
||||
|
||||
d_assert(mutex_lock(id) == CORE_OK, return (mutex_id)NULL,);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
c_uint32_t atomic_read32(volatile c_uint32_t *mem)
|
||||
{
|
||||
return *mem;
|
||||
}
|
||||
|
||||
void atomic_set32(volatile c_uint32_t *mem, c_uint32_t val)
|
||||
{
|
||||
DECLARE_MUTEX_LOCKED(mutex, mem);
|
||||
|
||||
*mem = val;
|
||||
|
||||
MUTEX_UNLOCK(mutex);
|
||||
}
|
||||
|
||||
c_uint32_t atomic_add32(volatile c_uint32_t *mem, c_uint32_t val)
|
||||
{
|
||||
c_uint32_t old_value;
|
||||
DECLARE_MUTEX_LOCKED(mutex, mem);
|
||||
|
||||
old_value = *mem;
|
||||
*mem += val;
|
||||
|
||||
MUTEX_UNLOCK(mutex);
|
||||
|
||||
return old_value;
|
||||
}
|
||||
|
||||
void atomic_sub32(volatile c_uint32_t *mem, c_uint32_t val)
|
||||
{
|
||||
DECLARE_MUTEX_LOCKED(mutex, mem);
|
||||
*mem -= val;
|
||||
MUTEX_UNLOCK(mutex);
|
||||
}
|
||||
|
||||
c_uint32_t atomic_inc32(volatile c_uint32_t *mem)
|
||||
{
|
||||
return atomic_add32(mem, 1);
|
||||
}
|
||||
|
||||
int atomic_dec32(volatile c_uint32_t *mem)
|
||||
{
|
||||
c_uint32_t new;
|
||||
DECLARE_MUTEX_LOCKED(mutex, mem);
|
||||
|
||||
(*mem)--;
|
||||
new = *mem;
|
||||
|
||||
MUTEX_UNLOCK(mutex);
|
||||
|
||||
return new;
|
||||
}
|
||||
|
||||
c_uint32_t atomic_cas32(volatile c_uint32_t *mem, c_uint32_t with,
|
||||
c_uint32_t cmp)
|
||||
{
|
||||
c_uint32_t prev;
|
||||
DECLARE_MUTEX_LOCKED(mutex, mem);
|
||||
|
||||
prev = *mem;
|
||||
if (prev == cmp) {
|
||||
*mem = with;
|
||||
}
|
||||
|
||||
MUTEX_UNLOCK(mutex);
|
||||
|
||||
return prev;
|
||||
}
|
||||
|
||||
c_uint32_t atomic_xchg32(volatile c_uint32_t *mem, c_uint32_t val)
|
||||
{
|
||||
c_uint32_t prev;
|
||||
DECLARE_MUTEX_LOCKED(mutex, mem);
|
||||
|
||||
prev = *mem;
|
||||
*mem = val;
|
||||
|
||||
MUTEX_UNLOCK(mutex);
|
||||
|
||||
return prev;
|
||||
}
|
||||
|
||||
void* atomic_casptr(void *volatile *mem, void *with, const void *cmp)
|
||||
{
|
||||
void *prev;
|
||||
DECLARE_MUTEX_LOCKED(mutex, *mem);
|
||||
|
||||
prev = *(void **)mem;
|
||||
if (prev == cmp) {
|
||||
*mem = with;
|
||||
}
|
||||
|
||||
MUTEX_UNLOCK(mutex);
|
||||
|
||||
return prev;
|
||||
}
|
||||
|
||||
void* atomic_xchgptr(void *volatile *mem, void *with)
|
||||
{
|
||||
void *prev;
|
||||
DECLARE_MUTEX_LOCKED(mutex, *mem);
|
||||
|
||||
prev = *(void **)mem;
|
||||
*mem = with;
|
||||
|
||||
MUTEX_UNLOCK(mutex);
|
||||
|
||||
return prev;
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "core_msgq.h"
|
||||
#include "core_tlv.h"
|
||||
#include "core_timer.h"
|
||||
#include "core_atomic.h"
|
||||
#include "core_mutex.h"
|
||||
#include "core_cond.h"
|
||||
#include "core_rwlock.h"
|
||||
@@ -42,6 +43,7 @@ status_t core_initialize(void)
|
||||
semaphore_init();
|
||||
cond_init();
|
||||
rwlock_init();
|
||||
atomic_init();
|
||||
thread_init();
|
||||
net_init();
|
||||
file_init();
|
||||
@@ -72,6 +74,7 @@ void core_terminate(void)
|
||||
file_final();
|
||||
net_final();
|
||||
thread_final();
|
||||
atomic_final();
|
||||
rwlock_final();
|
||||
cond_final();
|
||||
semaphore_final();
|
||||
|
||||
@@ -4,10 +4,10 @@ bin_PROGRAMS = testcore
|
||||
|
||||
testcore_SOURCES = \
|
||||
abts.c testds.c testfsm.c testnetlib.c testthread.c testtlv.c \
|
||||
testaes.c testfile.c testlock.c testsha.c testtime.c testutil.c \
|
||||
testaes.c testfile.c testlock.c testatomic.c testsha.c testtime.c \
|
||||
testdir.c testfilecopy.c testmsgq.c testsleep.c testtimer.c \
|
||||
testpkbuf.c testmisc.c \
|
||||
abts.h abts_tests.h testutil.h
|
||||
abts.h abts_tests.h testutil.c testutil.h
|
||||
|
||||
testcore_LDADD = \
|
||||
$(top_srcdir)/lib/core/src/libcore.la
|
||||
|
||||
@@ -33,6 +33,7 @@ const struct testlist {
|
||||
{testtimer},
|
||||
{testthread},
|
||||
{testlock},
|
||||
{testatomic},
|
||||
{testfile},
|
||||
{testfilecopy},
|
||||
{testdir},
|
||||
|
||||
491
lib/core/test/testatomic.c
Normal file
491
lib/core/test/testatomic.c
Normal file
@@ -0,0 +1,491 @@
|
||||
#include "core_atomic.h"
|
||||
#include "core_thread.h"
|
||||
#include "core_mutex.h"
|
||||
#include "testutil.h"
|
||||
|
||||
static void test_set32(abts_case *tc, void *data)
|
||||
{
|
||||
c_uint32_t y32;
|
||||
atomic_set32(&y32, 2);
|
||||
ABTS_INT_EQUAL(tc, 2, y32);
|
||||
}
|
||||
|
||||
static void test_read32(abts_case *tc, void *data)
|
||||
{
|
||||
c_uint32_t y32;
|
||||
atomic_set32(&y32, 2);
|
||||
ABTS_INT_EQUAL(tc, 2, atomic_read32(&y32));
|
||||
}
|
||||
|
||||
static void test_dec32(abts_case *tc, void *data)
|
||||
{
|
||||
c_uint32_t y32;
|
||||
int rv;
|
||||
|
||||
atomic_set32(&y32, 2);
|
||||
|
||||
rv = atomic_dec32(&y32);
|
||||
ABTS_INT_EQUAL(tc, 1, y32);
|
||||
ABTS_ASSERT(tc, "atomic_dec returned zero when it shouldn't", rv != 0);
|
||||
|
||||
rv = atomic_dec32(&y32);
|
||||
ABTS_INT_EQUAL(tc, 0, y32);
|
||||
ABTS_ASSERT(tc, "atomic_dec didn't returned zero when it should", rv == 0);
|
||||
}
|
||||
|
||||
static void test_xchg32(abts_case *tc, void *data)
|
||||
{
|
||||
c_uint32_t oldval;
|
||||
c_uint32_t y32;
|
||||
|
||||
atomic_set32(&y32, 100);
|
||||
oldval = atomic_xchg32(&y32, 50);
|
||||
|
||||
ABTS_INT_EQUAL(tc, 100, oldval);
|
||||
ABTS_INT_EQUAL(tc, 50, y32);
|
||||
}
|
||||
|
||||
static void test_xchgptr(abts_case *tc, void *data)
|
||||
{
|
||||
int a;
|
||||
void *ref = "little piggy";
|
||||
void *target_ptr = ref;
|
||||
void *old_ptr;
|
||||
|
||||
old_ptr = atomic_xchgptr(&target_ptr, &a);
|
||||
ABTS_PTR_EQUAL(tc, ref, old_ptr);
|
||||
ABTS_PTR_EQUAL(tc, &a, (void *) target_ptr);
|
||||
}
|
||||
|
||||
static void test_cas_equal(abts_case *tc, void *data)
|
||||
{
|
||||
c_uint32_t casval = 0;
|
||||
c_uint32_t oldval;
|
||||
|
||||
oldval = atomic_cas32(&casval, 12, 0);
|
||||
ABTS_INT_EQUAL(tc, 0, oldval);
|
||||
ABTS_INT_EQUAL(tc, 12, casval);
|
||||
}
|
||||
|
||||
static void test_cas_equal_nonnull(abts_case *tc, void *data)
|
||||
{
|
||||
c_uint32_t casval = 12;
|
||||
c_uint32_t oldval;
|
||||
|
||||
oldval = atomic_cas32(&casval, 23, 12);
|
||||
ABTS_INT_EQUAL(tc, 12, oldval);
|
||||
ABTS_INT_EQUAL(tc, 23, casval);
|
||||
}
|
||||
|
||||
static void test_cas_notequal(abts_case *tc, void *data)
|
||||
{
|
||||
c_uint32_t casval = 12;
|
||||
c_uint32_t oldval;
|
||||
|
||||
oldval = atomic_cas32(&casval, 23, 2);
|
||||
ABTS_INT_EQUAL(tc, 12, oldval);
|
||||
ABTS_INT_EQUAL(tc, 12, casval);
|
||||
}
|
||||
|
||||
static void test_casptr_equal(abts_case *tc, void *data)
|
||||
{
|
||||
int a;
|
||||
void *target_ptr = NULL;
|
||||
void *old_ptr;
|
||||
|
||||
old_ptr = atomic_casptr(&target_ptr, &a, NULL);
|
||||
ABTS_PTR_EQUAL(tc, NULL, old_ptr);
|
||||
ABTS_PTR_EQUAL(tc, &a, (void *) target_ptr);
|
||||
}
|
||||
|
||||
static void test_casptr_equal_nonnull(abts_case *tc, void *data)
|
||||
{
|
||||
int a, b;
|
||||
void *target_ptr = &a;
|
||||
void *old_ptr;
|
||||
|
||||
old_ptr = atomic_casptr(&target_ptr, &b, &a);
|
||||
ABTS_PTR_EQUAL(tc, &a, old_ptr);
|
||||
ABTS_PTR_EQUAL(tc, &b, (void *) target_ptr);
|
||||
}
|
||||
|
||||
static void test_casptr_notequal(abts_case *tc, void *data)
|
||||
{
|
||||
int a, b;
|
||||
void *target_ptr = &a;
|
||||
void *old_ptr;
|
||||
|
||||
old_ptr = atomic_casptr(&target_ptr, &a, &b);
|
||||
ABTS_PTR_EQUAL(tc, &a, old_ptr);
|
||||
ABTS_PTR_EQUAL(tc, &a, (void *) target_ptr);
|
||||
}
|
||||
|
||||
static void test_add32(abts_case *tc, void *data)
|
||||
{
|
||||
c_uint32_t oldval;
|
||||
c_uint32_t y32;
|
||||
|
||||
atomic_set32(&y32, 23);
|
||||
oldval = atomic_add32(&y32, 4);
|
||||
ABTS_INT_EQUAL(tc, 23, oldval);
|
||||
ABTS_INT_EQUAL(tc, 27, y32);
|
||||
}
|
||||
|
||||
static void test_add32_neg(abts_case *tc, void *data)
|
||||
{
|
||||
c_uint32_t oldval;
|
||||
c_uint32_t y32;
|
||||
|
||||
atomic_set32(&y32, 23);
|
||||
oldval = atomic_add32(&y32, -10);
|
||||
ABTS_INT_EQUAL(tc, 23, oldval);
|
||||
ABTS_INT_EQUAL(tc, 13, y32);
|
||||
}
|
||||
|
||||
static void test_inc32(abts_case *tc, void *data)
|
||||
{
|
||||
c_uint32_t oldval;
|
||||
c_uint32_t y32;
|
||||
|
||||
atomic_set32(&y32, 23);
|
||||
oldval = atomic_inc32(&y32);
|
||||
ABTS_INT_EQUAL(tc, 23, oldval);
|
||||
ABTS_INT_EQUAL(tc, 24, y32);
|
||||
}
|
||||
|
||||
static void test_set_add_inc_sub(abts_case *tc, void *data)
|
||||
{
|
||||
c_uint32_t y32;
|
||||
|
||||
atomic_set32(&y32, 0);
|
||||
atomic_add32(&y32, 20);
|
||||
atomic_inc32(&y32);
|
||||
atomic_sub32(&y32, 10);
|
||||
|
||||
ABTS_INT_EQUAL(tc, 11, y32);
|
||||
}
|
||||
|
||||
static void test_wrap_zero(abts_case *tc, void *data)
|
||||
{
|
||||
c_uint32_t y32;
|
||||
c_uint32_t rv;
|
||||
c_uint32_t minus1 = (c_uint32_t)-1;
|
||||
char str[256];;
|
||||
|
||||
atomic_set32(&y32, 0);
|
||||
rv = atomic_dec32(&y32);
|
||||
|
||||
ABTS_ASSERT(tc, "atomic_dec32 on zero returned zero.", rv != 0);
|
||||
sprintf(str, "zero wrap failed: 0 - 1 = %d", y32);
|
||||
ABTS_ASSERT(tc, str, y32 == minus1);
|
||||
}
|
||||
|
||||
static void test_inc_neg1(abts_case *tc, void *data)
|
||||
{
|
||||
c_uint32_t y32 = (c_uint32_t)-1;
|
||||
c_uint32_t minus1 = (c_uint32_t)-1;
|
||||
c_uint32_t rv;
|
||||
char str[256];
|
||||
|
||||
rv = atomic_inc32(&y32);
|
||||
|
||||
ABTS_ASSERT(tc, "atomic_inc32 didn't return the old value.", rv == minus1);
|
||||
sprintf(str, "zero wrap failed: -1 + 1 = %d", y32);
|
||||
ABTS_ASSERT(tc, str, y32 == 0);
|
||||
}
|
||||
|
||||
void *THREAD_FUNC thread_func_mutex(thread_id id, void *data);
|
||||
void *THREAD_FUNC thread_func_atomic(thread_id id, void *data);
|
||||
|
||||
mutex_id thread_lock;
|
||||
volatile c_uint32_t mutex_locks = 0;
|
||||
volatile c_uint32_t atomic_ops = 0;
|
||||
status_t exit_ret_val = 123; /* just some made up number to check on later */
|
||||
|
||||
#define NUM_THREADS 40
|
||||
#define NUM_ITERATIONS 20000
|
||||
|
||||
void *THREAD_FUNC thread_func_mutex(thread_id id, void *data)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < NUM_ITERATIONS; i++) {
|
||||
mutex_lock(thread_lock);
|
||||
mutex_locks++;
|
||||
mutex_unlock(thread_lock);
|
||||
}
|
||||
thread_exit(id, exit_ret_val);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *THREAD_FUNC thread_func_atomic(thread_id id, void *data)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < NUM_ITERATIONS ; i++) {
|
||||
atomic_inc32(&atomic_ops);
|
||||
atomic_add32(&atomic_ops, 2);
|
||||
atomic_dec32(&atomic_ops);
|
||||
atomic_dec32(&atomic_ops);
|
||||
}
|
||||
thread_exit(id, exit_ret_val);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void test_atomics_threaded(abts_case *tc, void *data)
|
||||
{
|
||||
thread_id t1[NUM_THREADS];
|
||||
thread_id t2[NUM_THREADS];
|
||||
status_t rv;
|
||||
int i;
|
||||
|
||||
#ifdef HAVE_PTHREAD_SETCONCURRENCY
|
||||
pthread_setconcurrency(8);
|
||||
#endif
|
||||
|
||||
rv = mutex_create(&thread_lock, MUTEX_DEFAULT);
|
||||
ABTS_ASSERT(tc, "Could not create lock", rv == CORE_OK) ;
|
||||
|
||||
for (i = 0; i < NUM_THREADS; i++) {
|
||||
status_t r1, r2;
|
||||
r1 = thread_create(&t1[i], NULL, thread_func_mutex, NULL);
|
||||
r2 = thread_create(&t2[i], NULL, thread_func_atomic, NULL);
|
||||
ABTS_ASSERT(tc, "Failed creating threads", !r1 && !r2);
|
||||
}
|
||||
|
||||
for (i = 0; i < NUM_THREADS; i++) {
|
||||
status_t s1, s2;
|
||||
thread_join(&s1, t1[i]);
|
||||
thread_join(&s2, t2[i]);
|
||||
|
||||
ABTS_ASSERT(tc, "Invalid return value from thread_join",
|
||||
s1 == exit_ret_val && s2 == exit_ret_val);
|
||||
}
|
||||
|
||||
ABTS_INT_EQUAL(tc, NUM_THREADS * NUM_ITERATIONS, mutex_locks);
|
||||
ABTS_INT_EQUAL(tc, NUM_THREADS * NUM_ITERATIONS,
|
||||
atomic_read32(&atomic_ops));
|
||||
|
||||
rv = mutex_delete(thread_lock);
|
||||
ABTS_ASSERT(tc, "Failed creating threads", rv == CORE_OK);
|
||||
}
|
||||
|
||||
#undef NUM_THREADS
|
||||
#define NUM_THREADS 7
|
||||
|
||||
typedef struct tbox_t tbox_t;
|
||||
|
||||
struct tbox_t {
|
||||
abts_case *tc;
|
||||
c_uint32_t *mem;
|
||||
c_uint32_t preval;
|
||||
c_uint32_t postval;
|
||||
c_uint32_t loop;
|
||||
void (*func)(tbox_t *box);
|
||||
};
|
||||
|
||||
static CORE_INLINE void busyloop_read32(tbox_t *tbox)
|
||||
{
|
||||
c_uint32_t val;
|
||||
|
||||
do {
|
||||
val = atomic_read32(tbox->mem);
|
||||
|
||||
if (val != tbox->preval)
|
||||
thread_yield();
|
||||
else
|
||||
break;
|
||||
} while (1);
|
||||
}
|
||||
|
||||
static void busyloop_set32(tbox_t *tbox)
|
||||
{
|
||||
do {
|
||||
busyloop_read32(tbox);
|
||||
atomic_set32(tbox->mem, tbox->postval);
|
||||
} while (--tbox->loop);
|
||||
}
|
||||
|
||||
static void busyloop_add32(tbox_t *tbox)
|
||||
{
|
||||
c_uint32_t val;
|
||||
|
||||
do {
|
||||
busyloop_read32(tbox);
|
||||
val = atomic_add32(tbox->mem, tbox->postval);
|
||||
mutex_lock(thread_lock);
|
||||
ABTS_INT_EQUAL(tbox->tc, val, tbox->preval);
|
||||
mutex_unlock(thread_lock);
|
||||
} while (--tbox->loop);
|
||||
}
|
||||
|
||||
static void busyloop_sub32(tbox_t *tbox)
|
||||
{
|
||||
do {
|
||||
busyloop_read32(tbox);
|
||||
atomic_sub32(tbox->mem, tbox->postval);
|
||||
} while (--tbox->loop);
|
||||
}
|
||||
|
||||
static void busyloop_inc32(tbox_t *tbox)
|
||||
{
|
||||
c_uint32_t val;
|
||||
|
||||
do {
|
||||
busyloop_read32(tbox);
|
||||
val = atomic_inc32(tbox->mem);
|
||||
mutex_lock(thread_lock);
|
||||
ABTS_INT_EQUAL(tbox->tc, val, tbox->preval);
|
||||
mutex_unlock(thread_lock);
|
||||
} while (--tbox->loop);
|
||||
}
|
||||
|
||||
static void busyloop_dec32(tbox_t *tbox)
|
||||
{
|
||||
c_uint32_t val;
|
||||
|
||||
do {
|
||||
busyloop_read32(tbox);
|
||||
val = atomic_dec32(tbox->mem);
|
||||
mutex_lock(thread_lock);
|
||||
ABTS_INT_NEQUAL(tbox->tc, 0, val);
|
||||
mutex_unlock(thread_lock);
|
||||
} while (--tbox->loop);
|
||||
}
|
||||
|
||||
static void busyloop_cas32(tbox_t *tbox)
|
||||
{
|
||||
c_uint32_t val;
|
||||
|
||||
do {
|
||||
do {
|
||||
val = atomic_cas32(tbox->mem, tbox->postval, tbox->preval);
|
||||
|
||||
if (val != tbox->preval)
|
||||
thread_yield();
|
||||
else
|
||||
break;
|
||||
} while (1);
|
||||
} while (--tbox->loop);
|
||||
}
|
||||
|
||||
static void busyloop_xchg32(tbox_t *tbox)
|
||||
{
|
||||
c_uint32_t val;
|
||||
|
||||
do {
|
||||
busyloop_read32(tbox);
|
||||
val = atomic_xchg32(tbox->mem, tbox->postval);
|
||||
mutex_lock(thread_lock);
|
||||
ABTS_INT_EQUAL(tbox->tc, val, tbox->preval);
|
||||
mutex_unlock(thread_lock);
|
||||
} while (--tbox->loop);
|
||||
}
|
||||
|
||||
static void *THREAD_FUNC thread_func_busyloop(thread_id id, void *data)
|
||||
{
|
||||
tbox_t *tbox = data;
|
||||
|
||||
tbox->func(tbox);
|
||||
|
||||
thread_exit(id, 0);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void test_atomics_busyloop_threaded(abts_case *tc, void *data)
|
||||
{
|
||||
unsigned int i;
|
||||
status_t rv;
|
||||
c_uint32_t count = 0;
|
||||
tbox_t tbox[NUM_THREADS];
|
||||
thread_id thread[NUM_THREADS];
|
||||
|
||||
rv = mutex_create(&thread_lock, MUTEX_DEFAULT);
|
||||
ABTS_ASSERT(tc, "Could not create lock", rv == CORE_OK);
|
||||
|
||||
/* get ready */
|
||||
for (i = 0; i < NUM_THREADS; i++) {
|
||||
tbox[i].tc = tc;
|
||||
tbox[i].mem = &count;
|
||||
tbox[i].loop = 50;
|
||||
}
|
||||
|
||||
tbox[0].preval = 98;
|
||||
tbox[0].postval = 3891;
|
||||
tbox[0].func = busyloop_add32;
|
||||
|
||||
tbox[1].preval = 3989;
|
||||
tbox[1].postval = 1010;
|
||||
tbox[1].func = busyloop_sub32;
|
||||
|
||||
tbox[2].preval = 2979;
|
||||
tbox[2].postval = 0; /* not used */
|
||||
tbox[2].func = busyloop_inc32;
|
||||
|
||||
tbox[3].preval = 2980;
|
||||
tbox[3].postval = 16384;
|
||||
tbox[3].func = busyloop_set32;
|
||||
|
||||
tbox[4].preval = 16384;
|
||||
tbox[4].postval = 0; /* not used */
|
||||
tbox[4].func = busyloop_dec32;
|
||||
|
||||
tbox[5].preval = 16383;
|
||||
tbox[5].postval = 1048576;
|
||||
tbox[5].func = busyloop_cas32;
|
||||
|
||||
tbox[6].preval = 1048576;
|
||||
tbox[6].postval = 98; /* goto tbox[0] */
|
||||
tbox[6].func = busyloop_xchg32;
|
||||
|
||||
/* get set */
|
||||
for (i = 0; i < NUM_THREADS; i++) {
|
||||
rv = thread_create(&thread[i], NULL, thread_func_busyloop,
|
||||
&tbox[i]);
|
||||
ABTS_ASSERT(tc, "Failed creating thread", rv == CORE_OK);
|
||||
}
|
||||
|
||||
/* go! */
|
||||
atomic_set32(tbox->mem, 98);
|
||||
|
||||
for (i = 0; i < NUM_THREADS; i++) {
|
||||
status_t retval;
|
||||
rv = thread_join(&retval, thread[i]);
|
||||
ABTS_ASSERT(tc, "Thread join failed", rv == CORE_OK);
|
||||
ABTS_ASSERT(tc, "Invalid return value from thread_join", retval == 0);
|
||||
}
|
||||
|
||||
ABTS_INT_EQUAL(tbox->tc, 98, count);
|
||||
|
||||
rv = mutex_delete(thread_lock);
|
||||
ABTS_ASSERT(tc, "Failed creating threads", rv == CORE_OK);
|
||||
}
|
||||
|
||||
abts_suite *testatomic(abts_suite *suite)
|
||||
{
|
||||
suite = ADD_SUITE(suite)
|
||||
|
||||
abts_run_test(suite, test_set32, NULL);
|
||||
abts_run_test(suite, test_read32, NULL);
|
||||
abts_run_test(suite, test_dec32, NULL);
|
||||
abts_run_test(suite, test_xchg32, NULL);
|
||||
abts_run_test(suite, test_xchgptr, NULL);
|
||||
abts_run_test(suite, test_cas_equal, NULL);
|
||||
abts_run_test(suite, test_cas_equal_nonnull, NULL);
|
||||
abts_run_test(suite, test_cas_notequal, NULL);
|
||||
abts_run_test(suite, test_casptr_equal, NULL);
|
||||
abts_run_test(suite, test_casptr_equal_nonnull, NULL);
|
||||
abts_run_test(suite, test_casptr_notequal, NULL);
|
||||
abts_run_test(suite, test_add32, NULL);
|
||||
abts_run_test(suite, test_add32_neg, NULL);
|
||||
abts_run_test(suite, test_inc32, NULL);
|
||||
abts_run_test(suite, test_set_add_inc_sub, NULL);
|
||||
abts_run_test(suite, test_wrap_zero, NULL);
|
||||
abts_run_test(suite, test_inc_neg1, NULL);
|
||||
|
||||
abts_run_test(suite, test_atomics_threaded, NULL);
|
||||
abts_run_test(suite, test_atomics_busyloop_threaded, NULL);
|
||||
|
||||
return suite;
|
||||
}
|
||||
@@ -67,6 +67,7 @@ abts_suite *testtime(abts_suite *suite);
|
||||
abts_suite *testtimer(abts_suite *suite);
|
||||
abts_suite *testthread(abts_suite *suite);
|
||||
abts_suite *testlock(abts_suite *suite);
|
||||
abts_suite *testatomic(abts_suite *suite);
|
||||
abts_suite *testfile(abts_suite *suite);
|
||||
abts_suite *testfilecopy(abts_suite *suite);
|
||||
abts_suite *testdir(abts_suite *suite);
|
||||
|
||||
Reference in New Issue
Block a user