1
0

lock 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. .TH LOCK 2
  2. .SH NAME
  3. lock, canlock, unlock,
  4. qlock, canqlock, qunlock,
  5. rlock, runlock, canrlock,
  6. wlock, wunlock, canwlock,
  7. incref, decref
  8. \- shared memory spin locks, rendez-vous locks, reader-writer locks, and
  9. atomic increment and decrement
  10. .SH SYNOPSIS
  11. .B
  12. #include <u.h>
  13. .br
  14. .B
  15. #include <libc.h>
  16. .PP
  17. .B
  18. void lock(Lock *l)
  19. .PP
  20. .B
  21. int canlock(Lock *l)
  22. .PP
  23. .B
  24. void unlock(Lock *l)
  25. .PP
  26. .B
  27. void qlock(QLock *l)
  28. .PP
  29. .B
  30. void qunlock(QLock *l)
  31. .PP
  32. .B
  33. int canqlock(QLock *l)
  34. .PP
  35. .B
  36. void rlock(RWLock *l)
  37. .PP
  38. .B
  39. void runlock(RWLock *l)
  40. .PP
  41. .B
  42. int canrlock(RWLock *l)
  43. .PP
  44. .B
  45. void wlock(RWLock *l)
  46. .PP
  47. .B
  48. void wunlock(RWLock *l)
  49. .PP
  50. .B
  51. int canwlock(RWLock *l)
  52. .PP
  53. .PP
  54. .B
  55. #include <thread.h>
  56. .PP
  57. .B
  58. typedef struct Ref {
  59. .br
  60. .B
  61. long ref;
  62. .br
  63. .B
  64. } Ref;
  65. .PP
  66. .B
  67. void incref(Ref*)
  68. .PP
  69. .B
  70. long decref(Ref*)
  71. .SH DESCRIPTION
  72. These routines are used to synchronize processes sharing memory.
  73. .PP
  74. The first group
  75. .RI ( lock ,
  76. .IR canlock ,
  77. .IR unlock )
  78. uses spin locks in shared memory.
  79. The second group
  80. .RI ( qlock ,
  81. .IR canqlock ,
  82. .IR qunlock ),
  83. uses rendezvous locks in shared
  84. memory.
  85. The third group
  86. .RI ( rlock ,
  87. .IR runlock ,
  88. .IR canrlock ,
  89. .IR wlock ,
  90. .IR wunlock ,
  91. .IR canwlock ),
  92. also uses rendezvous locks but has slightly different
  93. semantics.
  94. .PP
  95. Locks work in regular programs as well as programs that use the thread library
  96. (see
  97. .IR thread (2)).
  98. The thread library replaces the
  99. .B rendezvous
  100. system call
  101. (see
  102. .IR rendezvous (2))
  103. with its own implementation,
  104. .BR threadrendezvous ,
  105. so that threads as well as processes may be synchronized by locking calls
  106. in threaded programs.
  107. .PP
  108. Used carelessly, spin locks can be expensive and can easily generate deadlocks.
  109. Their use is discouraged, especially in programs that use the
  110. thread library because they prevent context switches between threads.
  111. .PP
  112. .I Lock
  113. blocks until the lock has been obtained.
  114. .I Canlock
  115. is non-blocking.
  116. It tries to obtain a lock and returns a non-zero value if it
  117. was successful, 0 otherwise.
  118. .I Unlock
  119. releases a lock.
  120. .PP
  121. .B QLocks
  122. have the same interface but are not spin locks; instead if the lock is taken
  123. .I qlock
  124. will suspend execution of the calling task until it is released.
  125. .PP
  126. Although
  127. .B Locks
  128. are the more primitive lock, they have limitations; for example,
  129. they cannot synchronize between tasks in the same
  130. .IR proc .
  131. Use
  132. .B QLocks
  133. instead.
  134. .PP
  135. .B RWLocks
  136. manage access to a data structure that has distinct readers and writers.
  137. .I Rlock
  138. grants read access;
  139. .I runlock
  140. releases it.
  141. .I Wlock
  142. grants write access;
  143. .I wunlock
  144. releases it.
  145. .I Canrlock
  146. and
  147. .I canwlock
  148. are the non-blocking versions.
  149. There may be any number of simultaneous readers,
  150. but only one writer.
  151. Moreover,
  152. if write access is granted no one may have
  153. read access until write access is released.
  154. .PP
  155. All types of lock should be initialized to all zeros before use; this
  156. puts them in the unlocked state.
  157. .PP
  158. A
  159. .B Ref
  160. contains a
  161. .B long
  162. that can be incremented and decremented atomically:
  163. .I Incref
  164. increments the
  165. .I Ref
  166. in one atomic operation.
  167. .I Decref
  168. atomically decrements the
  169. .B Ref
  170. and returns zero if the resulting value is zero, non-zero otherwise.
  171. .SH SOURCE
  172. .B /sys/src/libc/port/lock.c
  173. .br
  174. .B /sys/src/libc/9sys/qlock.c
  175. .br
  176. .B /sys/src/libthread/ref.c
  177. .SH SEE ALSO
  178. .I rfork
  179. in
  180. .IR fork (2)