mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-22 02:53:09 +00:00
Fix #1552
This commit is contained in:
@@ -521,14 +521,12 @@ snlua_signal(struct snlua *l, int signal) {
|
|||||||
skynet_error(l->ctx, "recv a signal %d", signal);
|
skynet_error(l->ctx, "recv a signal %d", signal);
|
||||||
if (signal == 0) {
|
if (signal == 0) {
|
||||||
if (ATOM_LOAD(&l->trap) == 0) {
|
if (ATOM_LOAD(&l->trap) == 0) {
|
||||||
int zero = 0;
|
|
||||||
// only one thread can set trap ( l->trap 0->1 )
|
// 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;
|
return;
|
||||||
lua_sethook (l->activeL, signal_hook, LUA_MASKCOUNT, 1);
|
lua_sethook (l->activeL, signal_hook, LUA_MASKCOUNT, 1);
|
||||||
// finish set ( l->trap 1 -> -1 )
|
// finish set ( l->trap 1 -> -1 )
|
||||||
int one = 1;
|
ATOM_CAS(&l->trap, 1, -1);
|
||||||
ATOM_CAS(&l->trap, one, -1);
|
|
||||||
}
|
}
|
||||||
} else if (signal == 1) {
|
} else if (signal == 1) {
|
||||||
skynet_error(l->ctx, "Current Memory %.3fK", (float)l->mem / 1024);
|
skynet_error(l->ctx, "Current Memory %.3fK", (float)l->mem / 1024);
|
||||||
|
|||||||
@@ -13,6 +13,8 @@
|
|||||||
#define ATOM_LOAD(ptr) (*(ptr))
|
#define ATOM_LOAD(ptr) (*(ptr))
|
||||||
#define ATOM_STORE(ptr, v) (*(ptr) = v)
|
#define ATOM_STORE(ptr, v) (*(ptr) = v)
|
||||||
#define ATOM_CAS(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval)
|
#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_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_FINC(ptr) __sync_fetch_and_add(ptr, 1)
|
||||||
#define ATOM_FDEC(ptr) __sync_fetch_and_sub(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_INIT(ref, v) atomic_init(ref, v)
|
||||||
#define ATOM_LOAD(ptr) atomic_load(ptr)
|
#define ATOM_LOAD(ptr) atomic_load(ptr)
|
||||||
#define ATOM_STORE(ptr, v) atomic_store(ptr, v)
|
#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_FINC(ptr) atomic_fetch_add(ptr, 1)
|
||||||
#define ATOM_FDEC(ptr) atomic_fetch_sub(ptr, 1)
|
#define ATOM_FDEC(ptr) atomic_fetch_sub(ptr, 1)
|
||||||
#define ATOM_FADD(ptr,n) atomic_fetch_add(ptr, n)
|
#define ATOM_FADD(ptr,n) atomic_fetch_add(ptr, n)
|
||||||
|
|||||||
@@ -19,8 +19,8 @@ static ATOM_SIZET _used_memory = 0;
|
|||||||
static ATOM_SIZET _memory_block = 0;
|
static ATOM_SIZET _memory_block = 0;
|
||||||
|
|
||||||
struct mem_data {
|
struct mem_data {
|
||||||
uint32_t handle;
|
ATOM_ULONG handle;
|
||||||
ssize_t allocated;
|
ATOM_SIZET allocated;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct mem_cookie {
|
struct mem_cookie {
|
||||||
@@ -44,19 +44,19 @@ static struct mem_data mem_stats[SLOT_SIZE];
|
|||||||
#define raw_realloc je_realloc
|
#define raw_realloc je_realloc
|
||||||
#define raw_free je_free
|
#define raw_free je_free
|
||||||
|
|
||||||
static ssize_t*
|
static ATOM_SIZET *
|
||||||
get_allocated_field(uint32_t handle) {
|
get_allocated_field(uint32_t handle) {
|
||||||
int h = (int)(handle & (SLOT_SIZE - 1));
|
int h = (int)(handle & (SLOT_SIZE - 1));
|
||||||
struct mem_data *data = &mem_stats[h];
|
struct mem_data *data = &mem_stats[h];
|
||||||
uint32_t old_handle = data->handle;
|
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) {
|
if(old_handle == 0 || old_alloc <= 0) {
|
||||||
// data->allocated may less than zero, because it may not count at start.
|
// 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;
|
return 0;
|
||||||
}
|
}
|
||||||
if (old_alloc < 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) {
|
if(data->handle != handle) {
|
||||||
@@ -69,7 +69,7 @@ inline static void
|
|||||||
update_xmalloc_stat_alloc(uint32_t handle, size_t __n) {
|
update_xmalloc_stat_alloc(uint32_t handle, size_t __n) {
|
||||||
ATOM_FADD(&_used_memory, __n);
|
ATOM_FADD(&_used_memory, __n);
|
||||||
ATOM_FINC(&_memory_block);
|
ATOM_FINC(&_memory_block);
|
||||||
ssize_t* allocated = get_allocated_field(handle);
|
ATOM_SIZET * allocated = get_allocated_field(handle);
|
||||||
if(allocated) {
|
if(allocated) {
|
||||||
ATOM_FADD(allocated, __n);
|
ATOM_FADD(allocated, __n);
|
||||||
}
|
}
|
||||||
@@ -79,7 +79,7 @@ inline static void
|
|||||||
update_xmalloc_stat_free(uint32_t handle, size_t __n) {
|
update_xmalloc_stat_free(uint32_t handle, size_t __n) {
|
||||||
ATOM_FSUB(&_used_memory, __n);
|
ATOM_FSUB(&_used_memory, __n);
|
||||||
ATOM_FDEC(&_memory_block);
|
ATOM_FDEC(&_memory_block);
|
||||||
ssize_t* allocated = get_allocated_field(handle);
|
ATOM_SIZET * allocated = get_allocated_field(handle);
|
||||||
if(allocated) {
|
if(allocated) {
|
||||||
ATOM_FSUB(allocated, __n);
|
ATOM_FSUB(allocated, __n);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,8 +31,7 @@ rwlock_rlock(struct rwlock *lock) {
|
|||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
rwlock_wlock(struct rwlock *lock) {
|
rwlock_wlock(struct rwlock *lock) {
|
||||||
int clear = 0;
|
while (!ATOM_CAS(&lock->write,0,1)) {}
|
||||||
while (!ATOM_CAS(&lock->write,clear,1)) {}
|
|
||||||
while(ATOM_LOAD(&lock->read)) {}
|
while(ATOM_LOAD(&lock->read)) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -595,8 +595,7 @@ cmd_logon(struct skynet_context * context, const char * param) {
|
|||||||
if (lastf == NULL) {
|
if (lastf == NULL) {
|
||||||
f = skynet_log_open(context, handle);
|
f = skynet_log_open(context, handle);
|
||||||
if (f) {
|
if (f) {
|
||||||
uintptr_t exp = 0;
|
if (!ATOM_CAS_POINTER(&ctx->logfile, 0, (uintptr_t)f)) {
|
||||||
if (!ATOM_CAS_POINTER(&ctx->logfile, exp, (uintptr_t)f)) {
|
|
||||||
// logfile opens in other thread, close this one.
|
// logfile opens in other thread, close this one.
|
||||||
fclose(f);
|
fclose(f);
|
||||||
}
|
}
|
||||||
@@ -617,8 +616,7 @@ cmd_logoff(struct skynet_context * context, const char * param) {
|
|||||||
FILE * f = (FILE *)ATOM_LOAD(&ctx->logfile);
|
FILE * f = (FILE *)ATOM_LOAD(&ctx->logfile);
|
||||||
if (f) {
|
if (f) {
|
||||||
// logfile may close in other thread
|
// logfile may close in other thread
|
||||||
uintptr_t fptr = (uintptr_t)f;
|
if (ATOM_CAS_POINTER(&ctx->logfile, (uintptr_t)f, (uintptr_t)NULL)) {
|
||||||
if (ATOM_CAS_POINTER(&ctx->logfile, fptr, (uintptr_t)NULL)) {
|
|
||||||
skynet_log_close(context, f, handle);
|
skynet_log_close(context, f, handle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1316,7 +1316,7 @@ inc_sending_ref(struct socket *s, int id) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// inc sending only matching the same socket id
|
// 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;
|
return;
|
||||||
// atom inc failed, retry
|
// atom inc failed, retry
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user