logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

pthread_cleanup_pop.3p (9817B)


  1. '\" et
  2. .TH PTHREAD_CLEANUP_POP "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
  3. .\"
  4. .SH PROLOG
  5. This manual page is part of the POSIX Programmer's Manual.
  6. The Linux implementation of this interface may differ (consult
  7. the corresponding Linux manual page for details of Linux behavior),
  8. or the interface may not be implemented on Linux.
  9. .\"
  10. .SH NAME
  11. pthread_cleanup_pop,
  12. pthread_cleanup_push
  13. \(em establish cancellation handlers
  14. .SH SYNOPSIS
  15. .LP
  16. .nf
  17. #include <pthread.h>
  18. .P
  19. void pthread_cleanup_pop(int \fIexecute\fP);
  20. void pthread_cleanup_push(void (*\fIroutine\fP)(void*), void *\fIarg\fP);
  21. .fi
  22. .SH DESCRIPTION
  23. The
  24. \fIpthread_cleanup_pop\fR()
  25. function shall remove the routine at the top of the calling thread's
  26. cancellation cleanup stack and optionally invoke it (if
  27. .IR execute
  28. is non-zero).
  29. .P
  30. The
  31. \fIpthread_cleanup_push\fR()
  32. function shall push the specified cancellation cleanup handler
  33. .IR routine
  34. onto the calling thread's cancellation cleanup stack. The cancellation
  35. cleanup handler shall be popped from the cancellation cleanup stack and
  36. invoked with the argument
  37. .IR arg
  38. when:
  39. .IP " *" 4
  40. The thread exits (that is, calls
  41. \fIpthread_exit\fR()).
  42. .IP " *" 4
  43. The thread acts upon a cancellation request.
  44. .IP " *" 4
  45. The thread calls
  46. \fIpthread_cleanup_pop\fR()
  47. with a non-zero
  48. .IR execute
  49. argument.
  50. .P
  51. It is unspecified whether
  52. \fIpthread_cleanup_push\fR()
  53. and
  54. \fIpthread_cleanup_pop\fR()
  55. are macros or functions. If a macro definition is suppressed in order
  56. to access an actual function, or a program defines an external
  57. identifier with any of these names, the behavior is undefined.
  58. The application shall ensure that they appear as statements,
  59. and in pairs within the same lexical scope (that is, the
  60. \fIpthread_cleanup_push\fR()
  61. macro may be thought to expand to a token list whose first token is
  62. .BR '{'
  63. with
  64. \fIpthread_cleanup_pop\fR()
  65. expanding to a token list whose last token is the corresponding
  66. .BR '}' ).
  67. .P
  68. The effect of calling
  69. \fIlongjmp\fR()
  70. or
  71. \fIsiglongjmp\fR()
  72. is undefined if there have been any calls to
  73. \fIpthread_cleanup_push\fR()
  74. or
  75. \fIpthread_cleanup_pop\fR()
  76. made without the matching call since the jump buffer was filled. The
  77. effect of calling
  78. \fIlongjmp\fR()
  79. or
  80. \fIsiglongjmp\fR()
  81. from inside a cancellation cleanup handler is also undefined unless the
  82. jump buffer was also filled in the cancellation cleanup handler.
  83. .P
  84. The effect of the use of
  85. .BR return ,
  86. .BR break ,
  87. .BR continue ,
  88. and
  89. .BR goto
  90. to prematurely leave a code block described by a pair of
  91. \fIpthread_cleanup_push\fR()
  92. and
  93. \fIpthread_cleanup_pop\fR()
  94. functions calls is undefined.
  95. .SH "RETURN VALUE"
  96. The
  97. \fIpthread_cleanup_push\fR()
  98. and
  99. \fIpthread_cleanup_pop\fR()
  100. functions shall not return a value.
  101. .SH ERRORS
  102. No errors are defined.
  103. .P
  104. These functions shall not return an error code of
  105. .BR [EINTR] .
  106. .LP
  107. .IR "The following sections are informative."
  108. .SH EXAMPLES
  109. The following is an example using thread primitives to implement a
  110. cancelable, writers-priority read-write lock:
  111. .sp
  112. .RS 4
  113. .nf
  114. typedef struct {
  115. pthread_mutex_t lock;
  116. pthread_cond_t rcond,
  117. wcond;
  118. int lock_count; /* < 0 .. Held by writer. */
  119. /* > 0 .. Held by lock_count readers. */
  120. /* = 0 .. Held by nobody. */
  121. int waiting_writers; /* Count of waiting writers. */
  122. } rwlock;
  123. .P
  124. void
  125. waiting_reader_cleanup(void *arg)
  126. {
  127. rwlock *l;
  128. .P
  129. l = (rwlock *) arg;
  130. pthread_mutex_unlock(&l->lock);
  131. }
  132. .P
  133. void
  134. lock_for_read(rwlock *l)
  135. {
  136. pthread_mutex_lock(&l->lock);
  137. pthread_cleanup_push(waiting_reader_cleanup, l);
  138. while ((l->lock_count < 0) || (l->waiting_writers != 0))
  139. pthread_cond_wait(&l->rcond, &l->lock);
  140. l->lock_count++;
  141. /*
  142. * Note the pthread_cleanup_pop executes
  143. * waiting_reader_cleanup.
  144. */
  145. pthread_cleanup_pop(1);
  146. }
  147. .P
  148. void
  149. release_read_lock(rwlock *l)
  150. {
  151. pthread_mutex_lock(&l->lock);
  152. if (--l->lock_count == 0)
  153. pthread_cond_signal(&l->wcond);
  154. pthread_mutex_unlock(&l->lock);
  155. }
  156. .P
  157. void
  158. waiting_writer_cleanup(void *arg)
  159. {
  160. rwlock *l;
  161. .P
  162. l = (rwlock *) arg;
  163. if ((--l->waiting_writers == 0) && (l->lock_count >= 0)) {
  164. /*
  165. * This only happens if we have been canceled. If the
  166. * lock is not held by a writer, there may be readers who
  167. * were blocked because waiting_writers was positive; they
  168. * can now be unblocked.
  169. */
  170. pthread_cond_broadcast(&l->rcond);
  171. }
  172. pthread_mutex_unlock(&l->lock);
  173. }
  174. .P
  175. void
  176. lock_for_write(rwlock *l)
  177. {
  178. pthread_mutex_lock(&l->lock);
  179. l->waiting_writers++;
  180. pthread_cleanup_push(waiting_writer_cleanup, l);
  181. while (l->lock_count != 0)
  182. pthread_cond_wait(&l->wcond, &l->lock);
  183. l->lock_count = -1;
  184. /*
  185. * Note the pthread_cleanup_pop executes
  186. * waiting_writer_cleanup.
  187. */
  188. pthread_cleanup_pop(1);
  189. }
  190. .P
  191. void
  192. release_write_lock(rwlock *l)
  193. {
  194. pthread_mutex_lock(&l->lock);
  195. l->lock_count = 0;
  196. if (l->waiting_writers == 0)
  197. pthread_cond_broadcast(&l->rcond);
  198. else
  199. pthread_cond_signal(&l->wcond);
  200. pthread_mutex_unlock(&l->lock);
  201. }
  202. .P
  203. /*
  204. * This function is called to initialize the read/write lock.
  205. */
  206. void
  207. initialize_rwlock(rwlock *l)
  208. {
  209. pthread_mutex_init(&l->lock, pthread_mutexattr_default);
  210. pthread_cond_init(&l->wcond, pthread_condattr_default);
  211. pthread_cond_init(&l->rcond, pthread_condattr_default);
  212. l->lock_count = 0;
  213. l->waiting_writers = 0;
  214. }
  215. .P
  216. reader_thread()
  217. {
  218. lock_for_read(&lock);
  219. pthread_cleanup_push(release_read_lock, &lock);
  220. /*
  221. * Thread has read lock.
  222. */
  223. pthread_cleanup_pop(1);
  224. }
  225. .P
  226. writer_thread()
  227. {
  228. lock_for_write(&lock);
  229. pthread_cleanup_push(release_write_lock, &lock);
  230. /*
  231. * Thread has write lock.
  232. */
  233. pthread_cleanup_pop(1);
  234. }
  235. .fi
  236. .P
  237. .RE
  238. .SH "APPLICATION USAGE"
  239. The two routines that push and pop cancellation cleanup handlers,
  240. \fIpthread_cleanup_push\fR()
  241. and
  242. \fIpthread_cleanup_pop\fR(),
  243. can be thought of as left and right-parentheses. They always need to
  244. be matched.
  245. .SH RATIONALE
  246. The restriction that the two routines that push and pop
  247. cancellation cleanup handlers,
  248. \fIpthread_cleanup_push\fR()
  249. and
  250. \fIpthread_cleanup_pop\fR(),
  251. have to appear in the same lexical scope allows for efficient macro or
  252. compiler implementations and efficient storage management. A sample
  253. implementation of these routines as macros might look like this:
  254. .sp
  255. .RS 4
  256. .nf
  257. #define pthread_cleanup_push(rtn,arg) { \e
  258. struct _pthread_handler_rec __cleanup_handler, **__head; \e
  259. __cleanup_handler.rtn = rtn; \e
  260. __cleanup_handler.arg = arg; \e
  261. (void) pthread_getspecific(_pthread_handler_key, &__head); \e
  262. __cleanup_handler.next = *__head; \e
  263. *__head = &__cleanup_handler;
  264. .P
  265. #define pthread_cleanup_pop(ex) \e
  266. *__head = __cleanup_handler.next; \e
  267. if (ex) (*__cleanup_handler.rtn)(__cleanup_handler.arg); \e
  268. }
  269. .fi
  270. .P
  271. .RE
  272. .P
  273. A more ambitious implementation of these routines might do even better
  274. by allowing the compiler to note that the
  275. cancellation cleanup handler is a constant and can be expanded inline.
  276. .P
  277. This volume of POSIX.1\(hy2017 currently leaves unspecified the effect of calling
  278. \fIlongjmp\fR()
  279. from a signal handler executing in a POSIX System Interfaces function.
  280. If an implementation wants to allow this and give the programmer
  281. reasonable behavior, the
  282. \fIlongjmp\fR()
  283. function has to call all cancellation cleanup handlers that have been
  284. pushed but not popped since the time
  285. \fIsetjmp\fR()
  286. was called.
  287. .P
  288. Consider a multi-threaded function called by a thread that uses
  289. signals. If a signal were delivered to a signal handler during the
  290. operation of
  291. \fIqsort\fR()
  292. and that handler were to call
  293. \fIlongjmp\fR()
  294. (which, in turn, did
  295. .IR not
  296. call the cancellation cleanup handlers) the helper threads created by
  297. the
  298. \fIqsort\fR()
  299. function would not be canceled. Instead, they would continue to execute
  300. and write into the argument array even though the array might have been
  301. popped off the stack.
  302. .P
  303. Note that the specified cleanup handling mechanism is especially tied
  304. to the C language and, while the requirement for a uniform mechanism
  305. for expressing cleanup is language-independent, the mechanism used in
  306. other languages may be quite different. In addition, this mechanism is
  307. really only necessary due to the lack of a real exception mechanism in
  308. the C language, which would be the ideal solution.
  309. .P
  310. There is no notion of a cancellation cleanup-safe function. If an
  311. application has no cancellation points in its signal handlers, blocks
  312. any signal whose handler may have cancellation points while calling
  313. async-unsafe functions, or disables cancellation while calling
  314. async-unsafe functions, all functions may be safely called from
  315. cancellation cleanup routines.
  316. .SH "FUTURE DIRECTIONS"
  317. None.
  318. .SH "SEE ALSO"
  319. .IR "\fIpthread_cancel\fR\^(\|)",
  320. .IR "\fIpthread_setcancelstate\fR\^(\|)"
  321. .P
  322. The Base Definitions volume of POSIX.1\(hy2017,
  323. .IR "\fB<pthread.h>\fP"
  324. .\"
  325. .SH COPYRIGHT
  326. Portions of this text are reprinted and reproduced in electronic form
  327. from IEEE Std 1003.1-2017, Standard for Information Technology
  328. -- Portable Operating System Interface (POSIX), The Open Group Base
  329. Specifications Issue 7, 2018 Edition,
  330. Copyright (C) 2018 by the Institute of
  331. Electrical and Electronics Engineers, Inc and The Open Group.
  332. In the event of any discrepancy between this version and the original IEEE and
  333. The Open Group Standard, the original IEEE and The Open Group Standard
  334. is the referee document. The original Standard can be obtained online at
  335. http://www.opengroup.org/unix/online.html .
  336. .PP
  337. Any typographical or formatting errors that appear
  338. in this page are most likely
  339. to have been introduced during the conversion of the source files to
  340. man page format. To report such errors, see
  341. https://www.kernel.org/doc/man-pages/reporting_bugs.html .