1.\" $FreeBSD$ 2.Dd March 29, 2015 3.Dt PTHREAD_TESTCANCEL 3 4.Os 5.Sh NAME 6.Nm pthread_setcancelstate , 7.Nm pthread_setcanceltype , 8.Nm pthread_testcancel 9.Nd set cancelability state 10.Sh LIBRARY 11.Lb libpthread 12.Sh SYNOPSIS 13.In pthread.h 14.Ft int 15.Fn pthread_setcancelstate "int state" "int *oldstate" 16.Ft int 17.Fn pthread_setcanceltype "int type" "int *oldtype" 18.Ft void 19.Fn pthread_testcancel "void" 20.Sh DESCRIPTION 21The 22.Fn pthread_setcancelstate 23function atomically both sets the calling thread's cancelability state 24to the indicated 25.Fa state 26and, if 27.Fa oldstate 28is not 29.Dv NULL , 30returns the previous cancelability state at the location referenced by 31.Fa oldstate . 32Legal values for 33.Fa state 34are 35.Dv PTHREAD_CANCEL_ENABLE 36and 37.Dv PTHREAD_CANCEL_DISABLE . 38.Pp 39The 40.Fn pthread_setcanceltype 41function atomically both sets the calling thread's cancelability type 42to the indicated 43.Fa type 44and, if 45.Fa oldtype 46is not 47.Dv NULL , 48returns the previous cancelability type at the location referenced by 49.Fa oldtype . 50Legal values for 51.Fa type 52are 53.Dv PTHREAD_CANCEL_DEFERRED 54and 55.Dv PTHREAD_CANCEL_ASYNCHRONOUS . 56.Pp 57The cancelability state and type of any newly created threads, including the 58thread in which 59.Fn main 60was first invoked, are 61.Dv PTHREAD_CANCEL_ENABLE 62and 63.Dv PTHREAD_CANCEL_DEFERRED 64respectively. 65.Pp 66The 67.Fn pthread_testcancel 68function creates a cancellation point in the calling thread. 69The 70.Fn pthread_testcancel 71function has no effect if cancelability is disabled. 72.Pp 73.Ss Cancelability States 74The cancelability state of a thread determines the action taken upon 75receipt of a cancellation request. 76The thread may control cancellation in 77a 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 102functions: 103.Fn accept , 104.Fn accept4 , 105.Fn aio_suspend , 106.Fn connect , 107.Fn close , 108.Fn creat , 109.Fn fsync , 110.Fn kevent , 111.Fn mq_receive , 112.Fn mq_send , 113.Fn mq_timedreceive , 114.Fn mq_timedsend , 115.Fn msync , 116.Fn nanosleep , 117.Fn open , 118.Fn openat , 119.Fn pause , 120.Fn poll , 121.Fn pselect , 122.Fn pthread_cond_timedwait , 123.Fn pthread_cond_wait , 124.Fn pthread_join , 125.Fn pthread_testcancel , 126.Fn read , 127.Fn readv , 128.Fn recv , 129.Fn recvfrom , 130.Fn recvmsg , 131.Fn select , 132.Fn sem_timedwait , 133.Fn sem_wait , 134.Fn send , 135.Fn sendmsg , 136.Fn sendto , 137.Fn sigsuspend , 138.Fn sigtimedwait , 139.Fn sigwaitinfo , 140.Fn sigwait , 141.Fn sleep , 142.Fn system , 143.Fn tcdrain , 144.Fn usleep , 145.Fn wait , 146.Fn wait3 , 147.Fn wait4 , 148.Fn waitpid , 149.Fn write , 150.Fn writev . 151.Pp 152The 153.Fn fcntl 154function is a cancellation point if 155.Fa cmd 156is 157.Dv F_SETLKW . 158.Pp 159The 160.Fn kevent 161function is a cancellation point if it is potentially blocking, 162i.e. when the 163.Fa nevents 164argument is non-zero. 165.Sh RETURN VALUES 166If successful, the 167.Fn pthread_setcancelstate 168and 169.Fn pthread_setcanceltype 170functions will return zero. 171Otherwise, an error number shall be returned to 172indicate the error. 173.Pp 174The 175.Fn pthread_setcancelstate 176and 177.Fn pthread_setcanceltype 178functions are used to control the points at which a thread may be 179asynchronously canceled. 180For cancellation control to be usable in modular 181fashion, some rules must be followed. 182.Pp 183For purposes of this discussion, consider an object to be a generalization 184of a procedure. 185It is a set of procedures and global variables written as 186a unit and called by clients not known by the object. 187Objects may depend 188on other objects. 189.Pp 190First, cancelability should only be disabled on entry to an object, never 191explicitly enabled. 192On exit from an object, the cancelability state should 193always be restored to its value on entry to the object. 194.Pp 195This follows from a modularity argument: if the client of an object (or the 196client of an object that uses that object) has disabled cancelability, it is 197because the client does not want to have to worry about how to clean up if the 198thread is canceled while executing some sequence of actions. 199If an object 200is called in such a state and it enables cancelability and a cancellation 201request is pending for that thread, then the thread will be canceled, 202contrary to the wish of the client that disabled. 203.Pp 204Second, the cancelability type may be explicitly set to either 205.Em deferred 206or 207.Em asynchronous 208upon entry to an object. 209But as with the cancelability state, on exit from 210an object that cancelability type should always be restored to its value on 211entry to the object. 212.Pp 213Finally, only functions that are cancel-safe may be called from a thread that 214is asynchronously cancelable. 215.Sh ERRORS 216The function 217.Fn pthread_setcancelstate 218may fail with: 219.Bl -tag -width Er 220.It Bq Er EINVAL 221The specified state is not 222.Dv PTHREAD_CANCEL_ENABLE 223or 224.Dv PTHREAD_CANCEL_DISABLE . 225.El 226.Pp 227The function 228.Fn pthread_setcanceltype 229may fail with: 230.Bl -tag -width Er 231.It Bq Er EINVAL 232The specified state is not 233.Dv PTHREAD_CANCEL_DEFERRED 234or 235.Dv PTHREAD_CANCEL_ASYNCHRONOUS . 236.El 237.Sh SEE ALSO 238.Xr pthread_cancel 3 239.Sh STANDARDS 240The 241.Fn pthread_testcancel 242function conforms to 243.St -p1003.1-96 . 244The standard allows implementations to make many more functions 245cancellation points. 246.Sh AUTHORS 247This manual page was written by 248.An David Leonard Aq Mt d@openbsd.org 249for the 250.Ox 251implementation of 252.Xr pthread_cancel 3 . 253