PTHREAD...ELSTATE(3POSIX)

πŸ’… PROLOG πŸ’…

okay bestie, quick heads up before we dive in: this page is part of the POSIX Programmer's Manual (it's PTHREAD...ELSTATE(3POSIX), section 3POSIX, in case you were wondering where we are). The Linux implementation of this interface may differ β€” so pls go consult the corresponding Linux manual page for the actual Linux tea β€” or honestly, the interface might not even be implemented on Linux at all. πŸ™ˆ Not me warning you up front.


✨ NAME ✨

pthread_setcancelstate, pthread_setcanceltype, pthread_testcancel β€” set cancelability state πŸŽ€


🌸 SYNOPSIS 🌸

#include <pthread.h>

int pthread_setcancelstate(int state, int *oldstate);
int pthread_setcanceltype(int type, int *oldtype);
void pthread_testcancel(void);

πŸ’– DESCRIPTION πŸ’–

So here's the vibe. The pthread_setcancelstate() function shall atomically do two things at once: set the calling thread's cancelability state to the indicated state, and hand you back the previous cancelability state at the location referenced by oldstate. The legal values for state are PTHREAD_CANCEL_ENABLE and PTHREAD_CANCEL_DISABLE. periodt. πŸ’…

The pthread_setcanceltype() function shall also atomically both set the calling thread's cancelability type to the indicated type and return the previous cancelability type at the location referenced by oldtype. The legal values for type are PTHREAD_CANCEL_DEFERRED and PTHREAD_CANCEL_ASYNCHRONOUS. iconic behavior honestly.

Now, the cancelability state and type of any newly created threads β€” including the thread in which main() was first invoked, yes, even that one, the main character πŸ‘‘ β€” shall be PTHREAD_CANCEL_ENABLE and PTHREAD_CANCEL_DEFERRED respectively. That's the default, no cap.

And pthread_testcancel()? It shall create a cancellation point in the calling thread. But lowkey, it shall have no effect if cancelability is disabled. So don't be shocked when nothing happens, bestie. 😭


⭐ RETURN VALUE ⭐

If everything's slay: pthread_setcancelstate() and pthread_setcanceltype() shall return zero. Otherwise, an error number shall be returned to indicate the error. that's it, that's the deal 🫢


πŸ”₯ ERRORS πŸ”₯

The pthread_setcancelstate() function may fail if:

  • EINVAL β€” the specified state is not PTHREAD_CANCEL_ENABLE or PTHREAD_CANCEL_DISABLE.

The pthread_setcanceltype() function may fail if:

  • EINVAL β€” the specified type is not PTHREAD_CANCEL_DEFERRED or PTHREAD_CANCEL_ASYNCHRONOUS.
Also, important, fr fr: these functions shall not return an error code of [EINTR]. Just so we're clear. πŸ’€

The following sections are informative. (i.e. the lore, not the rules ✨)


🌈 EXAMPLES 🌈

None. πŸ’€ yeah. nothing. moving on bestie.


πŸ¦‹ APPLICATION USAGE πŸ¦‹

okay so picture this: you wanna write a signal handler for an asynchronous signal, and you need it to run safely in a cancellable thread. Well, pthread_setcancelstate() must be used to disable cancellation for the duration of any calls that the signal handler makes which are cancellation points. That's the move. πŸ’…

BUT. plot twist. 😭 The standard does not permit strictly conforming applications to call pthread_setcancelstate() from a signal handler, since it is not currently required to be async-signal-safe. The audacity, honestly.

So on implementations where pthread_setcancelstate() is not async-signal-safe, your alternatives are:

  1. make sure the corresponding signals are blocked during execution of functions that are not async-cancel-safe, or
  2. make sure cancellation is disabled during times when those signals could be delivered.

Implementations are strongly encouraged to make pthread_setcancelstate() async-signal-safe. like, please. we're begging. πŸ™


🧚 RATIONALE 🧚

The pthread_setcancelstate() and pthread_setcanceltype() functions control the points at which a thread may be asynchronously canceled. For cancellation control to be usable in a modular fashion, some rules need to be followed. buckle up, we're getting into the lore πŸ“

An object can be considered to be a generalization of a procedure. It is a set of procedures and global variables written as a unit and called by clients not known by the object. Objects may depend on other objects. (objects all the way down, iykyk)

First rule: cancelability should only be disabled on entry to an object, never explicitly enabled. On exit from an object, the cancelability state should always be restored to its value on entry to the object. ✨

Why tho? Modularity argument, here we go: if the client of an object (or the client of an object that uses that object) has disabled cancelability, it's because the client does not want to be concerned about cleaning up if the thread is canceled while executing some sequence of actions. If an object is called in such a state and it enables cancelability and a cancellation request is pending for that thread, then the thread is canceled β€” contrary to the wish of the client that disabled. rude, honestly.

Second rule: the cancelability type may be explicitly set to either deferred or asynchronous upon entry to an object. But, same energy as the cancelability state: on exit from an object, the cancelability type should always be restored to its value on entry to the object. πŸŽ€

Finally: only functions that are cancel-safe may be called from a thread that is asynchronously cancelable. no exceptions bestie, this one's serious πŸ’œ


🌟 FUTURE DIRECTIONS 🌟

The pthread_setcancelstate() function may be added to the table of async-signal-safe functions in Section 2.4.3, Signal Actions. manifesting it for us fr fr πŸ•―οΈβœ¨


πŸ‘‘ SEE ALSO πŸ‘‘

  • pthread_cancel()
  • The Base Definitions volume of POSIX.1‐2017, <pthread.h>

Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


and that's the whole page bestie!! πŸ’–βœ¨ this absolute banger of a manpage is brought to you by IEEE/The Open Group, 2017 β€” yeah, 2017, we're serving vintage POSIX realness πŸŽ€ go forth and set your cancelability state responsibly, mwah πŸ’…πŸ¦‹