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_cond_timedwait.3p (18052B)


  1. '\" et
  2. .TH PTHREAD_COND_TIMEDWAIT "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_cond_timedwait,
  12. pthread_cond_wait
  13. \(em wait on a condition
  14. .SH SYNOPSIS
  15. .LP
  16. .nf
  17. #include <pthread.h>
  18. .P
  19. int pthread_cond_timedwait(pthread_cond_t *restrict \fIcond\fP,
  20. pthread_mutex_t *restrict \fImutex\fP,
  21. const struct timespec *restrict \fIabstime\fP);
  22. int pthread_cond_wait(pthread_cond_t *restrict \fIcond\fP,
  23. pthread_mutex_t *restrict \fImutex\fP);
  24. .fi
  25. .SH DESCRIPTION
  26. The
  27. \fIpthread_cond_timedwait\fR()
  28. and
  29. \fIpthread_cond_wait\fR()
  30. functions shall block on a condition variable. The application shall
  31. ensure that these functions are called with
  32. .IR mutex
  33. locked by the calling thread; otherwise, an error (for
  34. PTHREAD_MUTEX_ERRORCHECK and robust mutexes) or undefined behavior
  35. (for other mutexes) results.
  36. .P
  37. These functions atomically release
  38. .IR mutex
  39. and cause the calling thread to block on the condition variable
  40. .IR cond ;
  41. atomically here means ``atomically with respect to access by another
  42. thread to the mutex and then the condition variable''. That is, if
  43. another thread is able to acquire the mutex after the about-to-block
  44. thread has released it, then a subsequent call to
  45. \fIpthread_cond_broadcast\fR()
  46. or
  47. \fIpthread_cond_signal\fR()
  48. in that thread shall behave as if it were issued after the
  49. about-to-block thread has blocked.
  50. .P
  51. Upon successful return, the mutex shall have been locked and shall be
  52. owned by the calling thread. If
  53. .IR mutex
  54. is a robust mutex where an owner terminated while holding the lock and
  55. the state is recoverable, the mutex shall be acquired even though the
  56. function returns an error code.
  57. .P
  58. When using condition variables there is always a Boolean predicate
  59. involving shared variables associated with each condition wait that is
  60. true if the thread should proceed. Spurious wakeups from the
  61. \fIpthread_cond_timedwait\fR()
  62. or
  63. \fIpthread_cond_wait\fR()
  64. functions may occur. Since the return from
  65. \fIpthread_cond_timedwait\fR()
  66. or
  67. \fIpthread_cond_wait\fR()
  68. does not imply anything about the value of this predicate, the
  69. predicate should be re-evaluated upon such return.
  70. .P
  71. When a thread waits on a condition variable, having specified a
  72. particular mutex to either the
  73. \fIpthread_cond_timedwait\fR()
  74. or the
  75. \fIpthread_cond_wait\fR()
  76. operation, a dynamic binding is formed between that mutex and condition
  77. variable that remains in effect as long as at least one thread is
  78. blocked on the condition variable. During this time, the effect of an
  79. attempt by any thread to wait on that condition variable using a
  80. different mutex is undefined. Once all waiting threads have been
  81. unblocked (as by the
  82. \fIpthread_cond_broadcast\fR()
  83. operation), the next wait operation on that condition variable shall
  84. form a new dynamic binding with the mutex specified by that wait
  85. operation. Even though the dynamic binding between condition variable
  86. and mutex may be removed or replaced between the time a thread is
  87. unblocked from a wait on the condition variable and the time that it
  88. returns to the caller or begins cancellation cleanup, the unblocked
  89. thread shall always re-acquire the mutex specified in the condition
  90. wait operation call from which it is returning.
  91. .P
  92. A condition wait (whether timed or not) is a cancellation point. When
  93. the cancelability type of a thread is set to PTHREAD_CANCEL_DEFERRED,
  94. a side-effect of acting upon a cancellation request while in a
  95. condition wait is that the mutex is (in effect) re-acquired before
  96. calling the first cancellation cleanup handler. The effect is as if
  97. the thread were unblocked, allowed to execute up to the point of
  98. returning from the call to
  99. \fIpthread_cond_timedwait\fR()
  100. or
  101. \fIpthread_cond_wait\fR(),
  102. but at that point notices the cancellation request and instead of
  103. returning to the caller of
  104. \fIpthread_cond_timedwait\fR()
  105. or
  106. \fIpthread_cond_wait\fR(),
  107. starts the thread cancellation activities, which includes calling
  108. cancellation cleanup handlers.
  109. .P
  110. A thread that has been unblocked because it has been canceled while
  111. blocked in a call to
  112. \fIpthread_cond_timedwait\fR()
  113. or
  114. \fIpthread_cond_wait\fR()
  115. shall not consume any condition signal that may be directed concurrently
  116. at the condition variable if there are other threads blocked on the
  117. condition variable.
  118. .P
  119. The
  120. \fIpthread_cond_timedwait\fR()
  121. function shall be equivalent to
  122. \fIpthread_cond_wait\fR(),
  123. except that an error is returned if the absolute time specified by
  124. .IR abstime
  125. passes (that is, system time equals or exceeds
  126. .IR abstime )
  127. before the condition
  128. .IR cond
  129. is signaled or broadcasted, or if the absolute time specified by
  130. .IR abstime
  131. has already been passed at the time of the call. When such timeouts
  132. occur,
  133. \fIpthread_cond_timedwait\fR()
  134. shall nonetheless release and re-acquire the mutex referenced by
  135. .IR mutex ,
  136. and may consume a condition signal directed concurrently at the condition
  137. variable.
  138. .P
  139. The condition variable shall have a clock attribute which specifies
  140. the clock that shall be used to measure the time specified by the
  141. .IR abstime
  142. argument. The
  143. \fIpthread_cond_timedwait\fR()
  144. function is also a cancellation point.
  145. .P
  146. If a signal is delivered to a thread waiting for a condition variable,
  147. upon return from the signal handler the thread resumes waiting for the
  148. condition variable as if it was not interrupted, or it shall return
  149. zero due to spurious wakeup.
  150. .P
  151. The behavior is undefined if the value specified by the
  152. .IR cond
  153. or
  154. .IR mutex
  155. argument to these functions does not refer to an initialized
  156. condition variable or an initialized mutex object, respectively.
  157. .SH "RETURN VALUE"
  158. Except for
  159. .BR [ETIMEDOUT] ,
  160. .BR [ENOTRECOVERABLE] ,
  161. and
  162. .BR [EOWNERDEAD] ,
  163. all these error checks shall act as if they were performed immediately
  164. at the beginning of processing for the function and shall cause an
  165. error return, in effect, prior to modifying the state of the mutex
  166. specified by
  167. .IR mutex
  168. or the condition variable specified by
  169. .IR cond .
  170. .P
  171. Upon successful completion, a value of zero shall be returned; otherwise,
  172. an error number shall be returned to indicate the error.
  173. .SH ERRORS
  174. These functions shall fail if:
  175. .TP
  176. .BR ENOTRECOVERABLE
  177. .br
  178. The state protected by the mutex is not recoverable.
  179. .TP
  180. .BR EOWNERDEAD
  181. .br
  182. The mutex is a robust mutex and the process containing the previous
  183. owning thread terminated while holding the mutex lock. The mutex lock
  184. shall be acquired by the calling thread and it is up to the new owner
  185. to make the state consistent.
  186. .TP
  187. .BR EPERM
  188. The mutex type is PTHREAD_MUTEX_ERRORCHECK or the mutex
  189. is a robust mutex, and the current thread does not own the mutex.
  190. .P
  191. The
  192. \fIpthread_cond_timedwait\fR()
  193. function shall fail if:
  194. .TP
  195. .BR ETIMEDOUT
  196. The time specified by
  197. .IR abstime
  198. to
  199. \fIpthread_cond_timedwait\fR()
  200. has passed.
  201. .TP
  202. .BR EINVAL
  203. The
  204. .IR abstime
  205. argument specified a nanosecond value less than zero or greater than
  206. or equal to 1000 million.
  207. .br
  208. .P
  209. These functions may fail if:
  210. .TP
  211. .BR EOWNERDEAD
  212. .br
  213. The mutex is a robust mutex and the previous owning thread terminated
  214. while holding the mutex lock. The mutex lock shall be acquired by the
  215. calling thread and it is up to the new owner to make the state consistent.
  216. .P
  217. These functions shall not return an error code of
  218. .BR [EINTR] .
  219. .LP
  220. .IR "The following sections are informative."
  221. .SH EXAMPLES
  222. None.
  223. .SH "APPLICATION USAGE"
  224. Applications that have assumed that non-zero return values are errors
  225. will need updating for use with robust mutexes, since a valid return
  226. for a thread acquiring a mutex which is protecting a currently
  227. inconsistent state is
  228. .BR [EOWNERDEAD] .
  229. Applications that do not check the error returns, due to ruling out the
  230. possibility of such errors arising, should not use robust mutexes. If
  231. an application is supposed to work with normal and robust mutexes, it
  232. should check all return values for error conditions and if necessary
  233. take appropriate action.
  234. .SH RATIONALE
  235. If an implementation detects that the value specified by the
  236. .IR cond
  237. argument to
  238. \fIpthread_cond_timedwait\fR()
  239. or
  240. \fIpthread_cond_wait\fR()
  241. does not refer to an initialized condition variable, or detects that
  242. the value specified by the
  243. .IR mutex
  244. argument to
  245. \fIpthread_cond_timedwait\fR()
  246. or
  247. \fIpthread_cond_wait\fR()
  248. does not refer to an initialized mutex object, it is recommended
  249. that the function should fail and report an
  250. .BR [EINVAL]
  251. error.
  252. .SS "Condition Wait Semantics"
  253. .P
  254. It is important to note that when
  255. \fIpthread_cond_wait\fR()
  256. and
  257. \fIpthread_cond_timedwait\fR()
  258. return without error, the associated predicate may still be false.
  259. Similarly, when
  260. \fIpthread_cond_timedwait\fR()
  261. returns with the timeout error, the associated predicate may be true
  262. due to an unavoidable race between the expiration of the timeout and
  263. the predicate state change.
  264. .P
  265. The application needs to recheck the predicate on any return because it
  266. cannot be sure there is another thread waiting on the thread to handle
  267. the signal, and if there is not then the signal is lost. The burden is
  268. on the application to check the predicate.
  269. .P
  270. Some implementations, particularly on a multi-processor, may sometimes
  271. cause multiple threads to wake up when the condition variable is
  272. signaled simultaneously on different processors.
  273. .P
  274. In general, whenever a condition wait returns, the thread has to
  275. re-evaluate the predicate associated with the condition wait to
  276. determine whether it can safely proceed, should wait again, or should
  277. declare a timeout. A return from the wait does not imply that the
  278. associated predicate is either true or false.
  279. .P
  280. It is thus recommended that a condition wait be enclosed in the
  281. equivalent of a ``while loop'' that checks the predicate.
  282. .SS "Timed Wait Semantics"
  283. .P
  284. An absolute time measure was chosen for specifying the timeout
  285. parameter for two reasons. First, a relative time measure can be
  286. easily implemented on top of a function that specifies absolute time,
  287. but there is a race condition associated with specifying an absolute
  288. timeout on top of a function that specifies relative timeouts. For
  289. example, assume that
  290. \fIclock_gettime\fR()
  291. returns the current time and
  292. \fIcond_relative_timed_wait\fR()
  293. uses relative timeouts:
  294. .sp
  295. .RS 4
  296. .nf
  297. clock_gettime(CLOCK_REALTIME, &now)
  298. reltime = sleep_til_this_absolute_time -now;
  299. cond_relative_timed_wait(c, m, &reltime);
  300. .fi
  301. .P
  302. .RE
  303. .P
  304. If the thread is preempted between the first statement and the last
  305. statement,
  306. the thread blocks for too long. Blocking, however, is irrelevant if an
  307. absolute timeout is used. An absolute timeout also need not be
  308. recomputed if it is used multiple times in a loop, such as that
  309. enclosing a condition wait.
  310. .P
  311. For cases when the system clock is advanced discontinuously by an
  312. operator, it is expected that implementations process any timed wait
  313. expiring at an intervening time as if that time had actually occurred.
  314. .SS "Cancellation and Condition Wait"
  315. .P
  316. A condition wait, whether timed or not, is a cancellation point. That
  317. is, the functions
  318. \fIpthread_cond_wait\fR()
  319. or
  320. \fIpthread_cond_timedwait\fR()
  321. are points where a pending (or concurrent) cancellation request is
  322. noticed. The reason for this is that an indefinite wait is possible at
  323. these points\(emwhatever event is being waited for, even if the program
  324. is totally correct, might never occur; for example, some input data
  325. being awaited might never be sent. By making condition wait a
  326. cancellation point, the thread can be canceled and perform its
  327. cancellation cleanup handler even though it may be stuck in some
  328. indefinite wait.
  329. .P
  330. A side-effect of acting on a cancellation request while a thread is
  331. blocked on a condition variable is to re-acquire the mutex before
  332. calling any of the cancellation cleanup handlers. This is done in order
  333. to ensure that the cancellation cleanup handler is executed in the same
  334. state as the critical code that lies both before and after the call to
  335. the condition wait function. This rule is also required when
  336. interfacing to POSIX threads from languages, such as Ada or C++, which
  337. may choose to map cancellation onto a language exception; this rule
  338. ensures that each exception handler guarding a critical section can
  339. always safely depend upon the fact that the associated mutex has
  340. already been locked regardless of exactly where within the critical
  341. section the exception was raised. Without this rule, there would not be
  342. a uniform rule that exception handlers could follow regarding the lock,
  343. and so coding would become very cumbersome.
  344. .P
  345. Therefore, since
  346. .IR some
  347. statement has to be made regarding the state of the lock when a
  348. cancellation is delivered during a wait, a definition has been chosen
  349. that makes application coding most convenient and error free.
  350. .P
  351. When acting on a cancellation request while a thread is blocked on a
  352. condition variable, the implementation is required to ensure that the
  353. thread does not consume any condition signals directed at that
  354. condition variable if there are any other threads waiting on that
  355. condition variable. This rule is specified in order to avoid deadlock
  356. conditions that could occur if these two independent requests (one
  357. acting on a thread and the other acting on the condition variable) were
  358. not processed independently.
  359. .SS "Performance of Mutexes and Condition Variables"
  360. .P
  361. Mutexes are expected to be locked only for a few instructions. This
  362. practice is almost automatically enforced by the desire of programmers
  363. to avoid long serial regions of execution (which would reduce total
  364. effective parallelism).
  365. .P
  366. When using mutexes and condition variables, one tries to ensure that
  367. the usual case is to lock the mutex, access shared data, and unlock the
  368. mutex. Waiting on a condition variable should be a relatively rare
  369. situation. For example, when implementing a read-write lock, code
  370. that acquires a read-lock typically needs only to increment the count
  371. of readers (under mutual-exclusion) and return. The calling thread
  372. would actually wait on the condition variable only when there is
  373. already an active writer. So the efficiency of a synchronization
  374. operation is bounded by the cost of mutex lock/unlock and not by
  375. condition wait. Note that in the usual case there is no context
  376. switch.
  377. .P
  378. This is not to say that the efficiency of condition waiting is
  379. unimportant. Since there needs to be at least one context switch per
  380. Ada rendezvous, the efficiency of waiting on a condition variable is
  381. important. The cost of waiting on a condition variable should be
  382. little more than the minimal cost for a context switch plus the time to
  383. unlock and lock the mutex.
  384. .SS "Features of Mutexes and Condition Variables"
  385. .P
  386. It had been suggested that the mutex acquisition and release be
  387. decoupled from condition wait. This was rejected because it is the
  388. combined nature of the operation that, in fact, facilitates realtime
  389. implementations. Those implementations can atomically move a
  390. high-priority thread between the condition variable and the mutex in a
  391. manner that is transparent to the caller. This can prevent extra
  392. context switches and provide more deterministic acquisition of a mutex
  393. when the waiting thread is signaled. Thus, fairness and priority
  394. issues can be dealt with directly by the scheduling discipline.
  395. Furthermore, the current condition wait operation matches existing
  396. practice.
  397. .SS "Scheduling Behavior of Mutexes and Condition Variables"
  398. .P
  399. Synchronization primitives that attempt to interfere with scheduling
  400. policy by specifying an ordering rule are considered undesirable.
  401. Threads waiting on mutexes and condition variables are selected to
  402. proceed in an order dependent upon the scheduling policy rather than in
  403. some fixed order (for example, FIFO or priority). Thus, the scheduling
  404. policy determines which thread(s) are awakened and allowed to proceed.
  405. .SS "Timed Condition Wait"
  406. .P
  407. The
  408. \fIpthread_cond_timedwait\fR()
  409. function allows an application to give up waiting for a particular
  410. condition after a given amount of time. An example of its use
  411. follows:
  412. .sp
  413. .RS 4
  414. .nf
  415. (void) pthread_mutex_lock(&t.mn);
  416. t.waiters++;
  417. clock_gettime(CLOCK_REALTIME, &ts);
  418. ts.tv_sec += 5;
  419. rc = 0;
  420. while (! mypredicate(&t) && rc == 0)
  421. rc = pthread_cond_timedwait(&t.cond, &t.mn, &ts);
  422. t.waiters--;
  423. if (rc == 0 || mypredicate(&t))
  424. setmystate(&t);
  425. (void) pthread_mutex_unlock(&t.mn);
  426. .fi
  427. .P
  428. .RE
  429. .P
  430. By making the timeout parameter absolute, it does not need to be
  431. recomputed each time the program checks its blocking predicate. If the
  432. timeout was relative, it would have to be recomputed before each call.
  433. This would be especially difficult since such code would need to take
  434. into account the possibility of extra wakeups that result from extra
  435. broadcasts or signals on the condition variable that occur before
  436. either the predicate is true or the timeout is due.
  437. .SH "FUTURE DIRECTIONS"
  438. None.
  439. .SH "SEE ALSO"
  440. .IR "\fIpthread_cond_broadcast\fR\^(\|)"
  441. .P
  442. The Base Definitions volume of POSIX.1\(hy2017,
  443. .IR "Section 4.12" ", " "Memory Synchronization",
  444. .IR "\fB<pthread.h>\fP"
  445. .\"
  446. .SH COPYRIGHT
  447. Portions of this text are reprinted and reproduced in electronic form
  448. from IEEE Std 1003.1-2017, Standard for Information Technology
  449. -- Portable Operating System Interface (POSIX), The Open Group Base
  450. Specifications Issue 7, 2018 Edition,
  451. Copyright (C) 2018 by the Institute of
  452. Electrical and Electronics Engineers, Inc and The Open Group.
  453. In the event of any discrepancy between this version and the original IEEE and
  454. The Open Group Standard, the original IEEE and The Open Group Standard
  455. is the referee document. The original Standard can be obtained online at
  456. http://www.opengroup.org/unix/online.html .
  457. .PP
  458. Any typographical or formatting errors that appear
  459. in this page are most likely
  460. to have been introduced during the conversion of the source files to
  461. man page format. To report such errors, see
  462. https://www.kernel.org/doc/man-pages/reporting_bugs.html .