taslock.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. uvlong maxlockcycles;
  9. uvlong maxilockcycles;
  10. ulong maxlockpc;
  11. ulong maxilockpc;
  12. struct
  13. {
  14. ulong locks;
  15. ulong glare;
  16. ulong inglare;
  17. } lockstats;
  18. static void
  19. inccnt(Ref *r)
  20. {
  21. _xinc(&r->ref);
  22. }
  23. static int
  24. deccnt(Ref *r)
  25. {
  26. int x;
  27. x = _xdec(&r->ref);
  28. if(x < 0)
  29. panic("deccnt pc=0x%lux", getcallerpc(&r));
  30. return x;
  31. }
  32. static void
  33. dumplockmem(char *tag, Lock *l)
  34. {
  35. uchar *cp;
  36. int i;
  37. iprint("%s: ", tag);
  38. cp = (uchar*)l;
  39. for(i = 0; i < 64; i++)
  40. iprint("%2.2ux ", cp[i]);
  41. iprint("\n");
  42. }
  43. void
  44. lockloop(Lock *l, ulong pc)
  45. {
  46. Proc *p;
  47. p = l->p;
  48. print("lock 0x%lux loop key 0x%lux pc 0x%lux held by pc 0x%lux proc %lud\n",
  49. l, l->key, pc, l->pc, p ? p->pid : 0);
  50. dumpaproc(up);
  51. if(p != nil)
  52. dumpaproc(p);
  53. }
  54. int
  55. lock(Lock *l)
  56. {
  57. int i;
  58. ulong pc;
  59. pc = getcallerpc(&l);
  60. lockstats.locks++;
  61. if(up)
  62. inccnt(&up->nlocks); /* prevent being scheded */
  63. if(tas(&l->key) == 0){
  64. if(up)
  65. up->lastlock = l;
  66. l->pc = pc;
  67. l->p = up;
  68. l->isilock = 0;
  69. #ifdef LOCKCYCLES
  70. cycles(&l->lockcycles);
  71. #endif
  72. return 0;
  73. }
  74. if(up)
  75. deccnt(&up->nlocks);
  76. lockstats.glare++;
  77. for(;;){
  78. lockstats.inglare++;
  79. i = 0;
  80. while(l->key){
  81. if(conf.nmach < 2 && up && up->edf && (up->edf->flags & Admitted)){
  82. /*
  83. * Priority inversion, yield on a uniprocessor; on a
  84. * multiprocessor, the other processor will unlock
  85. */
  86. print("inversion 0x%lux pc 0x%lux proc %lud held by pc 0x%lux proc %lud\n",
  87. l, pc, up ? up->pid : 0, l->pc, l->p ? l->p->pid : 0);
  88. up->edf->d = todget(nil); /* yield to process with lock */
  89. }
  90. if(i++ > 100000000){
  91. i = 0;
  92. lockloop(l, pc);
  93. }
  94. }
  95. if(up)
  96. inccnt(&up->nlocks);
  97. if(tas(&l->key) == 0){
  98. if(up)
  99. up->lastlock = l;
  100. l->pc = pc;
  101. l->p = up;
  102. l->isilock = 0;
  103. #ifdef LOCKCYCLES
  104. cycles(&l->lockcycles);
  105. #endif
  106. return 1;
  107. }
  108. if(up)
  109. deccnt(&up->nlocks);
  110. }
  111. }
  112. void
  113. ilock(Lock *l)
  114. {
  115. ulong x;
  116. ulong pc;
  117. pc = getcallerpc(&l);
  118. lockstats.locks++;
  119. x = splhi();
  120. if(tas(&l->key) != 0){
  121. lockstats.glare++;
  122. /*
  123. * Cannot also check l->pc and l->m here because
  124. * they might just not be set yet, or the lock might
  125. * even have been let go.
  126. */
  127. if(!l->isilock){
  128. dumplockmem("ilock:", l);
  129. panic("corrupt ilock %p pc=%luX m=%p isilock=%d",
  130. l, l->pc, l->m, l->isilock);
  131. }
  132. if(l->m == MACHP(m->machno))
  133. panic("ilock: deadlock on cpu%d pc=%luX lockpc=%luX\n",
  134. m->machno, pc, l->pc);
  135. for(;;){
  136. lockstats.inglare++;
  137. splx(x);
  138. while(l->key)
  139. ;
  140. x = splhi();
  141. if(tas(&l->key) == 0)
  142. goto acquire;
  143. }
  144. }
  145. acquire:
  146. m->ilockdepth++;
  147. if(up)
  148. up->lastilock = l;
  149. l->sr = x;
  150. l->pc = pc;
  151. l->p = up;
  152. l->isilock = 1;
  153. l->m = MACHP(m->machno);
  154. #ifdef LOCKCYCLES
  155. cycles(&l->lockcycles);
  156. #endif
  157. }
  158. int
  159. canlock(Lock *l)
  160. {
  161. if(up)
  162. inccnt(&up->nlocks);
  163. if(tas(&l->key)){
  164. if(up)
  165. deccnt(&up->nlocks);
  166. return 0;
  167. }
  168. if(up)
  169. up->lastlock = l;
  170. l->pc = getcallerpc(&l);
  171. l->p = up;
  172. l->m = MACHP(m->machno);
  173. l->isilock = 0;
  174. #ifdef LOCKCYCLES
  175. cycles(&l->lockcycles);
  176. #endif
  177. return 1;
  178. }
  179. void
  180. unlock(Lock *l)
  181. {
  182. #ifdef LOCKCYCLES
  183. uvlong x;
  184. cycles(&x);
  185. l->lockcycles = x - l->lockcycles;
  186. if(l->lockcycles > maxlockcycles){
  187. maxlockcycles = l->lockcycles;
  188. maxlockpc = l->pc;
  189. }
  190. #endif
  191. if(l->key == 0)
  192. print("unlock: not locked: pc %luX\n", getcallerpc(&l));
  193. if(l->isilock)
  194. print("unlock of ilock: pc %lux, held by %lux\n", getcallerpc(&l), l->pc);
  195. if(l->p != up)
  196. 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);
  197. l->m = nil;
  198. l->key = 0;
  199. coherence();
  200. if(up && deccnt(&up->nlocks) == 0 && up->delaysched && islo()){
  201. /*
  202. * Call sched if the need arose while locks were held
  203. * But, don't do it from interrupt routines, hence the islo() test
  204. */
  205. sched();
  206. }
  207. }
  208. void
  209. iunlock(Lock *l)
  210. {
  211. ulong sr;
  212. #ifdef LOCKCYCLES
  213. uvlong x;
  214. cycles(&x);
  215. l->lockcycles = x - l->lockcycles;
  216. if(l->lockcycles > maxilockcycles){
  217. maxilockcycles = l->lockcycles;
  218. maxilockpc = l->pc;
  219. }
  220. #endif
  221. if(l->key == 0)
  222. print("iunlock: not locked: pc %luX\n", getcallerpc(&l));
  223. if(!l->isilock)
  224. print("iunlock of lock: pc %lux, held by %lux\n", getcallerpc(&l), l->pc);
  225. if(islo())
  226. print("iunlock while lo: pc %lux, held by %lux\n", getcallerpc(&l), l->pc);
  227. sr = l->sr;
  228. l->m = nil;
  229. l->key = 0;
  230. coherence();
  231. m->ilockdepth--;
  232. if(up)
  233. up->lastilock = nil;
  234. splx(sr);
  235. }