static void * _worker(void *p) { struct worker_parm *wp = p; int id = wp->id; struct monitor *m = wp->m; struct skynet_monitor *sm = m->m[id]; skynet_initthread(THREAD_WORKER); for (;;) { if (skynet_context_message_dispatch(sm)) { CHECK_ABORT if (pthread_mutex_lock(&m->mutex) == 0) { ++ m->sleep; // "spurious wakeup" is harmless, // because skynet_context_message_dispatch() can be call at any time. pthread_cond_wait(&m->cond, &m->mutex); -- m->sleep; if (pthread_mutex_unlock(&m->mutex)) { fprintf(stderr, "unlock mutex error"); exit(1); } } } } return NULL; }
static void * thread_socket(void *p) { struct monitor * m = p; skynet_initthread(THREAD_SOCKET); for (;;) { int r = skynet_socket_poll(); if (r==0) break; if (r<0) { CHECK_ABORT continue; } wakeup(m,0); }
static void * thread_socket(void *p) { struct monitor * m = p; skynet_initthread(THREAD_SOCKET); for (;;) { int r = skynet_socket_poll(); if (r==0) break; if (r<0) { CHECK_ABORT continue; } wakeup(m,0); // 网络 IO 消息在 skynet 的设计里是 2 等公民. 而且一个网络消息通常只需要一个 worker 去处理就够了 }
static void * _timer(void *p) { struct monitor * m = p; skynet_initthread(THREAD_TIMER); for (;;) { skynet_updatetime(); CHECK_ABORT wakeup(m,m->count-1); //usleep(2500); Sleep(2); } // wakeup socket thread skynet_socket_exit(); // wakeup all worker thread pthread_cond_broadcast(&m->cond); return NULL; }
static void * thread_socket(void *p) { struct monitor * m = p; // set socket thread's specific data to THREAD_SOCKET skynet_initthread(THREAD_SOCKET); // continue to poll socket and signal worker thread to process for (;;) { int r = skynet_socket_poll(); if (r==0) break; if (r<0) { CHECK_ABORT continue; } wakeup(m,0); }
static void *thread_socket(void *p) { struct monitor *m = p; skynet_initthread(THREAD_SOCKET); for(;;) { int r = skynet_socket_poll(); if(r == 0) break; if(r < 0) { CHECK_ABORT continue; } // 有socket消息返回 wakeup(m, 0); // 全部线程都睡眠的情况下才唤醒一个工作线程(即只要有工作线程处于工作状态,则不需要唤醒) }
static void * _monitor(void *p) { struct monitor * m = p; int i; int n = m->count; skynet_initthread(THREAD_MONITOR); for (;;) { CHECK_ABORT for (i=0;i<n;i++) { skynet_monitor_check(m->m[i]); } for (i=0;i<5;i++) { CHECK_ABORT #if defined(__WIN32__) Sleep(1000); #else sleep(1); #endif } } return NULL; }