mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 04:33:05 +00:00
use stdatomic (#1317)
This commit is contained in:
@@ -52,7 +52,7 @@ struct skynet_context {
|
||||
char result[32];
|
||||
uint32_t handle;
|
||||
int session_id;
|
||||
int ref;
|
||||
ATOM_INT ref;
|
||||
int message_count;
|
||||
bool init;
|
||||
bool endless;
|
||||
@@ -62,7 +62,7 @@ struct skynet_context {
|
||||
};
|
||||
|
||||
struct skynet_node {
|
||||
int total;
|
||||
ATOM_INT total;
|
||||
int init;
|
||||
uint32_t monitor_exit;
|
||||
pthread_key_t handle_key;
|
||||
@@ -73,17 +73,17 @@ static struct skynet_node G_NODE;
|
||||
|
||||
int
|
||||
skynet_context_total() {
|
||||
return G_NODE.total;
|
||||
return ATOM_LOAD(&G_NODE.total);
|
||||
}
|
||||
|
||||
static void
|
||||
context_inc() {
|
||||
ATOM_INC(&G_NODE.total);
|
||||
ATOM_FINC(&G_NODE.total);
|
||||
}
|
||||
|
||||
static void
|
||||
context_dec() {
|
||||
ATOM_DEC(&G_NODE.total);
|
||||
ATOM_FDEC(&G_NODE.total);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
@@ -137,11 +137,11 @@ skynet_context_new(const char * name, const char *param) {
|
||||
|
||||
ctx->mod = mod;
|
||||
ctx->instance = inst;
|
||||
ctx->ref = 2;
|
||||
ATOM_INIT(&ctx->ref , 2);
|
||||
ctx->cb = NULL;
|
||||
ctx->cb_ud = NULL;
|
||||
ctx->session_id = 0;
|
||||
ctx->logfile = NULL;
|
||||
ATOM_INIT(&ctx->logfile, NULL);
|
||||
|
||||
ctx->init = false;
|
||||
ctx->endless = false;
|
||||
@@ -194,7 +194,7 @@ skynet_context_newsession(struct skynet_context *ctx) {
|
||||
|
||||
void
|
||||
skynet_context_grab(struct skynet_context *ctx) {
|
||||
ATOM_INC(&ctx->ref);
|
||||
ATOM_FINC(&ctx->ref);
|
||||
}
|
||||
|
||||
void
|
||||
@@ -207,8 +207,9 @@ skynet_context_reserve(struct skynet_context *ctx) {
|
||||
|
||||
static void
|
||||
delete_context(struct skynet_context *ctx) {
|
||||
if (ctx->logfile) {
|
||||
fclose(ctx->logfile);
|
||||
FILE *f = ATOM_LOAD(&ctx->logfile);
|
||||
if (f) {
|
||||
fclose(f);
|
||||
}
|
||||
skynet_module_instance_release(ctx->mod, ctx->instance);
|
||||
skynet_mq_mark_release(ctx->queue);
|
||||
@@ -219,7 +220,7 @@ delete_context(struct skynet_context *ctx) {
|
||||
|
||||
struct skynet_context *
|
||||
skynet_context_release(struct skynet_context *ctx) {
|
||||
if (ATOM_DEC(&ctx->ref) == 0) {
|
||||
if (ATOM_FDEC(&ctx->ref) == 1) {
|
||||
delete_context(ctx);
|
||||
return NULL;
|
||||
}
|
||||
@@ -264,8 +265,9 @@ dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) {
|
||||
pthread_setspecific(G_NODE.handle_key, (void *)(uintptr_t)(ctx->handle));
|
||||
int type = msg->sz >> MESSAGE_TYPE_SHIFT;
|
||||
size_t sz = msg->sz & MESSAGE_TYPE_MASK;
|
||||
if (ctx->logfile) {
|
||||
skynet_log_output(ctx->logfile, msg->source, type, msg->session, msg->data, sz);
|
||||
FILE *f = ATOM_LOAD(&ctx->logfile);
|
||||
if (f) {
|
||||
skynet_log_output(f, msg->source, type, msg->session, msg->data, sz);
|
||||
}
|
||||
++ctx->message_count;
|
||||
int reserve_msg;
|
||||
@@ -589,11 +591,13 @@ cmd_logon(struct skynet_context * context, const char * param) {
|
||||
if (ctx == NULL)
|
||||
return NULL;
|
||||
FILE *f = NULL;
|
||||
FILE * lastf = ctx->logfile;
|
||||
FILE * lastf = ATOM_LOAD(&ctx->logfile);
|
||||
if (lastf == NULL) {
|
||||
f = skynet_log_open(context, handle);
|
||||
if (f) {
|
||||
if (!ATOM_CAS_POINTER(&ctx->logfile, NULL, f)) {
|
||||
ATOM_POINTER exp;
|
||||
ATOM_INIT(&exp, 0);
|
||||
if (!ATOM_CAS_POINTER(&ctx->logfile, exp, f)) {
|
||||
// logfile opens in other thread, close this one.
|
||||
fclose(f);
|
||||
}
|
||||
@@ -611,7 +615,7 @@ cmd_logoff(struct skynet_context * context, const char * param) {
|
||||
struct skynet_context * ctx = skynet_handle_grab(handle);
|
||||
if (ctx == NULL)
|
||||
return NULL;
|
||||
FILE * f = ctx->logfile;
|
||||
FILE * f = ATOM_LOAD(&ctx->logfile);
|
||||
if (f) {
|
||||
// logfile may close in other thread
|
||||
if (ATOM_CAS_POINTER(&ctx->logfile, f, NULL)) {
|
||||
@@ -806,7 +810,7 @@ skynet_context_send(struct skynet_context * ctx, void * msg, size_t sz, uint32_t
|
||||
|
||||
void
|
||||
skynet_globalinit(void) {
|
||||
G_NODE.total = 0;
|
||||
ATOM_INIT(&G_NODE.total , 0);
|
||||
G_NODE.monitor_exit = 0;
|
||||
G_NODE.init = 1;
|
||||
if (pthread_key_create(&G_NODE.handle_key, NULL)) {
|
||||
|
||||
Reference in New Issue
Block a user