lock.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include "u.h"
  2. #include "lib.h"
  3. #include "dat.h"
  4. #include "fns.h"
  5. #include "io.h"
  6. #include "mem.h"
  7. void
  8. lock(Lock *l)
  9. {
  10. int i;
  11. /*
  12. * Try the fast grab first
  13. */
  14. loop:
  15. if(tas(l) == 0)
  16. return;
  17. for(i = 0; i < 1000000; i++) {
  18. if(tas(l) == 0)
  19. return;
  20. /* If we are spl low resched */
  21. if(getstatus() & IFLAG)
  22. sched();
  23. }
  24. l->sbsem = 0;
  25. print("lock loop 0x%lux held by pc 0x%lux\n", (ulong)l, l->pc);
  26. goto loop;
  27. }
  28. void
  29. ilock(Lock *l)
  30. {
  31. l->sr = splhi();
  32. lock(l);
  33. }
  34. void
  35. iunlock(Lock *l)
  36. {
  37. ulong sr;
  38. sr = l->sr;
  39. l->sbsem = 0;
  40. l->pc = 0;
  41. splx(sr);
  42. }
  43. int
  44. canlock(Lock *l)
  45. {
  46. if (tas(l) == 0)
  47. return 1;
  48. return 0;
  49. }
  50. void
  51. unlock(Lock *l)
  52. {
  53. l->pc = 0;
  54. l->sbsem = 0;
  55. }
  56. void
  57. qlock(QLock *q)
  58. {
  59. User *p;
  60. int i;
  61. lock(q);
  62. if(!q->locked){
  63. q->locked = 1;
  64. unlock(q);
  65. goto out;
  66. }
  67. p = q->tail;
  68. if(p == 0)
  69. q->head = u;
  70. else
  71. p->qnext = u;
  72. q->tail = u;
  73. u->qnext = 0;
  74. u->state = Queueing;
  75. u->has.want = q;
  76. unlock(q);
  77. sched();
  78. u->has.want = 0;
  79. out:
  80. if(1 && u) {
  81. for(i=0; i<NHAS; i++)
  82. if(u->has.q[i] == 0) {
  83. u->has.q[i] = q;
  84. return;
  85. }
  86. print("NHAS(%d) too small\n", NHAS);
  87. }
  88. }
  89. int
  90. canqlock(QLock *q)
  91. {
  92. int i;
  93. lock(q);
  94. if(q->locked){
  95. unlock(q);
  96. return 0;
  97. }
  98. q->locked = 1;
  99. unlock(q);
  100. if(1 && u) {
  101. for(i=0; i<NHAS; i++)
  102. if(u->has.q[i] == 0) {
  103. u->has.q[i] = q;
  104. return 1;
  105. }
  106. print("NHAS(%d) too small\n", NHAS);
  107. }
  108. return 1;
  109. }
  110. void
  111. qunlock(QLock *q)
  112. {
  113. User *p;
  114. int i;
  115. lock(q);
  116. p = q->head;
  117. if(p) {
  118. q->head = p->qnext;
  119. if(q->head == 0)
  120. q->tail = 0;
  121. unlock(q);
  122. ready(p);
  123. } else {
  124. q->locked = 0;
  125. unlock(q);
  126. }
  127. if(1 && u) {
  128. for(i=0; i<NHAS; i++)
  129. if(u->has.q[i] == q) {
  130. u->has.q[i] = 0;
  131. return;
  132. }
  133. panic("qunlock: not there %lux, called from %lux\n",
  134. (ulong)q, getcallerpc(&q));
  135. }
  136. }
  137. /*
  138. * readers/writers lock
  139. * allows 1 writer or many readers
  140. */
  141. void
  142. rlock(RWlock *l)
  143. {
  144. QLock *q;
  145. qlock(&l->wr); /* wait here for writers and exclusion */
  146. q = &l->rd; /* first reader in, qlock(&l->rd) */
  147. lock(q);
  148. q->locked = 1;
  149. l->nread++;
  150. unlock(q);
  151. qunlock(&l->wr);
  152. }
  153. void
  154. runlock(RWlock *l)
  155. {
  156. QLock *q;
  157. User *p;
  158. int n;
  159. q = &l->rd;
  160. lock(q);
  161. n = l->nread - 1;
  162. l->nread = n;
  163. if(n == 0) { /* last reader out, qunlock(&l->rd) */
  164. p = q->head;
  165. if(p) {
  166. q->head = p->qnext;
  167. if(q->head == 0)
  168. q->tail = 0;
  169. unlock(q);
  170. ready(p);
  171. return;
  172. }
  173. q->locked = 0;
  174. }
  175. unlock(q);
  176. }
  177. void
  178. wlock(RWlock *l)
  179. {
  180. qlock(&l->wr); /* wait here for writers and exclusion */
  181. qlock(&l->rd); /* wait here for last reader */
  182. }
  183. void
  184. wunlock(RWlock *l)
  185. {
  186. qunlock(&l->rd);
  187. qunlock(&l->wr);
  188. }