/** * 写线程模式 */ int swFactoryThread_dispatch(swFactory *factory, swEventData *buf) { swFactoryThread *this = factory->object; int pti; int ret; uint64_t flag = 1; int datasize = sizeof(int)*3 + buf->info.len + 1; char *data; swServer *serv = factory->ptr; if(serv->dispatch_mode == SW_DISPATCH_ROUND) { //使用平均分配 pti = this->writer_pti; if (this->writer_pti >= this->writer_num) { this->writer_pti = 0; pti = 0; } this->writer_pti++; } else { //使用fd取摸来散列 pti = buf->info.fd % this->writer_num; } data = sw_malloc(datasize); if(data == NULL) { swTrace("malloc fail\n"); return SW_ERR; } memcpy(data, buf, datasize); //send data ptr. use event_fd if (swRingQueue_push(&(this->queues[pti]), (void *) data) < 0) { swWarn("swRingQueue_push fail.Buffer is full.Writer=%d\n", pti); return SW_ERR; } else { ret = this->writers[pti].evfd.write(&this->writers[pti].evfd, &flag, sizeof(flag)); if(ret < 0) { swWarn("Send queue notice fail.errno=%d\n", errno); } return ret; } }
static PHP_METHOD(swoole_ringqueue, push) { swRingQueue *queue = swoole_get_object(getThis()); zval *zdata; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zdata) == FAILURE) { RETURN_FALSE; } zdata = sw_zval_dup(zdata); if (swRingQueue_push(queue, zdata) < 0) { efree(zdata); RETURN_FALSE; } else { Z_TRY_ADDREF_P(zdata); RETURN_TRUE; } }
int swThreadPool_dispatch(swThreadPool *pool, void *task, int task_len) { int ret; pool->cond.lock(&pool->cond); #ifdef SW_THREADPOOL_USE_CHANNEL ret = swChannel_in(pool->chan, task, task_len); #else ret = swRingQueue_push(&pool->queue, task); #endif pool->cond.unlock(&pool->cond); if (ret < 0) { SwooleG.error = EAGAIN; return SW_ERR; } sw_atomic_t *task_num = &pool->task_num; sw_atomic_fetch_add(task_num, 1); return pool->cond.notify(&pool->cond); }