1.\" $OpenBSD: pthread_testcancel.3,v 1.14 2012/03/22 01:15:37 guenther Exp $ 2.\" 3.\" 4.\" David Leonard, 1999. Public Domain. 5.\" 6.Dd $Mdocdate: March 22 2012 $ 7.Dt PTHREAD_TESTCANCEL 3 8.Os 9.Sh NAME 10.Nm pthread_setcancelstate , 11.Nm pthread_setcanceltype , 12.Nm pthread_testcancel 13.Nd set cancelability state 14.Sh SYNOPSIS 15.Fd #include <pthread.h> 16.Ft int 17.Fn pthread_setcancelstate "int state" "int *oldstate" 18.Ft int 19.Fn pthread_setcanceltype "int type" "int *oldtype" 20.Ft void 21.Fn pthread_testcancel "void" 22.Sh DESCRIPTION 23The 24.Fn pthread_setcancelstate 25function atomically both sets the calling thread's cancelability state 26to the indicated 27.Fa state 28and, if 29.Fa oldstate 30is not 31.Dv NULL , 32returns the previous cancelability state at the location referenced by 33.Fa oldstate . 34Legal values for 35.Fa state 36are 37.Dv PTHREAD_CANCEL_ENABLE 38and 39.Dv PTHREAD_CANCEL_DISABLE . 40.Pp 41The 42.Fn pthread_setcanceltype 43function atomically both sets the calling thread's cancelability type 44to the indicated 45.Fa type 46and, if 47.Fa oldtype 48is not 49.Dv NULL , 50returns the previous cancelability type at the location referenced by 51.Fa oldtype . 52Legal values for 53.Fa type 54are 55.Dv PTHREAD_CANCEL_DEFERRED 56and 57.Dv PTHREAD_CANCEL_ASYNCHRONOUS . 58.Pp 59The cancelability state and type of any newly created threads, including the 60thread in which 61.Fn main 62was first invoked, are 63.Dv PTHREAD_CANCEL_ENABLE 64and 65.Dv PTHREAD_CANCEL_DEFERRED 66respectively. 67.Pp 68The 69.Fn pthread_testcancel 70function creates a cancellation point in the calling thread. 71The 72.Fn pthread_testcancel 73function has no effect if cancelability is disabled. 74.Ss Cancelability States 75The cancelability state of a thread determines the action taken upon 76receipt of a cancellation request. 77The thread may control cancellation in a number of ways. 78.Pp 79Each thread maintains its own 80.Dq cancelability state 81which may be encoded in two bits: 82.Bl -hang 83.It Em Cancelability Enable 84When cancelability is 85.Dv PTHREAD_CANCEL_DISABLE , 86cancellation requests against the target thread are held pending. 87.It Em Cancelability Type 88When cancelability is enabled and the cancelability type is 89.Dv PTHREAD_CANCEL_ASYNCHRONOUS , 90new or pending cancellation requests may be acted upon at any time. 91When cancelability is enabled and the cancelability type is 92.Dv PTHREAD_CANCEL_DEFERRED , 93cancellation requests are held pending until a cancellation point (see 94below) is reached. 95If cancelability is disabled, the setting of the 96cancelability type has no immediate effect as all cancellation requests 97are held pending; however, once cancelability is enabled again the new 98type will be in effect. 99.El 100.Ss Cancellation Points 101Cancellation points will occur when a thread is executing the following 102base interfaces: 103.Fn accept , 104.Fn close , 105.Fn connect , 106.Fn creat , 107.Fn fcntl "F_SETLKW" , 108.Fn fsync , 109.Fn lockf , 110.Fn msgrcv , 111.Fn msgsnd , 112.Fn msync , 113.Fn nanosleep , 114.Fn open , 115.Fn openat , 116.Fn pause , 117.Fn poll , 118.Fn pread , 119.Fn pthread_cond_timedwait , 120.Fn pthread_cond_wait , 121.Fn pthread_join , 122.Fn pthread_testcancel , 123.Fn read , 124.Fn readv , 125.Fn recv , 126.Fn recvfrom , 127.Fn recvmsg , 128.Fn select , 129.Fn sem_timedwait , 130.Fn sem_wait , 131.Fn send , 132.Fn sendmsg , 133.Fn sendto , 134.Fn sigsuspend , 135.Fn sigwait , 136.Fn sleep , 137.Fn system , 138.Fn tcdrain , 139.Fn wait , 140.Fn waitpid , 141.Fn write , 142.Fn writev . 143.Pp 144In addition, 145cancellation points will occur when a thread is executing the following 146extension interfaces: 147.Fn closefrom , 148.Fn preadv , 149.Fn pwritev , 150.Fn wait4 . 151.Sh RETURN VALUES 152If successful, the 153.Fn pthread_setcancelstate 154and 155.Fn pthread_setcanceltype 156functions will return zero. 157Otherwise, an error number shall be returned to indicate the error. 158.Pp 159The 160.Fn pthread_setcancelstate 161and 162.Fn pthread_setcanceltype 163functions are used to control the points at which a thread may be 164asynchronously cancelled. 165For cancellation control to be usable in modular 166fashion, some rules must be followed. 167.Pp 168For purposes of this discussion, consider an object to be a generalization 169of a procedure. 170It is a set of procedures and global variables written as 171a unit and called by clients not known by the object. 172Objects may depend on other objects. 173.Pp 174First, cancelability should only be disabled on entry to an object, never 175explicitly enabled. 176On exit from an object, the cancelability state should 177always be restored to its value on entry to the object. 178.Pp 179This follows from a modularity argument: if the client of an object (or the 180client of an object that uses that object) has disabled cancelability, it is 181because the client doesn't want to have to worry about how to clean up if the 182thread is cancelled while executing some sequence of actions. 183If an object 184is called in such a state and it enables cancelability and a cancellation 185request is pending for that thread, then the thread will be cancelled, 186contrary to the wish of the client that disabled. 187.Pp 188Second, the cancelability type may be explicitly set to either 189.Em deferred 190or 191.Em asynchronous 192upon entry to an object. 193But as with the cancelability state, on exit from 194an object that cancelability type should always be restored to its value on 195entry to the object. 196.Pp 197Finally, only functions that are cancel-safe may be called from a thread that 198is asynchronously cancelable. 199.Sh ERRORS 200The function 201.Fn pthread_setcancelstate 202may fail with: 203.Bl -tag -width Er 204.It Bq Er EINVAL 205The specified state is not 206.Dv PTHREAD_CANCEL_ENABLE 207or 208.Dv PTHREAD_CANCEL_DISABLE . 209.El 210.Pp 211The function 212.Fn pthread_setcanceltype 213may fail with: 214.Bl -tag -width Er 215.It Bq Er EINVAL 216The specified state is not 217.Dv PTHREAD_CANCEL_DEFERRED 218or 219.Dv PTHREAD_CANCEL_ASYNCHRONOUS . 220.El 221.Sh SEE ALSO 222.Xr pthread_cancel 3 223.Sh STANDARDS 224.Fn pthread_testcancel 225conforms to 226.St -p1003.1-96 227