int swAioBase_init(int max_aio_events) { if (swPipeBase_create(&swoole_aio_pipe, 0) < 0) { return SW_ERR; } if (SwooleAIO.thread_num <= 0) { SwooleAIO.thread_num = SW_AIO_THREAD_NUM_DEFAULT; } if (swThreadPool_create(&swAioBase_thread_pool, SwooleAIO.thread_num) < 0) { return SW_ERR; } swAioBase_thread_pool.onTask = swAioBase_thread_onTask; swAioBase_pipe_read = swoole_aio_pipe.getFd(&swoole_aio_pipe, 0); swAioBase_pipe_write = swoole_aio_pipe.getFd(&swoole_aio_pipe, 1); SwooleG.main_reactor->setHandle(SwooleG.main_reactor, SW_FD_AIO, swAioBase_onFinish); SwooleG.main_reactor->add(SwooleG.main_reactor, swAioBase_pipe_read, SW_FD_AIO); if (swThreadPool_run(&swAioBase_thread_pool) < 0) { return SW_ERR; } SwooleAIO.callback = swAio_callback_test; SwooleAIO.destroy = swAioBase_destroy; SwooleAIO.read = swAioBase_read; SwooleAIO.write = swAioBase_write; return SW_OK; }
swChannel* swChannel_create(int size, int maxlen, int flag) { assert(size > SW_CHANNEL_MIN_MEM + maxlen); int ret; void *mem; //是否使用共享内存 if (flag & SW_CHAN_SHM) { mem = sw_shm_malloc(size); } else { mem = sw_malloc(size); } if (mem == NULL) { swWarn("swChannel_create: malloc fail"); return NULL; } swChannel *object = mem; mem += sizeof(swChannel); bzero(object, sizeof(swChannel)); //允许溢出 object->size = size - maxlen; object->mem = mem; object->maxlen = maxlen; object->flag = flag; //是否启用锁 if (flag & SW_CHAN_LOCK) { //初始化锁 if (swMutex_create(&object->lock, 1) < 0) { swWarn("mutex init fail\n"); return NULL; } } //初始化通知系统 if (flag & SW_CHAN_NOTIFY) { #ifdef HAVE_EVENTFD ret = swPipeEventfd_create(&object->notify_fd, 1, 1); #else ret = swPipeBase_create(&object->notify_fd, 1); #endif if (ret < 0) { swWarn("notify_fd init fail\n"); return NULL; } } return object; }
int swPipeNotify_auto(swPipe *p, int blocking, int semaphore) { //eventfd是2.6.26提供的,timerfd是2.6.27提供的 #ifdef HAVE_EVENTFD return swPipeEventfd_create(p, blocking, semaphore); #else return swPipeBase_create(p, blocking); #endif }
int swAio_init(void) { if (SwooleAIO.init) { swWarn("AIO has already been initialized"); return SW_ERR; } if (!SwooleG.main_reactor) { swWarn("No eventloop, cannot initialized"); return SW_ERR; } if (swPipeBase_create(&_aio_pipe, 0) < 0) { return SW_ERR; } if (swMutex_create(&SwooleAIO.lock, 0) < 0) { swWarn("create mutex lock error"); return SW_ERR; } if (SwooleAIO.thread_num <= 0) { SwooleAIO.thread_num = SW_AIO_THREAD_NUM_DEFAULT; } if (swThreadPool_create(&pool, SwooleAIO.thread_num) < 0) { return SW_ERR; } pool.onTask = swAio_onTask; _pipe_read = _aio_pipe.getFd(&_aio_pipe, 0); _pipe_write = _aio_pipe.getFd(&_aio_pipe, 1); SwooleG.main_reactor->setHandle(SwooleG.main_reactor, SW_FD_AIO, swAio_onCompleted); SwooleG.main_reactor->add(SwooleG.main_reactor, _pipe_read, SW_FD_AIO); if (swThreadPool_run(&pool) < 0) { return SW_ERR; } SwooleAIO.init = 1; return SW_OK; }
int swFactoryThread_start(swFactory *factory) { swFactoryThread *this = factory->object; swThreadParam *param; int i; int ret; pthread_t pidt; ret = swFactory_check_callback(factory); if (ret < 0) { return SW_ERR; } for (i = 0; i < this->writer_num; i++) { #ifdef HAVE_EVENTFD ret = swPipeEventfd_create(&this->writers[i].evfd, 1, 1); #else ret = swPipeBase_create(&this->writers[i].evfd, 1); #endif if (ret < 0) { swTrace("create eventfd fail\n"); return SW_ERR; } param = sw_malloc(sizeof(swThreadParam)); if (param == NULL) { return SW_ERR; } param->object = factory; param->pti = i; if (pthread_create(&pidt, NULL, (void * (*)(void *)) swFactoryThread_writer_loop, (void *) param) < 0) { swTrace("pthread_create fail\n"); return SW_ERR; } if (swRingQueue_init(&this->queues[i], SW_RINGQUEUE_LEN) < 0) { swTrace("create ring queue fail\n"); return SW_ERR; } this->writers[i].ptid = pidt; //SW_START_SLEEP; } return SW_OK; }
int swAioGcc_init(int max_aio_events) { if (swPipeBase_create(&swoole_aio_pipe, 0) < 0) { return SW_ERR; } swSignal_set(SIGIO, swAioGcc_signal_handler, 1, 0); swAioGcc_pipe_read = swoole_aio_pipe.getFd(&swoole_aio_pipe, 0); swAioGcc_pipe_write = swoole_aio_pipe.getFd(&swoole_aio_pipe, 1); SwooleAIO.reactor->setHandle(SwooleAIO.reactor, SW_FD_AIO, swAioGcc_onFinish); SwooleAIO.reactor->add(SwooleAIO.reactor, swAioGcc_pipe_read, SW_FD_AIO); SwooleAIO.callback = swAio_callback_test; SwooleAIO.read = swAioGcc_aio_read; SwooleAIO.write = swAioGcc_write; SwooleAIO.destroy = swAioGcc_destroy; return SW_OK; }
/** * @elem_size 每个item内存块的长度 * @elem_max item最多个数 */ int swChan_create(swChan **chan_addr, void *mem, int mem_size, int elem_max, int elem_size) { int slab_size, ret; bzero(mem, sizeof(swChan)); //初始化内存块 if (mem_size <= 0 || mem == NULL || mem_size < SW_CHAN_MIN_MEM) { swWarn("error: mem_size <= %d or mem == NULL\n", SW_CHAN_MIN_MEM); return SW_ERR; } *chan_addr = mem; swChan *chan = *chan_addr; mem += sizeof(swChan); //去掉swChan结构占用的部分 if (swMutex_create(&chan->lock, 1) < 0) { swWarn("create mutex fail.\n"); return SW_ERR; } if (elem_max == 0) { elem_max = 65535; } #ifdef HAVE_EVENTFD ret = swPipeEventfd_create(&chan->notify_fd, 1, 1); #else ret = swPipeBase_create(&chan->notify_fd, 1); #endif if(ret < 0) { swWarn("create eventfd fail.\n"); return SW_ERR; } slab_size = sizeof(swChanElem)*elem_max; chan->elem_max = elem_max; chan->mem_size = mem_size - slab_size - sizeof(swChan);//允许溢出 chan->elems = (swChanElem *) mem; chan->mem = mem + slab_size; swMemPool_create(&chan->pool, chan->mem, chan->mem_size, elem_size); return SW_OK; }
int swTimer_start(swTimer *timer, int interval_ms) { struct timeval now; if (gettimeofday(&now, NULL) < 0) { swWarn("malloc fail\n"); return SW_ERR; } timer->interval_ms = interval_ms; timer->lasttime = interval_ms; int sec = interval_ms / 1000; int msec = (((float) interval_ms / 1000) - sec) * 1000; #ifdef HAVE_TIMERFD struct itimerspec timer_set; memset(&timer_set, 0, sizeof(timer_set)); timer->fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK | TFD_CLOEXEC); if (timer->fd < 0) { swError("create timerfd fail\n"); return SW_ERR; } timer_set.it_value.tv_sec = now.tv_sec + sec; timer_set.it_value.tv_nsec = now.tv_usec + msec * 1000; timer_set.it_interval.tv_sec = sec; timer_set.it_interval.tv_nsec = msec * 1000 * 1000; if (timerfd_settime(timer->fd, TFD_TIMER_ABSTIME, &timer_set, NULL) == -1) { swWarn("set timer fail\n"); return SW_ERR; } timer->use_pipe = 0; #else struct itimerval timer_set; int ret; //eventfd是2.6.26提供的,timerfd是2.6.27提供的 #ifdef HAVE_EVENTFD ret = swPipeEventfd_create(&timer->pipe, 0, 0); #else ret = swPipeBase_create(&timer->pipe, 0); #endif if (ret < 0) { swWarn("create timer pipe fail"); return SW_ERR; } memset(&timer_set, 0, sizeof(timer_set)); timer_set.it_value.tv_sec = sec; timer_set.it_value.tv_usec = msec * 1000; timer_set.it_interval.tv_sec = sec; timer_set.it_interval.tv_usec = msec * 1000; if (setitimer(ITIMER_REAL, &timer_set, NULL) < 0) { swWarn("set timer fail"); return SW_ERR; } timer->fd = timer->pipe.getFd(&timer->pipe, 0); timer->use_pipe = 1; #endif return SW_OK; }