diff --git a/service-src/service_snlua.c b/service-src/service_snlua.c index 4e9381ce..d7871d53 100644 --- a/service-src/service_snlua.c +++ b/service-src/service_snlua.c @@ -521,14 +521,12 @@ snlua_signal(struct snlua *l, int signal) { skynet_error(l->ctx, "recv a signal %d", signal); if (signal == 0) { if (ATOM_LOAD(&l->trap) == 0) { - int zero = 0; // only one thread can set trap ( l->trap 0->1 ) - if (!ATOM_CAS(&l->trap, zero, 1)) + if (!ATOM_CAS(&l->trap, 0, 1)) return; lua_sethook (l->activeL, signal_hook, LUA_MASKCOUNT, 1); // finish set ( l->trap 1 -> -1 ) - int one = 1; - ATOM_CAS(&l->trap, one, -1); + ATOM_CAS(&l->trap, 1, -1); } } else if (signal == 1) { skynet_error(l->ctx, "Current Memory %.3fK", (float)l->mem / 1024); diff --git a/skynet-src/atomic.h b/skynet-src/atomic.h index 5acc32df..4dd3ba82 100644 --- a/skynet-src/atomic.h +++ b/skynet-src/atomic.h @@ -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) diff --git a/skynet-src/malloc_hook.c b/skynet-src/malloc_hook.c index d02113cf..44c71830 100644 --- a/skynet-src/malloc_hook.c +++ b/skynet-src/malloc_hook.c @@ -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); } diff --git a/skynet-src/rwlock.h b/skynet-src/rwlock.h index 28a49c24..2b954bd0 100644 --- a/skynet-src/rwlock.h +++ b/skynet-src/rwlock.h @@ -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)) {} } diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index e90133b9..e4d7ea3a 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -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); } } diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index f0e1a395..d1a912ee 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -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 {