use stdatomic (#1317)

This commit is contained in:
云风
2021-01-11 10:54:34 +08:00
committed by GitHub
parent 284df5430b
commit b164e3a8a9
12 changed files with 201 additions and 138 deletions

View File

@@ -10,7 +10,7 @@
#include "atomic.h"
struct mc_package {
int reference;
ATOM_INT reference;
uint32_t size;
void *data;
};
@@ -18,7 +18,7 @@ struct mc_package {
static int
pack(lua_State *L, void *data, size_t size) {
struct mc_package * pack = skynet_malloc(sizeof(struct mc_package));
pack->reference = 0;
ATOM_INIT(&pack->reference, 0);
pack->size = (uint32_t)size;
pack->data = data;
struct mc_package ** ret = skynet_malloc(sizeof(*ret));
@@ -91,10 +91,10 @@ static int
mc_bindrefer(lua_State *L) {
struct mc_package ** pack = lua_touserdata(L,1);
int ref = luaL_checkinteger(L,2);
if ((*pack)->reference != 0) {
if (ATOM_LOAD(&(*pack)->reference) != 0) {
return luaL_error(L, "Can't bind a multicast package more than once");
}
(*pack)->reference = ref;
ATOM_STORE(&(*pack)->reference , ref);
lua_pushlightuserdata(L, *pack);
@@ -110,7 +110,7 @@ static int
mc_closelocal(lua_State *L) {
struct mc_package *pack = lua_touserdata(L,1);
int ref = ATOM_DEC(&pack->reference);
int ref = ATOM_FDEC(&pack->reference)-1;
if (ref <= 0) {
skynet_free(pack->data);
skynet_free(pack);

View File

@@ -40,7 +40,7 @@ struct node {
struct state {
int dirty;
int ref;
ATOM_INT ref;
struct table * root;
};
@@ -383,7 +383,7 @@ convert_stringmap(struct context *ctx, struct table *tbl) {
lua_pushvalue(L, 1);
struct state * s = lua_newuserdatauv(L, sizeof(*s), 1);
s->dirty = 0;
s->ref = 0;
ATOM_INIT(&s->ref , 0);
s->root = tbl;
lua_replace(L, 1);
lua_replace(L, -2);
@@ -665,7 +665,7 @@ releaseobj(lua_State *L) {
struct ctrl *c = lua_touserdata(L, 1);
struct table *tbl = c->root;
struct state *s = lua_touserdata(tbl->L, 1);
ATOM_DEC(&s->ref);
ATOM_FDEC(&s->ref);
c->root = NULL;
c->update = NULL;
@@ -676,7 +676,7 @@ static int
lboxconf(lua_State *L) {
struct table * tbl = get_table(L,1);
struct state * s = lua_touserdata(tbl->L, 1);
ATOM_INC(&s->ref);
ATOM_FINC(&s->ref);
struct ctrl * c = lua_newuserdatauv(L, sizeof(*c), 1);
c->root = tbl;
@@ -712,7 +712,7 @@ static int
lgetref(lua_State *L) {
struct table *tbl = get_table(L,1);
struct state * s = lua_touserdata(tbl->L, 1);
lua_pushinteger(L , s->ref);
lua_pushinteger(L , ATOM_LOAD(&s->ref));
return 1;
}
@@ -721,7 +721,7 @@ static int
lincref(lua_State *L) {
struct table *tbl = get_table(L,1);
struct state * s = lua_touserdata(tbl->L, 1);
int ref = ATOM_INC(&s->ref);
int ref = ATOM_FINC(&s->ref)+1;
lua_pushinteger(L , ref);
return 1;
@@ -731,7 +731,7 @@ static int
ldecref(lua_State *L) {
struct table *tbl = get_table(L,1);
struct state * s = lua_touserdata(tbl->L, 1);
int ref = ATOM_DEC(&s->ref);
int ref = ATOM_FDEC(&s->ref)-1;
lua_pushinteger(L , ref);
return 1;

View File

@@ -13,7 +13,7 @@
struct stm_object {
struct rwlock lock;
int reference;
ATOM_INT reference;
struct stm_copy * copy;
};
@@ -27,7 +27,7 @@ struct stm_copy {
static struct stm_copy *
stm_newcopy(void * msg, int32_t sz) {
struct stm_copy * copy = skynet_malloc(sizeof(*copy));
copy->reference = 1;
ATOM_INIT(&copy->reference, 1);
copy->sz = sz;
copy->msg = msg;
@@ -38,7 +38,7 @@ static struct stm_object *
stm_new(void * msg, int32_t sz) {
struct stm_object * obj = skynet_malloc(sizeof(*obj));
rwlock_init(&obj->lock);
obj->reference = 1;
ATOM_INIT(&obj->reference , 1);
obj->copy = stm_newcopy(msg, sz);
return obj;
@@ -48,7 +48,7 @@ static void
stm_releasecopy(struct stm_copy *copy) {
if (copy == NULL)
return;
if (ATOM_DEC(&copy->reference) == 0) {
if (ATOM_FDEC(&copy->reference) <= 1) {
skynet_free(copy->msg);
skynet_free(copy);
}
@@ -61,7 +61,7 @@ stm_release(struct stm_object *obj) {
// writer release the stm object, so release the last copy .
stm_releasecopy(obj->copy);
obj->copy = NULL;
if (--obj->reference > 0) {
if (ATOM_FDEC(&obj->reference) > 1) {
// stm object grab by readers, reset the copy to NULL.
rwlock_wunlock(&obj->lock);
return;
@@ -73,7 +73,7 @@ stm_release(struct stm_object *obj) {
static void
stm_releasereader(struct stm_object *obj) {
rwlock_rlock(&obj->lock);
if (ATOM_DEC(&obj->reference) == 0) {
if (ATOM_FDEC(&obj->reference) == 1) {
// last reader, no writer. so no need to unlock
assert(obj->copy == NULL);
skynet_free(obj);