diff --git a/skynet_handle.c b/skynet_handle.c index 6e9f3a0b..d64c2bea 100644 --- a/skynet_handle.c +++ b/skynet_handle.c @@ -37,19 +37,20 @@ skynet_handle_register(struct skynet_context *ctx) { for (;;) { int i; for (i=0;islot_size;i++) { - int hash = (i+s->handle_index) & (s->slot_size-1); + uint32_t handle = (i+s->handle_index) & HANDLE_MASK; + int hash = handle & (s->slot_size-1); if (s->slot[hash] == NULL) { s->slot[hash] = ctx; - uint32_t handle = s->handle_index + i ; - skynet_context_init(ctx, handle); + s->handle_index = handle + 1; rwlock_wunlock(&s->lock); - s->handle_index = handle + 1; - - return (handle & HANDLE_MASK) | s->harbor; + handle |= s->harbor; + skynet_context_init(ctx, handle); + return handle; } } + assert((s->slot_size*2 - 1) <= HANDLE_MASK); struct skynet_context ** new_slot = malloc(s->slot_size * 2 * sizeof(struct skynet_context *)); memset(new_slot, 0, s->slot_size * 2 * sizeof(struct skynet_context *)); for (i=0;islot_size;i++) { @@ -76,18 +77,17 @@ skynet_handle_retire(uint32_t handle) { s->slot[hash] = NULL; int i; - int j=0; - for (i=0;iname_count;i++,j++) { + int j=0, n=s->name_count; + for (i=0; iname[i].handle == handle) { free(s->name[i].name); - ++j; - --s->name_count; - } else { - if (i!=j) { - s->name[i] = s->name[j]; - } + continue; + } else if (i!=j) { + s->name[j] = s->name[i]; } + ++j; } + s->name_count = j; } rwlock_wunlock(&s->lock); @@ -128,7 +128,6 @@ skynet_handle_findname(const char * name) { int c = strcmp(n->name, name); if (c==0) { handle = n->handle; - handle |= s->harbor; break; } if (c<0) { diff --git a/skynet_harbor.c b/skynet_harbor.c index 9eaec98c..5d589f4c 100644 --- a/skynet_harbor.c +++ b/skynet_harbor.c @@ -313,6 +313,36 @@ _report_zmq_error(int rc) { } } +static int +_isdecimal(int c) { + return c>='0' && c<='9'; +} + +// Name-updating protocols: +// +// 1) harbor_id=harbor_address +// 2) context_name=context_handle +static int +_split_name(uint8_t *buf, int len, int *np) { + uint8_t *sep; + if (len > 0 && _isdecimal(buf[0])) { + int i=0; + int n=0; + do { + n = n*10 + (buf[i]-'0'); + } while(++izmq_local,&content,0); _report_zmq_error(rc); int sz = zmq_msg_size(&content); - int i; - int n = 0; uint8_t * buffer = zmq_msg_data(&content); - for (i=0;iqueue,&msg)) { printf("goback %x\n",msg.destination); + __sync_lock_test_and_set(&Z->notice_event, 1); goto _goback; } } diff --git a/skynet_server.c b/skynet_server.c index 95677821..c91d7712 100644 --- a/skynet_server.c +++ b/skynet_server.c @@ -158,12 +158,18 @@ skynet_context_message_dispatch(void) { assert(ctx->in_global_queue); + int re_q = 1; struct skynet_message msg; if (skynet_mq_pop(q,&msg)) { // empty queue __sync_lock_release(&ctx->in_global_queue); - skynet_context_release(ctx); - return 0; + if (skynet_mq_pop(q,&msg)) { + skynet_context_release(ctx); + return 0; + } + if (__sync_lock_test_and_set(&ctx->in_global_queue, 1)) { + re_q = 0; + } } if (ctx->cb == NULL) { @@ -178,7 +184,9 @@ skynet_context_message_dispatch(void) { skynet_context_release(ctx); - skynet_globalmq_push(q); + if (re_q) { + skynet_globalmq_push(q); + } return 0; }