Source code of Windows XP (NT5)
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.

464 lines
11 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // The include files for supporting classes.
  4. //
  5. // The include files for supporting classes consists of
  6. // classes refered to or used in this class. The structure
  7. // of each source module is as follows:
  8. // 1. Include files.
  9. // 2. Constants local to the class.
  10. // 3. Data structures local to the class.
  11. // 4. Data initializations.
  12. // 5. Static functions.
  13. // 6. Class functions.
  14. // Sections that are not required are omitted.
  15. //
  16. ///////////////////////////////////////////////////////////////////////////////
  17. #include "precomp.hxx"
  18. #define DLL_IMPLEMENTATION
  19. #define IMPLEMENTATION_EXPORT
  20. #include <Sharelok.h>
  21. //////////////////////////////////////////////////////////////////////
  22. //
  23. // Class constructor.
  24. //
  25. // Create a new lock and initialize it. This call is not
  26. // thread safe and should only be made in a single thread
  27. // environment.
  28. //
  29. //////////////////////////////////////////////////////////////////////
  30. CSharelock::CSharelock( SBIT32 lNewMaxSpins, SBIT32 lNewMaxUsers )
  31. {
  32. //
  33. // Set the initial state.
  34. //
  35. m_lExclusive = 0;
  36. m_lTotalUsers = 0;
  37. m_lWaiting = 0;
  38. //
  39. // Check the configurable values.
  40. //
  41. if ( lNewMaxSpins > 0 )
  42. {
  43. m_lMaxSpins = lNewMaxSpins;
  44. }
  45. else
  46. {
  47. throw (TEXT("Maximum spins invalid in constructor for CSharelock"));
  48. }
  49. if ( (lNewMaxUsers > 0) && (lNewMaxUsers <= m_MaxShareLockUsers) )
  50. {
  51. m_lMaxUsers = lNewMaxUsers;
  52. }
  53. else
  54. {
  55. throw (TEXT("Maximum share invalid in constructor for CSharelock"));
  56. }
  57. //
  58. // Create a semaphore to sleep on when the spin count exceeds
  59. // its maximum.
  60. //
  61. if ( (m_hSemaphore = CreateSemaphore( NULL, 0, m_MaxShareLockUsers, NULL )) == NULL)
  62. {
  63. throw (TEXT("Create semaphore in constructor for CSharelock"));
  64. }
  65. #ifdef _DEBUG
  66. //
  67. // Set the initial state of any debug variables.
  68. //
  69. m_lTotalExclusiveLocks = 0;
  70. m_lTotalShareLocks = 0;
  71. m_lTotalSleeps = 0;
  72. m_lTotalSpins = 0;
  73. m_lTotalTimeouts = 0;
  74. m_lTotalWaits = 0;
  75. #endif
  76. }
  77. //////////////////////////////////////////////////////////////////////
  78. //
  79. // Sleep waiting for the lock.
  80. //
  81. // We have decided it is time to sleep waiting for the lock
  82. // to become free.
  83. //
  84. //////////////////////////////////////////////////////////////////////
  85. BOOLEAN CSharelock::SleepWaitingForLock( SBIT32 lSleep )
  86. {
  87. //
  88. // We have been spinning waiting for the lock but it
  89. // has not become free. Hence, it is now time to
  90. // give up and sleep for a while.
  91. //
  92. (void) InterlockedIncrement( (LPLONG) & m_lWaiting );
  93. //
  94. // Just before we go to sleep we do one final check
  95. // to make sure that the lock is still busy and that
  96. // there is someone to wake us up when it becomes free.
  97. //
  98. if ( m_lTotalUsers > 0 )
  99. {
  100. #ifdef _DEBUG
  101. //
  102. // Count the number of times we have slept on this lock.
  103. //
  104. (void) InterlockedIncrement( (LPLONG) & m_lTotalSleeps );
  105. #endif
  106. //
  107. // When we sleep we awoken when the lock becomes free
  108. // or when we timeout. If we timeout we simply exit
  109. // after decrementing various counters.
  110. if (WaitForSingleObject( m_hSemaphore, lSleep ) != WAIT_OBJECT_0 )
  111. {
  112. #ifdef _DEBUG
  113. //
  114. // Count the number of times we have timed out
  115. // on this lock.
  116. //
  117. (void) InterlockedIncrement( (LPLONG) & m_lTotalTimeouts );
  118. #endif
  119. return FALSE;
  120. }
  121. }
  122. else
  123. {
  124. //
  125. // Lucky - the lock was just freed so lets
  126. // decrement the sleep count and exit without
  127. // sleeping.
  128. //
  129. (void) InterlockedDecrement( (LPLONG) & m_lWaiting );
  130. }
  131. return TRUE;
  132. }
  133. //////////////////////////////////////////////////////////////////////
  134. //
  135. // Update the spin limit.
  136. //
  137. // Update the maximum number of spins while waiting for the lock.
  138. //
  139. //////////////////////////////////////////////////////////////////////
  140. BOOLEAN CSharelock::UpdateMaxSpins( SBIT32 lNewMaxSpins )
  141. {
  142. if ( lNewMaxSpins > 0 )
  143. {
  144. m_lMaxSpins = lNewMaxSpins;
  145. return TRUE;
  146. }
  147. else
  148. {
  149. return FALSE;
  150. }
  151. }
  152. //////////////////////////////////////////////////////////////////////
  153. //
  154. // Update the sharing limit.
  155. //
  156. // Update the maximum number of users that can share the lock.
  157. //
  158. //////////////////////////////////////////////////////////////////////
  159. BOOLEAN CSharelock::UpdateMaxUsers( SBIT32 lNewMaxUsers )
  160. {
  161. if ( (lNewMaxUsers > 0) && (lNewMaxUsers <= m_MaxShareLockUsers) )
  162. {
  163. ClaimExclusiveLock();
  164. m_lMaxUsers = lNewMaxUsers;
  165. ReleaseExclusiveLock();
  166. return TRUE;
  167. }
  168. else
  169. {
  170. return FALSE;
  171. }
  172. }
  173. //////////////////////////////////////////////////////////////////////
  174. //
  175. // Wait for an exclusive lock.
  176. //
  177. // Wait for the spinlock to become free and then claim it.
  178. //
  179. //////////////////////////////////////////////////////////////////////
  180. BOOLEAN CSharelock::WaitForExclusiveLock( SBIT32 lSleep )
  181. {
  182. #ifdef _DEBUG
  183. register SBIT32 lSpins = 0;
  184. register SBIT32 lWaits = 0;
  185. #endif
  186. while ( m_lTotalUsers != 1 )
  187. {
  188. //
  189. // The lock is busy so release it and spin waiting
  190. // for it to become free.
  191. //
  192. (void) InterlockedDecrement( (LPLONG) & m_lTotalUsers );
  193. //
  194. // Find out if we are allowed to spin and sleep if
  195. // necessary.
  196. //
  197. if ( (lSleep > 0) || (lSleep == INFINITE) )
  198. {
  199. register SBIT32 lCount;
  200. //
  201. // Wait by spinning and repeatedly testing the lock.
  202. // We exit when the lock becomes free or the spin limit
  203. // is exceeded.
  204. //
  205. for (lCount = (NumProcessors() < 2) ? 0 : m_lMaxSpins;
  206. (lCount > 0) && (m_lTotalUsers > 0);
  207. lCount -- )
  208. ;
  209. #ifdef _DEBUG
  210. lSpins += (m_lMaxSpins - lCount);
  211. lWaits ++;
  212. #endif
  213. //
  214. // We have exhusted our spin count so it is time to
  215. // sleep waiting for the lock to clear.
  216. //
  217. if ( lCount == 0 )
  218. {
  219. //
  220. // We have decide that we need to sleep but are
  221. // still holding an exclusive lock so lets drop it
  222. // before sleeping.
  223. //
  224. (void) InterlockedDecrement( (LPLONG) & m_lExclusive );
  225. //
  226. // We have decide to go to sleep. If the sleep time
  227. // is not 'INFINITE' then we must subtract the time
  228. // we sleep from our maximum sleep time. If the
  229. // sleep time is 'INFINITE' then we can just skip
  230. // this step.
  231. //
  232. if ( lSleep != INFINITE )
  233. {
  234. register DWORD dwStartTime = GetTickCount();
  235. if ( ! SleepWaitingForLock( lSleep ) )
  236. {
  237. return FALSE;
  238. }
  239. lSleep -= ((GetTickCount() - dwStartTime) + 1);
  240. lSleep = (lSleep > 0) ? lSleep : 0;
  241. }
  242. else
  243. {
  244. if ( ! SleepWaitingForLock( lSleep ) )
  245. {
  246. return FALSE;
  247. }
  248. }
  249. //
  250. // We have woken up again so lets reclaim the
  251. // exclusive lock we had earlier.
  252. //
  253. (void) InterlockedIncrement( (LPLONG) & m_lExclusive );
  254. }
  255. }
  256. else
  257. {
  258. //
  259. // We have decide that we need to exit but are still
  260. // holding an exclusive lock. so lets drop it and leave.
  261. //
  262. (void) InterlockedDecrement( (LPLONG) & m_lExclusive );
  263. return FALSE;
  264. }
  265. //
  266. // Lets test the lock again.
  267. //
  268. InterlockedIncrement( (LPLONG) & m_lTotalUsers );
  269. }
  270. #ifdef _DEBUG
  271. (void) InterlockedExchangeAdd( (LPLONG) & m_lTotalSpins, (LONG) lSpins );
  272. (void) InterlockedExchangeAdd( (LPLONG) & m_lTotalWaits, (LONG) lWaits );
  273. #endif
  274. return TRUE;
  275. }
  276. //////////////////////////////////////////////////////////////////////
  277. //
  278. // Wait for a shared lock.
  279. //
  280. // Wait for the lock to become free and then claim it.
  281. //
  282. //////////////////////////////////////////////////////////////////////
  283. BOOLEAN CSharelock::WaitForShareLock( SBIT32 lSleep )
  284. {
  285. #ifdef _DEBUG
  286. register SBIT32 lSpins = 0;
  287. register SBIT32 lWaits = 0;
  288. #endif
  289. while ( (m_lExclusive > 0) || (m_lTotalUsers > m_lMaxUsers) )
  290. {
  291. //
  292. // The lock is busy so release it and spin waiting
  293. // for it to become free.
  294. //
  295. (void) InterlockedDecrement( (LPLONG) & m_lTotalUsers );
  296. if ( (lSleep > 0) || (lSleep == INFINITE) )
  297. {
  298. register SBIT32 lCount;
  299. //
  300. // Wait by spinning and repeatedly testing the lock.
  301. // We exit when the lock becomes free or the spin limit
  302. // is exceeded.
  303. //
  304. for (lCount = (NumProcessors() < 2) ? 0 : m_lMaxSpins;
  305. (lCount > 0) && ((m_lExclusive > 0) || (m_lTotalUsers >= m_lMaxUsers));
  306. lCount -- )
  307. ;
  308. #ifdef _DEBUG
  309. lSpins += (m_lMaxSpins - lCount);
  310. lWaits ++;
  311. #endif
  312. //
  313. // We have exhusted our spin count so it is time to
  314. // sleep waiting for the lock to clear.
  315. //
  316. if ( lCount == 0 )
  317. {
  318. //
  319. // We have decide to go to sleep. If the sleep time
  320. // is not 'INFINITE' then we must subtract the time
  321. // we sleep from our maximum sleep time. If the
  322. // sleep time is 'INFINITE' then we can just skip
  323. // this step.
  324. //
  325. if ( lSleep != INFINITE )
  326. {
  327. register DWORD dwStartTime = GetTickCount();
  328. if ( ! SleepWaitingForLock( lSleep ) )
  329. {
  330. return FALSE;
  331. }
  332. lSleep -= ((GetTickCount() - dwStartTime) + 1);
  333. lSleep = (lSleep > 0) ? lSleep : 0;
  334. }
  335. else
  336. {
  337. if ( ! SleepWaitingForLock( lSleep ) )
  338. {
  339. return FALSE;
  340. }
  341. }
  342. }
  343. }
  344. else
  345. {
  346. return FALSE;
  347. }
  348. //
  349. // Lets test the lock again.
  350. //
  351. (void) InterlockedIncrement( (LPLONG) & m_lTotalUsers );
  352. }
  353. #ifdef _DEBUG
  354. (void) InterlockedExchangeAdd( (LPLONG) & m_lTotalSpins, (LONG) lSpins );
  355. (void) InterlockedExchangeAdd( (LPLONG) & m_lTotalWaits, (LONG) lWaits );
  356. #endif
  357. return TRUE;
  358. }
  359. //////////////////////////////////////////////////////////////////////
  360. //
  361. // Wake all sleepers.
  362. //
  363. // Wake all the sleepers who are waiting for the spinlock.
  364. // All sleepers are woken because this is much more efficent
  365. // and it is known that the lock latency is short.
  366. //
  367. //////////////////////////////////////////////////////////////////////
  368. void CSharelock::WakeAllSleepers( void )
  369. {
  370. register LONG lWakeup = InterlockedExchange( (LPLONG) & m_lWaiting, 0 );
  371. if ( lWakeup > 0 )
  372. {
  373. //
  374. // Wake up all sleepers as the lock has just been freed.
  375. // It is a straight race to decide who gets the lock next.
  376. //
  377. if ( ! ReleaseSemaphore( m_hSemaphore, lWakeup, NULL ) )
  378. {
  379. throw (TEXT("Wakeup failed in ReleaseLock()"));
  380. }
  381. }
  382. else
  383. {
  384. //
  385. // When multiple threads pass through the critical section rapidly
  386. // it is possible for the 'Waiting' count to become negative.
  387. // This should be very rare but such a negative value needs to be
  388. // preserved.
  389. //
  390. InterlockedExchangeAdd( (LPLONG) & m_lWaiting, lWakeup );
  391. }
  392. }
  393. //////////////////////////////////////////////////////////////////////
  394. //
  395. // Class destructor.
  396. //
  397. // Destroy a lock. This call is not thread safe and should
  398. // only be made in a single thread environment.
  399. //
  400. //////////////////////////////////////////////////////////////////////
  401. CSharelock::~CSharelock( void )
  402. {
  403. if ( ! CloseHandle( m_hSemaphore ) )
  404. {
  405. throw (TEXT("Close semaphore in destructor for CSharelock"));
  406. }
  407. }