1 Star 0 Fork 6

northqd/async

forked from 方瑾/async 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
io.c 2.38 KB
一键复制 编辑 原始数据 按行查看 历史
方瑾 提交于 2019-12-26 11:32 . 异常处理
#include "io.h"
void creatIoServicePthread(IoService service) {
service->serviceThreadInfo = newServiceThreadInfo();
if (service->serviceThreadInfo == NULL) {
return;
}
pthread_cond_init(&(service->serviceThreadInfo->notifyCond), NULL);
pthread_cond_init(&(service->serviceThreadInfo->waitTimeoutCond), NULL);
pthread_mutex_init(&(service->serviceThreadInfo->notify), NULL);
pthread_mutex_init(&(service->serviceThreadInfo->undeal), NULL);
pthread_mutex_init(&(service->serviceThreadInfo->finish), NULL);
pthread_mutex_init(&(service->serviceThreadInfo->waitTimeout), NULL);
pthread_mutex_init(&(service->serviceThreadInfo->event), NULL);
pthread_create(&(service->serviceThreadInfo->pid), NULL, runIO, service);
}
void runIO(let args) {
// 允许退出线程
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
// 异步取消,线程接到取消信号后立即退出
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
IoService service = (IoService)args;
puts("io server start ....");
while (True) {
// 检查取消点
pthread_testcancel();
if (service->undealQueue->length != 0) {
// 从待处理中取出任务
pthread_mutex_lock(&(service->serviceThreadInfo->undeal));
IoEvent ioEvent = (IoEvent)service->undealQueue->shift(service->undealQueue);
pthread_mutex_unlock(&(service->serviceThreadInfo->undeal));
// 处理io事件
_sleep(100);
switch (ioEvent->eventType) {
case AccessFileEvent:
ioEvent->result = access(ioEvent->path, ioEvent->otherParams) == 0 ? True : False;
ioEvent->error = NULL;
break;
default: break;
}
pthread_mutex_lock(&(service->serviceThreadInfo->waitTimeout));
pthread_cond_signal(&(service->serviceThreadInfo->waitTimeoutCond), &(service->serviceThreadInfo->waitTimeout));
pthread_mutex_unlock(&(service->serviceThreadInfo->waitTimeout));
// 已完成的任务放到已完成队列中
pthread_mutex_lock(&(service->serviceThreadInfo->finish));
service->finishQueue->push(service->finishQueue, ioEvent);
pthread_mutex_unlock(&(service->serviceThreadInfo->finish));
}
else {
// 没有待处理任务,则等待主线程唤醒
pthread_mutex_lock(&(service->serviceThreadInfo->notify));
pthread_cond_wait(&(service->serviceThreadInfo->notifyCond), &(service->serviceThreadInfo->notify));
pthread_mutex_unlock(&(service->serviceThreadInfo->notify));
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/northqd2018/async.git
git@gitee.com:northqd2018/async.git
northqd2018
async
async
master

搜索帮助