溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何進(jìn)行基于linuxthreads2.0.1線程源碼分析cancel.c

發(fā)布時(shí)間:2021-12-09 09:32:26 來(lái)源:億速云 閱讀:126 作者:柒染 欄目:大數(shù)據(jù)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)如何進(jìn)行基于linuxthreads2.0.1線程源碼分析cancel.c,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

cancel.c實(shí)現(xiàn)了線程的是否可取消,取消類型,取消線程,設(shè)置線程退出時(shí)需要執(zhí)行的函數(shù)列表等功能。

   
     
 
    
    


/* Thread cancellation */

#include <errno.h>
#include "pthread.h"
#include "internals.h"
#include "restart.h"

/*
修改線程的可取消屬性。有一個(gè)取消點(diǎn)
取消狀態(tài)分為可取消,不可取消
   不可取消的時(shí)候,收到取消信號(hào),忽略
   可取消的時(shí)候,收到取消信號(hào)的時(shí)候,根據(jù)取消類型做處理。
     立即處理
     不立刻處理,到下一個(gè)取消點(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;
}
// 見(jiàn)上一個(gè)函數(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ā)送取消請(qǐng)求,線程收到該信號(hào)是否處理,怎么處理取決于線程本身對(duì)于取消的相關(guān)配置
int pthread_cancel(pthread_t thread)
{
 thread->p_canceled = 1;
 kill(thread->p_pid, PTHREAD_SIG_CANCEL);
 return 0;
}
// 設(shè)置一個(gè)取消點(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);
}
// 鏈表中新增一個(gè)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;
}
// 刪除一個(gè)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;
}
// 新增一個(gè)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ù)配套。刪除一個(gè)clean節(jié)點(diǎn),execute控制是否需要執(zhí)行刪除的這個(gè)節(jié)點(diǎn),恢復(fù)線程的取消類型,是一個(gè)有取消點(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);
}
// 線程退出的時(shí)候(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)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI