qlock.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include <trace.h>
  15. QLockstats qlockstats;
  16. static void
  17. lockstat(uintptr pc, uint64_t w)
  18. {
  19. addwaitstat(pc, w, WSqlock);
  20. }
  21. static void
  22. slockstat(uintptr pc, uint64_t w)
  23. {
  24. addwaitstat(pc, w, WSslock);
  25. }
  26. void
  27. qlock(QLock *q)
  28. {
  29. Proc *p;
  30. uint64_t t0;
  31. cycles(&t0);
  32. if(m->ilockdepth != 0)
  33. print("qlock: %#p: ilockdepth %d", getcallerpc(&q), m->ilockdepth);
  34. if(up != nil && up->nlocks)
  35. print("qlock: %#p: nlocks %d", getcallerpc(&q), up->nlocks);
  36. if(!canlock(&q->use)){
  37. lock(&q->use);
  38. slockstat(getcallerpc(&q), t0);
  39. }
  40. qlockstats.qlock++;
  41. if(!q->locked) {
  42. q->locked = 1;
  43. q->pc = getcallerpc(&q);
  44. unlock(&q->use);
  45. return;
  46. }
  47. if(up == nil)
  48. panic("qlock");
  49. qlockstats.qlockq++;
  50. p = q->tail;
  51. if(p == 0)
  52. q->head = up;
  53. else
  54. p->qnext = up;
  55. q->tail = up;
  56. up->qnext = 0;
  57. up->state = Queueing;
  58. up->qpc = getcallerpc(&q);
  59. if(up->trace)
  60. proctrace(up, SLock, 0);
  61. unlock(&q->use);
  62. sched();
  63. lockstat(getcallerpc(&q), t0);
  64. }
  65. int
  66. canqlock(QLock *q)
  67. {
  68. if(!canlock(&q->use))
  69. return 0;
  70. if(q->locked){
  71. unlock(&q->use);
  72. return 0;
  73. }
  74. q->locked = 1;
  75. q->pc = getcallerpc(&q);
  76. unlock(&q->use);
  77. return 1;
  78. }
  79. void
  80. qunlock(QLock *q)
  81. {
  82. Proc *p;
  83. uint64_t t0;
  84. if(!canlock(&q->use)){
  85. cycles(&t0);
  86. lock(&q->use);
  87. slockstat(getcallerpc(&q), t0);
  88. }
  89. if (q->locked == 0)
  90. print("qunlock called with qlock not held, from %#p\n",
  91. getcallerpc(&q));
  92. p = q->head;
  93. if(p){
  94. q->head = p->qnext;
  95. if(q->head == 0)
  96. q->tail = 0;
  97. unlock(&q->use);
  98. q->pc = p->qpc;
  99. ready(p);
  100. return;
  101. }
  102. q->locked = 0;
  103. q->pc = 0;
  104. unlock(&q->use);
  105. }
  106. void
  107. rlock(RWlock *q)
  108. {
  109. Proc *p;
  110. uint64_t t0;
  111. cycles(&t0);
  112. if(!canlock(&q->use)){
  113. lock(&q->use);
  114. slockstat(getcallerpc(&q), t0);
  115. }
  116. qlockstats.rlock++;
  117. if(q->writer == 0 && q->head == nil){
  118. /* no writer, go for it */
  119. q->readers++;
  120. unlock(&q->use);
  121. return;
  122. }
  123. qlockstats.rlockq++;
  124. p = q->tail;
  125. if(up == nil)
  126. panic("rlock");
  127. if(p == 0)
  128. q->head = up;
  129. else
  130. p->qnext = up;
  131. q->tail = up;
  132. up->qnext = 0;
  133. up->state = QueueingR;
  134. if(up->trace)
  135. proctrace(up, SLock, 0);
  136. unlock(&q->use);
  137. sched();
  138. lockstat(getcallerpc(&q), t0);
  139. }
  140. void
  141. runlock(RWlock *q)
  142. {
  143. Proc *p;
  144. uint64_t t0;
  145. if(!canlock(&q->use)){
  146. cycles(&t0);
  147. lock(&q->use);
  148. slockstat(getcallerpc(&q), t0);
  149. }
  150. p = q->head;
  151. if(--(q->readers) > 0 || p == nil){
  152. unlock(&q->use);
  153. return;
  154. }
  155. /* start waiting writer */
  156. if(p->state != QueueingW)
  157. panic("runlock");
  158. q->head = p->qnext;
  159. if(q->head == 0)
  160. q->tail = 0;
  161. q->writer = 1;
  162. unlock(&q->use);
  163. ready(p);
  164. }
  165. void
  166. wlock(RWlock *q)
  167. {
  168. Proc *p;
  169. uint64_t t0;
  170. cycles(&t0);
  171. if(!canlock(&q->use)){
  172. lock(&q->use);
  173. slockstat(getcallerpc(&q), t0);
  174. }
  175. qlockstats.wlock++;
  176. if(q->readers == 0 && q->writer == 0){
  177. /* noone waiting, go for it */
  178. q->wpc = getcallerpc(&q);
  179. q->wproc = up;
  180. q->writer = 1;
  181. unlock(&q->use);
  182. return;
  183. }
  184. /* wait */
  185. qlockstats.wlockq++;
  186. p = q->tail;
  187. if(up == nil)
  188. panic("wlock");
  189. if(p == nil)
  190. q->head = up;
  191. else
  192. p->qnext = up;
  193. q->tail = up;
  194. up->qnext = 0;
  195. up->state = QueueingW;
  196. if(up->trace)
  197. proctrace(up, SLock, 0);
  198. unlock(&q->use);
  199. sched();
  200. lockstat(getcallerpc(&q), t0);
  201. }
  202. void
  203. wunlock(RWlock *q)
  204. {
  205. Proc *p;
  206. uint64_t t0;
  207. if(!canlock(&q->use)){
  208. cycles(&t0);
  209. lock(&q->use);
  210. slockstat(getcallerpc(&q), t0);
  211. }
  212. p = q->head;
  213. if(p == nil){
  214. q->writer = 0;
  215. unlock(&q->use);
  216. return;
  217. }
  218. if(p->state == QueueingW){
  219. /* start waiting writer */
  220. q->head = p->qnext;
  221. if(q->head == nil)
  222. q->tail = nil;
  223. unlock(&q->use);
  224. ready(p);
  225. return;
  226. }
  227. if(p->state != QueueingR)
  228. panic("wunlock");
  229. /* waken waiting readers */
  230. while(q->head != nil && q->head->state == QueueingR){
  231. p = q->head;
  232. q->head = p->qnext;
  233. q->readers++;
  234. ready(p);
  235. }
  236. if(q->head == nil)
  237. q->tail = nil;
  238. q->writer = 0;
  239. unlock(&q->use);
  240. }
  241. /* same as rlock but punts if there are any writers waiting */
  242. int
  243. canrlock(RWlock *q)
  244. {
  245. uint64_t t0;
  246. if(!canlock(&q->use)){
  247. cycles(&t0);
  248. lock(&q->use);
  249. slockstat(getcallerpc(&q), t0);
  250. }
  251. qlockstats.rlock++;
  252. if(q->writer == 0 && q->head == nil){
  253. /* no writer, go for it */
  254. q->readers++;
  255. unlock(&q->use);
  256. return 1;
  257. }
  258. unlock(&q->use);
  259. return 0;
  260. }