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.

143 lines
3.5 KiB

  1. //+------------------------------------------------------------
  2. //
  3. // Copyright (C) 1998, Microsoft Corporation
  4. //
  5. // File: spinlock.cpp
  6. //
  7. // Contents: Simple Spinlock package used by CLdapConnection
  8. //
  9. // Classes:
  10. //
  11. // Functions:
  12. // AcquireSpinLockSingleProc
  13. // AcquireSpinLockMultiProc
  14. // InitializeSpinLock
  15. // ReleaseSpinLock
  16. //
  17. // History:
  18. // jstamerj 980511 17:26:26: Created.
  19. //
  20. //-------------------------------------------------------------
  21. #include "precomp.h"
  22. PFN_ACQUIRESPINLOCK g_AcquireSpinLock;
  23. //+----------------------------------------------------------------------------
  24. //
  25. // Function: InitializeSpinLock
  26. //
  27. // Synopsis: Initializes a SPIN_LOCK
  28. //
  29. // Arguments: [psl] -- Pointer to SPIN_LOCK to initialize
  30. //
  31. // Returns: Nothing. *psl is in released state when this functionr returns
  32. //
  33. //-----------------------------------------------------------------------------
  34. VOID InitializeSpinLock(
  35. PSPIN_LOCK psl)
  36. {
  37. *psl = 0;
  38. if(g_AcquireSpinLock == NULL) {
  39. // Determine multi or single proc
  40. SYSTEM_INFO si;
  41. GetSystemInfo(&si);
  42. if(si.dwNumberOfProcessors > 1) {
  43. g_AcquireSpinLock = AcquireSpinLockMultipleProc;
  44. } else {
  45. g_AcquireSpinLock = AcquireSpinLockSingleProc;
  46. }
  47. }
  48. }
  49. //+----------------------------------------------------------------------------
  50. //
  51. // Function: AcquireSpinLockMultiProc
  52. //
  53. // Synopsis: Acquire a lock, spinning while it is unavailable.
  54. // Optimized for multi proc machines
  55. //
  56. // Arguments: [psl] -- Pointer to SPIN_LOCK to acquire
  57. //
  58. // Returns: Nothing. *psl is in acquired state when this function returns
  59. //
  60. //-----------------------------------------------------------------------------
  61. VOID AcquireSpinLockMultipleProc(
  62. volatile PSPIN_LOCK psl)
  63. {
  64. do {
  65. //
  66. // Spin while the lock is unavailable
  67. //
  68. while (*psl > 0) {
  69. ;
  70. }
  71. //
  72. // Lock just became available, try to grab it
  73. //
  74. } while ( InterlockedIncrement(psl) != 1 );
  75. }
  76. //+----------------------------------------------------------------------------
  77. //
  78. // Function: AcquireSpinLockSingleProc
  79. //
  80. // Synopsis: Acquire a lock, spinning while it is unavailable.
  81. // Optimized for single proc machines
  82. //
  83. // Arguments: [psl] -- Pointer to SPIN_LOCK to acquire
  84. //
  85. // Returns: Nothing. *psl is in acquired state when this function returns
  86. //
  87. //-----------------------------------------------------------------------------
  88. VOID AcquireSpinLockSingleProc(
  89. volatile PSPIN_LOCK psl)
  90. {
  91. do {
  92. //
  93. // Spin while the lock is unavailable
  94. //
  95. while (*psl > 0) {
  96. Sleep(0);
  97. }
  98. //
  99. // Lock just became available, try to grab it
  100. //
  101. } while ( InterlockedIncrement(psl) != 1 );
  102. }
  103. //+----------------------------------------------------------------------------
  104. //
  105. // Function: ReleaseSpinLock
  106. //
  107. // Synopsis: Releases an acquired spin lock
  108. //
  109. // Arguments: [psl] -- Pointer to SPIN_LOCK to release.
  110. //
  111. // Returns: Nothing. *psl is in released state when this function returns
  112. //
  113. //-----------------------------------------------------------------------------
  114. VOID ReleaseSpinLock(
  115. PSPIN_LOCK psl)
  116. {
  117. _ASSERT( *psl > 0 );
  118. InterlockedExchange( psl, 0 );
  119. }