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; }
int swFactoryThread_create(swFactory *factory, int worker_num) { swFactoryThread *object; swServer *serv = factory->ptr; object = sw_calloc(worker_num, sizeof(swFactoryThread)); if (object == NULL) { swWarn("malloc[0] failed"); return SW_ERR; } if (swThreadPool_create(&object->workers, worker_num) < 0) { return SW_ERR; } int i; swReactorThread *thread; for (i = 0; i < serv->reactor_num; i++) { thread = swServer_get_thread(serv, i); swMutex_create(&thread->lock, 0); } object->worker_num = worker_num; factory->object = object; factory->dispatch = swFactoryThread_dispatch; factory->finish = swFactoryThread_finish; factory->end = swFactory_end; factory->start = swFactoryThread_start; factory->shutdown = swFactoryThread_shutdown; factory->notify = swFactory_notify; factory->onTask = NULL; factory->onFinish = NULL; object->workers.onStart = swFactoryThread_onStart; object->workers.onStop = swFactoryThread_onStop; object->workers.onTask = swFactoryThread_onTask; object->workers.ptr1 = factory->ptr; object->workers.ptr2 = factory; return SW_OK; }