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.

110 lines
2.6 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. win32simplelock.h
  5. Abstract:
  6. works downlevel to Win95/NT3.
  7. can be statically initialized, but not with all zeros.
  8. never runs out of memory
  9. does not wait or boost-upon-exit efficiently.
  10. must be held for only short periods of time.
  11. should perhaps be called spinlock
  12. can be taken recursively.
  13. can only be taken exclusively, NOT reader/writer.
  14. acquire has a "SleepCount" parameter:
  15. 0 is like TryEnterCriticalSection
  16. INFINITE is like EnterCriticalSection
  17. SHOULD have a spincount to scale hot locks on multiprocs
  18. Author:
  19. Jay Krell (JayKrell) August 2001
  20. Revision History:
  21. --*/
  22. #pragma once
  23. #if defined(__cplusplus)
  24. extern "C" {
  25. #endif
  26. typedef struct _WIN32_SIMPLE_LOCK {
  27. DWORD Size;
  28. LONG Lock;
  29. DWORD OwnerThreadId;
  30. LONG Waiters;
  31. ULONG EntryCount;
  32. PVOID Extra[2];
  33. } WIN32_SIMPLE_LOCK, *PWIN32_SIMPLE_LOCK;
  34. #define WIN32_INIT_SIMPLE_LOCK { sizeof(WIN32_SIMPLE_LOCK), -1 }
  35. #define WIN32_ACQUIRE_SIMPLE_LOCK_WAS_NOT_RECURSIVE_ACQUIRE (0x00000001)
  36. #define WIN32_ACQUIRE_SIMPLE_LOCK_WAS_RECURSIVE_ACQUIRE (0x00000002)
  37. #define WIN32_ACQUIRE_SIMPLE_LOCK_WAS_FIRST_ACQUIRE (0x00000004) /* useful for an exactly one one shot */
  38. DWORD
  39. Win32AcquireSimpleLock(
  40. PWIN32_SIMPLE_LOCK SimpleLock,
  41. DWORD SleepCount
  42. #if defined(__cplusplus)
  43. = INFINITE
  44. #endif
  45. );
  46. #define WIN32_RELEASE_SIMPLE_LOCK_WAS_RECURSIVE_RELEASE (0x80000000)
  47. #define WIN32_RELEASE_SIMPLE_LOCK_WAS_NOT_RECURSIVE_RELEASE (0x40000000)
  48. DWORD
  49. Win32ReleaseSimpleLock(
  50. PWIN32_SIMPLE_LOCK SimpleLock
  51. );
  52. #if defined(__cplusplus)
  53. }
  54. #endif
  55. #if defined(__cplusplus)
  56. class CWin32SimpleLock
  57. {
  58. public:
  59. WIN32_SIMPLE_LOCK Base;
  60. DWORD Acquire(DWORD SleepCount = INFINITE) { return Win32AcquireSimpleLock(&Base, SleepCount); }
  61. void Release() { Win32ReleaseSimpleLock(&Base); }
  62. operator PWIN32_SIMPLE_LOCK() { return &Base; }
  63. operator WIN32_SIMPLE_LOCK&() { return Base; }
  64. };
  65. class CWin32SimpleLockHolder
  66. {
  67. public:
  68. CWin32SimpleLockHolder(CWin32SimpleLock * Lock) : m_Lock(Lock), m_Result(0)
  69. {
  70. m_Result = Lock->Acquire(INFINITE);
  71. }
  72. void Release()
  73. {
  74. if (m_Lock != NULL)
  75. {
  76. m_Lock->Release();
  77. m_Lock = NULL;
  78. }
  79. }
  80. ~CWin32SimpleLockHolder()
  81. {
  82. Release();
  83. }
  84. CWin32SimpleLock* m_Lock;
  85. DWORD m_Result;
  86. };
  87. #endif // __cplusplus