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; }
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; }