diff --git a/skynet-src/skynet_harbor.c b/skynet-src/skynet_harbor.c index 48814c83..379fce80 100644 --- a/skynet-src/skynet_harbor.c +++ b/skynet-src/skynet_harbor.c @@ -31,5 +31,17 @@ skynet_harbor_init(int harbor) { void skynet_harbor_start(void *ctx) { + // the HARBOR must be reserved to ensure the pointer is valid. + // It will be released at last by calling skynet_harbor_exit + skynet_context_reserve(ctx); REMOTE = ctx; } + +void +skynet_harbor_exit() { + struct skynet_context * ctx = REMOTE; + REMOTE= NULL; + if (ctx) { + skynet_context_release(ctx); + } +} diff --git a/skynet-src/skynet_harbor.h b/skynet-src/skynet_harbor.h index b699f625..62982455 100644 --- a/skynet-src/skynet_harbor.h +++ b/skynet-src/skynet_harbor.h @@ -26,5 +26,6 @@ void skynet_harbor_send(struct remote_message *rmsg, uint32_t source, int sessio int skynet_harbor_message_isremote(uint32_t handle); void skynet_harbor_init(int harbor); void skynet_harbor_start(void * ctx); +void skynet_harbor_exit(); #endif diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 50978b10..0a3a6b00 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -178,6 +178,14 @@ skynet_context_grab(struct skynet_context *ctx) { __sync_add_and_fetch(&ctx->ref,1); } +void +skynet_context_reserve(struct skynet_context *ctx) { + skynet_context_grab(ctx); + // don't count the context reserved, because skynet abort (the worker threads terminate) only when the total context is 0 . + // the reserved context will be release at last. + context_dec(); +} + static void delete_context(struct skynet_context *ctx) { if (ctx->logfile) { diff --git a/skynet-src/skynet_server.h b/skynet-src/skynet_server.h index 27b6b768..92e125b0 100644 --- a/skynet-src/skynet_server.h +++ b/skynet-src/skynet_server.h @@ -10,6 +10,7 @@ struct skynet_monitor; struct skynet_context * skynet_context_new(const char * name, const char * parm); void skynet_context_grab(struct skynet_context *); +void skynet_context_reserve(struct skynet_context *ctx); struct skynet_context * skynet_context_release(struct skynet_context *); uint32_t skynet_context_handle(struct skynet_context *); int skynet_context_push(uint32_t handle, struct skynet_message *message); diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index bef06b4c..93ef4485 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -236,4 +236,5 @@ skynet_start(struct skynet_config * config) { if (config->daemon) { daemon_exit(config->daemon); } + skynet_harbor_exit(); }