qlock.c 4.7 KB

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