taslock.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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(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. m->ilockdepth++;
  113. if(up)
  114. up->lastilock = l;
  115. l->sr = x;
  116. l->pc = pc;
  117. l->p = up;
  118. l->isilock = 1;
  119. return;
  120. }
  121. lockstats.glare++;
  122. if(conf.nmach < 2){
  123. dumplockmem("ilock:", l);
  124. panic("ilock: no way out: pc %luX\n", pc);
  125. }
  126. for(;;){
  127. lockstats.inglare++;
  128. splx(x);
  129. while(l->key)
  130. ;
  131. x = splhi();
  132. if(tas(&l->key) == 0){
  133. m->ilockdepth++;
  134. if(up)
  135. up->lastilock = l;
  136. l->sr = x;
  137. l->pc = pc;
  138. l->p = up;
  139. l->isilock = 1;
  140. return;
  141. }
  142. }
  143. }
  144. int
  145. canlock(Lock *l)
  146. {
  147. if(up)
  148. inccnt(&up->nlocks);
  149. if(tas(&l->key)){
  150. if(up)
  151. deccnt(&up->nlocks);
  152. return 0;
  153. }
  154. if(up)
  155. up->lastlock = l;
  156. l->pc = getcallerpc(&l);
  157. l->p = up;
  158. l->isilock = 0;
  159. return 1;
  160. }
  161. void
  162. unlock(Lock *l)
  163. {
  164. if(l->key == 0)
  165. print("unlock: not locked: pc %luX\n", getcallerpc(&l));
  166. if(l->isilock)
  167. print("unlock of ilock: pc %lux, held by %lux\n", getcallerpc(&l), l->pc);
  168. if(l->p != up)
  169. 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);
  170. l->key = 0;
  171. coherence();
  172. if(up && deccnt(&up->nlocks) == 0 && up->delaysched && islo()){
  173. /*
  174. * Call sched if the need arose while locks were held
  175. * But, don't do it from interrupt routines, hence the islo() test
  176. */
  177. sched();
  178. }
  179. }
  180. void
  181. iunlock(Lock *l)
  182. {
  183. ulong sr;
  184. if(l->key == 0)
  185. print("iunlock: not locked: pc %luX\n", getcallerpc(&l));
  186. if(!l->isilock)
  187. print("iunlock of lock: pc %lux, held by %lux\n", getcallerpc(&l), l->pc);
  188. if(islo())
  189. print("iunlock while lo: pc %lux, held by %lux\n", getcallerpc(&l), l->pc);
  190. sr = l->sr;
  191. l->key = 0;
  192. coherence();
  193. m->ilockdepth--;
  194. if(up)
  195. up->lastilock = nil;
  196. splx(sr);
  197. }