taslock.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. for(;;){
  133. lockstats.inglare++;
  134. splx(x);
  135. while(l->key)
  136. ;
  137. x = splhi();
  138. if(tas(&l->key) == 0)
  139. goto acquire;
  140. }
  141. }
  142. acquire:
  143. m->ilockdepth++;
  144. if(up)
  145. up->lastilock = l;
  146. l->sr = x;
  147. l->pc = pc;
  148. l->p = up;
  149. l->isilock = 1;
  150. l->m = MACHP(m->machno);
  151. #ifdef LOCKCYCLES
  152. cycles(&l->lockcycles);
  153. #endif
  154. }
  155. int
  156. canlock(Lock *l)
  157. {
  158. if(up)
  159. inccnt(&up->nlocks);
  160. if(tas(&l->key)){
  161. if(up)
  162. deccnt(&up->nlocks);
  163. return 0;
  164. }
  165. if(up)
  166. up->lastlock = l;
  167. l->pc = getcallerpc(&l);
  168. l->p = up;
  169. l->m = MACHP(m->machno);
  170. l->isilock = 0;
  171. #ifdef LOCKCYCLES
  172. cycles(&l->lockcycles);
  173. #endif
  174. return 1;
  175. }
  176. void
  177. unlock(Lock *l)
  178. {
  179. #ifdef LOCKCYCLES
  180. uvlong x;
  181. cycles(&x);
  182. l->lockcycles = x - l->lockcycles;
  183. if(l->lockcycles > maxlockcycles){
  184. maxlockcycles = l->lockcycles;
  185. maxlockpc = l->pc;
  186. }
  187. #endif
  188. if(l->key == 0)
  189. print("unlock: not locked: pc %luX\n", getcallerpc(&l));
  190. if(l->isilock)
  191. print("unlock of ilock: pc %lux, held by %lux\n", getcallerpc(&l), l->pc);
  192. if(l->p != up)
  193. 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);
  194. l->m = nil;
  195. l->key = 0;
  196. coherence();
  197. if(up && deccnt(&up->nlocks) == 0 && up->delaysched && islo()){
  198. /*
  199. * Call sched if the need arose while locks were held
  200. * But, don't do it from interrupt routines, hence the islo() test
  201. */
  202. sched();
  203. }
  204. }
  205. void
  206. iunlock(Lock *l)
  207. {
  208. ulong sr;
  209. #ifdef LOCKCYCLES
  210. uvlong x;
  211. cycles(&x);
  212. l->lockcycles = x - l->lockcycles;
  213. if(l->lockcycles > maxilockcycles){
  214. maxilockcycles = l->lockcycles;
  215. maxilockpc = l->pc;
  216. }
  217. #endif
  218. if(l->key == 0)
  219. print("iunlock: not locked: pc %luX\n", getcallerpc(&l));
  220. if(!l->isilock)
  221. print("iunlock of lock: pc %lux, held by %lux\n", getcallerpc(&l), l->pc);
  222. if(islo())
  223. print("iunlock while lo: pc %lux, held by %lux\n", getcallerpc(&l), l->pc);
  224. sr = l->sr;
  225. l->m = nil;
  226. l->key = 0;
  227. coherence();
  228. m->ilockdepth--;
  229. if(up)
  230. up->lastilock = nil;
  231. splx(sr);
  232. }