Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze cancel.c based on Linux thread 2.0.1 thread source code

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to analyze cancel.c based on Linux thread 2.0.1 thread source code. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Cancel.c implements the functions of whether the thread can be cancelled, cancel the type, cancel the thread, and set the list of functions that need to be executed when the thread exits.

/ * Thread cancellation * /

# include

# include "pthread.h"

# include "internals.h"

# include "restart.h"

/ *

Modifies the cancelable property of a thread. There is a cancellation point.

Cancellation status is divided into cancelable and non-cancelable

When it cannot be cancelled, the cancellation signal is received and ignored.

When it can be canceled, when the cancellation signal is received, it will be processed according to the cancellation type.

Deal with immediately

Do not deal with it immediately, and then process the cancellation type that determines the state of the thread at the next cancellation point.

, /

Int pthread_setcancelstate (int state, int * oldstate)

{

Pthread_t self = thread_self ()

If (state

< PTHREAD_CANCEL_ENABLE || state >

PTHREAD_CANCEL_DISABLE)

Return EINVAL

/ / Save the old state

If (oldstate! = NULL) * oldstate = self- > p_cancelstate

/ / set the new status

Self- > p_cancelstate = state

/ / determine whether the thread has been cancelled, is currently set to a cancelable state, and needs to be processed immediately, then exit directly

If (self- > p_canceled & &

Self- > p_cancelstate = = PTHREAD_CANCEL_ENABLE & &

Self- > p_canceltype = = PTHREAD_CANCEL_ASYNCHRONOUS)

Pthread_exit (PTHREAD_CANCELED)

Return 0

}

/ / see the previous function

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

}

/ / send a cancellation request to the thread. Whether the thread receives the signal and how to handle it depends on the thread's own configuration for cancellation.

Int pthread_cancel (pthread_t thread)

{

Thread- > p_canceled = 1

Kill (thread- > p_pid, PTHREAD_SIG_CANCEL)

Return 0

}

/ / set a cancellation point

Void pthread_testcancel (void)

{

Pthread_t self = thread_self ()

/ / determine whether the thread has been cancelled and is cancelable, then exit

If (self- > p_canceled & & self- > p_cancelstate = = PTHREAD_CANCEL_ENABLE)

Pthread_exit (PTHREAD_CANCELED)

}

/ / add a clean function to the linked list

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

/ / head insertion

Buffer- > prev = self- > p_cleanup

Self- > p_cleanup = buffer

}

/ / Delete a clean node, and execute determines whether it needs to be executed.

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

}

/ / add a clean node, save the old cancellation type, and set the new cancellation type to 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

}

/ / match the above function. Delete a clean node, execute controls whether the deleted node needs to be performed, and restores the cancellation type of the thread, which is a function with a cancellation point.

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)

}

/ / call the node that executes the clean linked list when the thread exits (pthread_exit)

Void _ pthread_perform_cleanup (void)

{

Pthread_t self = thread_self ()

Struct _ pthread_cleanup_buffer * c

For (c = self- > pendant 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

The above is the editor for you to share how to analyze cancel.c based on Linux thread 2.0.1 thread source code, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report