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; }
static void* swThreadPool_loop(void *arg) { swThreadParam *param = arg; swThreadPool *pool = param->object; int id = param->pti; int ret; void *task; if (pool->onStart) { pool->onStart(pool, id); } while (SwooleG.running) { pool->cond.lock(&pool->cond); if (pool->shutdown) { pool->cond.unlock(&pool->cond); swTrace("thread [%d] will exit\n", id); pthread_exit(NULL); } if (pool->task_num == 0) { pool->cond.wait(&pool->cond); } swTrace("thread [%d] is starting to work\n", id); ret = swRingQueue_pop(&pool->queue, &task); pool->cond.unlock(&pool->cond); if (ret >= 0) { sw_atomic_t *task_num = &pool->task_num; sw_atomic_fetch_sub(task_num, 1); pool->onTask(pool, (void *) task, ret); } } if (pool->onStop) { pool->onStop(pool, id); } pthread_exit(NULL); return NULL; }
static PHP_METHOD(swoole_ringqueue, pop) { swRingQueue *queue = swoole_get_object(getThis()); zval *zdata; int n = swRingQueue_pop(queue, (void**)&zdata); if (n < 0) { RETURN_FALSE; } RETVAL_ZVAL(zdata, 0, 0); efree(zdata); }