|
|
|
@ -0,0 +1,122 @@
|
|
|
|
|
// 版权声明,表明这段代码是Sogou, Inc.在2019年版权所有的,并且这段代码是在Apache License 2.0下授权的。
|
|
|
|
|
/*
|
|
|
|
|
Copyright (c) 2019 Sogou, Inc.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
|
|
|
|
|
Author: Xie Han (xiehan@sogou-inc.com)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stddef.h> // 包含标准定义头文件,比如NULL和size_t的定义
|
|
|
|
|
#include <stdlib.h> // 包含标准库头文件,比如malloc和free函数的定义
|
|
|
|
|
#include "poller.h" // 包含poller的头文件,定义了单线程poller的接口
|
|
|
|
|
#include "mpoller.h" // 包含多线程poller的头文件,定义了mpoller的接口
|
|
|
|
|
|
|
|
|
|
// 外部声明的函数,用于创建和销毁poller对象。
|
|
|
|
|
extern poller_t* __poller_create(void**, const struct poller_params*);
|
|
|
|
|
extern void __poller_destroy(poller_t*);
|
|
|
|
|
|
|
|
|
|
// 静态函数,用于创建多线程poller对象。
|
|
|
|
|
static int __mpoller_create(const struct poller_params* params,
|
|
|
|
|
mpoller_t* mpoller)
|
|
|
|
|
{
|
|
|
|
|
void** nodes_buf = (void**)calloc(params->max_open_files, sizeof(void*)); // 分配内存用于保存poller对象
|
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
|
|
if (nodes_buf) // 如果内存分配成功
|
|
|
|
|
{
|
|
|
|
|
for (i = 0; i < mpoller->nthreads; i++) // 循环创建多个poller对象
|
|
|
|
|
{
|
|
|
|
|
mpoller->poller[i] = __poller_create(nodes_buf, params); // 创建单个poller
|
|
|
|
|
if (!mpoller->poller[i]) // 如果创建失败
|
|
|
|
|
break; // 退出循环
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i == mpoller->nthreads) // 如果所有poller对象都创建成功
|
|
|
|
|
{
|
|
|
|
|
mpoller->nodes_buf = nodes_buf; // 保存nodes_buf指针
|
|
|
|
|
return 0; // 返回成功
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (i > 0) // 如果创建失败,销毁已经创建的poller对象
|
|
|
|
|
__poller_destroy(mpoller->poller[--i]);
|
|
|
|
|
|
|
|
|
|
free(nodes_buf); // 释放nodes_buf内存
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1; // 返回失败
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 函数用于创建多线程poller对象。
|
|
|
|
|
mpoller_t* mpoller_create(const struct poller_params* params, size_t nthreads)
|
|
|
|
|
{
|
|
|
|
|
mpoller_t* mpoller; // 声明mpoller对象指针
|
|
|
|
|
size_t size; // 声明size变量,用于计算所需内存大小
|
|
|
|
|
|
|
|
|
|
if (nthreads == 0) // 如果线程数为0,则默认为1
|
|
|
|
|
nthreads = 1;
|
|
|
|
|
|
|
|
|
|
size = offsetof(mpoller_t, poller) + nthreads * sizeof(void*); // 计算所需内存大小
|
|
|
|
|
mpoller = (mpoller_t*)malloc(size); // 分配内存
|
|
|
|
|
if (mpoller) // 如果内存分配成功
|
|
|
|
|
{
|
|
|
|
|
mpoller->nthreads = (unsigned int)nthreads; // 设置线程数
|
|
|
|
|
if (__mpoller_create(params, mpoller) >= 0) // 如果创建成功
|
|
|
|
|
return mpoller; // 返回mpoller对象
|
|
|
|
|
|
|
|
|
|
free(mpoller); // 如果创建失败,释放内存
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL; // 返回NULL
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 函数用于启动多线程poller对象。
|
|
|
|
|
int mpoller_start(mpoller_t* mpoller)
|
|
|
|
|
{
|
|
|
|
|
size_t i; // 循环变量
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < mpoller->nthreads; i++) // 循环启动每个poller线程
|
|
|
|
|
{
|
|
|
|
|
if (poller_start(mpoller->poller[i]) < 0) // 如果启动失败
|
|
|
|
|
break; // 退出循环
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i == mpoller->nthreads) // 如果所有线程都启动成功
|
|
|
|
|
return 0; // 返回成功
|
|
|
|
|
|
|
|
|
|
while (i > 0) // 如果启动失败,停止已经启动的线程
|
|
|
|
|
poller_stop(mpoller->poller[--i]);
|
|
|
|
|
|
|
|
|
|
return -1; // 返回失败
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 函数用于停止多线程poller对象。
|
|
|
|
|
void mpoller_stop(mpoller_t* mpoller)
|
|
|
|
|
{
|
|
|
|
|
size_t i; // 循环变量
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < mpoller->nthreads; i++) // 循环停止每个poller线程
|
|
|
|
|
poller_stop(mpoller->poller[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 函数用于销毁多线程poller对象。
|
|
|
|
|
void mpoller_destroy(mpoller_t* mpoller)
|
|
|
|
|
{
|
|
|
|
|
size_t i; // 循环变量
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < mpoller->nthreads; i++) // 循环销毁每个poller对象
|
|
|
|
|
__poller_destroy(mpoller->poller[i]);
|
|
|
|
|
|
|
|
|
|
free(mpoller->nodes_buf); // 释放nodes_buf内存
|
|
|
|
|
free(mpoller); // 释放mpoller对象内存
|
|
|
|
|
}
|