int swWorker_create(swWorker *worker) { /** * Create shared memory storage */ void *store = sw_shm_malloc(SwooleG.serv->buffer_output_size); if (store == NULL) { swWarn("malloc for worker->store failed."); return SW_ERR; } swPipe *worker_notify = sw_malloc(sizeof(swPipe)); if (worker_notify == NULL) { swWarn("malloc for worker->notify failed."); sw_shm_free(store); return SW_ERR; } /** * Create notify pipe */ if (swPipeNotify_auto(worker_notify, 1, 0)) { sw_shm_free(store); sw_free(worker_notify); return SW_ERR; } worker->notify = worker_notify; worker->store.ptr = store; worker->store.lock = 0; return SW_OK; }
int swAioLinux_init(int max_aio_events) { swoole_aio_context = 0; if (io_setup(SW_AIO_MAX_EVENTS, &swoole_aio_context) < 0) { swWarn("io_setup() failed. Error: %s[%d]", strerror(errno), errno); return SW_ERR; } if (swPipeNotify_auto(&swoole_aio_pipe, 0, 0) < 0) { return SW_ERR; } swoole_aio_eventfd = swoole_aio_pipe.getFd(&swoole_aio_pipe, 0); SwooleG.main_reactor->setHandle(SwooleG.main_reactor, SW_FD_AIO, swAioLinux_onFinish); SwooleG.main_reactor->add(SwooleG.main_reactor, swoole_aio_eventfd, SW_FD_AIO); SwooleAIO.callback = swAio_callback_test; SwooleAIO.destroy = swAioLinux_destroy; SwooleAIO.read = swAioLinux_read; SwooleAIO.write = swAioLinux_write; return SW_OK; }
/** * create timer */ int swTimer_init(int interval, int use_pipe) { swTimer *timer = &SwooleG.timer; timer->interval = interval; timer->lasttime = interval; #ifndef HAVE_TIMERFD SwooleG.use_timerfd = 0; #endif timer->list = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, free); if (!timer->list) { return SW_ERR; } if (SwooleG.use_timerfd) { if (swTimer_timerfd_set(timer, interval) < 0) { return SW_ERR; } timer->use_pipe = 0; } else { if (use_pipe) { if (swPipeNotify_auto(&timer->pipe, 0, 0) < 0) { return SW_ERR; } timer->fd = timer->pipe.getFd(&timer->pipe, 0); timer->use_pipe = 1; } else { timer->fd = 1; timer->use_pipe = 0; } if (swTimer_signal_set(timer, interval) < 0) { return SW_ERR; } swSignal_add(SIGALRM, swTimer_signal_handler); } if (timer->fd > 1) { SwooleG.main_reactor->setHandle(SwooleG.main_reactor, SW_FD_TIMER, swTimer_event_handler); SwooleG.main_reactor->add(SwooleG.main_reactor, SwooleG.timer.fd, SW_FD_TIMER); } timer->add = swTimer_add; timer->del = swTimer_del; timer->select = swTimer_select; timer->free = swTimer_free; return SW_OK; }
/** * 创建定时器 */ int swTimer_create(swTimer *timer, int interval) { timer->interval = interval; timer->lasttime = interval; #if defined(HAVE_TIMERFD) && SW_WORKER_IPC_MODE == 1 if (swTimer_timerfd_set(timer, interval) < 0) { return SW_ERR; } timer->use_pipe = 0; #else //can not use timerfd #if SW_WORKER_IPC_MODE == 2 timer->fd = 1; #else if (swPipeNotify_auto(&timer->pipe, 0, 0) < 0) { return SW_ERR; } timer->fd = timer->pipe.getFd(&timer->pipe, 0); timer->use_pipe = 1; #endif if (swTimer_signal_set(timer, interval) < 0) { return SW_ERR; } //end #endif return SW_OK; }
/** * create timer */ int swTimer_create(swTimer *timer, int interval, int use_pipe) { timer->interval = interval; timer->lasttime = interval; #ifndef HAVE_TIMERFD SwooleG.use_timerfd = 0; #endif timer->list = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N); if (!timer->list) { return SW_ERR; } if (SwooleG.use_timerfd) { if (swTimer_timerfd_set(timer, interval) < 0) { return SW_ERR; } timer->use_pipe = 0; } else { if (use_pipe) { if (swPipeNotify_auto(&timer->pipe, 0, 0) < 0) { return SW_ERR; } timer->fd = timer->pipe.getFd(&timer->pipe, 0); timer->use_pipe = 1; } else { timer->fd = 1; timer->use_pipe = 0; } if (swTimer_signal_set(timer, interval) < 0) { return SW_ERR; } } 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++) { if (swPipeNotify_auto(&this->writers[i].evfd, 1, 1) < 0) { swWarn("create eventfd fail"); 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; }