taslock.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "../port/error.h"
  7. #include "edf.h"
  8. struct
  9. {
  10. ulong locks;
  11. ulong glare;
  12. ulong inglare;
  13. } lockstats;
  14. static void
  15. inccnt(Ref *r)
  16. {
  17. _xinc(&r->ref);
  18. }
  19. static int
  20. deccnt(Ref *r)
  21. {
  22. int x;
  23. x = _xdec(&r->ref);
  24. if(x < 0)
  25. panic("decref pc=0x%lux", getcallerpc(&r));
  26. return x;
  27. }
  28. static void
  29. dumplockmem(char *tag, Lock *l)
  30. {
  31. uchar *cp;
  32. int i;
  33. iprint("%s: ", tag);
  34. cp = (uchar*)l;
  35. for(i = 0; i < 64; i++)
  36. iprint("%2.2ux ", cp[i]);
  37. iprint("\n");
  38. }
  39. void
  40. lockloop(Lock *l, ulong pc)
  41. {
  42. Proc *p;
  43. p = l->p;
  44. print("lock 0x%lux loop key 0x%lux pc 0x%lux held by pc 0x%lux proc %lud\n",
  45. l, l->key, pc, l->pc, p ? p->pid : 0);
  46. dumpaproc(up);
  47. if(p != nil)
  48. dumpaproc(p);
  49. }
  50. int
  51. lock(Lock *l)
  52. {
  53. int i;
  54. ulong pc;
  55. pc = getcallerpc(&l);
  56. lockstats.locks++;
  57. if(up)
  58. inccnt(&up->nlocks); /* prevent being scheded */
  59. if(tas(&l->key) == 0){
  60. if(up)
  61. up->lastlock = l;
  62. l->pc = pc;
  63. l->p = up;
  64. l->isilock = 0;
  65. return 0;
  66. }
  67. if(up)
  68. deccnt(&up->nlocks);
  69. lockstats.glare++;
  70. for(;;){
  71. lockstats.inglare++;
  72. i = 0;
  73. while(l->key){
  74. if(up && up->edf && (up->edf->flags & Admitted)){
  75. /* priority inversion, yield */
  76. print("inversion 0x%lux pc 0x%lux proc %lud held by pc 0x%lux proc %lud\n",
  77. l, pc, up ? up->pid : 0, l->pc, l->p ? l->p->pid : 0);
  78. edfyield();
  79. }
  80. if(i++ > 100000000){
  81. i = 0;
  82. lockloop(l, pc);
  83. }
  84. }
  85. if(up)
  86. inccnt(&up->nlocks);
  87. if(tas(&l->key) == 0){
  88. if(up)
  89. up->lastlock = l;
  90. l->pc = pc;
  91. l->p = up;
  92. l->isilock = 0;
  93. return 1;
  94. }
  95. if(up)
  96. deccnt(&up->nlocks);
  97. }
  98. return 0; /* For the compiler */
  99. }
  100. void
  101. ilock(Lock *l)
  102. {
  103. ulong x;
  104. ulong pc;
  105. pc = getcallerpc(&l);
  106. lockstats.locks++;
  107. x = splhi();
  108. if(tas(&l->key) == 0){
  109. m->ilockdepth++;
  110. if(up)
  111. up->lastilock = l;
  112. l->sr = x;
  113. l->pc = pc;
  114. l->p = up;
  115. l->isilock = 1;
  116. return;
  117. }
  118. lockstats.glare++;
  119. if(conf.nmach < 2){
  120. dumplockmem("ilock:", l);
  121. panic("ilock: no way out: pc %luX\n", pc);
  122. }
  123. for(;;){
  124. lockstats.inglare++;
  125. splx(x);
  126. while(l->key)
  127. ;
  128. x = splhi();
  129. if(tas(&l->key) == 0){
  130. m->ilockdepth++;
  131. if(up)
  132. up->lastilock = l;
  133. l->sr = x;
  134. l->pc = pc;
  135. l->p = up;
  136. l->isilock = 1;
  137. return;
  138. }
  139. }
  140. }
  141. int
  142. canlock(Lock *l)
  143. {
  144. if(up)
  145. inccnt(&up->nlocks);
  146. if(tas(&l->key)){
  147. if(up)
  148. deccnt(&up->nlocks);
  149. return 0;
  150. }
  151. if(up)
  152. up->lastlock = l;
  153. l->pc = getcallerpc(&l);
  154. l->p = up;
  155. l->isilock = 0;
  156. return 1;
  157. }
  158. void
  159. unlock(Lock *l)
  160. {
  161. if(l->key == 0)
  162. print("unlock: not locked: pc %luX\n", getcallerpc(&l));
  163. if(l->isilock)
  164. print("unlock of ilock: pc %lux, held by %lux\n", getcallerpc(&l), l->pc);
  165. if(l->p != up)
  166. print("unlock: up changed: pc %lux, acquired at pc %lux, lock p 0x%p, unlock up 0x%p\n", getcallerpc(&l), l->pc, l->p, up);
  167. l->key = 0;
  168. coherence();
  169. if(up && deccnt(&up->nlocks) == 0 && up->delaysched && islo()){
  170. /*
  171. * Call sched if the need arose while locks were held
  172. * But, don't do it from interrupt routines, hence the islo() test
  173. */
  174. sched();
  175. }
  176. }
  177. void
  178. iunlock(Lock *l)
  179. {
  180. ulong sr;
  181. if(l->key == 0)
  182. print("iunlock: not locked: pc %luX\n", getcallerpc(&l));
  183. if(!l->isilock)
  184. print("iunlock of lock: pc %lux, held by %lux\n", getcallerpc(&l), l->pc);
  185. if(islo())
  186. print("iunlock while lo: pc %lux, held by %lux\n", getcallerpc(&l), l->pc);
  187. sr = l->sr;
  188. l->key = 0;
  189. coherence();
  190. m->splpc = getcallerpc(&l);
  191. m->ilockdepth--;
  192. if(up)
  193. up->lastilock = nil;
  194. splxpc(sr);
  195. }