/** * 使用消息队列通信 */ int swFactoryProcess_writer_loop_queue(swThreadParam *param) { swFactory *factory = param->object; swFactoryProcess *object = factory->object; swServer *serv = factory->ptr; int ret; int pti = param->pti; swQueue_data sdata; //必须加1,msg_type必须不能为0 sdata.mtype = pti + 1; swSingalNone(); while (swoole_running > 0) { swTrace("[Writer]wt_queue[%ld]->out wait", sdata.mtype); int ret = object->wt_queue.out(&object->wt_queue, &sdata, sizeof(sdata.mdata)); if (ret < 0) { swWarn("[writer]wt_queue->out fail.Error: %s [%d]", strerror(errno), errno); } else { swFactoryProcess_writer_excute(factory, (swEventData *)sdata.mdata); } } pthread_exit((void *) param); return SW_OK; }
/** * 使用Unix Socket通信 */ int swFactoryProcess_writer_loop_unsock(swThreadParam *param) { swFactory *factory = param->object; swFactoryProcess *object = factory->object; int pti = param->pti; swReactor *reactor = &(object->writers[pti].reactor); struct timeval tmo; tmo.tv_sec = 3; tmo.tv_usec = 0; reactor->factory = factory; reactor->id = pti; if (swReactorSelect_create(reactor) < 0) { swWarn("swReactorSelect_create fail\n"); pthread_exit((void *) param); } swSingalNone(); reactor->setHandle(reactor, SW_FD_PIPE, swFactoryProcess_writer_receive); reactor->wait(reactor, &tmo); reactor->free(reactor); pthread_exit((void *) param); return SW_OK; }
/** * 使用Unix Socket通信 */ int swFactoryProcess_writer_loop_unsock(swThreadParam *param) { swFactory *factory = param->object; swFactoryProcess *object = factory->object; int pti = param->pti; swReactor *reactor = &(object->writers[pti].reactor); struct timeval tmo; tmo.tv_sec = 3; tmo.tv_usec = 0; reactor->factory = factory; reactor->id = pti; //worker过多epoll效率更高 if (swReactor_auto(reactor, SW_REACTOR_MAXEVENTS) < 0) { pthread_exit((void *) param); return SW_ERR; } swSingalNone(); reactor->setHandle(reactor, SW_FD_PIPE, swReactorThread_onPipeReceive); reactor->wait(reactor, &tmo); reactor->free(reactor); pthread_exit((void *) param); return SW_OK; }
static int swFactoryThread_writer_loop(swThreadParam *param) { swFactory *factory = param->object; swServer *serv = factory->ptr; swFactoryThread *this = factory->object; int pti = param->pti; int ret; swEventData *req; uint64_t flag; //cpu affinity setting #if HAVE_CPU_AFFINITY if (serv->open_cpu_affinity) { cpu_set_t cpu_set; CPU_ZERO(&cpu_set); CPU_SET(pti % SW_CPU_NUM, &cpu_set); if (0 != pthread_setaffinity_np(pthread_self(), sizeof(cpu_set), &cpu_set)) { swTrace("pthread_setaffinity_np set fail\n"); } } #endif if (serv->onWorkerStart != NULL) { serv->onWorkerStart(serv, pti); } swSingalNone(); //main loop while (swoole_running > 0) { if (swRingQueue_pop(&(this->queues[pti]), (void **) &req) == 0) { factory->last_from_id = req->info.from_id; factory->onTask(factory, req); sw_free(req); } else { ret = this->writers[pti].evfd.read(&this->writers[pti].evfd, &flag, sizeof(flag)); if (ret < 0) { swTrace("read fail.errno=%d", errno); } } } //shutdown this->writers[pti].evfd.close(&this->writers[pti].evfd); if (serv->onWorkerStop != NULL) { serv->onWorkerStop(serv, pti); } sw_free(param); pthread_exit(SW_OK); return SW_OK; }
int static cpReactor_thread_loop(int *id) { struct timeval timeo; timeo.tv_sec = CP_REACTOR_TIMEO_SEC; timeo.tv_usec = CP_REACTOR_TIMEO_USEC; swSingalNone(); int epfd = epoll_create(512); //这个参数没用 CPGS->reactor_threads[*id].epfd = epfd; epoll_wait_handle handles[CP_MAX_EVENT]; handles[EPOLLIN] = cpReactor_client_receive; handles[EPOLLPRI] = cpReactor_client_release; handles[EPOLL_CLOSE] = cpReactor_client_close; cpEpoll_wait(handles, &timeo, epfd); free(id); pthread_exit(0); return SUCCESS; }