taslock.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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("deccnt 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(conf.nmach < 2 && up && up->edf && (up->edf->flags & Admitted)){
  75. /*
  76. * Priority inversion, yield on a uniprocessor; on a
  77. * multiprocessor, the other processor will unlock
  78. */
  79. print("inversion 0x%lux pc 0x%lux proc %lud held by pc 0x%lux proc %lud\n",
  80. l, pc, up ? up->pid : 0, l->pc, l->p ? l->p->pid : 0);
  81. up->edf->d = todget(nil); /* yield to process with lock */
  82. }
  83. if(i++ > 100000000){
  84. i = 0;
  85. lockloop(l, pc);
  86. }
  87. }
  88. if(up)
  89. inccnt(&up->nlocks);
  90. if(tas(&l->key) == 0){
  91. if(up)
  92. up->lastlock = l;
  93. l->pc = pc;
  94. l->p = up;
  95. l->isilock = 0;
  96. return 1;
  97. }
  98. if(up)
  99. deccnt(&up->nlocks);
  100. }
  101. return 0; /* For the compiler */
  102. }
  103. void
  104. ilock(Lock *l)
  105. {
  106. ulong x;
  107. ulong pc;
  108. pc = getcallerpc(&l);
  109. lockstats.locks++;
  110. x = splhi();
  111. if(tas(&l->key) != 0){
  112. lockstats.glare++;
  113. /*
  114. * Cannot also check l->pc and l->m here because
  115. * they might just not be set yet, or the lock might
  116. * even have been let go.
  117. */
  118. if(!l->isilock){
  119. dumplockmem("ilock:", l);
  120. panic("corrupt ilock %p pc=%luX m=%p isilock=%d",
  121. l, l->pc, l->m, l->isilock);
  122. }
  123. if(l->m == MACHP(m->machno))
  124. panic("ilock: deadlock on cpu%d pc=%luX lockpc=%luX\n",
  125. m->machno, pc, l->pc);
  126. for(;;){
  127. lockstats.inglare++;
  128. splx(x);
  129. while(l->key)
  130. ;
  131. x = splhi();
  132. if(tas(&l->key) == 0)
  133. goto acquire;
  134. }
  135. }
  136. acquire:
  137. m->ilockdepth++;
  138. if(up)
  139. up->lastilock = l;
  140. l->sr = x;
  141. l->pc = pc;
  142. l->p = up;
  143. l->isilock = 1;
  144. l->m = MACHP(m->machno);
  145. }
  146. int
  147. canlock(Lock *l)
  148. {
  149. if(up)
  150. inccnt(&up->nlocks);
  151. if(tas(&l->key)){
  152. if(up)
  153. deccnt(&up->nlocks);
  154. return 0;
  155. }
  156. if(up)
  157. up->lastlock = l;
  158. l->pc = getcallerpc(&l);
  159. l->p = up;
  160. l->m = MACHP(m->machno);
  161. l->isilock = 0;
  162. return 1;
  163. }
  164. void
  165. unlock(Lock *l)
  166. {
  167. if(l->key == 0)
  168. print("unlock: not locked: pc %luX\n", getcallerpc(&l));
  169. if(l->isilock)
  170. print("unlock of ilock: pc %lux, held by %lux\n", getcallerpc(&l), l->pc);
  171. if(l->p != up)
  172. 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);
  173. l->m = nil;
  174. l->key = 0;
  175. coherence();
  176. if(up && deccnt(&up->nlocks) == 0 && up->delaysched && islo()){
  177. /*
  178. * Call sched if the need arose while locks were held
  179. * But, don't do it from interrupt routines, hence the islo() test
  180. */
  181. sched();
  182. }
  183. }
  184. void
  185. iunlock(Lock *l)
  186. {
  187. ulong sr;
  188. if(l->key == 0)
  189. print("iunlock: not locked: pc %luX\n", getcallerpc(&l));
  190. if(!l->isilock)
  191. print("iunlock of lock: pc %lux, held by %lux\n", getcallerpc(&l), l->pc);
  192. if(islo())
  193. print("iunlock while lo: pc %lux, held by %lux\n", getcallerpc(&l), l->pc);
  194. sr = l->sr;
  195. l->m = nil;
  196. l->key = 0;
  197. coherence();
  198. m->ilockdepth--;
  199. if(up)
  200. up->lastilock = nil;
  201. splx(sr);
  202. }