這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)如何進(jìn)行基于linuxthreads2.0.1線程源碼分析cancel.c,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
公司主營業(yè)務(wù):網(wǎng)站設(shè)計制作、成都網(wǎng)站制作、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出廣平免費(fèi)做網(wǎng)站回饋大家。
cancel.c實(shí)現(xiàn)了線程的是否可取消,取消類型,取消線程,設(shè)置線程退出時需要執(zhí)行的函數(shù)列表等功能。
/* Thread cancellation */
#include
#include "pthread.h"
#include "internals.h"
#include "restart.h"
/*
修改線程的可取消屬性。有一個取消點(diǎn)
取消狀態(tài)分為可取消,不可取消
不可取消的時候,收到取消信號,忽略
可取消的時候,收到取消信號的時候,根據(jù)取消類型做處理。
立即處理
不立刻處理,到下一個取消點(diǎn),判定線程的狀態(tài)的取消類型再處理
*/
int pthread_setcancelstate(int state, int * oldstate)
{
pthread_t self = thread_self();
if (state < PTHREAD_CANCEL_ENABLE || state > PTHREAD_CANCEL_DISABLE)
return EINVAL;
// 保存舊的狀態(tài)
if (oldstate != NULL) *oldstate = self->p_cancelstate;
// 設(shè)置新的狀態(tài)
self->p_cancelstate = state;
// 判斷線程是否被取消了,并且當(dāng)前被設(shè)置成可取消狀態(tài),并且是需要馬上處理的,則直接退出
if (self->p_canceled &&
self->p_cancelstate == PTHREAD_CANCEL_ENABLE &&
self->p_canceltype == PTHREAD_CANCEL_ASYNCHRONOUS)
pthread_exit(PTHREAD_CANCELED);
return 0;
}
// 見上一個函數(shù)
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;
}
// 給線程發(fā)送取消請求,線程收到該信號是否處理,怎么處理取決于線程本身對于取消的相關(guān)配置
int pthread_cancel(pthread_t thread)
{
thread->p_canceled = 1;
kill(thread->p_pid, PTHREAD_SIG_CANCEL);
return 0;
}
// 設(shè)置一個取消點(diǎn)
void pthread_testcancel(void)
{
pthread_t self = thread_self();
// 判斷線程是不是已經(jīng)被取消,并且是可取消的,則退出
if (self->p_canceled && self->p_cancelstate == PTHREAD_CANCEL_ENABLE)
pthread_exit(PTHREAD_CANCELED);
}
// 鏈表中新增一個clean函數(shù)
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節(jié)點(diǎn),execute判斷是否需要執(zhí)行
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節(jié)點(diǎn),保存舊的取消類型,設(shè)置新的取消類型為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;
}
// 和上面的函數(shù)配套。刪除一個clean節(jié)點(diǎn),execute控制是否需要執(zhí)行刪除的這個節(jié)點(diǎn),恢復(fù)線程的取消類型,是一個有取消點(diǎn)的函數(shù)
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)調(diào)用執(zhí)行clean鏈表的節(jié)點(diǎn)
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
上述就是小編為大家分享的如何進(jìn)行基于linuxthreads2.0.1線程源碼分析cancel.c了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。