/share/man/man9/rwlock.9

https://bitbucket.org/freebsd/freebsd-head/ · Unknown · 311 lines · 311 code · 0 blank · 0 comment · 0 complexity · 6b625f6fde06e0db42b861a9bb0cc516 MD5 · raw file

  1. .\" Copyright (c) 2006 Gleb Smirnoff <glebius@FreeBSD.org>
  2. .\" All rights reserved.
  3. .\"
  4. .\" Redistribution and use in source and binary forms, with or without
  5. .\" modification, are permitted provided that the following conditions
  6. .\" are met:
  7. .\" 1. Redistributions of source code must retain the above copyright
  8. .\" notice, this list of conditions and the following disclaimer.
  9. .\" 2. Redistributions in binary form must reproduce the above copyright
  10. .\" notice, this list of conditions and the following disclaimer in the
  11. .\" documentation and/or other materials provided with the distribution.
  12. .\"
  13. .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  14. .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  17. .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19. .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20. .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23. .\" SUCH DAMAGE.
  24. .\"
  25. .\" $FreeBSD$
  26. .\"
  27. .Dd November 16, 2011
  28. .Dt RWLOCK 9
  29. .Os
  30. .Sh NAME
  31. .Nm rwlock ,
  32. .Nm rw_init ,
  33. .Nm rw_init_flags,
  34. .Nm rw_destroy ,
  35. .Nm rw_rlock ,
  36. .Nm rw_wlock ,
  37. .Nm rw_runlock ,
  38. .Nm rw_wunlock ,
  39. .Nm rw_unlock ,
  40. .Nm rw_try_rlock ,
  41. .Nm rw_try_upgrade ,
  42. .Nm rw_try_wlock ,
  43. .Nm rw_downgrade ,
  44. .Nm rw_sleep ,
  45. .Nm rw_initialized ,
  46. .Nm rw_wowned ,
  47. .Nm rw_assert ,
  48. .Nm RW_SYSINIT
  49. .Nd kernel reader/writer lock
  50. .Sh SYNOPSIS
  51. .In sys/param.h
  52. .In sys/lock.h
  53. .In sys/rwlock.h
  54. .Ft void
  55. .Fn rw_init "struct rwlock *rw" "const char *name"
  56. .Ft void
  57. .Fn rw_init_flags "struct rwlock *rw" "const char *name" "int opts"
  58. .Ft void
  59. .Fn rw_destroy "struct rwlock *rw"
  60. .Ft void
  61. .Fn rw_rlock "struct rwlock *rw"
  62. .Ft void
  63. .Fn rw_wlock "struct rwlock *rw"
  64. .Ft int
  65. .Fn rw_try_rlock "struct rwlock *rw"
  66. .Ft int
  67. .Fn rw_try_wlock "struct rwlock *rw"
  68. .Ft void
  69. .Fn rw_runlock "struct rwlock *rw"
  70. .Ft void
  71. .Fn rw_wunlock "struct rwlock *rw"
  72. .Ft void
  73. .Fn rw_unlock "struct rwlock *rw"
  74. .Ft int
  75. .Fn rw_try_upgrade "struct rwlock *rw"
  76. .Ft void
  77. .Fn rw_downgrade "struct rwlock *rw"
  78. .Ft int
  79. .Fn rw_sleep "void *chan" "struct rwlock *rw" "int priority" "const char *wmesg" "int timo"
  80. .Ft int
  81. .Fn rw_initialized "const struct rwlock *rw"
  82. .Ft int
  83. .Fn rw_wowned "const struct rwlock *rw"
  84. .Pp
  85. .Cd "options INVARIANTS"
  86. .Cd "options INVARIANT_SUPPORT"
  87. .Ft void
  88. .Fn rw_assert "const struct rwlock *rw" "int what"
  89. .In sys/kernel.h
  90. .Fn RW_SYSINIT "name" "struct rwlock *rw" "const char *desc"
  91. .Sh DESCRIPTION
  92. Reader/writer locks allow shared access to protected data by multiple threads,
  93. or exclusive access by a single thread.
  94. The threads with shared access are known as
  95. .Em readers
  96. since they only read the protected data.
  97. A thread with exclusive access is known as a
  98. .Em writer
  99. since it can modify protected data.
  100. .Pp
  101. Although reader/writer locks look very similar to
  102. .Xr sx 9
  103. locks, their usage pattern is different.
  104. Reader/writer locks can be treated as mutexes (see
  105. .Xr mutex 9 )
  106. with shared/exclusive semantics.
  107. Unlike
  108. .Xr sx 9 ,
  109. an
  110. .Nm
  111. can be locked while holding a non-spin mutex, and an
  112. .Nm
  113. cannot be held while sleeping.
  114. The
  115. .Nm
  116. locks have priority propagation like mutexes, but priority
  117. can be propagated only to writers.
  118. This limitation comes from the fact that readers
  119. are anonymous.
  120. Another important property is that readers can always recurse,
  121. and exclusive locks can be made recursive selectively.
  122. .Ss Macros and Functions
  123. .Bl -tag -width indent
  124. .It Fn rw_init "struct rwlock *rw" "const char *name"
  125. Initialize structure located at
  126. .Fa rw
  127. as reader/writer lock, described by name
  128. .Fa name .
  129. The description is used solely for debugging purposes.
  130. This function must be called before any other operations
  131. on the lock.
  132. .It Fn rw_init_flags "struct rwlock *rw" "const char *name" "int opts"
  133. Initialize the rw lock just like the
  134. .Fn rw_init
  135. function, but specifying a set of optional flags to alter the
  136. behaviour of
  137. .Fa rw ,
  138. through the
  139. .Fa opts
  140. argument.
  141. It contains one or more of the following flags:
  142. .Bl -tag -width ".Dv RW_NOPROFILE"
  143. .It Dv RW_DUPOK
  144. Witness should not log messages about duplicate locks being acquired.
  145. .It Dv RW_NOPROFILE
  146. Do not profile this lock.
  147. .It Dv RW_NOWITNESS
  148. Instruct
  149. .Xr witness 4
  150. to ignore this lock.
  151. .It Dv RW_QUIET
  152. Do not log any operations for this lock via
  153. .Xr ktr 4 .
  154. .It Dv RW_RECURSE
  155. Allow threads to recursively acquire exclusive locks for
  156. .Fa rw .
  157. .El
  158. .It Fn rw_rlock "struct rwlock *rw"
  159. Lock
  160. .Fa rw
  161. as a reader.
  162. If any thread holds this lock exclusively, the current thread blocks,
  163. and its priority is propagated to the exclusive holder.
  164. The
  165. .Fn rw_rlock
  166. function can be called when the thread has already acquired reader
  167. access on
  168. .Fa rw .
  169. This is called
  170. .Dq "recursing on a lock" .
  171. .It Fn rw_wlock "struct rwlock *rw"
  172. Lock
  173. .Fa rw
  174. as a writer.
  175. If there are any shared owners of the lock, the current thread blocks.
  176. The
  177. .Fn rw_wlock
  178. function can be called recursively only if
  179. .Fa rw
  180. has been initialized with the
  181. .Dv RW_RECURSE
  182. option enabled.
  183. .It Fn rw_try_rlock "struct rwlock *rw"
  184. Try to lock
  185. .Fa rw
  186. as a reader.
  187. This function will return true if the operation succeeds, otherwise 0
  188. will be returned.
  189. .It Fn rw_try_wlock "struct rwlock *rw"
  190. Try to lock
  191. .Fa rw
  192. as a writer.
  193. This function will return true if the operation succeeds, otherwise 0
  194. will be returned.
  195. .It Fn rw_runlock "struct rwlock *rw"
  196. This function releases a shared lock previously acquired by
  197. .Fn rw_rlock .
  198. .It Fn rw_wunlock "struct rwlock *rw"
  199. This function releases an exclusive lock previously acquired by
  200. .Fn rw_wlock .
  201. .It Fn rw_unlock "struct rwlock *rw"
  202. This function releases a shared lock previously acquired by
  203. .Fn rw_rlock
  204. or an exclusive lock previously acquired by
  205. .Fn rw_wlock .
  206. .It Fn rw_try_upgrade "struct rwlock *rw"
  207. Attempt to upgrade a single shared lock to an exclusive lock.
  208. The current thread must hold a shared lock of
  209. .Fa rw .
  210. This will only succeed if the current thread holds the only shared lock on
  211. .Fa rw ,
  212. and it only holds a single shared lock.
  213. If the attempt succeeds
  214. .Fn rw_try_upgrade
  215. will return a non-zero value,
  216. and the current thread will hold an exclusive lock.
  217. If the attempt fails
  218. .Fn rw_try_upgrade
  219. will return zero,
  220. and the current thread will still hold a shared lock.
  221. .It Fn rw_downgrade "struct rwlock *rw"
  222. Convert an exclusive lock into a single shared lock.
  223. The current thread must hold an exclusive lock of
  224. .Fa rw .
  225. .It Fn rw_sleep "void *chan" "struct rwlock *rw" "int priority" "const char *wmesg" "int timo"
  226. Atomically release
  227. .Fa rw
  228. while waiting for an event.
  229. For more details on the parameters to this function,
  230. see
  231. .Xr sleep 9 .
  232. .It Fn rw_initialized "const struct rwlock *rw"
  233. This function returns non-zero if
  234. .Fa rw
  235. has been initialized, and zero otherwise.
  236. .It Fn rw_destroy "struct rwlock *rw"
  237. This functions destroys a lock previously initialized with
  238. .Fn rw_init .
  239. The
  240. .Fa rw
  241. lock must be unlocked.
  242. .It Fn rw_wowned "const struct rwlock *rw"
  243. This function returns a non-zero value if the current thread owns an
  244. exclusive lock on
  245. .Fa rw .
  246. .It Fn rw_assert "const struct rwlock *rw" "int what"
  247. This function allows assertions specified in
  248. .Fa what
  249. to be made about
  250. .Fa rw .
  251. If the assertions are not true and the kernel is compiled
  252. with
  253. .Cd "options INVARIANTS"
  254. and
  255. .Cd "options INVARIANT_SUPPORT" ,
  256. the kernel will panic.
  257. Currently the following assertions are supported:
  258. .Bl -tag -width ".Dv RA_UNLOCKED"
  259. .It Dv RA_LOCKED
  260. Assert that current thread holds either a shared or exclusive lock
  261. of
  262. .Fa rw .
  263. .It Dv RA_RLOCKED
  264. Assert that current thread holds a shared lock of
  265. .Fa rw .
  266. .It Dv RA_WLOCKED
  267. Assert that current thread holds an exclusive lock of
  268. .Fa rw .
  269. .It Dv RA_UNLOCKED
  270. Assert that current thread holds neither a shared nor exclusive lock of
  271. .Fa rw .
  272. .El
  273. .El
  274. .Sh SEE ALSO
  275. .Xr locking 9 ,
  276. .Xr mutex 9 ,
  277. .Xr panic 9 ,
  278. .Xr sema 9 ,
  279. .Xr sx 9
  280. .Sh HISTORY
  281. These
  282. functions appeared in
  283. .Fx 7.0 .
  284. .Sh AUTHORS
  285. .An -nosplit
  286. The
  287. .Nm
  288. facility was written by
  289. .An "John Baldwin" .
  290. This manual page was written by
  291. .An "Gleb Smirnoff" .
  292. .Sh BUGS
  293. If
  294. .Dv WITNESS
  295. is not included in the kernel,
  296. then it is impossible to assert that the current thread does or does not
  297. hold a read lock.
  298. In the
  299. .Pf non- Dv WITNESS
  300. case, the
  301. .Dv RA_LOCKED
  302. and
  303. .Dv RA_RLOCKED
  304. assertions merely check that some thread holds a read lock.
  305. .Pp
  306. Reader/writer is a bit of an awkward name.
  307. An
  308. .Nm
  309. can also be called a
  310. .Dq Robert Watson
  311. lock if desired.