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_mutex_lock.3p (10185B)


  1. '\" et
  2. .TH PTHREAD_MUTEX_LOCK "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_mutex_lock,
  12. pthread_mutex_trylock,
  13. pthread_mutex_unlock
  14. \(em lock and unlock a mutex
  15. .SH SYNOPSIS
  16. .LP
  17. .nf
  18. #include <pthread.h>
  19. .P
  20. int pthread_mutex_lock(pthread_mutex_t *\fImutex\fP);
  21. int pthread_mutex_trylock(pthread_mutex_t *\fImutex\fP);
  22. int pthread_mutex_unlock(pthread_mutex_t *\fImutex\fP);
  23. .fi
  24. .SH DESCRIPTION
  25. The mutex object referenced by
  26. .IR mutex
  27. shall be locked by a call to
  28. \fIpthread_mutex_lock\fR()
  29. that returns zero or
  30. .BR [EOWNERDEAD] .
  31. If the mutex is already locked by another thread, the calling thread
  32. shall block until the mutex becomes available. This operation shall
  33. return with the mutex object referenced by
  34. .IR mutex
  35. in the locked state with the calling thread as its owner. If a thread
  36. attempts to relock a mutex that it has already locked,
  37. \fIpthread_mutex_lock\fR()
  38. shall behave as described in the
  39. .BR Relock
  40. column of the following table. If a thread attempts to unlock a mutex
  41. that it has not locked or a mutex which is unlocked,
  42. \fIpthread_mutex_unlock\fR()
  43. shall behave as described in the
  44. .BR "Unlock When Not Owner"
  45. column of the following table.
  46. .TS
  47. center box tab(!);
  48. cB | cB | cB | cB
  49. l | l | l | l.
  50. Mutex Type!Robustness!Relock!Unlock When Not Owner
  51. _
  52. NORMAL!non-robust!deadlock!undefined behavior
  53. _
  54. NORMAL!robust!deadlock!error returned
  55. _
  56. ERRORCHECK!either!error returned!error returned
  57. _
  58. RECURSIVE!either!recursive!error returned
  59. !!(see below)
  60. _
  61. DEFAULT!non-robust!undefined!undefined behavior\s-2\(dg\s+2
  62. !!behavior\s-2\(dg\s+2
  63. _
  64. DEFAULT!robust!undefined!error returned
  65. !!behavior\s-2\(dg\s+2
  66. .TE
  67. .IP "\(dg" 6
  68. If the mutex type is PTHREAD_MUTEX_DEFAULT, the behavior of
  69. \fIpthread_mutex_lock\fR()
  70. may correspond to one of the three other standard mutex types as described
  71. in the table above. If it does not correspond to one of those three,
  72. the behavior is undefined for the cases marked \(dg.
  73. .P
  74. Where the table indicates recursive behavior, the mutex shall maintain
  75. the concept of a lock count. When a thread successfully acquires a
  76. mutex for the first time, the lock count shall be set to one. Every
  77. time a thread relocks this mutex, the lock count shall be incremented
  78. by one. Each time the thread unlocks the mutex, the lock count shall be
  79. decremented by one. When the lock count reaches zero, the mutex shall
  80. become available for other threads to acquire.
  81. .P
  82. The
  83. \fIpthread_mutex_trylock\fR()
  84. function shall be equivalent to
  85. \fIpthread_mutex_lock\fR(),
  86. except that if the mutex object referenced by
  87. .IR mutex
  88. is currently locked (by any thread, including the current thread), the
  89. call shall return immediately. If the mutex type is
  90. PTHREAD_MUTEX_RECURSIVE and the mutex is currently owned by the
  91. calling thread, the mutex lock count shall be incremented by one and
  92. the
  93. \fIpthread_mutex_trylock\fR()
  94. function shall immediately return success.
  95. .P
  96. The
  97. \fIpthread_mutex_unlock\fR()
  98. function shall release the mutex object referenced by
  99. .IR mutex .
  100. The manner in which a mutex is released is dependent upon the mutex's type
  101. attribute. If there are threads blocked on the mutex object referenced by
  102. .IR mutex
  103. when
  104. \fIpthread_mutex_unlock\fR()
  105. is called, resulting in the mutex becoming available, the scheduling
  106. policy shall determine which thread shall acquire the mutex.
  107. .P
  108. (In the case of PTHREAD_MUTEX_RECURSIVE mutexes, the mutex shall become
  109. available when the count reaches zero and the calling thread no longer
  110. has any locks on this mutex.)
  111. .P
  112. If a signal is delivered to a thread waiting for a mutex, upon return
  113. from the signal handler the thread shall resume waiting for the mutex
  114. as if it was not interrupted.
  115. .P
  116. If
  117. .IR mutex
  118. is a robust mutex and the process containing the owning thread
  119. terminated while holding the mutex lock, a call to
  120. \fIpthread_mutex_lock\fR()
  121. shall return the error value
  122. .BR [EOWNERDEAD] .
  123. If
  124. .IR mutex
  125. is a robust mutex and the owning thread terminated while holding the
  126. mutex lock, a call to
  127. \fIpthread_mutex_lock\fR()
  128. may return the error value
  129. .BR [EOWNERDEAD]
  130. even if the process in which the owning thread resides has not
  131. terminated. In these cases, the mutex is locked by the thread but the
  132. state it protects is marked as inconsistent. The application should
  133. ensure that the state is made consistent for reuse and when that is
  134. complete call
  135. \fIpthread_mutex_consistent\fR().
  136. If the application is unable to recover the state, it should unlock the
  137. mutex without a prior call to
  138. \fIpthread_mutex_consistent\fR(),
  139. after which the mutex is marked permanently unusable.
  140. .P
  141. If
  142. .IR mutex
  143. does not refer to an initialized mutex object, the behavior of
  144. \fIpthread_mutex_lock\fR(),
  145. \fIpthread_mutex_trylock\fR(),
  146. and
  147. \fIpthread_mutex_unlock\fR()
  148. is undefined.
  149. .SH "RETURN VALUE"
  150. If successful, the
  151. \fIpthread_mutex_lock\fR(),
  152. \fIpthread_mutex_trylock\fR(),
  153. and
  154. \fIpthread_mutex_unlock\fR()
  155. functions shall return zero; otherwise, an error number shall be
  156. returned to indicate the error.
  157. .SH ERRORS
  158. The
  159. \fIpthread_mutex_lock\fR()
  160. and
  161. \fIpthread_mutex_trylock\fR()
  162. functions shall fail if:
  163. .TP
  164. .BR EAGAIN
  165. The mutex could not be acquired because the maximum number of recursive
  166. locks for
  167. .IR mutex
  168. has been exceeded.
  169. .TP
  170. .BR EINVAL
  171. The
  172. .IR mutex
  173. was created with the protocol attribute having the value
  174. PTHREAD_PRIO_PROTECT
  175. and the calling thread's priority is higher than the mutex's current
  176. priority ceiling.
  177. .TP
  178. .BR ENOTRECOVERABLE
  179. .br
  180. The state protected by the mutex is not recoverable.
  181. .TP
  182. .BR EOWNERDEAD
  183. .br
  184. The mutex is a robust mutex and the process containing the previous
  185. owning thread terminated while holding the mutex lock. The mutex lock
  186. shall be acquired by the calling thread and it is up to the new owner
  187. to make the state consistent.
  188. .P
  189. The
  190. \fIpthread_mutex_lock\fR()
  191. function shall fail if:
  192. .TP
  193. .BR EDEADLK
  194. The mutex type is PTHREAD_MUTEX_ERRORCHECK and the current
  195. thread already owns the mutex.
  196. .P
  197. The
  198. \fIpthread_mutex_trylock\fR()
  199. function shall fail if:
  200. .TP
  201. .BR EBUSY
  202. The
  203. .IR mutex
  204. could not be acquired because it was already locked.
  205. .P
  206. The
  207. \fIpthread_mutex_unlock\fR()
  208. function shall fail if:
  209. .TP
  210. .BR EPERM
  211. The mutex type is PTHREAD_MUTEX_ERRORCHECK or PTHREAD_MUTEX_RECURSIVE,
  212. or the mutex is a robust mutex, and the current thread does not own
  213. the mutex.
  214. .br
  215. .P
  216. The
  217. \fIpthread_mutex_lock\fR()
  218. and
  219. \fIpthread_mutex_trylock\fR()
  220. functions may fail if:
  221. .TP
  222. .BR EOWNERDEAD
  223. .br
  224. The mutex is a robust mutex and the previous owning thread terminated
  225. while holding the mutex lock. The mutex lock shall be acquired by the
  226. calling thread and it is up to the new owner to make the state consistent.
  227. .P
  228. The
  229. \fIpthread_mutex_lock\fR()
  230. function may fail if:
  231. .TP
  232. .BR EDEADLK
  233. A deadlock condition was detected.
  234. .P
  235. These functions shall not return an error code of
  236. .BR [EINTR] .
  237. .LP
  238. .IR "The following sections are informative."
  239. .SH EXAMPLES
  240. None.
  241. .SH "APPLICATION USAGE"
  242. Applications that have assumed that non-zero return values are errors
  243. will need updating for use with robust mutexes, since a valid return
  244. for a thread acquiring a mutex which is protecting a currently
  245. inconsistent state is
  246. .BR [EOWNERDEAD] .
  247. Applications that do not check the error returns, due to ruling out the
  248. possibility of such errors arising, should not use robust mutexes. If
  249. an application is supposed to work with normal and robust mutexes it
  250. should check all return values for error conditions and if necessary
  251. take appropriate action.
  252. .SH RATIONALE
  253. Mutex objects are intended to serve as a low-level primitive from which
  254. other thread synchronization functions can be built. As such, the
  255. implementation of mutexes should be as efficient as possible, and this
  256. has ramifications on the features available at the interface.
  257. .P
  258. The mutex functions and the particular default settings of the mutex
  259. attributes have been motivated by the desire to not preclude fast,
  260. inlined implementations of mutex locking and unlocking.
  261. .P
  262. Since most attributes only need to be checked when a thread is going to
  263. be blocked, the use of attributes does not slow the (common)
  264. mutex-locking case.
  265. .P
  266. Likewise, while being able to extract the thread ID of the owner of a
  267. mutex might be desirable, it would require storing the current thread
  268. ID when each mutex is locked, and this could incur unacceptable levels
  269. of overhead. Similar arguments apply to a
  270. .IR mutex_tryunlock
  271. operation.
  272. .P
  273. For further rationale on the extended mutex types, see the Rationale (Informative) volume of POSIX.1\(hy2017,
  274. .IR "Threads Extensions".
  275. .P
  276. If an implementation detects that the value specified by the
  277. .IR mutex
  278. argument does not refer to an initialized mutex object, it is
  279. recommended that the function should fail and report an
  280. .BR [EINVAL]
  281. error.
  282. .SH "FUTURE DIRECTIONS"
  283. None.
  284. .SH "SEE ALSO"
  285. .ad l
  286. .IR "\fIpthread_mutex_consistent\fR\^(\|)",
  287. .IR "\fIpthread_mutex_destroy\fR\^(\|)",
  288. .IR "\fIpthread_mutex_timedlock\fR\^(\|)",
  289. .IR "\fIpthread_mutexattr_getrobust\fR\^(\|)"
  290. .ad b
  291. .P
  292. The Base Definitions volume of POSIX.1\(hy2017,
  293. .IR "Section 4.12" ", " "Memory Synchronization",
  294. .IR "\fB<pthread.h>\fP"
  295. .\"
  296. .SH COPYRIGHT
  297. Portions of this text are reprinted and reproduced in electronic form
  298. from IEEE Std 1003.1-2017, Standard for Information Technology
  299. -- Portable Operating System Interface (POSIX), The Open Group Base
  300. Specifications Issue 7, 2018 Edition,
  301. Copyright (C) 2018 by the Institute of
  302. Electrical and Electronics Engineers, Inc and The Open Group.
  303. In the event of any discrepancy between this version and the original IEEE and
  304. The Open Group Standard, the original IEEE and The Open Group Standard
  305. is the referee document. The original Standard can be obtained online at
  306. http://www.opengroup.org/unix/online.html .
  307. .PP
  308. Any typographical or formatting errors that appear
  309. in this page are most likely
  310. to have been introduced during the conversion of the source files to
  311. man page format. To report such errors, see
  312. https://www.kernel.org/doc/man-pages/reporting_bugs.html .