如何进行基于linuxthreads2.0.1线程源码分析cancel.c
发表于:2024-10-27 作者:千家信息网编辑
千家信息网最后更新 2024年10月27日,这期内容当中小编将会给大家带来有关如何进行基于linuxthreads2.0.1线程源码分析cancel.c,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。cance
千家信息网最后更新 2024年10月27日如何进行基于linuxthreads2.0.1线程源码分析cancel.c
这期内容当中小编将会给大家带来有关如何进行基于linuxthreads2.0.1线程源码分析cancel.c,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
cancel.c实现了线程的是否可取消,取消类型,取消线程,设置线程退出时需要执行的函数列表等功能。
/* Thread cancellation */
#include
#include "pthread.h"
#include "internals.h"
#include "restart.h"
/*
修改线程的可取消属性。有一个取消点
取消状态分为可取消,不可取消
不可取消的时候,收到取消信号,忽略
可取消的时候,收到取消信号的时候,根据取消类型做处理。
立即处理
不立刻处理,到下一个取消点,判定线程的状态的取消类型再处理
*/
int pthread_setcancelstate(int state, int * oldstate)
{
pthread_t self = thread_self();
if (state < PTHREAD_CANCEL_ENABLE || state > PTHREAD_CANCEL_DISABLE)
return EINVAL;
// 保存旧的状态
if (oldstate != NULL) *oldstate = self->p_cancelstate;
// 设置新的状态
self->p_cancelstate = state;
// 判断线程是否被取消了,并且当前被设置成可取消状态,并且是需要马上处理的,则直接退出
if (self->p_canceled &&
self->p_cancelstate == PTHREAD_CANCEL_ENABLE &&
self->p_canceltype == PTHREAD_CANCEL_ASYNCHRONOUS)
pthread_exit(PTHREAD_CANCELED);
return 0;
}
// 见上一个函数
int pthread_setcanceltype(int type, int * oldtype)
{
pthread_t self = thread_self();
if (type < PTHREAD_CANCEL_DEFERRED || type > PTHREAD_CANCEL_ASYNCHRONOUS)
return EINVAL;
if (oldtype != NULL) *oldtype = self->p_canceltype;
self->p_canceltype = type;
if (self->p_canceled &&
self->p_cancelstate == PTHREAD_CANCEL_ENABLE &&
self->p_canceltype == PTHREAD_CANCEL_ASYNCHRONOUS)
pthread_exit(PTHREAD_CANCELED);
return 0;
}
// 给线程发送取消请求,线程收到该信号是否处理,怎么处理取决于线程本身对于取消的相关配置
int pthread_cancel(pthread_t thread)
{
thread->p_canceled = 1;
kill(thread->p_pid, PTHREAD_SIG_CANCEL);
return 0;
}
// 设置一个取消点
void pthread_testcancel(void)
{
pthread_t self = thread_self();
// 判断线程是不是已经被取消,并且是可取消的,则退出
if (self->p_canceled && self->p_cancelstate == PTHREAD_CANCEL_ENABLE)
pthread_exit(PTHREAD_CANCELED);
}
// 链表中新增一个clean函数
void _pthread_cleanup_push(struct _pthread_cleanup_buffer * buffer,
void (*routine)(void *), void * arg)
{
pthread_t self = thread_self();
buffer->routine = routine;
buffer->arg = arg;
// 头插法
buffer->prev = self->p_cleanup;
self->p_cleanup = buffer;
}
// 删除一个clean节点,execute判断是否需要执行
void _pthread_cleanup_pop(struct _pthread_cleanup_buffer * buffer,
int execute)
{
pthread_t self = thread_self();
if (execute) buffer->routine(buffer->arg);
self->p_cleanup = buffer->prev;
}
// 新增一个clean节点,保存旧的取消类型,设置新的取消类型为PTHREAD_CANCEL_DEFERRED
void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer * buffer,
void (*routine)(void *), void * arg)
{
pthread_t self = thread_self();
buffer->routine = routine;
buffer->arg = arg;
buffer->canceltype = self->p_canceltype;
buffer->prev = self->p_cleanup;
self->p_canceltype = PTHREAD_CANCEL_DEFERRED;
self->p_cleanup = buffer;
}
// 和上面的函数配套。删除一个clean节点,execute控制是否需要执行删除的这个节点,恢复线程的取消类型,是一个有取消点的函数
void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer * buffer,
int execute)
{
pthread_t self = thread_self();
if (execute) buffer->routine(buffer->arg);
self->p_cleanup = buffer->prev;
self->p_canceltype = buffer->canceltype;
if (self->p_canceled &&
self->p_cancelstate == PTHREAD_CANCEL_ENABLE &&
self->p_canceltype == PTHREAD_CANCEL_ASYNCHRONOUS)
pthread_exit(PTHREAD_CANCELED);
}
// 线程退出的时候(pthread_exit)调用执行clean链表的节点
void __pthread_perform_cleanup(void)
{
pthread_t self = thread_self();
struct _pthread_cleanup_buffer * c;
for (c = self->p_cleanup; c != NULL; c = c->prev) c->routine(c->arg);
}
#ifndef PIC
/* We need a hook to force the cancelation wrappers to be linked in when
static libpthread is used. */
extern const int __pthread_provide_wrappers;
static const int * const __pthread_require_wrappers =
&__pthread_provide_wrappers;
#endif
上述就是小编为大家分享的如何进行基于linuxthreads2.0.1线程源码分析cancel.c了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。
线程
处理
类型
函数
状态
节点
分析
时候
信号
源码
内容
专业
中小
内容丰富
功能
取决于
就是
属性
文章
更多
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
java软件开发加班情况
数据库创建表的常用方法有
计算机网络技术基础第四章答案
枣庄智慧社区软件开发
网络安全画手绘一等奖
重庆机器视觉软件开发
普陀区智能软件开发加工厂
象山应用软件开发教程
幼儿网络安全教育部讲座
哪些网络安全培训好
国开数据库基础期末考试答案
有关的网络安全的论文
软件开发项目会议议程模板
广西常规软件开发公司
蔚来应用软件开发岗位
嘉定区网络金融网络技术服务热线
电脑服务器连接失败
普陀区网络技术服务介绍
网络技术与安全心得
app软件开发实名认证
项目软件开发需要做什么
暴雪服务器服务器检索内容失败
数据源没有数据库字段
服务器天天中毒怎么办
阿里网络技术面经
天津诚信网络技术服务售价
多盟智胜网络技术排名
数据库采集使用的技术
cc服务器显卡
云服务器无法上网