diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 017d7206..8f619ad4 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -245,7 +245,7 @@ skynet_context_dispatchall(struct skynet_context * ctx) { } struct message_queue * -skynet_context_message_dispatch(struct skynet_monitor *sm, struct message_queue *q) { +skynet_context_message_dispatch(struct skynet_monitor *sm, struct message_queue *q, int weight) { if (q == NULL) { q = skynet_globalmq_pop(); if (q==NULL) @@ -261,18 +261,27 @@ skynet_context_message_dispatch(struct skynet_monitor *sm, struct message_queue return skynet_globalmq_pop(); } + int i,n=1; struct skynet_message msg; - if (skynet_mq_pop(q,&msg)) { - skynet_context_release(ctx); - return skynet_globalmq_pop(); - } - skynet_monitor_trigger(sm, msg.source , handle); + for (i=0;i= 0) { + n = skynet_mq_length(q); + n >>= weight; + } - if (ctx->cb == NULL) { - skynet_free(msg.data); - } else { - _dispatch_message(ctx, &msg); + skynet_monitor_trigger(sm, msg.source , handle); + + if (ctx->cb == NULL) { + skynet_free(msg.data); + } else { + _dispatch_message(ctx, &msg); + } + + skynet_monitor_trigger(sm, 0,0); } assert(q == ctx->queue); @@ -285,8 +294,6 @@ skynet_context_message_dispatch(struct skynet_monitor *sm, struct message_queue } skynet_context_release(ctx); - skynet_monitor_trigger(sm, 0,0); - return q; } diff --git a/skynet-src/skynet_server.h b/skynet-src/skynet_server.h index be4e301f..5d07ba70 100644 --- a/skynet-src/skynet_server.h +++ b/skynet-src/skynet_server.h @@ -16,7 +16,7 @@ void skynet_context_init(struct skynet_context *, uint32_t handle); int skynet_context_push(uint32_t handle, struct skynet_message *message); void skynet_context_send(struct skynet_context * context, void * msg, size_t sz, uint32_t source, int type, int session); int skynet_context_newsession(struct skynet_context *); -struct message_queue * skynet_context_message_dispatch(struct skynet_monitor *, struct message_queue *); // return next queue +struct message_queue * skynet_context_message_dispatch(struct skynet_monitor *, struct message_queue *, int weight); // return next queue int skynet_context_total(); void skynet_context_dispatchall(struct skynet_context * context); // for skynet_error output before exit diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index c101da30..bef06b4c 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -27,6 +27,7 @@ struct monitor { struct worker_parm { struct monitor *m; int id; + int weight; }; #define CHECK_ABORT if (skynet_context_total()==0) break; @@ -118,12 +119,13 @@ static void * _worker(void *p) { struct worker_parm *wp = p; int id = wp->id; + int weight = wp->weight; struct monitor *m = wp->m; struct skynet_monitor *sm = m->m[id]; skynet_initthread(THREAD_WORKER); struct message_queue * q = NULL; for (;;) { - q = skynet_context_message_dispatch(sm, q); + q = skynet_context_message_dispatch(sm, q, weight); if (q == NULL) { CHECK_ABORT if (pthread_mutex_lock(&m->mutex) == 0) { @@ -169,10 +171,20 @@ _start(int thread) { create_thread(&pid[1], _timer, m); create_thread(&pid[2], _socket, m); + static int weight[] = { + -1, -1, -1, -1, 0, 0, 0, 0, + 1, 1, 1, 1, 1, 1, 1, 1, + 2, 2, 2, 2, 2, 2, 2, 2, + 3, 3, 3, 3, 3, 3, 3, 3, }; struct worker_parm wp[thread]; for (i=0;i