Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

432 lines
9.7 KiB

  1. /*++
  2. Copyright (c) 1999-2000 Microsoft Corporation
  3. Module Name:
  4. dbglocks.cxx
  5. Abstract:
  6. Locks support
  7. Author:
  8. George V. Reilly (GeorgeRe) 01-Mar-1999
  9. Revision History:
  10. --*/
  11. #include "precomp.hxx"
  12. #ifdef LOCK_INSTRUMENTATION
  13. # define LOCK_DEFAULT_NAME ("lkrdbg")
  14. LONG CSmallSpinLock::sm_cTotalLocks = 0;
  15. LONG CSpinLock::sm_cTotalLocks = 0;
  16. LONG CReaderWriterLock::sm_cTotalLocks = 0;
  17. LONG CReaderWriterLock2::sm_cTotalLocks = 0;
  18. LONG CReaderWriterLock3::sm_cTotalLocks = 0;
  19. LONG CReaderWriterLock4::sm_cTotalLocks = 0;
  20. #else // !LOCK_INSTRUMENTATION
  21. # define LOCK_DEFAULT_NAME
  22. #endif // !LOCK_INSTRUMENTATION
  23. const char*
  24. LockName(
  25. LOCK_LOCKTYPE lt)
  26. {
  27. const char* pszName = NULL;
  28. static char szName[40];
  29. switch (lt)
  30. {
  31. case LOCK_FAKELOCK:
  32. pszName = "CFakeLock";
  33. break;
  34. case LOCK_SMALLSPINLOCK:
  35. pszName = "CSmallSpinLock";
  36. break;
  37. case LOCK_SPINLOCK:
  38. pszName = "CSpinLock";
  39. break;
  40. case LOCK_CRITSEC:
  41. pszName = "CCritSec";
  42. break;
  43. case LOCK_READERWRITERLOCK:
  44. pszName = "CReaderWriterLock";
  45. break;
  46. case LOCK_READERWRITERLOCK2:
  47. pszName = "CReaderWriterLock2";
  48. break;
  49. case LOCK_READERWRITERLOCK3:
  50. pszName = "CReaderWriterLock3";
  51. break;
  52. case LOCK_READERWRITERLOCK4:
  53. pszName = "CReaderWriterLock4";
  54. break;
  55. #ifdef LOCKS_KERNEL_MODE
  56. case LOCK_KSPINLOCK:
  57. pszName = "CKSpinLock";
  58. break;
  59. case LOCK_FASTMUTEX:
  60. pszName = "CFastMutex";
  61. break;
  62. case LOCK_ERESOURCE:
  63. pszName = "CEResource";
  64. break;
  65. case LOCK_RTL_MRSW_LOCK:
  66. pszName = "CRtlMrswLock";
  67. break;
  68. #endif // LOCKS_KERNEL_MODE
  69. default:
  70. sprintf(szName, "UnknownLockType %d", lt);
  71. pszName = szName;
  72. break;
  73. }
  74. return pszName;
  75. };
  76. int
  77. LockSize(
  78. LOCK_LOCKTYPE lt)
  79. {
  80. int cb = 0;
  81. switch (lt)
  82. {
  83. case LOCK_FAKELOCK:
  84. cb = sizeof(CFakeLock);
  85. break;
  86. case LOCK_SMALLSPINLOCK:
  87. cb = sizeof(CSmallSpinLock);
  88. break;
  89. case LOCK_SPINLOCK:
  90. cb = sizeof(CSpinLock);
  91. break;
  92. case LOCK_CRITSEC:
  93. cb = sizeof(CCritSec);
  94. break;
  95. case LOCK_READERWRITERLOCK:
  96. cb = sizeof(CReaderWriterLock);
  97. break;
  98. case LOCK_READERWRITERLOCK2:
  99. cb = sizeof(CReaderWriterLock2);
  100. break;
  101. case LOCK_READERWRITERLOCK3:
  102. cb = sizeof(CReaderWriterLock3);
  103. break;
  104. case LOCK_READERWRITERLOCK4:
  105. cb = sizeof(CReaderWriterLock4);
  106. break;
  107. #ifdef LOCKS_KERNEL_MODE
  108. case LOCK_KSPINLOCK:
  109. cb = sizeof(CKSpinLock);
  110. break;
  111. case LOCK_FASTMUTEX:
  112. cb = sizeof(CFastMutex);
  113. break;
  114. case LOCK_ERESOURCE:
  115. cb = sizeof(CEResource);
  116. break;
  117. case LOCK_RTL_MRSW_LOCK:
  118. cb = sizeof(CRtlMrswLock);
  119. break;
  120. #endif // LOCKS_KERNEL_MODE
  121. default:
  122. cb = 0;
  123. break;
  124. }
  125. return cb;
  126. };
  127. BOOL
  128. Print_FakeLock(
  129. IN PVOID pvLock,
  130. IN INT nVerbose)
  131. {
  132. return TRUE;
  133. }
  134. BOOL
  135. Print_SmallSpinLock(
  136. IN PVOID pvLock,
  137. IN INT nVerbose)
  138. {
  139. CSmallSpinLock sl LOCK_DEFAULT_NAME;
  140. ReadMemory(pvLock, &sl, sizeof(sl), NULL);
  141. dprintf("CSmallSpinLock (%p): Thread = %x\n",
  142. pvLock, sl.m_lTid);
  143. memset(&sl, 0, sizeof(sl));
  144. return TRUE;
  145. }
  146. BOOL
  147. Print_SpinLock(
  148. IN PVOID pvLock,
  149. IN INT nVerbose)
  150. {
  151. CSpinLock sl LOCK_DEFAULT_NAME;
  152. ReadMemory(pvLock, &sl, sizeof(sl), NULL);
  153. dprintf("CSpinLock (%p): Thread = %d, Count = %d\n",
  154. pvLock,
  155. (sl.m_lTid & CSpinLock::SL_THREAD_MASK)
  156. >> CSpinLock::SL_THREAD_SHIFT,
  157. (sl.m_lTid & CSpinLock::SL_OWNER_MASK)
  158. >> CSpinLock::SL_OWNER_SHIFT);
  159. memset(&sl, 0, sizeof(sl));
  160. return TRUE;
  161. }
  162. BOOL
  163. Print_CritSec(
  164. IN PVOID pvLock,
  165. IN INT nVerbose)
  166. {
  167. return TRUE;
  168. }
  169. BOOL
  170. Print_ReaderWriterLock(
  171. IN PVOID pvLock,
  172. IN INT nVerbose)
  173. {
  174. CReaderWriterLock sl LOCK_DEFAULT_NAME;
  175. ReadMemory(pvLock, &sl, sizeof(sl), NULL);
  176. dprintf("CReaderWriterLock (%p): State = %x, Waiters = %d\n",
  177. pvLock, sl.m_nState, sl.m_cWaiting);
  178. memset(&sl, 0, sizeof(sl));
  179. return TRUE;
  180. }
  181. BOOL
  182. Print_ReaderWriterLock2(
  183. IN PVOID pvLock,
  184. IN INT nVerbose)
  185. {
  186. CReaderWriterLock2 sl LOCK_DEFAULT_NAME;
  187. ReadMemory(pvLock, &sl, sizeof(sl), NULL);
  188. dprintf("CReaderWriterLock2 (%p): State = %x, Waiters = %d\n",
  189. pvLock,
  190. (sl.m_lRW & CReaderWriterLock2::SL_STATE_MASK)
  191. >> CReaderWriterLock2::SL_STATE_SHIFT,
  192. (sl.m_lRW & CReaderWriterLock2::SL_WAITING_MASK)
  193. >> CReaderWriterLock2::SL_WAITING_SHIFT
  194. );
  195. memset(&sl, 0, sizeof(sl));
  196. return TRUE;
  197. }
  198. BOOL
  199. Print_ReaderWriterLock3(
  200. IN PVOID pvLock,
  201. IN INT nVerbose)
  202. {
  203. CReaderWriterLock3 sl LOCK_DEFAULT_NAME;
  204. ReadMemory(pvLock, &sl, sizeof(sl), NULL);
  205. int nReaders = ((sl.m_lRW & CReaderWriterLock3::SL_STATE_MASK)
  206. >> CReaderWriterLock3::SL_STATE_SHIFT);
  207. dprintf("CReaderWriterLock3 (%p): State = %x, Readers = %d, Waiters = %d, "
  208. "Thrd = %x, Cnt = %d\n",
  209. pvLock,
  210. (sl.m_lRW & CReaderWriterLock3::SL_STATE_MASK)
  211. >> CReaderWriterLock3::SL_STATE_SHIFT,
  212. nReaders == CReaderWriterLock3::SL_EXCLUSIVE ? 0 : nReaders,
  213. (sl.m_lRW & CReaderWriterLock3::SL_WAITING_MASK)
  214. >> CReaderWriterLock3::SL_WAITING_SHIFT,
  215. (sl.m_lTid & CReaderWriterLock3::SL_THREAD_MASK)
  216. >> CReaderWriterLock3::SL_THREAD_SHIFT,
  217. (sl.m_lTid & CReaderWriterLock3::SL_OWNER_MASK)
  218. >> CReaderWriterLock3::SL_OWNER_SHIFT
  219. );
  220. memset(&sl, 0, sizeof(sl));
  221. return TRUE;
  222. }
  223. BOOL
  224. Print_ReaderWriterLock4(
  225. IN PVOID pvLock,
  226. IN INT nVerbose)
  227. {
  228. CReaderWriterLock4 sl LOCK_DEFAULT_NAME;
  229. ReadMemory(pvLock, &sl, sizeof(sl), NULL);
  230. int nRW = (short) ((sl.m_lRW & CReaderWriterLock4::SL_STATE_MASK)
  231. >> CReaderWriterLock4::SL_STATE_SHIFT);
  232. int nWriterRecursion = ((sl.m_lRW & CReaderWriterLock4::SL_STATE_MASK)
  233. >> CReaderWriterLock4::SL_STATE_SHIFT);
  234. dprintf("CReaderWriterLock4 (%p): RW=%08x, Tid=%x - State = %x, "
  235. "Readers = %d, Waiters = %d, Thrd = %x, Cnt = %d\n",
  236. pvLock,
  237. (sl.m_lRW & CReaderWriterLock4::SL_STATE_MASK)
  238. >> CReaderWriterLock4::SL_STATE_SHIFT,
  239. nRW < 0 ? 0 : nRW,
  240. (sl.m_lRW & CReaderWriterLock4::SL_WAITING_MASK)
  241. >> CReaderWriterLock4::SL_WAITING_SHIFT,
  242. (sl.m_lTid & CReaderWriterLock4::SL_THREAD_MASK)
  243. >> CReaderWriterLock4::SL_THREAD_SHIFT,
  244. nRW < 0 ? -nRW : 0
  245. );
  246. memset(&sl, 0, sizeof(sl));
  247. return TRUE;
  248. }
  249. #ifdef LOCKS_KERNEL_MODE
  250. BOOL
  251. Print_KSpinLock(
  252. IN PVOID pvLock,
  253. IN INT nVerbose)
  254. {
  255. CKSpinLock sl LOCK_DEFAULT_NAME;
  256. ReadMemory(pvLock, &sl, sizeof(sl), NULL);
  257. dprintf("CKSpinLock (%p): \n",
  258. pvLock);
  259. memset(&sl, 0, sizeof(sl));
  260. return TRUE;
  261. }
  262. BOOL
  263. Print_FastMutex(
  264. IN PVOID pvLock,
  265. IN INT nVerbose)
  266. {
  267. CFastMutex sl LOCK_DEFAULT_NAME;
  268. ReadMemory(pvLock, &sl, sizeof(sl), NULL);
  269. dprintf("CFastMutex (%p): \n",
  270. pvLock);
  271. memset(&sl, 0, sizeof(sl));
  272. return TRUE;
  273. }
  274. BOOL
  275. Print_EResource(
  276. IN PVOID pvLock,
  277. IN INT nVerbose)
  278. {
  279. CEResource sl LOCK_DEFAULT_NAME;
  280. ReadMemory(pvLock, &sl, sizeof(sl), NULL);
  281. dprintf("CEResource (%p): \n",
  282. pvLock);
  283. memset(&sl, 0, sizeof(sl));
  284. return TRUE;
  285. }
  286. BOOL
  287. Print_RtlMrswLock(
  288. IN PVOID pvLock,
  289. IN INT nVerbose)
  290. {
  291. CRtlMrswLock sl LOCK_DEFAULT_NAME;
  292. ReadMemory(pvLock, &sl, sizeof(sl), NULL);
  293. dprintf("CRtlMrswLock (%p): \n",
  294. pvLock);
  295. memset(&sl, 0, sizeof(sl));
  296. return TRUE;
  297. }
  298. #endif // LOCKS_KERNEL_MODE
  299. BOOL
  300. PrintLock(
  301. LOCK_LOCKTYPE lt,
  302. IN PVOID pvLock,
  303. IN INT nVerbose)
  304. {
  305. BOOL f = FALSE;
  306. switch (lt)
  307. {
  308. case LOCK_FAKELOCK:
  309. f = Print_FakeLock(pvLock, nVerbose);
  310. break;
  311. case LOCK_SMALLSPINLOCK:
  312. f = Print_SmallSpinLock(pvLock, nVerbose);
  313. break;
  314. case LOCK_SPINLOCK:
  315. f = Print_SpinLock(pvLock, nVerbose);
  316. break;
  317. case LOCK_CRITSEC:
  318. f = Print_CritSec(pvLock, nVerbose);
  319. break;
  320. case LOCK_READERWRITERLOCK:
  321. f = Print_ReaderWriterLock(pvLock, nVerbose);
  322. break;
  323. case LOCK_READERWRITERLOCK2:
  324. f = Print_ReaderWriterLock2(pvLock, nVerbose);
  325. break;
  326. case LOCK_READERWRITERLOCK3:
  327. f = Print_ReaderWriterLock3(pvLock, nVerbose);
  328. break;
  329. case LOCK_READERWRITERLOCK4:
  330. f = Print_ReaderWriterLock4(pvLock, nVerbose);
  331. break;
  332. #ifdef LOCKS_KERNEL_MODE
  333. case LOCK_KSPINLOCK:
  334. f = Print_KSpinLock(pvLock, nVerbose);
  335. break;
  336. case LOCK_FASTMUTEX:
  337. f = Print_FastMutex(pvLock, nVerbose);
  338. break;
  339. case LOCK_ERESOURCE:
  340. f = Print_EResource(pvLock, nVerbose);
  341. break;
  342. case LOCK_RTL_MRSW_LOCK:
  343. f = Print_RtlMrswLock(pvLock, nVerbose);
  344. break;
  345. #endif // LOCKS_KERNEL_MODE
  346. default:
  347. f = FALSE;
  348. break;
  349. }
  350. return f;
  351. }