This commit is contained in:
Cloud Wu
2022-03-19 07:10:52 +08:00
parent eaba8f969c
commit d1ce950dc1
6 changed files with 37 additions and 21 deletions

View File

@@ -13,6 +13,8 @@
#define ATOM_LOAD(ptr) (*(ptr))
#define ATOM_STORE(ptr, v) (*(ptr) = v)
#define ATOM_CAS(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval)
#define ATOM_CAS_ULONG(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval)
#define ATOM_CAS_SIZET(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval)
#define ATOM_CAS_POINTER(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval)
#define ATOM_FINC(ptr) __sync_fetch_and_add(ptr, 1)
#define ATOM_FDEC(ptr) __sync_fetch_and_sub(ptr, 1)
@@ -31,8 +33,27 @@
#define ATOM_INIT(ref, v) atomic_init(ref, v)
#define ATOM_LOAD(ptr) atomic_load(ptr)
#define ATOM_STORE(ptr, v) atomic_store(ptr, v)
#define ATOM_CAS(ptr, oval, nval) atomic_compare_exchange_weak(ptr, &(oval), nval)
#define ATOM_CAS_POINTER(ptr, oval, nval) atomic_compare_exchange_weak(ptr, &(oval), nval)
static inline int
ATOM_CAS(atomic_int *ptr, int oval, int nval) {
return atomic_compare_exchange_weak(ptr, &(oval), nval);
}
static inline int
ATOM_CAS_SIZET(atomic_size_t *ptr, size_t oval, size_t nval) {
return atomic_compare_exchange_weak(ptr, &(oval), nval);
}
static inline int
ATOM_CAS_ULONG(atomic_ulong *ptr, unsigned long oval, unsigned long nval) {
return atomic_compare_exchange_weak(ptr, &(oval), nval);
}
static inline int
ATOM_CAS_POINTER(atomic_uintptr_t *ptr, uintptr_t oval, uintptr_t nval) {
return atomic_compare_exchange_weak(ptr, &(oval), nval);
}
#define ATOM_FINC(ptr) atomic_fetch_add(ptr, 1)
#define ATOM_FDEC(ptr) atomic_fetch_sub(ptr, 1)
#define ATOM_FADD(ptr,n) atomic_fetch_add(ptr, n)

View File

@@ -19,8 +19,8 @@ static ATOM_SIZET _used_memory = 0;
static ATOM_SIZET _memory_block = 0;
struct mem_data {
uint32_t handle;
ssize_t allocated;
ATOM_ULONG handle;
ATOM_SIZET allocated;
};
struct mem_cookie {
@@ -44,19 +44,19 @@ static struct mem_data mem_stats[SLOT_SIZE];
#define raw_realloc je_realloc
#define raw_free je_free
static ssize_t*
static ATOM_SIZET *
get_allocated_field(uint32_t handle) {
int h = (int)(handle & (SLOT_SIZE - 1));
struct mem_data *data = &mem_stats[h];
uint32_t old_handle = data->handle;
ssize_t old_alloc = data->allocated;
ssize_t old_alloc = (ssize_t)data->allocated;
if(old_handle == 0 || old_alloc <= 0) {
// data->allocated may less than zero, because it may not count at start.
if(!ATOM_CAS(&data->handle, old_handle, handle)) {
if(!ATOM_CAS_ULONG(&data->handle, old_handle, handle)) {
return 0;
}
if (old_alloc < 0) {
ATOM_CAS(&data->allocated, old_alloc, 0);
ATOM_CAS_SIZET(&data->allocated, (size_t)old_alloc, 0);
}
}
if(data->handle != handle) {
@@ -69,7 +69,7 @@ inline static void
update_xmalloc_stat_alloc(uint32_t handle, size_t __n) {
ATOM_FADD(&_used_memory, __n);
ATOM_FINC(&_memory_block);
ssize_t* allocated = get_allocated_field(handle);
ATOM_SIZET * allocated = get_allocated_field(handle);
if(allocated) {
ATOM_FADD(allocated, __n);
}
@@ -79,7 +79,7 @@ inline static void
update_xmalloc_stat_free(uint32_t handle, size_t __n) {
ATOM_FSUB(&_used_memory, __n);
ATOM_FDEC(&_memory_block);
ssize_t* allocated = get_allocated_field(handle);
ATOM_SIZET * allocated = get_allocated_field(handle);
if(allocated) {
ATOM_FSUB(allocated, __n);
}

View File

@@ -31,8 +31,7 @@ rwlock_rlock(struct rwlock *lock) {
static inline void
rwlock_wlock(struct rwlock *lock) {
int clear = 0;
while (!ATOM_CAS(&lock->write,clear,1)) {}
while (!ATOM_CAS(&lock->write,0,1)) {}
while(ATOM_LOAD(&lock->read)) {}
}

View File

@@ -595,8 +595,7 @@ cmd_logon(struct skynet_context * context, const char * param) {
if (lastf == NULL) {
f = skynet_log_open(context, handle);
if (f) {
uintptr_t exp = 0;
if (!ATOM_CAS_POINTER(&ctx->logfile, exp, (uintptr_t)f)) {
if (!ATOM_CAS_POINTER(&ctx->logfile, 0, (uintptr_t)f)) {
// logfile opens in other thread, close this one.
fclose(f);
}
@@ -617,8 +616,7 @@ cmd_logoff(struct skynet_context * context, const char * param) {
FILE * f = (FILE *)ATOM_LOAD(&ctx->logfile);
if (f) {
// logfile may close in other thread
uintptr_t fptr = (uintptr_t)f;
if (ATOM_CAS_POINTER(&ctx->logfile, fptr, (uintptr_t)NULL)) {
if (ATOM_CAS_POINTER(&ctx->logfile, (uintptr_t)f, (uintptr_t)NULL)) {
skynet_log_close(context, f, handle);
}
}

View File

@@ -1316,7 +1316,7 @@ inc_sending_ref(struct socket *s, int id) {
continue;
}
// inc sending only matching the same socket id
if (ATOM_CAS(&s->sending, sending, sending + 1))
if (ATOM_CAS_ULONG(&s->sending, sending, sending + 1))
return;
// atom inc failed, retry
} else {