您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關如何進行基于linuxthreads2.0.1線程源碼分析cancel.c,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
cancel.c實現了線程的是否可取消,取消類型,取消線程,設置線程退出時需要執行的函數列表等功能。
/* Thread cancellation */
#include <errno.h>
#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了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。